Struct ColorToken

Source
pub struct ColorToken<Role: ColorRole> {
    r: u8,
    g: u8,
    b: u8,
    a: u8,
    _role: PhantomData<Role>,
}
Expand description

A color token with its semantic role encoded in the type.

ColorToken<Role> represents an RGBA color value along with its semantic meaning in the design system. The role is tracked at compile time via the type parameter, enabling the compiler to prevent misuse.

§Type Parameter

The Role parameter must implement ColorRole and determines how the color can be used:

  • ColorToken<Surface> — Can be used where BackgroundRole is expected
  • ColorToken<Foreground> — Can be used where ForegroundRole is expected

§Memory Layout

Despite the type parameter, ColorToken has the same size as four bytes:

use fernctl::domain::tokens::color::*;
use std::mem::size_of;

assert_eq!(size_of::<ColorToken<Surface>>(), 4);

This is because PhantomData<Role> is a zero-sized type.

§Creating Colors

use fernctl::domain::tokens::color::*;

// From hex string (most common)
let color = ColorToken::<Surface>::from_hex("#313244").unwrap();

// From RGB values
let color = ColorToken::<Surface>::from_rgb(0x31, 0x32, 0x44);

// From RGBA values
let color = ColorToken::<Surface>::from_rgba(0x31, 0x32, 0x44, 0xFF);

§Converting to Output Formats

use fernctl::domain::tokens::color::*;

let color = ColorToken::<Surface>::from_hex("#313244").unwrap();

assert_eq!(color.to_hex(), "#313244");
assert_eq!(color.to_rgb_tuple(), (0x31, 0x32, 0x44));
assert_eq!(color.to_css_rgb(), "rgb(49, 50, 68)");

Fields§

§r: u8

Red component (0-255).

§g: u8

Green component (0-255).

§b: u8

Blue component (0-255).

§a: u8

Alpha component (0-255, where 255 is fully opaque).

§_role: PhantomData<Role>

Marker for the color’s semantic role.

Implementations§

Source§

impl<Role: ColorRole> ColorToken<Role>

Source

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

Creates a new color token from RGB values (fully opaque).

§Arguments
  • r — Red component (0-255)
  • g — Green component (0-255)
  • b — Blue component (0-255)
§Example
use fernctl::domain::tokens::color::*;

let color = ColorToken::<Surface>::from_rgb(0x31, 0x32, 0x44);
assert_eq!(color.alpha(), 255);
Source

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

Creates a new color token from RGBA values.

§Arguments
  • r — Red component (0-255)
  • g — Green component (0-255)
  • b — Blue component (0-255)
  • a — Alpha component (0-255, where 255 is fully opaque)
§Example
use fernctl::domain::tokens::color::*;

let color = ColorToken::<Surface>::from_rgba(0x31, 0x32, 0x44, 0x80);
assert_eq!(color.alpha(), 0x80);
Source

pub fn from_hex(hex: &str) -> Result<Self, ConfigError>

Creates a color token from a hex string.

Supports the following formats:

  • #RGB — 3-digit shorthand (expands to 6-digit)
  • #RRGGBB — 6-digit hex (opaque)
  • #RRGGBBAA — 8-digit hex with alpha

The # prefix is required.

§Errors

Returns ConfigError::InvalidColor if:

  • The string doesn’t start with #
  • The length is not 4, 7, or 9 characters
  • Any character is not a valid hex digit
§Example
use fernctl::domain::tokens::color::*;

// 6-digit hex
let color = ColorToken::<Surface>::from_hex("#313244").unwrap();

// 3-digit shorthand
let white = ColorToken::<Foreground>::from_hex("#fff").unwrap();

// 8-digit with alpha
let transparent = ColorToken::<Surface>::from_hex("#31324480").unwrap();

// Invalid
assert!(ColorToken::<Surface>::from_hex("313244").is_err());  // Missing #
assert!(ColorToken::<Surface>::from_hex("#gg0000").is_err()); // Invalid hex
Source

pub const fn red(&self) -> u8

Returns the red component (0-255).

Source

