Follow the desktop accent colour and colour scheme (XDG portal)
CI / check (pull_request) Successful in 10s
CI / native (pull_request) Successful in 1m18s

Read org.freedesktop.appearance accent-color and color-scheme from
org.freedesktop.portal.Settings over GDBus in the GTK stub and follow
SettingChanged. The accent recolours the selected tab, sliders, peak
meters and the checked Default button through an application-priority
GtkCssProvider (named colours and --accent-* variables too, for themes
that use them); the colour scheme sets the theme's dark/light variant.
Without a portal or accent the theme's own stays.

Colour conversion, WCAG contrast (readable foreground on the accent,
readable accent text on the window) and the CSS live in model/accent.mbt
with unit tests. The smoke test runs a fake portal on a private session
bus, switches the accent live from green to orange and renders the tabs
again (docs/screenshots/accent-*.png).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SpriteandClaude Opus 5.5 committed 2026-11-23 01:28:19 +00:00
1 parent bc675459d9
commit 74d85ecdfc
16 files changed
+947 -12

No files matched your search

+257
View File
@@ -0,0 +1,257 @@
// System accent colour: conversion of the XDG Desktop Portal value
// (org.freedesktop.appearance accent-color, an (ddd) sRGB tuple in 0..1),
// WCAG contrast checks, and the CSS that recolours moonmix with it.
///|
/// An sRGB colour, each channel 0..1.
#derive(Eq, Debug)
pub(all) struct Rgb {
r : Double
g : Double
b : Double
}
///|
pub let white : Rgb = { r: 1.0, g: 1.0, b: 1.0, }
///|
pub let black : Rgb = { r: 0.0, g: 0.0, b: 0.0, }
///|
/// The portal's accent colour, or None when it is unset. The spec says a
/// value outside 0..1 means "no accent colour" (GNOME sends that before the
/// user picks one); NaN counts as unset too.
pub fn Rgb::from_portal(r : Double, g : Double, b : Double) -> Rgb? {
fn ok(x : Double) -> Bool {
x >= 0.0 && x <= 1.0 // false for NaN
}
if ok(r) && ok(g) && ok(b) {
Some({ r, g, b, })
} else {
None
}
}
///|
fn byte(x : Double) -> Int32 {
let v = (x * 255.0 + 0.5).to_int()
if v < 0 {
0
} else if v > 255 {
255
} else {
v
}
}
///|
/// Parse "#rrggbb" or "#rgb" (case-insensitive).
pub fn Rgb::parse_hex(s : String) -> Rgb? {
fn nib(c : Char) -> Int32? {
match c {
'0'..<='9' => Some(c.to_int() - '0'.to_int())
'a'..<='f' => Some(c.to_int() - 'a'.to_int() + 10)
'A'..<='F' => Some(c.to_int() - 'A'.to_int() + 10)
_ => None
}
}
let cs = s.to_array()
if cs.length() == 0 || cs[0] != '#' {
return None
}
let ds = []
for c in cs[1:] {
match nib(c) {
Some(d) => ds.push(d)
None => return None
}
}
let (r, g, b) = match ds.length() {
6 => (ds[0] * 16 + ds[1], ds[2] * 16 + ds[3], ds[4] * 16 + ds[5])
3 => (ds[0] * 17, ds[1] * 17, ds[2] * 17)
_ => return None
}
Some({
r: r.to_double() / 255.0,
g: g.to_double() / 255.0,
b: b.to_double() / 255.0,
})
}
///|
/// "#rrggbb", for CSS.
pub fn Rgb::to_hex(self : Rgb) -> String {
let digits = "0123456789abcdef".to_array()
let out = StringBuilder()
out.write_char('#')
for x in [self.r, self.g, self.b] {
let v = byte(x)
out.write_char(digits[v / 16])
out.write_char(digits[v % 16])
}
out.to_string()
}
///|
/// Linear interpolation: t = 0 is self, t = 1 is other.
pub fn Rgb::mix(self : Rgb, other : Rgb, t : Double) -> Rgb {
{
r: self.r + (other.r - self.r) * t,
g: self.g + (other.g - self.g) * t,
b: self.b + (other.b - self.b) * t,
}
}
///|
/// WCAG 2 relative luminance (of the colour as it will be shown, i.e.
/// rounded to 8 bits).
pub fn Rgb::luminance(self : Rgb) -> Double {
fn lin(x : Double) -> Double {
let c = byte(x).to_double() / 255.0
if c <= 0.04045 {
c / 12.92
} else {
@math.pow((c + 0.055) / 1.055, 2.4)
}
}
0.2126 * lin(self.r) + 0.7152 * lin(self.g) + 0.0722 * lin(self.b)
}
///|
/// WCAG 2 contrast ratio, 1..21.
pub fn contrast_ratio(a : Rgb, b : Rgb) -> Double {
let la = a.luminance()
let lb = b.luminance()
let (hi, lo) = if la > lb { (la, lb) } else { (lb, la) }
(hi + 0.05) / (lo + 0.05)
}
///|
/// WCAG AA contrast for normal text.
pub const MIN_TEXT_CONTRAST : Double = 4.5
///|
/// WCAG AA contrast for bold/large text and UI components (the checked
/// "default" button's icon, selected rows' text).
pub const MIN_UI_CONTRAST : Double = 3.0
///|
/// Foreground for text and icons on an accent background: white, as GTK and
/// libadwaita use, while it reaches MIN_UI_CONTRAST; otherwise whichever of
/// white and black contrasts more (black on yellow, light green, orange…).
pub fn readable_fg(bg : Rgb) -> Rgb {
let on_white = contrast_ratio(bg, white)
if on_white >= MIN_UI_CONTRAST || on_white >= contrast_ratio(bg, black) {
white
} else {
black
}
}
///|
/// Window background of GTK's Default theme (theme_bg_color).
pub fn window_bg(dark : Bool) -> Rgb {
if dark {
{ r: 53.0 / 255.0, g: 53.0 / 255.0, b: 53.0 / 255.0, } // #353535
} else {
{ r: 246.0 / 255.0, g: 245.0 / 255.0, b: 244.0 / 255.0, } // #f6f5f4
}
}
///|
/// The accent as a text/link colour on the window background: the accent
/// itself if it is readable there, else darkened (light scheme) or lightened
/// (dark scheme) just enough to be.
pub fn accent_text_color(accent : Rgb, dark : Bool) -> Rgb {
let bg = window_bg(dark)
let toward = if dark { white } else { black }
for i in 0..<=20 {
let c = accent.mix(toward, i.to_double() * 0.05)
if contrast_ratio(c, bg) >= MIN_TEXT_CONTRAST {
return c
}
}
toward
}
///|
/// The colours derived from one accent.
#derive(Eq, Debug)
pub(all) struct AccentColors {
/// Backgrounds: selected tab underline, slider fill, level bar, the
/// checked "default" button.
bg : Rgb
/// Text and icons on `bg`.
fg : Rgb
/// Accent used as a text colour on the window background.
text : Rgb
/// Outline of filled buttons and checks: a slightly darker accent, as the
/// theme does.
border : Rgb
}
///|
pub fn AccentColors::new(accent : Rgb, dark : Bool) -> AccentColors {
{
bg: accent,
fg: readable_fg(accent),
text: accent_text_color(accent, dark),
border: accent.mix(black, 0.15),
}
}
///|
/// CSS (for a provider at APPLICATION priority) that makes the theme use
/// the accent. Overrides the accent named colours and CSS variables (for
/// Adwaita-style themes and GTK 3-style themes that use
/// theme_selected_bg_color), plus explicit rules for the widgets moonmix
/// uses, because GTK's built-in Default theme hardcodes its blue.
pub fn accent_css(accent : Rgb, dark : Bool) -> String {
let c = AccentColors::new(accent, dark)
let bg = c.bg.to_hex()
let fg = c.fg.to_hex()
let text = c.text.to_hex()
let border = c.border.to_hex()
let lines = [
"@define-color accent_bg_color \{bg};",
"@define-color accent_fg_color \{fg};",
"@define-color accent_color \{text};",
"@define-color theme_selected_bg_color \{bg};",
"@define-color theme_selected_fg_color \{fg};",
":root { --accent-bg-color: \{bg}; --accent-fg-color: \{fg}; --accent-color: \{text}; }",
"scale > trough > highlight { background-color: \{bg}; border-color: \{bg}; }",
"levelbar > trough > block.high, levelbar > trough > block:not(.empty):not(.low):not(.full) { background-color: \{bg}; border-color: \{bg}; }",
"notebook > header.top > tabs > tab:checked { box-shadow: inset 0 -4px \{bg}; }",
"notebook > header.bottom > tabs > tab:checked { box-shadow: inset 0 4px \{bg}; }",
"notebook > header.left > tabs > tab:checked { box-shadow: inset -4px 0 \{bg}; }",
"notebook > header.right > tabs > tab:checked { box-shadow: inset 4px 0 \{bg}; }",
"button.mm-default:checked { background-image: none; background-color: \{bg}; border-color: \{border}; color: \{fg}; }",
"check:checked, radio:checked, switch:checked { background-image: none; background-color: \{bg}; border-color: \{border}; color: \{fg}; }",
"row:selected, .view:selected { background-color: \{bg}; color: \{fg}; }",
"link, button.link { color: \{text}; }",
":focus-visible { outline-color: alpha(\{bg}, 0.5); }",
]
lines.join("\n") + "\n"
}
///|
/// The portal's color-scheme preference.
#derive(Eq, Debug)
pub(all) enum ColorScheme {
NoPreference
Dark
Light
}
///|
/// org.freedesktop.appearance color-scheme: 1 dark, 2 light, else none.
pub fn ColorScheme::from_portal(v : Int32) -> ColorScheme {
match v {
1 => Dark
2 => Light
_ => NoPreference
}
}
+120
View File
@@ -0,0 +1,120 @@
///|
fn hex(s : String) -> @model.Rgb {
@model.Rgb::parse_hex(s).unwrap()
}
///|
test "from_portal accepts 0..1 and rejects unset values" {
assert_eq(
@model.Rgb::from_portal(1.0, 0.5, 0.0),
Some({ r: 1.0, g: 0.5, b: 0.0, }),
)
assert_eq(@model.Rgb::from_portal(0.0, 0.0, 0.0), Some(@model.black))
// GNOME reports (-1, -1, -1) before an accent is chosen.
assert_eq(@model.Rgb::from_portal(-1.0, -1.0, -1.0), None)
assert_eq(@model.Rgb::from_portal(0.2, 1.5, 0.2), None)
assert_eq(@model.Rgb::from_portal(0.0 / 0.0, 0.2, 0.2), None)
}
///|
test "hex round trip" {
assert_eq(hex("#3584e4").to_hex(), "#3584e4")
assert_eq(hex("#ED5B00").to_hex(), "#ed5b00")
assert_eq(hex("#fff"), @model.white)
assert_eq(hex("#000000"), @model.black)
assert_eq(@model.Rgb::parse_hex("3584e4"), None)
assert_eq(@model.Rgb::parse_hex("#3584e"), None)
assert_eq(@model.Rgb::parse_hex("#zzzzzz"), None)
// Portal doubles are rounded to the nearest byte.
assert_eq(
@model.Rgb::from_portal(1.0, 0.471, 0.0).unwrap().to_hex(),
"#ff7800",
)
}
///|
test "contrast ratio" {
assert_eq(@model.contrast_ratio(@model.white, @model.black), 21.0)
assert_eq(@model.contrast_ratio(@model.black, @model.black), 1.0)
// GTK's blue against white (well known: ~3.76).
let r = @model.contrast_ratio(hex("#3584e4"), @model.white)
assert_true(r > 3.7 && r < 3.8)
// Symmetric.
assert_eq(@model.contrast_ratio(@model.white, hex("#3584e4")), r)
}
///|
test "readable foreground on accent backgrounds" {
// Dark accents keep white text.
assert_eq(@model.readable_fg(hex("#1c71d8")), @model.white)
assert_eq(@model.readable_fg(hex("#9141ac")), @model.white)
assert_eq(@model.readable_fg(hex("#c01c28")), @model.white)
// GTK's blue: below 4.5 with white (black would contrast more), but
// above 3, so it keeps white like GTK does.
assert_eq(@model.readable_fg(hex("#3584e4")), @model.white)
// Bright accents get black text.
assert_eq(@model.readable_fg(hex("#ff7800")), @model.black)
assert_eq(@model.readable_fg(hex("#f6d32d")), @model.black)
assert_eq(@model.readable_fg(hex("#33d17a")), @model.black)
// Whatever is chosen beats the alternative or meets AA.
for c in ["#3584e4", "#ff7800", "#2190a4", "#c88800", "#6f8396", "#ffffff"] {
let bg = hex(c)
let fg = @model.readable_fg(bg)
let other = if fg == @model.white { @model.black } else { @model.white }
assert_true(
@model.contrast_ratio(bg, fg) >= @model.MIN_UI_CONTRAST ||
@model.contrast_ratio(bg, fg) >= @model.contrast_ratio(bg, other),
)
}
}
///|
test "accent text colour is readable on the window background" {
for
c in [
"#3584e4", "#ff7800", "#f6d32d", "#33d17a", "#1c71d8", "#000000", "#ffffff",
] {
for dark in [false, true] {
let t = @model.accent_text_color(hex(c), dark)
assert_true(
@model.contrast_ratio(t, @model.window_bg(dark)) >=
@model.MIN_TEXT_CONTRAST,
)
}
}
// Already readable: unchanged.
assert_eq(@model.accent_text_color(hex("#1a5fb4"), false), hex("#1a5fb4"))
// Yellow on a light window is darkened, on a dark one kept.
assert_true(
@model.accent_text_color(hex("#f6d32d"), false).luminance() <
hex("#f6d32d").luminance(),
)
assert_eq(@model.accent_text_color(hex("#f6d32d"), true), hex("#f6d32d"))
}
///|
test "accent css overrides named colours and widget fills" {
let css = @model.accent_css(hex("#ff7800"), false)
assert_true(css.contains("@define-color accent_bg_color #ff7800;"))
assert_true(css.contains("@define-color accent_fg_color #000000;"))
assert_true(css.contains("@define-color theme_selected_bg_color #ff7800;"))
assert_true(css.contains("--accent-bg-color: #ff7800;"))
assert_true(
css.contains("scale > trough > highlight { background-color: #ff7800;"),
)
assert_true(css.contains("levelbar > trough > block.high"))
assert_true(css.contains("tab:checked { box-shadow: inset 0 -4px #ff7800; }"))
assert_true(css.contains("button.mm-default:checked"))
// The text colour is the darkened accent, not the raw one.
let text = @model.accent_text_color(hex("#ff7800"), false).to_hex()
assert_true(text != "#ff7800")
assert_true(css.contains("@define-color accent_color \{text};"))
}
///|
test "color scheme from portal" {
assert_eq(@model.ColorScheme::from_portal(0), @model.NoPreference)
assert_eq(@model.ColorScheme::from_portal(1), @model.Dark)
assert_eq(@model.ColorScheme::from_portal(2), @model.Light)
assert_eq(@model.ColorScheme::from_portal(7), @model.NoPreference)
}
+47
View File
@@ -14,12 +14,24 @@ pub const MAX_PERCENT : Double = 150.0
pub const METER_DECAY : Double = 0.04
pub const MIN_TEXT_CONTRAST : Double = 4.5
pub const MIN_UI_CONTRAST : Double = 3.0
pub const NORM_PERCENT : Double = 100.0
pub fn accent_css(Rgb, Bool) -> String
pub fn accent_text_color(Rgb, Bool) -> Rgb
pub let black : Rgb
pub fn clamp_percent(Double) -> Double
pub fn compare_text(String, String) -> Int32
pub fn contrast_ratio(Rgb, Rgb) -> Double
pub fn default_button_icons() -> Array[String]
pub fn device_bus(Node) -> String
@@ -54,6 +66,8 @@ pub fn meter_step(Double, Double) -> Double
pub fn percent_to_linear(Double) -> Double
pub fn readable_fg(Rgb) -> Rgb
pub fn rows(State, Tab) -> Array[Row]
pub fn scale_linked(Array[Double], Double) -> Array[Double]
@@ -66,9 +80,30 @@ pub fn symbolic(String) -> String
pub fn volume_icon(Double, Bool) -> String
pub let white : Rgb
pub fn window_bg(Bool) -> Rgb
// Errors
// Types and methods
#derive(Eq, @debug.Debug)
pub(all) struct AccentColors {
bg : Rgb
fg : Rgb
text : Rgb
border : Rgb
}
pub fn AccentColors::new(Rgb, Bool) -> Self
#derive(Eq, @debug.Debug)
pub(all) enum ColorScheme {
NoPreference
Dark
Light
}
pub fn ColorScheme::from_portal(Int32) -> Self
#derive(Eq, @debug.Debug)
pub(all) struct DeviceChoice {
id : Int32
@@ -127,6 +162,18 @@ pub(all) enum NodeKind {
}
pub fn NodeKind::from_media_class(String) -> Self
#derive(Eq, @debug.Debug)
pub(all) struct Rgb {
r : Double
g : Double
b : Double
}
pub fn Rgb::from_portal(Double, Double, Double) -> Self?
pub fn Rgb::luminance(Self) -> Double
pub fn Rgb::mix(Self, Self, Double) -> Self
pub fn Rgb::parse_hex(String) -> Self?
pub fn Rgb::to_hex(Self) -> String
#derive(Eq, @debug.Debug)
pub(all) struct Row {
id : Int32