[reportlab-users] RGBA Colors
Robin Becker
robin at reportlab.com
Wed Dec 2 09:37:01 EST 2009
Kevin D Smith wrote:
> I think I tripped across an issue with using alphas. I was drawing a box with borders (each component done with rect(...)). The background had an alpha set using an RGBA color, but the borders had an alpha value of 1. For whatever reason, the background and the top, right, and bottom borders came out with the alpha. The left border came out completely opaque. I'm not sure why just the left border came out opaque. I drew the objects in the order: background, left, top, right, bottom. In order to get the behavior I wanted, I had to save the state before drawing the background, then restore it afterward. This seems to be an issue with the alpha implementation even without the patch since I was setting the alpha explicitly for each of the components and it still happened.
>
> I've attached the test PDF that I created. The code is part of a much larger program so it would be tricky to separate it out, but I could probably work up a test case if needed.
I tried the following after applying your initial patch
##########################################
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.colors import Color, red, yellow, blue
c = Canvas('talpha2.pdf')
c.setFillColor(yellow)
c.rect(0,0,3*72,3*72,fill=1,stroke=0)
c.setFillColor(blue.clone(alpha=0.5))
c.rect(72,72,1*72,1*72,fill=1,stroke=0)
c.setFillColor(red.clone(alpha=1))
c.rect(72,72,2,72,fill=1,stroke=0)
c.rect(72,72,72,2,fill=1,stroke=0)
c.rect(72,2*72-2,72,2,fill=1,stroke=0)
c.rect(2*72-2,72,2,72,fill=1,stroke=0)
c.save()
##########################################
and that seems to do the right thing; howeever, I initially forgot that red
doesn't have a default alpha of 1 so my first attempt did have alphas=0.5 for
the border.
I have also tried the pure alpha=1 case ie
##########################################
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.colors import Color, red, yellow, blue
c = Canvas('talpha1.pdf')
c.setFillColor(yellow.clone(alpha=1))
c.rect(0,0,3*72,3*72,fill=1,stroke=0)
c.setFillColor(blue.clone(alpha=1))
c.rect(72,72,1*72,1*72,fill=1,stroke=0)
c.setFillColor(red.clone(alpha=1))
c.rect(72,72,2,72,fill=1,stroke=0)
c.rect(72,72,72,2,fill=1,stroke=0)
c.rect(72,2*72-2,72,2,fill=1,stroke=0)
c.rect(2*72-2,72,2,72,fill=1,stroke=0)
c.save()
##########################################
which as a result of our clever code never actually emits the alpha setting code
(because alpha=1 is the default). In my opinion that makes it easier to dispense
with all the alpha==None worries and just assume colours have an alpha which is
1 by default. In most cases we'll never actually see any extra pdf code emitted.
--
Robin Becker
More information about the reportlab-users
mailing list