Color

All Color values are wrapped in pyDraw as to make constructors and methods more clear. Colors are immutable, and can be created from names, RGB values, or a hex string.

Initialization

You can create a Color by passing in one of three options:

  • Name (string from Tkinter dataset)

  • RGB (either a tuple or just three arguments, [0-255])

  • Hex (supports 3-char: '#fff' and 6-char: '#ffffff')

color = Color('red')  # Get the red color
color = Color(255, 0, 0)  # Create red from RGB
color = Color('#ff0000')  # Create red from hex

Retrieving Values

The RGB values are available no matter how the Color was initialized:

color.red()  # Retrieve the red value (0-255)
color.green()  # Retrieve the green value (0-255)
color.blue()  # Retrieve the blue value (0-255)

color.rgb()  # Retrieve a tuple of all three color values

color.name()  # If initialized with a name, it is available
color.hex()  # If initialized with a hex, it is available

Random Color and All Colors

You can retrieve a random Color via:

Color.random()

Or you can generate it yourself by retrieving the full list of (named) Colors:

Color.all()

Note that these are static methods and are called on the Color class, rather than an instance.

To view all named colors, please see the second tab.

Cloning

You can easily clone a color (like most objects in pyDraw) as:

color.clone()

Advanced

🚀 Experienced Only: You can retrieve a "turtle-friendly" version of the Color with:

color.__value__()

Last updated