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 from a u32 RGB hexadecimal value with max alpha.

Examples
let c = Color::from_hex(0xF0FF00);
assert_eq!(c.channels(), [240, 255, 0, 255]);

Constructs a Color from a u32 RGBA hexadecimal value.

Examples
let c = Color::from_hex_alpha(0xF0FF_00FF);
assert_eq!(c.channels(), [240, 255, 0, 255]);

let c = Color::from_hex_alpha(0xF0FF_0080);
assert_eq!(c.channels(), [240, 255, 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 the given Mode and max alpha.

Examples
let c = Color::with_mode(ColorMode::Rgb, 0, 0, 128);
assert_eq!(c.channels(), [0, 0, 128, 255]);

let c = Color::with_mode(ColorMode::Hsb, 126.0, 50.0, 100.0);
assert_eq!(c.channels(), [128, 255, 140, 255]);

Constructs a Color with the given Mode and alpha.

Examples
let c = Color::with_mode_alpha(ColorMode::Rgb, 0.0, 0.0, 128.0, 50.0);
assert_eq!(c.channels(), [0, 0, 128, 50]);

let c = Color::with_mode_alpha(ColorMode::Hsb, 126.0, 50.0, 100.0, 0.8);
assert_eq!(c.channels(), [128, 255, 140, 204]);

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 Color with the given Mode and alpha using levels ranging from 0.0..=1.0.

Example
let c = Color::from_levels(ColorMode::Rgb, 0.4, 0.5, 1.0, 0.8);
assert_eq!(c.channels(), [102, 128, 255, 204]);

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 the u32 RGB hexadecimal value of a Color.

Examples
let c = Color::rgb(240, 255, 0);
assert_eq!(c.as_hex(), 0xF0FF00);

Returns the u32 RGBA hexadecimal value of a Color.

Examples
let c = Color::rgb(240, 255, 0);
assert_eq!(c.as_hex_alpha(), 0xF0FF00FF);

let c = Color::rgba(240, 255, 0, 128);
assert_eq!(c.as_hex_alpha(), 0xF0FF0080);

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.

Set the Color levels ranging from 0.0..=1.0 using the current Mode.

Example
let mut c = Color::rgba(128, 64, 128, 128);
c.set_levels([1.0, 0.5, 0.4, 1.0]);
assert_eq!(c.channels(), [255, 128, 102, 255]);

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 current color Mode.

Examples
let c = Color::rgb(100, 0, 0);
assert_eq!(c.mode(), ColorMode::Rgb);

let c = Color::hsb(100.0, 0.0, 0.0);
assert_eq!(c.mode(), ColorMode::Hsb);

Set the color Mode.

Example
let mut c = Color::rgb(100, 0, 0);
c.set_mode(ColorMode::Hsb);
assert_eq!(c.mode(), ColorMode::Hsb);

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]);

Returns Color as a Vec of [red, green, blue, alpha].

Example
let c = color!(100, 200, 50);
assert_eq!(c.to_vec(), vec![100, 200, 50, 255]);

Methods from Deref<Target = [u8; 4]>§

Returns a slice containing the entire array. Equivalent to &s[..].

🔬This is a nightly-only experimental API. (array_methods)

Borrows each element and returns an array of references with the same size as self.

Example
#![feature(array_methods)]

let floats = [3.1, 2.7, -1.0];
let float_refs: [&f64; 3] = floats.each_ref();
assert_eq!(float_refs, [&3.1, &2.7, &-1.0]);

This method is particularly useful if combined with other methods, like map. This way, you can avoid moving the original array if its elements are not Copy.

#![feature(array_methods)]

let strings = ["Ferris".to_string(), "♥".to_string(), "Rust".to_string()];
let is_ascii = strings.each_ref().map(|s| s.is_ascii());
assert_eq!(is_ascii, [true, false, true]);

// We can still access the original array: it has not been moved.
assert_eq!(strings.len(), 3);
🔬This is a nightly-only experimental API. (split_array)

Divides one array reference into two at an index.

The first will contain all indices from [0, M) (excluding the index M itself) and the second will contain all indices from [M, N) (excluding the index N itself).

