[reportlab-users] Buglet and fix in HexColor (2.6 and probably before)

Dinu Gherman gherman at darwin.in-berlin.de
Thu Jan 17 12:01:59 EST 2013


Hi,

I noticed a bug in reportlab.lib.colors.HexColor when actually using the
alpha parameter, e.g.:

In [1]: from reportlab.lib.colors import HexColor
In [2]: HexColor(16777215, alpha=0.5)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-6a54ff7a3c76> in <module>()
----> 1 HexColor(16777215, alpha=0.5)

/opt/lib/python2.7/site-packages/reportlab/lib/colors.pyc in HexColor(val, htmlOnly, alpha)
363 val = int(val,b)
364 if alpha:
--> 365 return Color((val>>24)&0xFF/255.0,((val>>16)&0xFF)/255.0,((val>>8)&0xFF)/255.0,(val&0xFF)/255.0)
366 return Color(((val>>16)&0xFF)/255.0,((val>>8)&0xFF)/255.0,(val&0xFF)/255.0)
367

TypeError: unsupported operand type(s) for &: 'int' and 'float'

You can find a fix below (either a verbose/readable one and a very short
alternative - your choice...).

Regards,

Dinu


$ cd /opt/lib/python2.7/site-packages/reportlab/lib
$ diff -u colors.py colors1.py
--- colors.py 2013-01-17 16:38:37.000000000 +0100
+++ colors1.py 2013-01-17 17:54:16.000000000 +0100
@@ -362,8 +362,18 @@
alpha = True
val = int(val,b)
if alpha:
- return Color((val>>24)&0xFF/255.0,((val>>16)&0xFF)/255.0,((val>>8)&0xFF)/255.0,(val&0xFF)/255.0)
- return Color(((val>>16)&0xFF)/255.0,((val>>8)&0xFF)/255.0,(val&0xFF)/255.0)
+ r = ((val >> 24) & 0xFF) / 255.0
+ g = ((val >> 16) & 0xFF) / 255.0
+ b = ((val >> 8) & 0xFF) / 255.0
+ a = (val & 0xFF) / 255.0
+ return Color(r, g, b, a)
+ # or return Color(*[((val >> off) & 0xFF) / 255.0 for off in (24, 16, 8, 0)])
+ else:
+ r = ((val >> 16) & 0xFF) / 255.0
+ g = ((val >> 8) & 0xFF) / 255.0
+ b = (val & 0xFF) / 255.0
+ return Color(r, g, b)
+ # or return Color(*[((val >> off) & 0xFF) / 255.0 for off in (16, 8, 0)])

def linearlyInterpolatedColor(c0, c1, x0, x1, x):
"""




More information about the reportlab-users mailing list