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 whereBackgroundRoleis expectedColorToken<Foreground>— Can be used whereForegroundRoleis 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: u8Red component (0-255).
g: u8Green component (0-255).
b: u8Blue component (0-255).
a: u8Alpha 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>
impl<Role: ColorRole> ColorToken<Role>
Sourcepub const fn from_rgba(r: u8, g: u8, b: u8, a: u8) -> Self
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);Sourcepub fn from_hex(hex: &str) -> Result<Self, ConfigError>
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 hexSourcepub const fn alpha(&self) -> u8
pub const fn alpha(&self) -> u8
Returns the alpha component (0-255, where 255 is fully opaque).
Sourcepub const fn to_rgb_tuple(&self) -> (u8, u8, u8)
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));Sourcepub const fn to_rgba_tuple(&self) -> (u8, u8, u8, u8)
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));Sourcepub fn to_hex(&self) -> String
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");Sourcepub fn to_css_rgb(&self) -> String
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)");Sourcepub fn role_name(&self) -> &'static str
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");Sourcepub const fn with_alpha(&self, alpha: u8) -> Self
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>
impl<Role: Clone + ColorRole> Clone for ColorToken<Role>
Source§fn clone(&self) -> ColorToken<Role>
fn clone(&self) -> ColorToken<Role>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<Role: ColorRole> Debug for ColorToken<Role>
impl<Role: ColorRole> Debug for ColorToken<Role>
Source§impl<Role: ColorRole> Default for ColorToken<Role>
impl<Role: ColorRole> Default for ColorToken<Role>
Source§impl<'de, Role: ColorRole> Deserialize<'de> for ColorToken<Role>
impl<'de, Role: ColorRole> Deserialize<'de> for ColorToken<Role>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<Role: ColorRole> Display for ColorToken<Role>
impl<Role: ColorRole> Display for ColorToken<Role>
Source§impl<Role: ColorRole> Serialize for ColorToken<Role>
impl<Role: ColorRole> Serialize for ColorToken<Role>
impl<Role: Copy + ColorRole> Copy for ColorToken<Role>
impl<Role: Eq + ColorRole> Eq for ColorToken<Role>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<D> OwoColorize for D
impl<D> OwoColorize for D
§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
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,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg] or
a color-specific method, such as [OwoColorize::on_yellow], Read more