Lightweight native Discord client for Windows, Linux and macOS, written in Rust. No Electron, no browser engine.
Rust desktop-appdiscorddiscord-clienteguielectron-alternativelightweightlinuxmacosnative-apprustwindows
//! The colours Discord derives from a Nitro profile theme's two colours
//! (web client module 543699, which uses chroma.js; read 2026-12-01).
use eframe::egui::Color32;
/// A profile theme is drawn on Discord's dark theme when its primary colour
/// is dark, else on the light one, whatever theme the app itself uses.
pub fn is_dark(primary: u32) -> bool {
let [r, g, b] = channels(primary);
1.0 - (0.299 * r + 0.587 * g + 0.114 * b) / 255.0 > 0.5
}
/// The full profile's window colour: the two colours mixed halfway in Lab,
/// with the lightness held at 10 % or less (80 % or more on a light theme).
pub fn modal_background(primary: u32, secondary: u32, dark: bool) -> Color32 {
let (a, b) = (rgb_to_lab(channels(primary)), rgb_to_lab(channels(secondary)));
let mixed = lab_to_rgb([(a[0] + b[0]) / 2.0, (a[1] + b[1]) / 2.0, (a[2] + b[2]) / 2.0]).map(|c| c.round().clamp(0.0, 255.0));
let [h, s, l] = rgb_to_hsl(mixed);
let l = (l * 100.0).round() / 100.0;
to_color(hsl_to_rgb([h, s, if dark { l.clamp(0.0, 0.1) } else { l.clamp(0.8, 1.0) }]))
}
/// The primary button on a themed profile: the primary colour, darkened or
/// brightened in Lab steps of 18 until white text on it has a contrast
/// between 7 and 10.
pub fn button_color(primary: u32) -> Color32 {
let mut c = channels(primary);
for _ in 0..100 {
let contrast = 1.05 / (luminance(c) + 0.05);
if (7.0..=10.0).contains(&contrast) {
break;
}
let mut lab = rgb_to_lab(c);
lab[0] += if contrast < 7.0 { -18.0 } else { 18.0 };
c = lab_to_rgb(lab);
}
to_color(c)
}
/// What Discord mixes into every surface of a themed profile: the primary
/// colour's hue at full saturation, nearly black (nearly white when light).
pub fn base_color(primary: u32, dark: bool) -> Color32 {
let [h, _, _] = rgb_to_hsl(channels(primary));
to_color(hsl_to_rgb([h, 1.0, if dark { 0.051 } else { 0.9 }]))
}
/// The full profile window's shade `share` of the way to its overlay:
/// Discord's `color-mix(in oklab, <overlay> share, <window colour>)`, the
/// overlay being 60 % black (60 % white on a light theme), laid over the
/// window colour. Its gradient runs from share 0.8 at the top to 0.2 at
/// 300 px.
pub fn modal_shade(window: Color32, dark: bool, share: f32) -> Color32 {
const OVERLAY_ALPHA: f32 = 0.6;
let bg = [window.r() as f32, window.g() as f32, window.b() as f32];
let over = rgb_to_oklab(if dark { [0.0; 3] } else { [255.0; 3] });
let under = rgb_to_oklab(bg);
// CSS mixes premultiplied: each colour counts by its share and its alpha.
let (w_over, w_under) = (OVERLAY_ALPHA * share, 1.0 - share);
let alpha = w_over + w_under;
let mixed = oklab_to_rgb(std::array::from_fn(|i| (over[i] * w_over + under[i] * w_under) / alpha));
// Then laid over the window colour, as the browser blends: in sRGB.
to_color(std::array::from_fn(|i| mixed[i] * alpha + bg[i] * (1.0 - alpha)))
}
fn linear(v: f32) -> f32 {
let v = (v / 255.0).clamp(0.0, 1.0);
if v <= 0.04045 {
v / 12.92
} else {
((v + 0.055) / 1.055).powf(2.4)
}
}
fn encoded(v: f32) -> f32 {
255.0 * if v <= 0.003_130_8 { 12.92 * v } else { 1.055 * v.max(0.0).powf(1.0 / 2.4) - 0.055 }
}
/// sRGB (0–255) to OKLab (Björn Ottosson's matrices, as CSS uses).
fn rgb_to_oklab(c: [f32; 3]) -> [f32; 3] {
let [r, g, b] = c.map(linear);
let l = (0.412_221_47 * r + 0.536_332_54 * g + 0.051_445_99 * b).cbrt();
let m = (0.211_903_5 * r + 0.680_699_5 * g + 0.107_396_96 * b).cbrt();
let s = (0.088_302_46 * r + 0.281_718_84 * g + 0.629_978_7 * b).cbrt();
[
0.210_454_26 * l + 0.793_617_8 * m - 0.004_072_047 * s,
1.977_998_5 * l - 2.428_592_2 * m + 0.450_593_7 * s,
0.025_904_037 * l + 0.782_771_77 * m - 0.808_675_77 * s,
]
}
fn oklab_to_rgb(lab: [f32; 3]) -> [f32; 3] {
let [ll, a, b] = lab;
let l = (ll + 0.396_337_78 * a + 0.215_803_76 * b).powi(3);
let m = (ll - 0.105_561_346 * a - 0.063_854_17 * b).powi(3);
let s = (ll - 0.089_484_18 * a - 1.291_485_5 * b).powi(3);
[
encoded(4.076_741_7 * l - 3.307_711_6 * m + 0.230_969_94 * s),
encoded(-1.268_438 * l + 2.609_757_4 * m - 0.341_319_38 * s),
encoded(-0.004_196_086_3 * l - 0.703_418_6 * m + 1.707_614_7 * s),
]
}
fn channels(c: u32) -> [f32; 3] {
[(c >> 16 & 255) as f32, (c >> 8 & 255) as f32, (c & 255) as f32]
}
fn to_color(c: [f32; 3]) -> Color32 {
let [r, g, b] = c.map(|v| v.round().clamp(0.0, 255.0) as u8);
Color32::from_rgb(r, g, b)
}
/// WCAG relative luminance.
fn luminance(c: [f32; 3]) -> f32 {
let lin = |v: f32| {
let v = (v / 255.0).clamp(0.0, 1.0);
if v <= 0.03928 {
v / 12.92
} else {
((v + 0.055) / 1.055).powf(2.4)
}
};
0.2126 * lin(c[0]) + 0.7152 * lin(c[1]) + 0.0722 * lin(c[2])
}
// CIE Lab (D65) with chroma.js's constants.
const WHITE: [f32; 3] = [0.950_47, 1.0, 1.088_83];
const T0: f32 = 0.137_931_03;
const T1: f32 = 0.206_896_55;
const T2: f32 = 0.128_418_55;
const T3: f32 = 0.008_856_452;
fn rgb_to_lab(c: [f32; 3]) -> [f32; 3] {
let lin = |v: f32| {
let v = v / 255.0;
if v <= 0.04045 {
v / 12.92
} else {
((v + 0.055) / 1.055).powf(2.4)
}
};
let f = |t: f32| if t > T3 { t.cbrt() } else { t / T2 + T0 };
let [r, g, b] = c.map(lin);
let x = f((0.412_456_4 * r + 0.357_576_1 * g + 0.180_437_5 * b) / WHITE[0]);
let y = f((0.212_672_9 * r + 0.715_152_2 * g + 0.072_175 * b) / WHITE[1]);
let z = f((0.019_333_9 * r + 0.119_192 * g + 0.950_304_1 * b) / WHITE[2]);
[(116.0 * y - 16.0).max(0.0), 500.0 * (x - y), 200.0 * (y - z)]
}
fn lab_to_rgb(lab: [f32; 3]) -> [f32; 3] {
let f = |t: f32| if t > T1 { t * t * t } else { T2 * (t - T0) };
let y = (lab[0] + 16.0) / 116.0;
let (x, z) = (y + lab[1] / 500.0, y - lab[2] / 200.0);
let (x, y, z) = (WHITE[0] * f(x), WHITE[1] * f(y), WHITE[2] * f(z));
let gamma = |v: f32| 255.0 * if v <= 0.003_04 { 12.92 * v } else { 1.055 * v.powf(1.0 / 2.4) - 0.055 };
[
gamma(3.240_454_2 * x - 1.537_138_5 * y - 0.498_531_4 * z),
gamma(-0.969_266 * x + 1.876_010_8 * y + 0.041_556 * z),
gamma(0.055_643_4 * x - 0.204_025_9 * y + 1.057_225_2 * z),
]
}
/// (hue in degrees, saturation, lightness), the last two 0–1.
fn rgb_to_hsl(c: [f32; 3]) -> [f32; 3] {
let [r, g, b] = c.map(|v| v / 255.0);
let (max, min) = (r.max(g).max(b), r.min(g).min(b));
let l = (max + min) / 2.0;
if max == min {
return [0.0, 0.0, l];
}
let d = max - min;
let s = if l < 0.5 { d / (max + min) } else { d / (2.0 - max - min) };
let h = if r == max {
(g - b) / d
} else if g == max {
2.0 + (b - r) / d
} else {
4.0 + (r - g) / d
};
[(h * 60.0).rem_euclid(360.0), s, l]
}
fn hsl_to_rgb(hsl: [f32; 3]) -> [f32; 3] {
let [h, s, l] = hsl;
if s == 0.0 {
return [l * 255.0; 3];
}
let q = if l < 0.5 { l * (1.0 + s) } else { l + s - l * s };
let p = 2.0 * l - q;
let channel = |t: f32| {
let t = t.rem_euclid(1.0);
255.0 * if t < 1.0 / 6.0 {
p + (q - p) * 6.0 * t
} else if t < 0.5 {
q
} else if t < 2.0 / 3.0 {
p + (q - p) * (2.0 / 3.0 - t) * 6.0
} else {
p
}
};
let h = h / 360.0;
[channel(h + 1.0 / 3.0), channel(h), channel(h - 1.0 / 3.0)]
}
#[cfg(test)]
mod tests {
use super::*;
fn close(a: Color32, b: (u8, u8, u8)) -> bool {
(a.r() as i32 - b.0 as i32).abs() <= 1 && (a.g() as i32 - b.1 as i32).abs() <= 1 && (a.b() as i32 - b.2 as i32).abs() <= 1
}
// A profile seen in Discord web on 2026-12-01: primary rgb(29, 61, 157),
// secondary rgb(88, 177, 215).
const PRIMARY: u32 = 0x1d3d9d;
const SECONDARY: u32 = 0x58b1d7;
#[test]
fn modal_background_matches_discord() {
// Discord: --profile-gradient-modal-background-color: rgb(13, 24, 38).
let c = modal_background(PRIMARY, SECONDARY, true);
assert!(close(c, (13, 24, 38)), "{c:?}");
}
#[test]
fn button_keeps_a_colour_that_already_contrasts() {
// Discord: --profile-gradient-button-color stayed rgb(29, 61, 157).
assert!(close(button_color(PRIMARY), (29, 61, 157)));
// A pale colour is darkened until white text reads on it.
let c = button_color(0xffe08a);
assert!(1.05 / (luminance([c.r() as f32, c.g() as f32, c.b() as f32]) + 0.05) >= 6.5, "{c:?}");
}
#[test]
fn dark_and_light_profiles() {
assert!(is_dark(PRIMARY));
assert!(!is_dark(0xffe08a));
// Discord: --custom-theme-base-color-dark: rgb(0, 7, 26).
assert!(close(base_color(PRIMARY, true), (0, 7, 26)), "{:?}", base_color(PRIMARY, true));
}
#[test]
fn modal_shade_matches_discord() {
// Discord web, 2026-12-01: a window colour of rgb(25, 26, 25) under a
// gradient of oklab(0.0636 / 0.68) to oklab(0.188 / 0.92), which the
// browser lays over it as about 9 at the top and 20 at 300 px.
let window = Color32::from_rgb(25, 26, 25);
let (top, foot) = (modal_shade(window, true, 0.8), modal_shade(window, true, 0.2));
assert!((8..=10).contains(&top.g()), "{top:?}");
assert!((19..=21).contains(&foot.g()), "{foot:?}");
// Nothing mixed in: the window colour itself.
assert!(close(modal_shade(window, true, 0.0), (25, 26, 25)));
}
#[test]
fn lab_round_trips() {
for c in [[29.0, 61.0, 157.0], [255.0, 255.0, 255.0], [0.0, 0.0, 0.0], [200.0, 30.0, 90.0]] {
let back = lab_to_rgb(rgb_to_lab(c));
let ok = oklab_to_rgb(rgb_to_oklab(c));
for i in 0..3 {
assert!((back[i] - c[i]).abs() < 0.6, "{c:?} -> {back:?}");
assert!((ok[i] - c[i]).abs() < 0.6, "{c:?} -> {ok:?}");
}
}
}
}