Color objects are created using the following functions. Color is represented using the RGB (red, green, blue) format.
Function | Description |
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:
Function | Description |
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:
Function | Description |
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. |
Color gradients
A color gradient is a smooth color progression from one color to another, which creates the illusion of continuity between the two color extremes.
The following function may be used used to create color gradients.
This function returns a list of RGB colors (i.e., a list of lists) starting with color1 (e.g., [0, 0, 0]) and ending (without including) color2 (e.g., [251, 147, 14], which is orange). The number of steps equals the number of colors in the list returned.
For example, the following creates a gradient list of 12 colors:
>>> colorGradient([0, 0, 0], [251, 147, 14], 12) [[0, 0, 0], [20, 12, 1], [41, 24, 2], [62, 36, 3], [83, 49, 4], [104, 61, 5], [125, 73, 7], [146, 85, 8], [167, 98, 9], [188, 110, 10], [209, 122, 11], [230, 134, 12]]
Notice how the above excludes the final color (i.e., [251, 147, 14]). This allows to create composite gradients (without duplication of colors). For example, the following:
black = [0, 0, 0] # RGB values for black orange = [251, 147, 14] # RGB values for orange white = [255, 255, 255] # RGB values for white cg = colorGradient(black, orange, 12) + colorGradient(orange, white, 12) + [white]
creates a list of gradient colors from black to orange, and from orange to white. Notice how the final color, white, has to be included separately (using list concatenation). Now, gc contains a total of 25 unique gradient colors.
Additional Color Functions
The following more advanced color functions are available:
Function | Description |
getHSBColor(hue, saturation, brightness) | Creates a Color object based on the specified values for the HSB color model. The saturation and brightness should be floats in the range 0.0 to 1.0. The hue should be any float. See more info. |
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. |
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]. |