[PIL]: Question On Changing Colour

Iain King iainking at gmail.com
Fri Oct 14 05:23:34 EDT 2005


Andrea Gavana wrote:
> I have tried your solution, 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)
> True
>
> 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...

Not with HSV.  The hue determines which 'color' it will be - red, blue,
indigo, whatever.  That Saturation determined how vibrant this 'color'
will be.  V is brightness (I can't remember what the V actually stands
for).  Each of these values scales from 0 to 1, or 0% to 100%, however
you want to thiink about it.   If you try and picture the gradient
you'd get by plotting this range as a line, then:
The H line would be a spectrum of colours, like a rainbow.
Say we pick H to be RGB #FF0000 - Red
The S line would be a gradient ranging from grey (absense of color) to
red.
The V line would be a gradient ranging from black (completely dark) to
red.

So on the HSV scale, grey is represented by a saturation of 0 - meaning
none of H is present in the color; the color in question being
determined purely by it's brightness (V).  So when you pick your HSV
triplet for a grey color, you have to set S to 0.  You can set H to
anything at all - because S is 0, no tint of H will appear in the color
at all.

Iain             http://www.snakebomb.com




More information about the Python-list mailing list