[PIL]: Question On Changing Colour

Andrea Gavana andrea_gavana at tin.it
Thu Oct 13 21:58:26 CEST 2005

Hello Terry,

> new_hue   # your 'basic color', just the hue part
> rgb_base  # color from the basic button image
> rgb_new   # the new color you want to replace rgb_base with
> rgb_new = hsv_to_rgb( (new_hue,) + rgb_to_hsv(rgb_base)[1:])

    thanks a lot for your suggestion! However, either I did not understand it correctly or I am doing something stupid in my code. Here is a small example:

from colorsys import *

# that is the old colour --> GREY
rgb_old = (0.7, 0.7, 0.7)

# Transform the new colour in HSV
hsv_old = rgb_to_hsv(rgb_old[0], rgb_old[1], rgb_old[2])

# this is the new colour --> BLUE
rgb_new = (0.0, 0.0, 1.0)

# Transform the new colour in HSV
hsv_new = rgb_to_hsv(rgb_new[0], rgb_new[1], rgb_new[2])

# I take only the Hue part of the new colour
new_hue = hsv_new[0]

# Get the new colour
rgb_new = hsv_to_rgb(new_hue, hsv_old[1], hsv_old[2])

print rgb_old
print rgb_new
print rgb_old == rgb_new

This prints:

(0.69999999999999996, 0.69999999999999996, 0.69999999999999996)
(0.69999999999999996, 0.69999999999999996, 0.69999999999999996)

So, no matter what colour I choose as a "new" colour, the Hue part of the new colour doesn't change in RGB. In other words, leaving the old value for "Saturation" and "Value" makes the presence of the "Hue" part useless. But why in the world does this happen? If a colour is defined by 3 values, changes in every single value should change the colour too... 
Ah, thanks God for the existence of RGB ;-)

Thanks a lot for every suggestion.


"Imagination Is The Only Weapon In The War Against Reality."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20051013/1583fef1/attachment.html>

More information about the Python-list mailing list