Skip to content

U0003 ui-colour-literal

Rule: UI code takes its colours from the theme, never writes them as values.

Code U0003 (stable, never reused)
Id ui-colour-literal
Category ui
Source spirit_level
Fix by hand

Level per tier

Tier Level Reason
small off
small-workspace off
medium off
big off
ai off
embedded off
ui warn a colour written as a value ignores the theme and the light and dark modes
egui warn colours come from ve_theme's tokens (ui.tokens().colors), which follow the light and dark theme
units-strict off
units-paranoid off

Parameters

Name Type Default Meaning
constructors list of strings [] functions that build a colour from values (Type::from_*: * matches within a segment); a call is reported when every argument is a literal
consts list of strings [] named colour constants (Type::* takes every upper-case constant of the type)
allowed list of strings [] constants that are no hard-coded colour (transparent)
macros list of strings [] macros that build a colour from a literal
hex-strings boolean true report CSS colour strings ("#1e1e2e")
theme-crates list of strings ["*_theme", "*-theme"] crates that are the theme, besides those the crate map marks theme = true
theme-paths list of strings ["**/theme.rs", "**/theme/**"] files that are the theme
toolkits list of strings ["egui", "eframe", "iced", "slint", "gtk4", "relm4", "dioxus", "leptos", "yew", "floem", "xilem", "makepad-widgets"] UI toolkits: a crate that depends on one directly is a UI crate, unless the crate map says ui = false

Why

One theme makes apps look alike, and lets them follow the light and dark modes. A colour written as a value in UI code ignores both: it stays the same in the dark theme, drifts from the palette when the palette changes, and is chosen anew in every app. rerun bans Color32::from_rgb and friends outside its design tokens for the same reason.

The lint reports, in UI crates outside the theme:

  • calls of a function in constructors whose arguments are all literals (Color32::from_rgb(190, 90, 0)); a colour computed from data is not reported;
  • named colour constants matching consts (Color32::RED), except the allowed ones (Color32::TRANSPARENT);
  • the macros in macros (hex_color!);
  • CSS colour strings ("#1e1e2e"), unless hex-strings is false.

The ui tier knows no toolkit, so its constructors, consts and macros are empty; the egui tier fills them. The theme is the crate the map marks theme = true, a crate matching theme-crates, or a file matching theme-paths.

How to fix

Take the colour from the theme's tokens (in vhrd_egui: ui.tokens().colors.warn, Tone::color, colors.cat[i] for categories). A colour the theme does not have yet is added to the theme, with a name for what it means.

Configure

[lints.ui-colour-literal]
level = "deny"
constructors = []
consts = []
allowed = []
macros = []
hex-strings = true
theme-crates = ["*_theme", "*-theme"]
theme-paths = ["**/theme.rs", "**/theme/**"]
toolkits = ["egui", "eframe", "iced", "slint", "gtk4", "relm4", "dioxus", "leptos", "yew", "floem", "xilem", "makepad-widgets"]
reason = "why this project wants it at this level"

Allow one finding in the code with // spirit-level: allow(ui-colour-literal) <reason> (waivers); the code works there too, as in [lints.U0003].