Module pix_engine::color::conversion
source · Expand description
Color conversion functions.
Provides methods for converting Colors:
Color::from_str
: Parse from a hexadecimal string (e.g. “#FF0”, or “#00FF00”).Color::from_slice
: Parse from an RGB/A slice (e.g. [0, 125, 0] or [255, 255, 0, 125]).Color::from_hex
: Parse from au32
RGBA hexadecimal value (e.g. 0x00FF00FF).Color::as_hex
: Convert back to au32
RGBA hexadecimal value.Color::inverted
: Invert the RGB colors channel-wise, ignoring the alpha channel.Color::lerp
: Linear interpolate between two colors, channel-wise, including the alpha channel.
Examples
use std::str::FromStr;
let c = Color::from_str("#F0F5BF")?;
assert_eq!(c.channels(), [240, 245, 191, 255]);
let vals = [128.0, 64.0, 0.0];
let c = Color::from_slice(ColorMode::Rgb, &vals)?;
assert_eq!(c.channels(), [128, 64, 0, 255]);
let c = Color::from_hex(0xF0FF_00FF);
assert_eq!(c.channels(), [240, 255, 0, 255]);
let c = Color::rgba(255, 0, 255, 125);
assert_eq!(c.inverted().as_hex(), 0xFF00_FF7D);
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]);