[reportlab-users] RGBA Colors
robin at reportlab.com
Tue Nov 24 11:53:48 EST 2009
Kevin D Smith wrote:
> A little while ago I asked if there was a way to get RGBA colors (like
> in CSS3) where the A component is an alpha value to set transparency.
> While doing some searching last night, I realized that the svn version
> of ReportLab already has alpha support; however, it didn't go quite as
> far as I had hoped. Methods were added to the canvas to set the fill
> and stroke opacity, which is nice, but if you are using Platypus, you
> don't have access to them.
> I messed around with the code a bit in textobject.py and colors.py to
> add alpha arguments to all of the color methods as well as an alpha
> component to rgb and cmyk colors. With the attached patch, you can
> specify colors with an alpha value that can be used in Platypus or
> anywhere else colors are used. I would like this patch to be considered
> for official use so that I don't have to remember to add this support
> for my projects. Does anyone know what that would take?
This looks like a nice patch, but there might be hidden problems. The opacity
patch was contributed and one of the approaches considered was that opacity
should go into the colour objects. However, since opacity is actually stored on
the canvas there are problems related to using a mixture of colours containing
alpha and non-alpha colours.
In the colour+a model setting a colour with a in [0,1] means an action is
carried out on the canvas.
setFill(red+alpha=0.5) ==> canvas alpha = 0.5
follow that by
setFill(black+alpha=None) canvas alpha remains 0.5
which I would argue is unexpected. Forgive me if your patch doesn't exactly
follow this model, but that's what my quick scan seems to imply.
We seem to need to get the incoming colour+alpha=None to restore the appropriate
alpha value. The problem then is what that value should be; who owns the alpha
I'm not against this idea, I did already consider such a mechanism. Perhaps
others would like to comment.
One other point is that it is very easy to make a flowable that allows platypus
to mess with canvas properties.
More information about the reportlab-users