This method converts a
Color object into the
three parameters of the HSV model, each in the range [0,
MAX_PARAM]. The algorithm is Foley & van Dam's
RGB_To_HSV”; see Section 2, “References”.
# - - - H S V M o d e l . c o l o r T o P a r a m s def colorToParams ( self, color ): """Convert a Color to the three HSV parameters. """
First we normalize the three RGB parameters to [0.0, 1.0].
See Section 7.4, “
ColorModel.normalize(): Normalize an integer
rNorm = ColorModel.normalize(color.r) gNorm = ColorModel.normalize(color.g) bNorm = ColorModel.normalize(color.b)
The value parameter
v is the maximum of the three
values. We also calculate their minimum.
v = maxColor = max ( rNorm, gNorm, bNorm ) minColor = min ( rNorm, gNorm, bNorm )
Next, calculate the saturation. If all the colors are zero, the saturation is zero.
if maxColor == 0.0: s = 0.0 else: s = ( maxColor - minColor ) / maxColor
Next, find the hue. If the saturation is zero, arbitrarily use a value of zero (red) for the hue.
if s == 0.0: h = 0.0 # Hue is undefined; use red arbitrarily
These three cases are: colors between Y and M; colors between C and Y; and colors between M and C. Negative values are then rotated to the range [0,6]. Finally, the hue is normalized to [0.0, 1.0].
else: delta = maxColor - minColor if rNorm == maxColor: # Between Y and M h = ( gNorm - bNorm ) / delta elif gNorm == maxColor: # Between C and Y h = 2.0 + ( bNorm - rNorm ) / delta else: # Between M and C h = 4.0 + ( rNorm - gNorm ) / delta if h < 0.0: h = h + 6.0 h = h / 6.0 # Normalize to [0,1]
Finally, return the three parameters, discretized back to
MAX_PARAM]. See Section 7.5, “
ColorModel.discretize(): Discretize an
integer color parameter”.
return ( ColorModel.discretize ( h ), ColorModel.discretize ( s ), ColorModel.discretize ( v ) )