EncodedColor

Struct EncodedColor 

Source
#[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: u8

The red component of the color.

§g: u8

The green component of the color.

§b: u8

The blue component of the color.

§a: u8

The alpha component of the color, normally the opacity in blending operations.

Implementations§

Source§

impl EncodedColor

Source

pub const fn new(r: u8, g: u8, b: u8, a: u8) -> Self

Creates a new encoded 32bit color.

Source

pub const fn with_r(self, r: u8) -> Self

Creates a new color, overriding red with provided value.

Source

pub const fn with_g(self, g: u8) -> Self

Creates a new color, overriding green with provided value.

Source

pub const fn with_b(self, b: u8) -> Self

Creates a new color, overriding blue with provided value.

Source

pub const fn with_a(self, a: u8) -> Self

Creates a new color, overriding alpha with provided value.

Source

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:

  • RRGGBBAA or RRGGBB.
  • #RRGGBBAA or #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..=F or a..=f
  • 0..=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());
Source

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:

  • RRGGBBAA or RRGGBB.
  • #RRGGBBAA or #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..=F or a..=f
  • 0..=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.

Source

pub const fn to_linear(self) -> LinearColor

Transforms this color into the Linear color space.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub const fn from_bits_u32(value: u32) -> Self

Recasts four u8s into EncodedColor

Source

pub const fn from_bits(value: [u8; 4]) -> Self

Recasts four u8s into EncodedColor

Source§

impl EncodedColor

Source

pub const WHITE: EncodedColor

A basic white (255, 255, 255, 255) with full opacity.

Source

pub const BLACK: EncodedColor

A basic black (0, 0, 0, 255) with full opacity.

Source

pub const CLEAR: EncodedColor

A black (0, 0, 0, 0) with zero opacity.

Source

pub const RED: EncodedColor

Full alpha Red (255, 0, 0, 255)

Source

pub const RED_CLEAR: EncodedColor

Zero alpha Red (255, 0, 0, 255)

Source

pub const GREEN: EncodedColor

Full alpha green (255, 0, 0, 255)

Source

pub const GREEN_CLEAR: EncodedColor

Zero alpha green (255, 0, 0, 255)

Source

pub const BLUE: EncodedColor

Full alpha blue (255, 0, 0, 255)

Source

pub const BLUE_CLEAR: EncodedColor

Zero alpha blue (255, 0, 0, 255)

Source

pub const YELLOW: EncodedColor

Full alpha Yellow (255, 255, 0, 255).

Source

pub const YELLOW_CLEAR: EncodedColor

Zero alpha Yellow (255, 255, 0, 0).

Source

pub const FUCHSIA: EncodedColor

God’s color (255, 0, 255, 255). The color of choice for graphics testing.

Source

pub const FUCHSIA_CLEAR: EncodedColor

God’s color but clear (255, 0, 255, 255). The color of choice for graphics testing.

Source

pub const TEAL: EncodedColor

Full alpha Teal (0, 255, 255, 255).

Source

pub const TEAL_CLEAR: EncodedColor

Zero alpha Teal (0, 255, 255, 0).

Trait Implementations§

Source§

impl Clone for EncodedColor

Source§

fn clone(&self) -> EncodedColor

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EncodedColor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for EncodedColor

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for EncodedColor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<[u8; 4]> for EncodedColor

Source§

fn from(o: [u8; 4]) -> Self

Converts to this type from the input type.
Source§

impl From<EncodedColor> for [u8; 4]

Source§

fn from(o: EncodedColor) -> Self

Converts to this type from the input type.
Source§

impl From<EncodedColor> for LinearColor

Source§

fn from(o: EncodedColor) -> Self

Converts to this type from the input type.
Source§

impl From<LinearColor> for EncodedColor

Source§

fn from(o: LinearColor) -> Self

Converts to this type from the input type.
Source§

impl FromStr for EncodedColor

Source§

type Err = HexCodeParseErr

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for EncodedColor

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl LowerHex for EncodedColor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Ord for EncodedColor

Source§

fn cmp(&self, other: &EncodedColor) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for EncodedColor

Source§

fn eq(&self, other: &EncodedColor) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for EncodedColor

Source§

fn partial_cmp(&self, other: &EncodedColor) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl UpperHex for EncodedColor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for EncodedColor

Source§

impl Eq for EncodedColor

Source§

impl StructuralPartialEq for EncodedColor

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.