// 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 } }