Expand description
§Type-Safe Color Tokens
This module provides color tokens with semantic roles encoded in the type system. The compiler ensures colors are used appropriately — you cannot accidentally use a text color as a background.
§Design Rationale
In a traditional design system, colors are just values:
ⓘ
let surface = "#313244";
let text = "#cdd6f4";
// Oops! Using text color as background — compiles but looks wrong
button.background = text;With type-safe colors, the compiler catches this mistake:
ⓘ
let surface: ColorToken<Surface> = ColorToken::from_hex("#313244")?;
let text: ColorToken<Foreground> = ColorToken::from_hex("#cdd6f4")?;
// This function only accepts background colors
fn set_background(color: impl BackgroundRole) { /* ... */ }
set_background(surface); // ✓ Compiles — Surface is a BackgroundRole
set_background(text); // ✗ Error! — Foreground is not a BackgroundRole§Color Roles
Colors are categorized by their semantic role:
| Role | Trait | Example Uses |
|---|---|---|
| Background | BackgroundRole | Panel backgrounds, button fills |
| Foreground | ForegroundRole | Text, icons |
| Accent | AccentRole | Focus rings, highlights |
| Status | StatusRole | Error, warning, success |
§Creating Colors
use fernctl::domain::tokens::color::*;
// From hex string
let color = ColorToken::<Surface>::from_hex("#313244").unwrap();
// From RGB values
let color = ColorToken::<Surface>::from_rgb(0x31, 0x32, 0x44);
// With alpha
let color = ColorToken::<Surface>::from_rgba(0x31, 0x32, 0x44, 0x80);§Zero-Cost Abstraction
The type parameter uses PhantomData and adds no runtime overhead.
A ColorToken<Surface> has the exact same memory layout as four u8s.
Modules§
- private 🔒
- Private module to seal the role traits.
Structs§
- Accent
- Accent color role.
- Background
- Primary background color role.
- Color
Token - A color token with its semantic role encoded in the type.
- Error
- Error status color role.
- Foreground
- Primary foreground (text) color role.
- Foreground
Dim - Dimmed/secondary foreground color role.
- Info
- Info status color role.
- Success
- Success status color role.
- Surface
- Elevated surface color role.
- Surface
Hover - Hover state for surfaces.
- Warning
- Warning status color role.
Traits§
- Accent
Role - Marker trait for accent colors.
- Background
Role - Marker trait for colors that can be used as backgrounds.
- Color
Role - Marker trait for all color roles.
- Foreground
Role - Marker trait for colors that can be used as foreground (text/icons).
- Status
Role - Marker trait for status indication colors.
Functions§
- parse_
hex_ 🔒byte - Parses a two-character hex byte.
- parse_
hex_ 🔒color - Parses a hex color string into RGBA components.
- parse_
hex_ 🔒digit - Parses a single hex digit.