Struct pix_engine::color::Color
source · [−]pub struct Color { /* private fields */ }Expand description
A color represented with a Mode.
Implementations
Constructs a Color from a slice of 1-4 values. The number of values
provided alter how they are interpreted similar to the color!, rgb!, hsb!, and
hsl! macros.
Errors
If the slice is empty or has more than 4 values, an error is returned.
Examples
let vals: Vec<f64> = vec![128.0, 64.0, 0.0];
let c = Color::from_slice(ColorMode::Rgb, &vals)?; // RGB Vec
assert_eq!(c.channels(), [128, 64, 0, 255]);
let vals: [f64; 4] = [128.0, 64.0, 0.0, 128.0];
let c = Color::from_slice(ColorMode::Rgb, &vals[..])?; // RGBA slice
assert_eq!(c.channels(), [128, 64, 0, 128]);Constructs a Color by inverting the RGBA values.
Example
let c = Color::from_hex(0xF0FF00);
assert_eq!(c.inverted().as_hex(), 0x0F00FF);Constructs an opaque Color blended over a given background, using an alpha value.
Constructs a Color by linear interpolating between two Colors by a given amount between
0.0 and 1.0.
Examples
let from = rgb!(255, 0, 0);
let to = rgb!(0, 100, 255);
let lerped = from.lerp(to, 0.5);
assert_eq!(lerped.channels(), [128, 50, 128, 255]);
let from = rgb!(255, 0, 0);
let to = hsb!(120.0, 80.0, 100.0, 0.5);
let lerped = from.lerp(to, 0.25); // `to` is implicity converted to RGB
assert_eq!(lerped.channels(), [204, 64, 13, 223]);Constructs a Color with red, green, blue and max alpha.
Example
let c = Color::new(0, 0, 128);
assert_eq!(c.channels(), [0, 0, 128, 255]);Constructs a Color with red, green, blue and alpha.
Example
let c = Color::new_alpha(0, 0, 128, 50);
assert_eq!(c.channels(), [0, 0, 128, 50]);Constructs a Color with red, green, blue and max alpha.
Alias for Color::new.
Example
let c = Color::rgb(128, 64, 0);
assert_eq!(c.channels(), [128, 64, 0, 255]);Constructs a Color with red, green, blue and alpha.
Alias for Color::new_alpha.
Example
let c = Color::rgba(128, 64, 128, 128);
assert_eq!(c.channels(), [128, 64, 128, 128]);Constructs a Color with hue, saturation, brightness and max alpha.
Example
let c = Color::hsb(126.0, 80.0, 50.0);
assert_eq!(c.channels(), [25, 128, 36, 255]);Constructs a Color with hue, saturation, brightness and alpha.
Example
let c = Color::hsba(126.0, 80.0, 50.0, 0.5);
assert_eq!(c.channels(), [25, 128, 36, 128]);Constructs a Color with hue, saturation, lightness and max alpha.
Example
let c = Color::hsl(126.0, 80.0, 50.0);
assert_eq!(c.channels(), [25, 230, 46, 255]);Constructs a Color with hue, saturation, lightness and alpha.
Example
let c = Color::hsla(126.0, 80.0, 50.0, 0.5);
assert_eq!(c.channels(), [25, 230, 46, 128]);Constructs a random Color with red, green, blue and max alpha.
Example
let c = Color::random();
// `c.channels()` will return something like:
// [207, 12, 217, 255]Constructs a random Color with red, green, blue and alpha.
Example
let c = Color::random_alpha();
// `c.channels()` will return something like:
// [132, 159, 233, 76]Returns a list of max values for each color channel based on Mode.
Examples
let c = Color::rgb(0, 0, 0);
assert_eq!(c.maxes(), [255.0, 255.0, 255.0, 255.0]);
let c = Color::hsb(0.0, 0.0, 0.0);
assert_eq!(c.maxes(), [360.0, 100.0, 100.0, 1.0]);
let c = Color::hsl(0.0, 0.0, 0.0);
assert_eq!(c.maxes(), [360.0, 100.0, 100.0, 1.0]);Returns the Color levels for the given Mode which range from 0.0..=1.0.
Returns the Color channels as [red, green, blue, alpha] which range from 0..=255.
Example
let c = Color::rgba(128, 64, 128, 128);
assert_eq!(c.channels(), [128, 64, 128, 128]);Returns the red Color channel ranging from 0..=255.
Example
let c = Color::rgb(100, 0, 0);
assert_eq!(c.red(), 100);Set the red Color channel ranging from 0..=255.
Example
let mut c = Color::default();
assert_eq!(c.channels(), [0, 0, 0, 255]);
c.set_red(100);
assert_eq!(c.channels(), [100, 0, 0, 255]);Returns the green Color channel ranging from 0..=255.
Example
let c = Color::rgb(0, 100, 0);
assert_eq!(c.green(), 100);Set the green Color channel ranging from 0..=255.
Example
let mut c = Color::default();
assert_eq!(c.channels(), [0, 0, 0, 255]);
c.set_green(100);
assert_eq!(c.channels(), [0, 100, 0, 255]);Returns the blue Color channel ranging from 0..=255.
Example
let c = Color::rgb(0, 0, 100);
assert_eq!(c.blue(), 100);Set the blue Color channel ranging from 0..=255.
Example
let mut c = Color::default();
assert_eq!(c.channels(), [0, 0, 0, 255]);
c.set_blue(100);
assert_eq!(c.channels(), [0, 0, 100, 255]);Returns the alpha Color channel ranging from 0..=255.
Examples
let c = Color::rgba(0, 0, 0, 100);
assert_eq!(c.alpha(), 100);Set the alpha Color channel ranging from 0..=255.
Examples
let mut c = Color::default();
assert_eq!(c.channels(), [0, 0, 0, 255]);
c.set_alpha(100);
assert_eq!(c.channels(), [0, 0, 0, 100]);Returns the hue ranging from 0.0..=360.0.
Example
let c = Color::rgb(0, 100, 0);
assert_eq!(c.hue(), 120.0);Set the hue ranging from 0.0..=360.0.
Example
let mut c = Color::rgb(128, 0, 0);
assert_eq!(c.channels(), [128, 0, 0, 255]);
c.set_hue(100.0);
assert_eq!(c.channels(), [43, 128, 0, 255]);Returns the saturation ranging from 0.0..=100.0.
Example
let c = Color::rgb(0, 100, 0);
assert_eq!(c.saturation(), 100.0);Set the saturation ranging from 0.0..=100.0. Defaults to Hsb if the
current mode is not Hsb or Hsl already.
Examples
let mut c = Color::rgb(128, 0, 0);
assert_eq!(c.channels(), [128, 0, 0, 255]);
c.set_saturation(50.0);
assert_eq!(c.channels(), [128, 64, 64, 255]);
let mut c = Color::rgb(128, 0, 0);
c.set_mode(ColorMode::Hsl);
assert_eq!(c.channels(), [128, 0, 0, 255]);
c.set_saturation(50.0);
assert_eq!(c.channels(), [96, 32, 32, 255]);Returns the brightness ranging from 0.0..=100.0.
Example
let c = Color::rgb(0, 102, 0);
assert_eq!(c.brightness(), 40.0);Set the brightness ranging from 0.0..=100.0.
Example
let mut c = Color::rgb(128, 0, 0);
assert_eq!(c.channels(), [128, 0, 0, 255]);
c.set_brightness(90.0);
assert_eq!(c.channels(), [230, 0, 0, 255]);Returns the lightness ranging from 0.0..=100.0.
Example
let c = Color::rgb(0, 102, 0);
assert_eq!(c.lightness(), 20.0);Set the lightness ranging from 0.0..=100.0.
Example
let mut c = Color::rgb(128, 0, 0);
assert_eq!(c.channels(), [128, 0, 0, 255]);
c.set_lightness(90.0);
assert_eq!(c.channels(), [255, 204, 204, 255]);Trait Implementations
Performs the += operation. Read more
Performs the += operation. Read more
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Display Color as “[r, g, b, a]”.
Performs the /= operation. Read more
Performs the /= operation. Read more
Performs the /= operation. Read more
Performs the /= operation. Read more
Performs the /= operation. Read more
Performs the /= operation. Read more
Performs the /= operation. Read more
Performs the /= operation. Read more
Performs the /= operation. Read more
Performs the /= operation. Read more
Performs the /= operation. Read more
Performs the /= operation. Read more
Performs the /= operation. Read more
Performs the /= operation. Read more
Converts to Color from a hexadecimal string.
Examples
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]);Performs the *= operation. Read more
Performs the *= operation. Read more
Performs the *= operation. Read more
Performs the *= operation. Read more
Performs the *= operation. Read more
Performs the *= operation. Read more
Performs the *= operation. Read more
Performs the *= operation. Read more
Performs the *= operation. Read more
Performs the *= operation. Read more
Performs the *= operation. Read more
Performs the *= operation. Read more
Performs the *= operation. Read more
Performs the *= operation. Read more
Performs the -= operation. Read more
Performs the -= operation. Read more
Auto Trait Implementations
impl RefUnwindSafe for Color
impl UnwindSafe for Color
Blanket Implementations
Mutably borrows from an owned value. Read more