pub const fn green(&self) -> u8

Returns the green component (0-255).

Source

pub const fn blue(&self) -> u8

Returns the blue component (0-255).

Source

pub const fn alpha(&self) -> u8

Returns the alpha component (0-255, where 255 is fully opaque).

Source

pub const fn is_opaque(&self) -> bool

Returns true if the color is fully opaque (alpha = 255).

Source

pub const fn to_rgb_tuple(&self) -> (u8, u8, u8)

Returns the color as an RGB tuple.

§Example
use fernctl::domain::tokens::color::*;

let color = ColorToken::<Surface>::from_hex("#313244").unwrap();
assert_eq!(color.to_rgb_tuple(), (0x31, 0x32, 0x44));
Source

pub const fn to_rgba_tuple(&self) -> (u8, u8, u8, u8)

Returns the color as an RGBA tuple.

§Example
use fernctl::domain::tokens::color::*;

let color = ColorToken::<Surface>::from_rgba(0x31, 0x32, 0x44, 0x80);
assert_eq!(color.to_rgba_tuple(), (0x31, 0x32, 0x44, 0x80));
Source

pub fn to_hex(&self) -> String

Returns the color as a hex string.

Returns 6-digit format for opaque colors, 8-digit for transparent.

§Example
use fernctl::domain::tokens::color::*;

let opaque = ColorToken::<Surface>::from_hex("#313244").unwrap();
assert_eq!(opaque.to_hex(), "#313244");

let transparent = ColorToken::<Surface>::from_rgba(0x31, 0x32, 0x44, 0x80);
assert_eq!(transparent.to_hex(), "#31324480");
Source

pub fn to_css_rgb(&self) -> String

Returns the color as a CSS rgb() or rgba() function.

§Example
use fernctl::domain::tokens::color::*;

let opaque = ColorToken::<Surface>::from_rgb(49, 50, 68);
assert_eq!(opaque.to_css_rgb(), "rgb(49, 50, 68)");

let transparent = ColorToken::<Surface>::from_rgba(49, 50, 68, 128);
assert_eq!(transparent.to_css_rgb(), "rgba(49, 50, 68, 0.50)");
Source

pub fn role_name(&self) -> &'static str

Returns the semantic role name of this color.

§Example
use fernctl::domain::tokens::color::*;

let color = ColorToken::<Surface>::from_rgb(0, 0, 0);
assert_eq!(color.role_name(), "surface");
Source

pub const fn with_alpha(&self, alpha: u8) -> Self

Creates a new color with a different alpha value.

§Example
use fernctl::domain::tokens::color::*;

let opaque = ColorToken::<Surface>::from_hex("#313244").unwrap();
let transparent = opaque.with_alpha(128);
assert_eq!(transparent.alpha(), 128);

Trait Implementations§

Source§

impl<Role: Clone + ColorRole> Clone for ColorToken<Role>

Source§

fn clone(&self) -> ColorToken<Role>

Returns a copy 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<Role: ColorRole> Debug for ColorToken<Role>

Source§

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

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

impl<Role: ColorRole> Default for ColorToken<Role>

Source§

fn default() -> Self

Returns a default color (black, fully opaque).

Source§

impl<'de, Role: ColorRole> Deserialize<'de> for ColorToken<Role>

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<Role: ColorRole> Display for ColorToken<Role>

Source§

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

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

impl<Role: Hash + ColorRole> Hash for ColorToken<Role>

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<Role: PartialEq + ColorRole> PartialEq for ColorToken<Role>

Source§

fn eq(&self, other: &ColorToken<Role>) -> 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<Role: ColorRole> Serialize for ColorToken<Role>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<Role: Copy + ColorRole> Copy for ColorToken<Role>

Source§

impl<Role: Eq + ColorRole> Eq for ColorToken<Role>

Source§

impl<Role: ColorRole> StructuralPartialEq for ColorToken<Role>

Auto Trait Implementations§

§

impl<Role> Freeze for ColorToken<Role>

§

impl<Role> RefUnwindSafe for ColorToken<Role>
where Role: RefUnwindSafe,

