Module pix_engine::color
source · Expand description
Color functions for drawing.
Each Color can be constructed with a Mode. The default mode and internal
representation is Rgb with values ranging from 0..=255
for red, green, blue, and alpha
transparency. Hsb and Hsl values range from 0.0..=360.0
for hue, 0.0..=100.0
for
saturation and brightness/lightness and 0.0..=1.0
for alpha transparency.
There are convience macros for flexible construction: color!, rgb!, hsb! and hsl! that take 1-4 parameters. The number of parameters provided alter how they are interpreted:
- Providing a single parameter constructs a grayscale color.
- Two parameters constructs a grayscale color with alpha transparency.
- Three parameters are used as
RGB
orHSB
/HSL
values. - Four parameters are used as
RGBB
orHSb
/HSL
values with alpha transparency.
If you’re not picky about color, there are the random and
random_alpha
methods.
Color also implements FromStr
allowing conversion from a 3, 4, 6, or
8-digit hexadecimal string.
The Color instance stores which Mode it was created with, modifying how manipulation
methods are interprted such as set_alpha
taking a range of 0.0..=255.0
or
0.0..=1.0
. The Mode can be changed any time to alter this behavior using
set_mode
.
There are also several named color constants available in the prelude matching the SVG 1.0 Color Keywords.
Examples
Rgb values range from 0..=255
for red, green, blue and alpha transparency.
use pix_engine::prelude::*;
let c = rgb!(55); // Grayscale
assert_eq!(c.channels(), [55, 55, 55, 255]);
let c = rgb!(55, 128); // Grayscale with alpha
assert_eq!(c.channels(), [55, 55, 55, 128]);
let c = rgb!(128, 0, 55); // Red, Green, Blue
assert_eq!(c.channels(), [128, 0, 55, 255]);
let c = rgb!(128, 0, 55, 128); // Red, Green, Blue, and Alpha
assert_eq!(c.channels(), [128, 0, 55, 128]);
Hsb
/Hsl
values range from 0.0..=360.0
for hue, 0.0..=100.0
for saturation and
brightness/lightness and 0.0..=1.0
for alpha transparency.
use pix_engine::prelude::*;
let c = hsb!(50.0); // Gray
assert_eq!(c.channels(), [128, 128, 128, 255]);
let c = hsb!(50.0, 0.5); // Gray with alpha
assert_eq!(c.channels(), [128, 128, 128, 128]);
let c = hsb!(342.0, 100.0, 80.0); // Hue, Saturation, Brightness
assert_eq!(c.channels(), [204, 0, 61, 255]);
let c = hsb!(342.0, 100.0, 80.0, 0.5); // Hue, Saturation, Brightness, Alpha
assert_eq!(c.channels(), [204, 0, 61, 128]);
SVG 1.0 Named color constants
use pix_engine::prelude::*;
let c = Color::ALICE_BLUE;
assert_eq!(c.channels(), [240, 248, 255, 255]);
let c = Color::DARK_ORCHID;
assert_eq!(c.channels(), [153, 50, 204, 255]);
From a hexadecimal string
use pix_engine::prelude::*;
use std::str::FromStr;
let c = Color::from_str("#F0F")?; // 3-digit Hex string
assert_eq!(c.channels(), [255, 0, 255, 255]);
let c = Color::from_str("#F0F5")?; // 4-digit Hex string
assert_eq!(c.channels(), [255, 0, 255, 85]);
let c = Color::from_str("#F0F5BF")?; // 6-digit Hex string
assert_eq!(c.channels(), [240, 245, 191, 255]);
let c = Color::from_str("#F0F5BF5F")?; // 8-digit Hex string
assert_eq!(c.channels(), [240, 245, 191, 95]);
Modules
Structs
- A color represented with a Mode.
Enums
- Color mode indicating level interpretation.