Module theme

Source
Expand description

Opt-in, caller-owned UI tokens in logical screen pixels, before viewport/DPR projection. No game palette, renderer default, global state, input, motion or flash preference is changed. Rebuild measured layout and share its rectangles with paint/hits after changing tokens; wrap, scroll or report overflow rather than reducing essential text. A fitted viewport can still scale these units; use a responsive viewport when readable pixel-sized UI is intended.

Palette RGB is unpremultiplied, normalized sRGB encoded and opaque. These are foreground roles on Canvas/Surface, not arbitrary fills behind text. Measurements decode via ferravex_math::Color::try_srgb_to_linear, then use linear-light relative luminance. They describe an ideal sRGB color pair, not physical-display accessibility, glyph legibility or renderer blending. Transparency is rejected by tokens/opaque measurements. contrast_on explicitly composites in linear light over a supplied opaque backdrop; a renderer blending encoded channels can produce a different result.

use ferravex_math::Color;
use ferravex_play::ui::theme::*;
let mut theme = UiTheme::preset(ThemeMode::Normal); // Explicit adoption.
theme.set_colors(&[(ColorRole::Accent, Color::from_hex(0xffdb80))])?;
assert_eq!(theme.text(TextRole::Body).size, 18.);
assert!(theme.role_contrast(ColorRole::Accent, ColorRole::Surface)? >= 4.5);
// Enlarge by author/user choice, never by available layout width.
let types = theme.types().try_scaled(1.5)?;
let spacing = Spacing { control_height: 56., ..theme.spacing() };
theme.set_metrics(types, spacing)?;
assert_eq!(theme.text(TextRole::Body).size, 27.);
assert_eq!(theme.state(ThemeState::Warning).cue.label, "WARNING");
theme = UiTheme::preset(ThemeMode::HighContrast); // Explicit reset/replacement.
assert_eq!(theme.types().body, 18.);

Renderer mapping (the executed task290 receipt recipe supplies full code): map TextTokens into TextStyle size/color; StatRowsStyle accepts those styles and spacing gaps. For ordinary retained ButtonStyle, use Surface for both fill/disabled fill, Text for ink, Focus for the outline, and body size. Supply an explicit unavailable label/shape; the renderer does not draw token cues automatically. A meter can compose Surface track, semantic foreground fill, and a separate Text value plus labeled tick/shape cues, sharing pure rectangles with custom software hosts. Text ON a semantic fill needs a separate pair check, since roles are validated on Canvas/Surface only.

Current task277 CardLayout::new(bounds, content) has no style argument: title 24/body/stat/action 18, padding 16/gap 12/action gap 8/height 44, PANEL, default TextStyle and ButtonStyle remain fixed. Passing tokens to that API does not theme it. Compose panels/wrapped text/stats/buttons via Ui::compose for explicit themed cards/inspectors; parent may later add an additive card style seam. Button padding/focus stroke are also renderer-fixed.

Structs§

Palette
Opaque normalized sRGB in ColorRole::ALL order. Private storage preserves channel/alpha validation. A palette alone has no contrast policy; UiTheme::try_new checks required pairs before adoption.
Spacing
Raw logical metrics. Gaps/inset 0..=4096, stroke 1..=64, control height 44..=4096 and at least body + 2 * compact_gap. A minimum layout request, not a text measurement/fit guarantee. Reflow/overflow remains consumer-owned.
StateCue
Render a descriptive localizable label (replace this English fallback when needed) AND a visible shape/pattern; never convey a state only by hue. Static cues require no flash/motion and have no input/remapping semantics.
StateTokens
TextTokens
Renderer-independent text inputs, on Canvas/Surface.
TypeScale
Essential label/body/heading sizes in logical pixels, each 16..=256, with label <= body <= heading. Raw authoring value; checked on theme adoption.
UiTheme
Validated metrics/palette with checked atomic edits. Fixed value storage; create/copy/reset per consumer, with no retained history or heap allocation.

Enums§

ColorRole
Order for Palette::try_new_srgb. All roles except Canvas/Surface are foregrounds on BOTH backgrounds. Focus/Border are non-text marks; all other foreground roles must satisfy the small-text threshold.
CuePattern
CueShape
Supplemental shape guidance; consumers draw it, not this pure module.
RgbEncoding
RGB units for measurement, never inferred from Color. Alpha is linear coverage in either encoding. All RGBA channels must be in 0..=1.
TextRole
ThemeError
Rejection before theme mutation. Contrast reports the first failed pair in role order. Ratios are unitless; colors are never silently clipped/repaired.
ThemeMode
Opt-in presets/contrast policies. Neither changes existing UI defaults.
ThemeState

Constants§

COLOR_ROLE_COUNT
Fixed palette capacity; no growing registry or heap storage.

Functions§

contrast_on
Decode all colors in the stated units; source-over background onto opaque backdrop, then foreground onto that result, in linear light. Return foreground-result versus background-result contrast. No premultiplied/HDR, encoded-channel blending, unknown-backdrop or physical-display claim.
contrast_ratio
Symmetric ratio 1..=21 for two opaque colors in the same stated units. Transparent inputs require contrast_on, not an assumed black backdrop.
relative_luminance
Relative luminance 0..=1 of opaque normalized RGB in the stated units. Linear input is not decoded a second time.