[reportlab-users] RGBA Colors
Kevin D Smith
Kevin.Smith at sixquickrun.com
Wed Dec 2 12:33:43 EST 2009
On Dec 2, 2009, at 8:37 AM, Robin Becker wrote:
> 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.
Here is a simplified test program the demonstrates the problem. You'll see that each color has an explicit alpha set. The top, right, and bottom borders come out with an alpha of 0.5 even though I am explicitly setting it to 1 for each of those. This program was run using an unpatched version of ReportLab, so this is an issue with the current implementation in the svn repository.
#!/usr/bin/env python
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.pagesizes import letter, A4
from reportlab.lib.units import inch
from reportlab.lib.colors import Color
def drawBox(c, width, height, s):
drawnwidth = s['border-left-width'] + s['padding-left'] + width + \
s['padding-right'] + s['border-right-width']
drawnheight = s['border-top-width'] + s['padding-top'] + height + \
s['padding-bottom'] + s['border-bottom-width']
totalwidth = s['margin-left'] + drawnwidth + s['margin-right']
totalheight = s['margin-top'] + drawnheight + s['margin-bottom']
c.saveState()
c.translate(s['margin-left'], -(s['margin-top']+drawnheight))
# Background
c.setFillColor(s['background-color'])
c.setFillAlpha(0.5)
print s['background-color']
c.rect(0, 0, drawnwidth, drawnheight, stroke=0, fill=1)
# Left border
c.translate(0.5*s['border-left-width'], 0)
c.saveState()
c.translate(-0.5*s['border-left-width'], 0)
c.setFillColor(s['border-left-color'])
c.setFillAlpha(1)
print s['border-left-color']
c.rect(0, 0, s['border-left-width'], drawnheight, stroke=0, fill=1)
c.restoreState()
c.translate(-0.5*s['border-left-width'], 0)
# Right border
c.translate(drawnwidth-0.5*s['border-right-width'], 0)
c.saveState()
c.translate(-0.5*s['border-right-width'], 0)
c.setFillColor(s['border-right-color'])
c.setFillAlpha(1)
print s['border-right-color']
c.rect(0, 0, s['border-right-width'], drawnheight, stroke=0, fill=1)
c.restoreState()
c.translate(-drawnwidth+0.5*s['border-right-width'], 0)
# Bottom border
c.translate(0, 0.5*s['border-bottom-width'])
c.saveState()
c.translate(0, -0.5*s['border-bottom-width'])
c.setFillColor(s['border-bottom-color'])
c.setFillAlpha(1)
print s['border-bottom-color']
c.rect(0, 0, drawnwidth, s['border-bottom-width'], stroke=0, fill=1)
c.restoreState()
c.translate(0, -0.5*s['border-bottom-width'])
# Top border
c.translate(0, drawnheight-0.5*s['border-top-width'])
c.saveState()
c.translate(0, -0.5*s['border-top-width'])
c.setFillColor(s['border-top-color'])
c.setFillAlpha(1)
print s['border-top-color']
c.rect(0, 0, drawnwidth, s['border-top-width'], stroke=0, fill=1)
c.restoreState()
c.translate(0, -drawnheight+0.5*s['border-top-width'])
c.translate(-s['margin-left'], s['margin-top']+drawnheight)
c.restoreState()
c = Canvas('test.pdf', pagesize=letter)
c.translate(0.5*inch, 10.5*inch)
style = {
'background-color': Color(255, 0, 0),
'padding-top': 0.25*inch,
'padding-right': 0.25*inch,
'padding-bottom': 0.25*inch,
'padding-left': 0.25*inch,
'margin-top': 0,
'margin-right': 0,
'margin-bottom': 0,
'margin-left': 0,
'border-top-color': Color(0, 0, 255),
'border-right-color': Color(0, 0, 255),
'border-bottom-color': Color(0, 0, 255),
'border-left-color': Color(0, 0, 255),
'border-top-width': 0.125*inch,
'border-right-width': 0.0625*inch,
'border-bottom-width': 0.125*inch,
'border-left-width': 0.0625*inch,
'border-top-style': ([1], [1]),
'border-right-style': ([1], [1]),
'border-bottom-style': ([1], [1]),
'border-left-style': ([1], [1]),
}
drawBox(c, 3*inch, 2*inch, style)
c.showPage()
c.save()
Kevin D Smith
Kevin.Smith at sixquickrun.com
More information about the reportlab-users
mailing list