waterui-color
Cross-platform color handling with support for multiple color spaces and perceptually-uniform transformations.
Overview
waterui-color provides the color system for WaterUI, offering comprehensive support for sRGB, Display P3, and OKLCH color spaces. The crate emphasizes perceptually-uniform color manipulation through the OKLCH color space, enabling predictable lightness, chroma, and hue adjustments that maintain consistent contrast relationships across light and dark themes.
Colors in WaterUI integrate seamlessly with the reactive system (nami) and can be used both as standalone values and as renderable views. The crate handles gamma correction, color space conversions, HDR headroom, and opacity management automatically.
This crate is part of the WaterUI framework and is typically accessed through the waterui::color module or the waterui::prelude.
Installation
Add to your Cargo.toml:
[]
= "0.1.0"
Or use through the main WaterUI crate:
[]
= "0.2"
Quick Start
use *;
// Create colors in different color spaces
let red = srgb;
let p3_green = p3;
let perceptual_blue = oklch;
// Parse from hex strings
let orange = srgb_hex;
// Apply transformations
let semi_transparent = srgb_hex.with_opacity;
let lighter = srgb.lighten;
let desaturated = oklch.desaturate;
// Mix colors
let blended = srgb.mix;
Core Concepts
Color Spaces
sRGB - Standard RGB color space used in most displays and web content. Values are gamma-encoded for display.
Display P3 - Wide color gamut space supporting more vivid colors than sRGB, commonly available on modern displays.
OKLCH - Perceptually-uniform color space recommended for UI work. Allows independent adjustment of:
- Lightness (0.0-1.0): Perceived brightness
- Chroma: Color intensity/saturation
- Hue (0-360 degrees): Color angle
Resolved Colors
All color types resolve to ResolvedColor, which stores colors in linear sRGB space with extended range support. This internal representation enables:
- Accurate color blending and interpolation
- HDR headroom for high dynamic range displays
- Opacity/alpha channel management
- Efficient conversion between color spaces
Reactive Integration
Colors implement the Resolvable trait, allowing them to work with WaterUI's reactive system. Colors can be derived from reactive signals and automatically update when dependencies change.
Examples
Basic Color Creation
use *;
// 8-bit RGB components
let red = srgb;
// Floating-point RGB (0.0-1.0)
let green = srgb_f32;
// Hexadecimal strings (with or without '#' prefix)
let blue = srgb_hex;
let cyan = srgb_hex;
// Packed 32-bit RGB
let magenta = srgb_u32;
// OKLCH (perceptually uniform)
let orange = oklch;
// Display P3 wide gamut
let vivid = p3;
// Transparent color
let clear = transparent;
Perceptual Color Manipulation
use *;
let base = srgb_hex;
// Lightness adjustments (perceptually uniform)
let lighter = base.clone.lighten; // Increase lightness by 20%
let darker = base.clone.darken; // Decrease lightness by 15%
// Saturation adjustments
let vibrant = base.clone.saturate; // Increase saturation
let muted = base.clone.desaturate; // Decrease saturation
// Hue rotation
let complementary = base.clone.hue_rotate; // Rotate 180 degrees
let analogous = base.clone.hue_rotate; // Rotate 30 degrees
Opacity and Mixing
use *;
// Apply opacity (alpha channel)
let semi_transparent = srgb_hex.with_opacity;
let faded = srgb.with_alpha; // Alias for with_opacity
// Mix two colors with linear interpolation
let purple = srgb.mix;
// Create gradient points
let start = srgb_hex;
let end = srgb_hex;
let midpoint = start.mix;
Color as a View
use *;
// Color implements View and can be used directly
// Use as background
Named Color Constants
use *;
// Pre-defined color constants (implement View)
API Overview
Color Creation
Color::srgb(r, g, b)- Create from 8-bit RGB values (0-255)Color::srgb_f32(r, g, b)- Create from float RGB values (0.0-1.0)Color::srgb_hex(hex)- Parse from hex string ("#RRGGBB", "0xRRGGBB", or "RRGGBB")Color::srgb_u32(rgb)- Create from packed 32-bit valueColor::oklch(l, c, h)- Create from OKLCH componentsColor::p3(r, g, b)- Create from Display P3 valuesColor::transparent()- Fully transparent black
Color Transformations
.lighten(amount)- Increase lightness in OKLCH space.darken(amount)- Decrease lightness in OKLCH space.saturate(amount)- Increase chroma/saturation.desaturate(amount)- Decrease chroma/saturation.hue_rotate(degrees)- Rotate hue by degrees.with_opacity(opacity)- Set opacity (0.0-1.0).with_alpha(alpha)- Alias forwith_opacity.mix(other, factor)- Linear interpolation between colors
Advanced
.with_headroom(headroom)- Set HDR headroom value.resolve(env)- Resolve to concrete color in given environmentColor::try_srgb_hex(hex)- Fallible hex parsing (returnsResult)
Color Space Types
Srgb- sRGB color space representationP3- Display P3 color space representationOklch- OKLCH color space representationResolvedColor- Internal linear RGB representation with metadata
Named Color Views
Material Design-inspired color constants that implement View:
Red, Pink, Purple, DeepPurple, Indigo, Blue, LightBlue, Cyan, Teal, Green, LightGreen, Lime, Yellow, Amber, Orange, DeepOrange, Brown, Grey, BlueGrey