Module color

Source
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:

RoleTraitExample Uses
BackgroundBackgroundRolePanel backgrounds, button fills
ForegroundForegroundRoleText, icons
AccentAccentRoleFocus rings, highlights
StatusStatusRoleError, 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.
ColorToken
A color token with its semantic role encoded in the type.
Error
Error status color role.
Foreground
Primary foreground (text) color role.
ForegroundDim
Dimmed/secondary foreground color role.
Info
Info status color role.
Success
Success status color role.
Surface
Elevated surface color role.
SurfaceHover
Hover state for surfaces.
Warning
Warning status color role.

Traits§

AccentRole
Marker trait for accent colors.
BackgroundRole
Marker trait for colors that can be used as backgrounds.
ColorRole
Marker trait for all color roles.
ForegroundRole
Marker trait for colors that can be used as foreground (text/icons).
StatusRole
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.