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

6. class Color: A specific color

An instance of the Color class is used to represent a specific color. There are two obvious ways to represent the red, green, and blue components: as floating point numbers or as integers. The obvious way to use a float is to let 0.0 represent none and let 1.0 represent all of a component. However, the “#RRGGBB” representation is also common, and in that form each component is an integer in the range [0, MAX_PARAM]. An internal precision of 16 bits per color is plenty.

Here is the class interface:

huey
# - - - - -   c l a s s   C o l o r

class Color:
    """Represents an arbitrary color.

      Exports:
        Color ( red, green, blue ):
          [ (red is the red value as a float in [0.0,1.0] or as an
            int in [0,MAX_PARAM]) and
            (green is the green value as a float in [0.0,1.0] or as an
            int in [0,MAX_PARAM]) and
            (blue is the blue value as a float in [0.0,1.0] or as an
            int in [0,MAX_PARAM]) ->
              return a new Color instance with those color values ]

Internally, a color is represented in the integer form. The three components are exported as read-only attributes.

huey
        .r:    [ the red component as an int in [0,MAX_PARAM] ]
        .g:    [ the green component as an int in [0,MAX_PARAM] ]
        .b:    [ the blue component as an int in [0,MAX_PARAM] ]

The .__str__() method defines the behavior of the built-in Python str() function when applied to instances of this class. See Section 6.3, “Color.__str__(): Convert to a string”.

huey
        .__str__(self):
          [ return self as a string "#RRGGBB" ]

The .__cmp__() function is used to compare two colors to see if they are the same color. See Section 6.4, “Color.__cmp__(): Compare two colors”.

huey
        .__cmp__(self, other):
          [ other is a Color instance ->
              if self's color name is less than other's ->
                return a negative number
              else if self's color name is greater than other's ->
                return a positive number
              else -> return 0 ]
    """