§

impl<Role> Send for ColorToken<Role>
where Role: Send,

§

impl<Role> Sync for ColorToken<Role>
where Role: Sync,

§

impl<Role> Unpin for ColorToken<Role>
where Role: Unpin,

§

impl<Role> UnwindSafe for ColorToken<Role>
where Role: UnwindSafe,

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
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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.

§

impl<D> OwoColorize for D

§

fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>
where C: Color,

Set the foreground color generically Read more
§

fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>
where C: Color,

Set the background color generically. Read more
§

fn black(&self) -> FgColorDisplay<'_, Black, Self>

Change the foreground color to black
§

fn on_black(&self) -> BgColorDisplay<'_, Black, Self>

Change the background color to black
§

fn red(&self) -> FgColorDisplay<'_, Red, Self>

Change the foreground color to red
§

fn on_red(&self) -> BgColorDisplay<'_, Red, Self>

Change the background color to red
§

fn green(&self) -> FgColorDisplay<'_, Green, Self>

Change the foreground color to green
§

fn on_green(&self) -> BgColorDisplay<'_, Green, Self>

Change the background color to green
§

fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>

Change the foreground color to yellow
§

fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>

Change the background color to yellow
§

fn blue(&self) -> FgColorDisplay<'_, Blue, Self>

Change the foreground color to blue
§

fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>

Change the background color to blue
§

fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to magenta
§

fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to magenta
§

fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>

Change the foreground color to purple
§

fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>

Change the background color to purple
§

fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>

Change the foreground color to cyan
§

fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>

Change the background color to cyan
§

fn white(&self) -> FgColorDisplay<'_, White, Self>

Change the foreground color to white
§

fn on_white(&self) -> BgColorDisplay<'_, White, Self>

Change the background color to white
§

fn default_color(&self) -> FgColorDisplay<'_, Default, Self>

Change the foreground color to the terminal default
§

fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>

Change the background color to the terminal default
§

fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>

Change the foreground color to bright black
§

fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>

Change the background color to bright black
§

fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>

Change the foreground color to bright red
§

fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>

Change the background color to bright red
§

fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>

Change the foreground color to bright green
§

fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>

Change the background color to bright green
§

fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>

Change the foreground color to bright yellow
§

fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>

Change the background color to bright yellow
§

fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>

Change the foreground color to bright blue
§

fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>

Change the background color to bright blue
§

fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright magenta
§

fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright magenta
§

fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>

Change the foreground color to bright purple
§

fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>

Change the background color to bright purple
§

fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>

Change the foreground color to bright cyan
§

fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>

Change the background color to bright cyan
§

fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>

Change the foreground color to bright white
§

fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>

Change the background color to bright white
§

fn bold(&self) -> BoldDisplay<'_, Self>

Make the text bold
§

fn dimmed(&self) -> DimDisplay<'_, Self>

Make the text dim
§

fn italic(&self) -> ItalicDisplay<'_, Self>

Make the text italicized
§

fn underline(&self) -> UnderlineDisplay<'_, Self>

Make the text underlined
Make the text blink
Make the text blink (but fast!)
§

fn reversed(&self) -> ReversedDisplay<'_, Self>

Swap the foreground and background colors
§

fn hidden(&self) -> HiddenDisplay<'_, Self>

Hide the text
§

fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>

Cross out the text
§

fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the foreground color at runtime. Only use if you do not know which color will be used at compile-time. If the color is constant, use either [OwoColorize::fg] or a color-specific method, such as [OwoColorize::green], Read more
§

fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>
where Color: DynColor,

Set the background color at runtime. Only use if you do not know what color to use at compile-time. If the color is constant, use either [OwoColorize::bg] or a color-specific method, such as [OwoColorize::on_yellow], Read more
§

fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the foreground color to a specific RGB value.
§

fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>

Set the background color to a specific RGB value.
§

fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>

Sets the foreground color to an RGB value.
§

fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>

Sets the background color to an RGB value.
§

fn style(&self, style: Style) -> Styled<&Self>

Apply a runtime-determined style
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,