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.
A more ergonomic syntax can also be used when passing a color to a method that supports any
type that implements Into<Color>
.
// A solid color with alpha = 1.0 // Syntax is [red, green, blue] and doesn't require explicitly writing the field names turtle.set_pen_color([133.0, 23.0, 96.0]); turtle.set_fill_color([133.0, 23.0, 96.0]); turtle.drawing_mut().set_background_color([133.0, 23.0, 96.0]); // This is a little easier to type than the equivalent: turtle.drawing_mut().set_background_color(Color {red: 133.0, green: 23.0, blue: 96.0, alpha: 1.0}); // Add an additional element to the array to specify the alpha // Syntax is [red, green, blue, alpha] turtle.set_pen_color([133.0, 23.0, 96.0, 0.5]); turtle.set_fill_color([133.0, 23.0, 96.0, 0.5]); turtle.drawing_mut().set_background_color([133.0, 23.0, 96.0, 0.5]); // This is a little easier to type than the equivalent: turtle.drawing_mut().set_background_color(Color {red: 133.0, green: 23.0, blue: 96.0, alpha: 0.5});
When creating a color this way, we will check whether or not the color is valid and provide an error message to let you know what happened.
// Color values must only go up to 255.0 turtle.set_pen_color([133.0, 256.0, 96.0]); // This will panic with an error message
Modules
extended |
Even more colors!! |
Structs
Color |
Type for representing a color. |
Constants
BEIGE |
beige |
BLACK |
black |
BLUE |
blue |
BROWN |
brown |
CORAL |
coral |
CYAN |
cyan |
GREEN |
green |
GREY |
grey |
LAVENDER |
lavender |
LIME |
lime |
MAGENTA |
magenta |
MAROON |
maroon |
MINT |
mint |
NAVY |
navy |
OLIVE |
olive |
ORANGE |
orange |
PINK |
pink |
PURPLE |
purple |
RED |
red |
TEAL |
teal |
TRANSPARENT |
transparent |
WHITE |
white |
YELLOW |
yellow |
Statics
COLORS |
A list of all of the colors |
COLOR_NAMES |
A list of all of the color names |