#[repr(C)]pub struct EncodedColor {
pub r: u8,
pub g: u8,
pub b: u8,
pub a: u8,
}Expand description
A color used in linear applications. On a technical level, this color is in sRGB; however, this name is not very clear.
In code, we will say that this Color is encoded. This is generally the same
colorspace that texels in a texture are in. This color space is not valid
to perform mixing operations between colors in, so we must convert this
color space into a different color, LinearColor, with to_linear
before we do such operations.
Fields§
§r: u8The red component of the color.
g: u8The green component of the color.
b: u8The blue component of the color.
a: u8The alpha component of the color, normally the opacity in blending operations.
Implementations§
Source§impl EncodedColor
impl EncodedColor
Sourcepub const fn with_r(self, r: u8) -> Self
pub const fn with_r(self, r: u8) -> Self
Creates a new color, overriding red with provided value.
Sourcepub const fn with_g(self, g: u8) -> Self
pub const fn with_g(self, g: u8) -> Self
Creates a new color, overriding green with provided value.
Sourcepub const fn with_b(self, b: u8) -> Self
pub const fn with_b(self, b: u8) -> Self
Creates a new color, overriding blue with provided value.
Sourcepub const fn with_a(self, a: u8) -> Self
pub const fn with_a(self, a: u8) -> Self
Creates a new color, overriding alpha with provided value.
Sourcepub const fn try_from_hex_code(input: &str) -> Result<Self, HexCodeParseErr>
pub const fn try_from_hex_code(input: &str) -> Result<Self, HexCodeParseErr>
Tries to convert a hex code, as a string, into an EncodedColor.
Colors may be in one of four forms:
RRGGBBAAorRRGGBB.#RRGGBBAAor#RRGGBB.
When the alpha is not provided in a 6-character (ignoring the possible # character),
then it will be 0.
use smol_rgb::EncodedColor;
assert_eq!(EncodedColor::try_from_hex_code("FEEEED").unwrap(), EncodedColor::new(254, 238, 237, 0));
assert_eq!(EncodedColor::try_from_hex_code("01090402").unwrap(), EncodedColor::new(1, 9, 4, 2));
assert_eq!(EncodedColor::try_from_hex_code("A1B9C4D2").unwrap(), EncodedColor::new(161, 185, 196, 210));
// adding a `#` doesn't matter
assert_eq!(EncodedColor::try_from_hex_code("#FEEEED").unwrap(), EncodedColor::try_from_hex_code("FEEEED").unwrap());
assert_eq!(EncodedColor::try_from_hex_code("#FEEEEDAA").unwrap(), EncodedColor::new(254, 238, 237, 170));
// you must submit either a 6 code or 8 code string. It may or may not have a leading `#`.
assert!(EncodedColor::try_from_hex_code("#2B0E1D").is_ok());
assert!(EncodedColor::try_from_hex_code("#2B0E1DD").is_err());
assert!(EncodedColor::try_from_hex_code("#2B0E1DB").is_err());
assert!(EncodedColor::try_from_hex_code("#2B0E1DBB").is_ok());
assert!(EncodedColor::try_from_hex_code("2B0E1DBB").is_ok());
assert!(EncodedColor::try_from_hex_code("2B0E1DB").is_err());
assert!(EncodedColor::try_from_hex_code("").is_err());The characters used must be valid hex digits, which is to say:
A..=Fora..=f0..=9
use smol_rgb::EncodedColor;
assert!(EncodedColor::try_from_hex_code("2B0E1DBB").is_ok());
assert!(EncodedColor::try_from_hex_code("2b0e1dbb").is_ok());
// caps and non-caps is chaotic, but allowable
assert!(EncodedColor::try_from_hex_code("2B0e1DbB").is_ok());
assert!(EncodedColor::try_from_hex_code("2B010203G").is_err());
assert!(EncodedColor::try_from_hex_code("2B010203GG").is_err());
// no hashtags except one at the start
assert!(EncodedColor::try_from_hex_code("2B010203#").is_err());
assert!(EncodedColor::try_from_hex_code("2B010203##").is_err());
assert!(EncodedColor::try_from_hex_code("##2B0102").is_err());
assert!(EncodedColor::try_from_hex_code("##2B010203").is_err());
assert!(EncodedColor::try_from_hex_code("🦀🦀").is_err());Sourcepub const fn from_hex_code(input: &str) -> Self
pub const fn from_hex_code(input: &str) -> Self
Convert a hex code, as a string, into an EncodedColor or panics.
Colors may be in one of four forms:
RRGGBBAAorRRGGBB.#RRGGBBAAor#RRGGBB.
When the alpha is not provided in a 6-character (ignoring the possible # character),
then it will be 0.
The characters used must be valid hex digits, which is to say:
A..=Fora..=f0..=9
use smol_rgb::EncodedColor;
assert_eq!(EncodedColor::from_hex_code("FEEEED"), EncodedColor::new(254, 238, 237, 0));
assert_eq!(EncodedColor::from_hex_code("01090402"), EncodedColor::new(1, 9, 4, 2));
assert_eq!(EncodedColor::from_hex_code("A1B9C4D2"), EncodedColor::new(161, 185, 196, 210));
// adding a `#` doesn't matter
assert_eq!(EncodedColor::from_hex_code("#FEEEED"), EncodedColor::from_hex_code("FEEEED"));
assert_eq!(EncodedColor::from_hex_code("#FEEEEDAA"), EncodedColor::new(254, 238, 237, 170));See EncodedColor::try_from_hex_code for a non-panicking variant.
Sourcepub const fn to_linear(self) -> LinearColor
pub const fn to_linear(self) -> LinearColor
Transforms this color into the Linear color space.
Sourcepub const fn to_encoded_f32s(self) -> [f32; 4]
pub const fn to_encoded_f32s(self) -> [f32; 4]
Converts this color to an [f32; 4] array. This is still in encoded space but they are converted to an f32. This is mostly for compatability with other libraries which sometimes need to f32s even while in encoded sRGB.
We use this dedicated function, rather than a From or Into because
this is an unusual use of f32s, and in general, this module acts as if
f32 == Linear and u8 == Encoded, though this is not technically true.
Sourcepub const fn to_array(self) -> [u8; 4]
pub const fn to_array(self) -> [u8; 4]
Creates an array representation of the color. This is useful for sending the color
to a uniform, but is the same memory representation as Self.
Sourcepub const fn from_encoded_f32s(input: [f32; 4]) -> Self
pub const fn from_encoded_f32s(input: [f32; 4]) -> Self
Converts this color to an [f32; 4] array. This is still in encoded space but they are converted to an f32. This is mostly for compatability with other libraries which sometimes need to f32s even while in encoded sRGB.
We use this dedicated function, rather than a From or Into because
this is an unusual use of f32s, and in general, this module acts as if
f32 == Linear and u8 == Encoded, though this is not technically true.
Sourcepub const fn from_rgba_u32(input: u32) -> Self
pub const fn from_rgba_u32(input: u32) -> Self
Converts a packed u32 to an encoded rgba struct.
Note, your colors must be in order of red, green, blue, alpha. For bgra support,
use from_bgra_u32.
This function might also has issues on non-little endian platforms, but look, you’re not on one of those.
Sourcepub const fn to_rgba_u32(self) -> u32
pub const fn to_rgba_u32(self) -> u32
Converts the encoded rgba struct to a packed u32 in rgba encoding.
This will output your colors in order of red, green, blue, alpha. For bgra support,
use to_bgra_u32.
This function might also have issues on non-little endian platforms, but look, you’re not on one of those.
Sourcepub const fn from_bgra_u32(input: u32) -> Self
pub const fn from_bgra_u32(input: u32) -> Self
Converts a packed u32 to an encoded rgba struct. On little endian platforms, this is a no-op.
Note, your colors must be in order of blue, green, red, alpha.
This function might also has issues on non-little endian platforms, but look, you’re not on one of those probably.
Sourcepub const fn to_bgra_u32(self) -> u32
pub const fn to_bgra_u32(self) -> u32
Converts the encoded rgba struct to a packed u32 in bgra encoding.
This will output your colors in order of red, green, blue, alpha. For bgra support,
use to_bgra_u32.
This function might also have issues on non-little endian platforms, but look, you’re not on one of those.
Sourcepub const fn from_bits_u32(value: u32) -> Self
pub const fn from_bits_u32(value: u32) -> Self
Recasts four u8s into EncodedColor
Source§impl EncodedColor
impl EncodedColor
Sourcepub const WHITE: EncodedColor
pub const WHITE: EncodedColor
A basic white (255, 255, 255, 255) with full opacity.
Sourcepub const BLACK: EncodedColor
pub const BLACK: EncodedColor
A basic black (0, 0, 0, 255) with full opacity.
Sourcepub const CLEAR: EncodedColor
pub const CLEAR: EncodedColor
A black (0, 0, 0, 0) with zero opacity.
Sourcepub const RED: EncodedColor
pub const RED: EncodedColor
Full alpha Red (255, 0, 0, 255)
Sourcepub const RED_CLEAR: EncodedColor
pub const RED_CLEAR: EncodedColor
Zero alpha Red (255, 0, 0, 255)
Sourcepub const GREEN: EncodedColor
pub const GREEN: EncodedColor
Full alpha green (255, 0, 0, 255)
Sourcepub const GREEN_CLEAR: EncodedColor
pub const GREEN_CLEAR: EncodedColor
Zero alpha green (255, 0, 0, 255)
Sourcepub const BLUE: EncodedColor
pub const BLUE: EncodedColor
Full alpha blue (255, 0, 0, 255)
Sourcepub const BLUE_CLEAR: EncodedColor
pub const BLUE_CLEAR: EncodedColor
Zero alpha blue (255, 0, 0, 255)
Sourcepub const YELLOW: EncodedColor
pub const YELLOW: EncodedColor
Full alpha Yellow (255, 255, 0, 255).
Sourcepub const YELLOW_CLEAR: EncodedColor
pub const YELLOW_CLEAR: EncodedColor
Zero alpha Yellow (255, 255, 0, 0).
Sourcepub const FUCHSIA: EncodedColor
pub const FUCHSIA: EncodedColor
God’s color (255, 0, 255, 255). The color of choice for graphics testing.
Sourcepub const FUCHSIA_CLEAR: EncodedColor
pub const FUCHSIA_CLEAR: EncodedColor
God’s color but clear (255, 0, 255, 255). The color of choice for graphics testing.
Sourcepub const TEAL: EncodedColor
pub const TEAL: EncodedColor
Full alpha Teal (0, 255, 255, 255).
Sourcepub const TEAL_CLEAR: EncodedColor
pub const TEAL_CLEAR: EncodedColor
Zero alpha Teal (0, 255, 255, 0).
Trait Implementations§
Source§impl Clone for EncodedColor
impl Clone for EncodedColor
Source§fn clone(&self) -> EncodedColor
fn clone(&self) -> EncodedColor
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more