[PIL]: Question On Changing Colour

Scott David Daniels scott.daniels at acm.org
Fri Oct 14 16:37:03 EDT 2005


<Confusion about applying hue to a grey value>
Try this:
 >>> import colorsys as cs
 >>> grey = (.7, .7, .7)
 >>> blue = (0., 0., 1.)
 >>> hsv_grey = cs.rgb_to_hsv(*grey)
 >>> hsv_blue = cs.rgb_to_hsv(*blue)
 >>> hsv_grey
(0.0, 0.0, 0.69999999999999996)
 >>> hsv_blue
(0.66666666666666663, 1.0, 1.0)

The problem is that the saturation of the grey is 0.  There
is no Hue to anything between black and white.  Maybe you want
something like:

def apply_hue(color, rgb):
     hue, _saturation, _value = cs.rgb_to_hsv(*color)
     _hue, saturation, value = cs.rgb_to_hsv(*rgb)
     return cs.hsv_to_rgb(hue, max(.1, saturation), value)

Or:

def apply_hs(color, rgb):
     hue, saturation, value = cs.rgb_to_hsv(*color)
     _hue, _saturation, value = cs.rgb_to_hsv(*rgb)
     return cs.hsv_to_rgb(hue, saturation, value)


--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list