This method converts a set of HSV parameters to a Color instance. This algorithm comes from Foley &
van Dam's algorithm “HSV_To_RGB”; see Section 2, “References”.
# - - - H S V M o d e l . p a r a m s T o C o l o r
def paramsToColor ( self, params ):
"""Convert the three HSV color parameters to a Color.
"""
First we normalize the three color components to [0.0,
1.0]; see Section 7.4, “ColorModel.normalize(): Normalize an integer
color parameter”.
h = ColorModel.normalize ( params[0] )
s = ColorModel.normalize ( params[1] )
v = ColorModel.normalize ( params[2] )
Note that the Color() constructor will accept
float values in [0.0, 1.0] as well as integer values in
[0,MAX_PARAM].
if s == 0.0:
return Color ( v, v, v )
Treat a hue of 1.0 or more as a hue of 0.0 due to wraparound.
Otherwise, normalize the value of h to the
half-open interval [0,6).
if h >= 1.0:
h = 0.0
else:
h = h * 6.0
The math.modf() function returns a tuple ( where f, i)
is the fractional part and f is the integral part. The rest follows Foley &
van Dam's algorithm, using the same names.
i
(f,i) = math.modf(h)
i = int ( i )
p = v * ( 1.0 - s )
q = v * ( 1.0 - s * f )
t = v * ( 1.0 - s * ( 1.0 - f ) )
if i == 0:
return Color ( v, t, p )
elif i == 1:
return Color ( q, v, p )
elif i == 2:
return Color ( p, v, t )
elif i == 3:
return Color ( p, q, v )
elif i == 4:
return Color ( t, p, v )
else:
return Color ( v, p, q )