Module turtle::color
[−]
[src]
Color types and constants
When setting a color, you can use a variety of different color names.
This module contains many of the most common colors that you might want to use. There is an
even more comprehensive list in the extended
module. Any of the color names
listed in this module or in the extended
module can be used as a color. You only need to
reference the color::extended
module if you want to use a specific color constant from that
module.
You can refer to a color by using its color name as a string literal. For example:
// This will set the turtle's pen color to BLACK turtle.set_pen_color("black"); // This is the same as the previous line turtle.set_pen_color(color::BLACK); // You can use any of the supported color names (including the ones from extended) turtle.set_pen_color("deep lilac");
You can also use hexadecimal color strings to get any color you want (even ones that aren't listed here).
turtle.set_pen_color("#3366ff"); turtle.set_pen_color("#36f");
Each color's constant name is in uppercase in the list below. The color name you should use to refer to it is in lower case next to the constant.
For your convenience, there are two static variables COLORS
and
COLOR_NAMES
which contain the values of all the color constants
and each of their names as strings. These static variables only contain the colors from this
module. The extended
module has its own static COLOR
and
COLOR_NAMES
variables.
Random Colors
You can also generate random colors. Here's an example:
use turtle::{random, Color}; turtle.set_pen_color(random::<Color>().opaque());
The syntax used in random::<Color>()
is referred to as
"turbofish" syntax.
See that documentation for more information.
Notice that you need to explicitly call the opaque()
method on the color in order to make sure that the color has an alpha value of 1.0. By default,
when you generate a random color, it's alpha value will be random as well.
See the examples directory on GitHub for more information.
Creating a Color from Values
Usually, you won't need to initialize a color this way since the above methods are quite convenient. However, in some situations it may be necessary to create a color with specific red, green, and blue values. The following example illustrates how to do that.
use turtle::Color; let my_color = Color {red: 255.0, green: 55.0, blue: 11.0, alpha: 1.0};
Note that when creating a color this way, we do not check if the values of each property are within their valid ranges.
Modules
extended |
Even more colors!! |
Structs
Color |
Type for representing a color. |
Constants
BEIGE | |
BLACK | |
BLUE | |
BROWN | |
CORAL | |
CYAN | |
GREEN | |
GREY | |
LAVENDER | |
LIME | |
MAGENTA | |
MAROON | |
MINT | |
NAVY | |
OLIVE | |
ORANGE | |
PINK | |
PURPLE | |
RED | |
TEAL | |
TRANSPARENT | |
WHITE | |
YELLOW |
Statics
COLORS |
A list of all of the colors |
COLOR_NAMES |
A list of all of the color names |