Color

Color objects are created using the following functions. Color is represented using the RGB (red, green, blue) format.

FunctionDescription
Color(red, green, blue)Creates an opaque color with the specified red, green, and blue values in the range (0 – 255).
Color(red, green, blue, alpha)Creates a color with the specified red, green, blue, and alpha (transparency) values in the range (0 – 255). An alpha value of 255 means the color is completely opaque, whereas alpha value of 0 means the color is completely transparent

For example:

color1 = Color(255, 255, 255)        # same as Color.WHITE
color2 = Color(255, 255, 255, 150)   # same as Color.WHITE, but semi-transparent

Also, the following Color constants are available:

Color.BLACK, Color.BLUE, Color.CYAN,
Color.DARK_GRAY, Color.GRAY, Color.GREEN,
Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
Color.PINK, Color.RED, Color.WHITE, Color.YELLOW

Color Functions

Once a color object has been created, the following functions are available:

FunctionDescription
color1.brighter()Creates a new Color that is a brighter version of this Color.
color1.darker()Creates a new Color that is a darker version of this Color.
color1.getRed()Returns the red component (0-255) of the color.
color1.getGreen()Returns the green component (0-255) of the color.
color1.getBlue()Returns the blue component (0-255) of the color.
color1.getAlpha()Returns the alpha (transparency) component (0-255) of the color.

How to use color

All GUI objects (e.g., Line, Circle, etc.) support the following functions:

FunctionDescription
object.setColor(color)Changes an object’s color to the specified color.  If the color parameter is omitted, a color selection dialog box will be presented.
object.getColor()Returns an object’s current color.

Additional Color Functions

The following more advanced color functions are available:

FunctionDescription
Color.getHSBColor(hue, saturation, brightness)Creates a Color object based on the specified values for the HSB color model. The hue, saturation, and brightness should be floats in the range 0.0 to 1.0.
See more info.
Color.HSBtoRGB(hue, saturation, brightness)Converts the components of a color, as specified by the HSB model, to an equivalent set of values for the default RGB model.
To get them, create a Color() from the value returned, and then use color.getRGB().
Color.RGBtoHSB(red, green, blue)Converts the components of a color, as specified by the default RGB model, to an equivalent set of values for the HSB model. Returns a list of floats, i.e., [hue, saturation, brightness].