Canvas 2d fill color by rgba string



  • I had some troubles with color setting via standard HTML encoding (turned out to be because the Qt format is AARRGGBB, was expecting RRGGBBAA). I used 'rgba(.....)' instead, but I am getting some weird behaviour. Have a sufficiently big canvas and the following onPaint function, then the problems are as listed in the comments:

    @
    onPaint: {
    var context = getContext("2d")
    context.clearRect(0, 0, width, height)
    context.beginPath()

                context.fillStyle = "green"
    
                //  should be red, assignment failed, is green
                context.fillStyle = "rgba(1.0,0.0,0.0,1.0)"
                context.fillRect(50, 50, 100, 50)
                context.fill()
    
                // Should be transparent and red, assignment failed, is green
                context.fillStyle = "rgba(0.99,0.0,0.0,0.1)"
                context.fillRect(200, 50, 100, 50)
                context.fill()
    
                // Should be transparent, has the right colour, but full opacity
                context.fillStyle = "rgba(255,0,0,10)"
                context.fillRect(350, 50, 100, 50)
                context.fill()
    
                // This works, which, frankly...
                context.fillStyle = "rgba(255,0,0,0.1)"
                context.fillRect(500, 50, 100, 50)
                context.fill()
    

    }
    @

    Tested with QT 5.4 on Ubuntu 14.04

    RESOLVED:
    The last format is indeed correct, as detailed in: http://doc.qt.io/qt-5/qml-qtquick-context2d.html#fillStyle-prop


  • Moderators

    Hi,

    As per "this":http://doc.qt.io/qt-5/qml-qtquick-context2d.html#fillStyle-prop the last one in your example is the valid format. Check out the other supported formats.



  • [quote author="p3c0" date="1423716663"]Hi,

    As per "this":http://doc.qt.io/qt-5/qml-qtquick-context2d.html#fillStyle-prop the last one in your example is the valid format. Check out the other supported formats.[/quote]

    Thank you for the answer, I missed this paragraph altogether.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.