Skip to main content

Module palette

Module palette 

Source
Expand description

Palette ramps generated from one seed colour.

ramp turns a seed into the twelve steps of a colour scale, in the step model of the reference tier (docs/tokens.md, after Radix Colors): 1 and 2 backgrounds, 3 to 5 quiet fills, 6 and 7 borders, 8 a solid fill being pressed, 9 the solid fill, 10 the solid fill under the pointer, 11 low-contrast text, 12 high-contrast text. The light and dark ramps of a theme pair come from the same seed (Scheme). Ramp adds the text colour that reads on step 9 (a scale’s contrast), and writes the scale as tokens.

How a ramp is made, in OkHsl (Okhsl, Björn Ottosson’s perceptual hue, saturation and lightness over Oklab, whose saturation is relative to the sRGB gamut, so no step leaves it):

  • Lightness per step is a fixed target for the backgrounds and borders (1 to 7) and the high-contrast text (12), taken from the built-in pop-light and pop-dark scales; steps 8, 10 and 11 follow step 9.
  • Step 9 is the seed when it reads: APCA (crate::apca) checks the text on it, and its lightness moves away from the text (darker in light, lighter in dark) until the text reaches Lc 60 and WCAG 4.5:1 with margin. egui-pop’s accent is also text on panels, so in a light scheme step 9 is dark enough for white text, in a dark one light enough for dark text.
  • Saturation is the seed’s, scaled per step (quiet backgrounds, full solids), then limited per hue band: greens and cyans turn neon, and yellows muddy, at full saturation in large areas.
  • Hue shifts with lightness, as warm and cool colours are seen to (lighter warm steps lean to yellow, lighter cool steps to cyan), by a few degrees at most.

clamp_seed brings a seed into what gives a usable ramp (not nearly black or white, saturation within its band); ramp applies it first. A grey seed gives a neutral ramp.

The approach follows egui_colors (MIT), which generates 12-step Radix-like scales in OkHsl with hue shifts, saturation limits per hue band and APCA; this is a re-derivation with documented targets, not its code. The OkHsl conversions follow Ottosson’s published reference implementation (MIT).

use egui_pop_tokens::palette::{self, Scheme};
use egui_pop_tokens::{Color, contrast};

let seed = Color::from_hex("#3a7bd5").unwrap();
let light = palette::Ramp::new(seed, Scheme::Light);
assert!(contrast::contrast_ratio(light.contrast, light.steps[8]) >= 4.5);
let dark = palette::ramp(seed, Scheme::Dark);
assert!(dark[0].is_dark() && !dark[11].is_dark());

Structs§

Backdrop
What a ramp is drawn with in a theme, when it is known: the ramp is then fitted to it (Ramp::fitted).
Okhsl
A colour in OkHsl: hue in degrees (0 to 360), saturation and lightness from 0 to 1, saturation relative to the most the sRGB gamut holds at that hue and lightness.
Ramp
A ramp and the text colour on its solid step.
Seed
A colour scale given by its seed in a token file (SEEDS_EXTENSION).

Enums§

Scheme
Which kind of theme a ramp is for.

Constants§

FITTED_RATIO
The WCAG ratio a fitted ramp reaches on the surfaces and text of its Backdrop: AA’s 4.5:1, with a small margin for rounding.
SEEDS_EXTENSION
The root $extensions key under which a token file gives colour scales by their seed rather than as twelve values:
SEED_LIGHTNESS
The lowest and highest lightness of a usable seed.
SOLID_LC
The Lc the text on step 9 reaches, at least (APCA’s floor for ordinary text).
SOLID_RATIO
The WCAG ratio the text on steps 9 and 10, and step 9 on the scheme’s backgrounds, reach at least: AA’s 4.5:1 with a margin for panels that are not the ramp’s own backgrounds.

Functions§

clamp_seed
A seed brought into what gives a usable ramp: opaque, its lightness within SEED_LIGHTNESS (nearly black or white seeds have no hue to speak of) and its saturation within its hue band’s limit for solids. A seed already usable comes back unchanged (to the byte).
is_usable_seed
Whether a seed gives a ramp as it is (clamp_seed leaves it).
mirror
A colour’s counterpart in a theme of the other scheme, light for dark: the same OkHsl hue and saturation (the chroma, as far as the sRGB gamut holds it at the new lightness), the lightness mirrored (1 - l), the alpha kept. Black and white swap, a mid grey stays; mirrored twice, a colour comes back (to within rounding), unless a mirror was clipped: near the cusp of its hue (the most saturated colours, #0000ff), the mirrored colour can lie just outside the sRGB gamut, and is clipped to it channel by channel, so its hue is kept but its saturation and lightness move a little, and mirroring it back does not give the colour it came from. What the theme editor’s “Generate the other variant” does to a colour (crate::ThemeDraft::generate_counterpart).
ramp
The twelve steps generated from a seed for a scheme (Ramp::new).
text_on
The text colour that reads on a solid fill, as APCA ranks them: white, or dark (nearly black), whichever has the larger Lc.