Next / Previous / Contents / TCC Help System / NM Tech homepage

8.2. HSVModel.paramsToColor()

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”.

huey
# - - -   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”.

huey
        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].

huey
        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).

huey
        if  h >= 1.0:
            h = 0.0
        else:
            h = h * 6.0

The math.modf() function returns a tuple (f, i) where f is the fractional part and i is the integral part. The rest follows Foley & van Dam's algorithm, using the same names.

huey
        (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 )