Panics

Panics if M > N.

Examples
#![feature(split_array)]

let v = [1, 2, 3, 4, 5, 6];

{
   let (left, right) = v.split_array_ref::<0>();
   assert_eq!(left, &[]);
   assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}

{
    let (left, right) = v.split_array_ref::<2>();
    assert_eq!(left, &[1, 2]);
    assert_eq!(right, &[3, 4, 5, 6]);
}

{
    let (left, right) = v.split_array_ref::<6>();
    assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
    assert_eq!(right, &[]);
}
🔬This is a nightly-only experimental API. (split_array)

Divides one array reference into two at an index from the end.

The first will contain all indices from [0, N - M) (excluding the index N - M itself) and the second will contain all indices from [N - M, N) (excluding the index N itself).

Panics

Panics if M > N.

Examples
#![feature(split_array)]

let v = [1, 2, 3, 4, 5, 6];

{
   let (left, right) = v.rsplit_array_ref::<0>();
   assert_eq!(left, &[1, 2, 3, 4, 5, 6]);
   assert_eq!(right, &[]);
}

{
    let (left, right) = v.rsplit_array_ref::<2>();
    assert_eq!(left, &[1, 2, 3, 4]);
    assert_eq!(right, &[5, 6]);
}

{
    let (left, right) = v.rsplit_array_ref::<6>();
    assert_eq!(left, &[]);
    assert_eq!(right, &[1, 2, 3, 4, 5, 6]);
}

Trait Implementations§

The resulting type after applying the + operator.
Performs the + operation. Read more
The resulting type after applying the + operator.
Performs the + operation. Read more
Performs the += operation. Read more
Performs the += operation. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Deref Color to &[u8; 4].

The resulting type after dereferencing.
Deserialize this value from the given Serde deserializer. Read more

Display Color as “[r, g, b, a]”.

Formats the value using the given formatter. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
Performs the / operation. Read more
The resulting type after applying the / operator.
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

Convert f32 to grayscale Color

Convert [f32; 2] to grayscale Color with alpha

Convert [f32; 3] to Color with max alpha

Convert [f32; 4] to Color

Convert f64 to grayscale Color

Convert [f64; 2] to grayscale Color with alpha

Convert [f64; 3] to Color with max alpha

Convert [f64; 4] to Color

Convert i16 to grayscale Color

Convert [i16; 2] to grayscale Color with alpha

Convert [i16; 3] to Color with max alpha

Convert [i16; 4] to Color

Convert i32 to grayscale Color

Convert [i32; 2] to grayscale Color with alpha

Convert [i32; 3] to Color with max alpha

Convert [i32; 4] to Color

Convert i8 to grayscale Color

Convert [i8; 2] to grayscale Color with alpha

Convert [i8; 3] to Color with max alpha

Convert [i8; 4] to Color

Convert u16 to grayscale Color

Convert [u16; 2] to grayscale Color with alpha

Convert [u16; 3] to Color with max alpha

Convert [u16; 4] to Color

Convert u32 to grayscale Color

Convert [u32; 2] to grayscale Color with alpha

Convert [u32; 3] to Color with max alpha

Convert [u32; 4] to Color

Convert u8 to grayscale Color

Convert [u8; 2] to grayscale Color with alpha

Convert [u8; 3] to Color with max alpha

Convert [u8; 4] to Color

Convert f32 to grayscale Color

Convert f64 to grayscale Color

Convert i16 to grayscale Color

Convert i32 to grayscale Color

Convert i8 to grayscale Color

Convert u16 to grayscale Color

Convert u32 to grayscale Color

Convert u8 to grayscale Color

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]);
The associated error which can be returned from parsing.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Formats the value using the given formatter.
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
Performs the * operation. Read more
The resulting type after applying the * operator.
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
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Serialize this value into the given Serde serializer. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
The resulting type after applying the - operator.
Performs the - operation. Read more
Performs the -= operation. Read more
Performs the -= operation. Read more

Try to create a Color from a hexadecimal string.

The type returned in the event of a conversion error.
Formats the value using the given formatter.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Checks if this value is equivalent to the given key. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.