#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[must_use]
pub struct Color(pub u32);
impl Color {
pub const NONE: Color = Color(0);
pub const BLACK: Color = Color::rgb(0x000000);
pub const WHITE: Color = Color::rgb(0xffffff);
pub const fn rgb(rgb: u32) -> Self {
Color(0xff00_0000 | rgb)
}
pub const fn with_alpha(self, alpha: f32) -> Self {
let a = (self.a() as f32 * alpha.clamp(0.0, 1.0) + 0.5) as u32;
Color(a << 24 | self.0 & 0x00ff_ffff)
}
pub const fn solid(self) -> Self {
Color(self.0 | 0xff00_0000)
}
pub const fn a(self) -> u8 {
(self.0 >> 24) as u8
}
pub const fn is_none(self) -> bool {
self.a() == 0
}
pub const fn is_dark(self) -> bool {
(self.0 >> 16 & 0xff) * 299 + (self.0 >> 8 & 0xff) * 587 + (self.0 & 0xff) * 114 < 128_000
}
#[inline]
pub fn lerp(self, other: Color, part: f32) -> Self {
let t = (part.clamp(0.0, 1.0) * 256.0) as u32;
let mix = |a: u32, b: u32| ((a * (256 - t) + b * t) >> 8) & 0x00ff_00ff;
Color(mix(self.0 & 0x00ff_00ff, other.0 & 0x00ff_00ff) | mix(self.0 >> 8 & 0x00ff_00ff, other.0 >> 8 & 0x00ff_00ff) << 8)
}
pub fn hsv(hue: f32, strong: f32, light: f32) -> Self {
let part = |ahead: f32| {
let ramp = ((hue.rem_euclid(1.0) * 6.0 + ahead).rem_euclid(6.0) - 3.0).abs() - 1.0;
(light * (1.0 - strong * (1.0 - ramp.clamp(0.0, 1.0))) * 255.0 + 0.5) as u32
};
Color(0xff00_0000 | part(0.0) << 16 | part(4.0) << 8 | part(2.0))
}
pub fn to_hsv(self) -> [f32; 3] {
let [r, g, b] = [self.0 >> 16, self.0 >> 8, self.0].map(|part| (part & 0xff) as f32 / 255.0);
let (most, spread) = (r.max(g).max(b), r.max(g).max(b) - r.min(g).min(b));
let sixth = match spread {
_ if spread == 0.0 => 0.0,
_ if most == r => (g - b) / spread,
_ if most == g => 2.0 + (b - r) / spread,
_ => 4.0 + (r - g) / spread,
};
[(sixth / 6.0).rem_euclid(1.0), if most == 0.0 { 0.0 } else { spread / most }, most]
}
pub fn parse(text: &str) -> Option<Self> {
let digits = text.trim().trim_start_matches('#');
let value = u32::from_str_radix(digits, 16).ok()?;
match digits.len() {
6 => Some(Color::rgb(value)),
8 => Some(Color(value.rotate_right(8))),
_ => None,
}
}
pub fn mix(self, other: Color, part: f32) -> Self {
let (t, [a, b]) = (part.clamp(0.0, 1.0), [self, other].map(|color| color.a() as f32 / 255.0));
let solid = a + (b - a) * t;
if solid <= 0.0 {
return Color::NONE;
}
let ([l1, a1, b1], [l2, a2, b2]) = (self.to_oklab(), other.to_oklab());
let weigh = |one: f32, two: f32| (one * a + (two * b - one * a) * t) / solid;
Color::from_oklab([weigh(l1, l2), weigh(a1, a2), weigh(b1, b2)]).with_alpha(solid)
}
pub fn lighter(self, by: f32) -> Self {
let [light, chroma, hue] = self.to_oklch();
Color::oklch(light + by, chroma, hue).with_alpha(self.a() as f32 / 255.0)
}
pub fn darker(self, by: f32) -> Self {
self.lighter(-by)
}
pub fn hsl(hue: f32, strong: f32, light: f32) -> Self {
let light = light.clamp(0.0, 1.0);
let most = light + strong.clamp(0.0, 1.0) * light.min(1.0 - light);
Color::hsv(hue, if most > 0.0 { 2.0 * (1.0 - light / most) } else { 0.0 }, most)
}
pub fn oklch(light: f32, chroma: f32, hue: f32) -> Self {
let (sin, cos) = (hue * std::f32::consts::TAU).sin_cos();
if light >= 1.0 || light <= 0.0 {
return if light >= 1.0 { Color::WHITE } else { Color::BLACK };
}
let chroma = chroma.max(0.0);
let fits = |chroma: f32| linear_of([light, chroma * cos, chroma * sin]).iter().all(|part| (-1e-4..=1.0001).contains(part));
let (mut fit, mut over) = (0.0, chroma);
if !fits(chroma) {
for _ in 0..24 {
let half = (fit + over) / 2.0;
(fit, over) = if fits(half) { (half, over) } else { (fit, half) };
}
return Color::from_oklab([light, fit * cos, fit * sin]);
}
Color::from_oklab([light, chroma * cos, chroma * sin])
}
pub fn to_oklch(self) -> [f32; 3] {
let [light, a, b] = self.to_oklab();
let chroma = a.hypot(b);
let hue = if chroma < 1e-6 { 0.0 } else { (b.atan2(a) / std::f32::consts::TAU).rem_euclid(1.0) };
[light, chroma, hue]
}
pub fn contrast(self, other: Color) -> f32 {
let under = other.solid();
let over = Color(self.over(under.0, 255));
let [one, two] = [over, under].map(|color| color.luminance());
((one.max(two) + 0.05) / (one.min(two) + 0.05)) as f32
}
pub fn series(nth: usize, ground: Color) -> Self {
Color::rgb(SERIES[ground.is_dark() as usize][nth % 8])
}
fn luminance(self) -> f64 {
let [red, green, blue] = self.linear().map(f64::from);
0.2126 * red + 0.7152 * green + 0.0722 * blue
}
fn linear(self) -> [f32; 3] {
let unbend = |part: u32| {
let part = (part & 0xff) as f32 / 255.0;
if part <= 0.04045 { part / 12.92 } else { ((part + 0.055) / 1.055).powf(2.4) }
};
[unbend(self.0 >> 16), unbend(self.0 >> 8), unbend(self.0)]
}
fn to_oklab(self) -> [f32; 3] {
let [r, g, b] = self.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 from_oklab(lab: [f32; 3]) -> Self {
let bend = |part: f32| {
let part = part.clamp(0.0, 1.0);
let bent = if part <= 0.003_130_8 { 12.92 * part } else { 1.055 * part.powf(1.0 / 2.4) - 0.055 };
(bent * 255.0 + 0.5) as u32
};
let [r, g, b] = linear_of(lab);
Color(0xff00_0000 | bend(r) << 16 | bend(g) << 8 | bend(b))
}
#[inline(always)]
pub(crate) fn over(self, under: u32, cover: u32) -> u32 {
let a = self.a() as u32 * cover / 255;
if a == 0 {
return under;
}
let (s, d) = (self.0 | 0xff00_0000, under);
if a == 255 {
return s;
}
let mix = |s: u32, d: u32| (d + (s.wrapping_sub(d).wrapping_mul(a) >> 8)) & 0x00ff_00ff;
mix(s & 0x00ff_00ff, d & 0x00ff_00ff) | mix(s >> 8 & 0x00ff_00ff, d >> 8 & 0x00ff_00ff) << 8
}
}
fn linear_of([light, a, b]: [f32; 3]) -> [f32; 3] {
let l = (light + 0.396_337_78 * a + 0.215_803_76 * b).powi(3);
let m = (light - 0.105_561_346 * a - 0.063_854_17 * b).powi(3);
let s = (light - 0.089_484_18 * a - 1.291_485_5 * b).powi(3);
[4.076_741_7 * l - 3.307_711_6 * m + 0.230_969_93 * s, -1.268_438 * l + 2.609_757_4 * m - 0.341_319_4 * s, -0.004_196_086_3 * l - 0.703_418_6 * m + 1.707_614_7 * s]
}
const SERIES: [[u32; 8]; 2] = [[0x2a78d6, 0xeb6834, 0x1baf7a, 0xeda100, 0xe87ba4, 0x008300, 0x6250d6, 0xe34948], [0x3987e5, 0xd95926, 0x199e70, 0xc98500, 0xd55181, 0x008300, 0x9085e9, 0xe66767]];
impl std::fmt::Display for Color {
fn fmt(&self, to: &mut std::fmt::Formatter) -> std::fmt::Result {
match self.a() {
255 => write!(to, "#{:06x}", self.0 & 0x00ff_ffff),
solid => write!(to, "#{:06x}{solid:02x}", self.0 & 0x00ff_ffff),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_colour_goes_to_hue_and_text_and_back() {
for (color, hsv) in [(0xff0000, [0.0, 1.0, 1.0]), (0x00ff00, [1.0 / 3.0, 1.0, 1.0]), (0x0000ff, [2.0 / 3.0, 1.0, 1.0]), (0x808080, [0.0, 0.0, 128.0 / 255.0])] {
assert_eq!(Color::rgb(color).to_hsv(), hsv, "{color:06x}");
assert_eq!(Color::hsv(hsv[0], hsv[1], hsv[2]), Color::rgb(color));
}
for color in [0x8b93ff, 0x123456, 0xfedcba, 0x000000, 0xffffff, 0x7f0001] {
let [hue, strong, light] = Color::rgb(color).to_hsv();
assert_eq!(Color::hsv(hue, strong, light), Color::rgb(color), "{color:06x} comes back the same");
}
assert_eq!(Color::rgb(0x8b93ff).to_string(), "#8b93ff");
assert_eq!(Color::rgb(0x8b93ff).with_alpha(0.5).to_string(), "#8b93ff80");
assert_eq!(Color::parse("#8b93ff"), Some(Color::rgb(0x8b93ff)));
assert_eq!(Color::parse("8B93FF80"), Some(Color(0x808b_93ff)));
assert_eq!(Color::parse("#12"), None);
}
#[test]
fn laying_a_colour_over_another() {
let red = Color::rgb(0xff0000);
assert_eq!(red.over(0x0000ff, 255), 0xffff0000);
assert_eq!(red.over(0x0000ff, 0), 0x0000ff);
let half = red.with_alpha(0.5).over(0x000000, 255);
assert!((0x7e7e0000..=0x80800000).contains(&half), "half red, half solid: {half:08x}");
assert_eq!(Color::rgb(0x123456).with_alpha(0.0), Color(0x00123456));
}
#[test]
fn colours_lie_in_oklab_where_web_pages_put_them() {
let known = [
(0xff0000, [0.62796, 0.25768, 0.08121]),
(0x00ff00, [0.86644, 0.29483, 0.39582]),
(0x0000ff, [0.45201, 0.31321, 0.73348]),
(0x8b93ff, [0.70436, 0.15663, 0.77323]),
(0xe8b04b, [0.79070, 0.13307, 0.22029]),
(0x123456, [0.31917, 0.07245, 0.69769]),
];
for (rgb, [light, chroma, hue]) in known {
let [l, c, h] = Color::rgb(rgb).to_oklch();
assert!((l - light).abs() < 2e-5 && (c - chroma).abs() < 2e-5 && (h - hue).abs() < 2e-5, "{rgb:06x}: {:?}", [l, c, h]);
assert_eq!(Color::oklch(light, chroma, hue), Color::rgb(rgb), "{rgb:06x} comes back from its numbers");
}
let [white, grey, _] = Color::WHITE.to_oklch();
assert!((white - 1.0).abs() < 1e-5 && grey < 1e-4);
for rgb in (0..4096u32).map(|i| ((i >> 8) * 17) << 16 | ((i >> 4 & 15) * 17) << 8 | ((i & 15) * 17)) {
let [l, c, h] = Color::rgb(rgb).to_oklch();
assert_eq!(Color::oklch(l, c, h), Color::rgb(rgb), "{rgb:06x}");
}
assert_eq!(Color::oklch(0.5, 0.0, 0.0), Color::rgb(0x636363));
let [l, c, h] = Color::oklch(0.7, 0.4, 0.4).to_oklch();
assert!((l - 0.7).abs() < 0.003 && (h - 0.4).abs() < 0.003 && c > 0.1 && c < 0.4, "{:?}", [l, c, h]);
let past = linear_of([0.7, (c + 0.004) * (0.4 * std::f32::consts::TAU).cos(), (c + 0.004) * (0.4 * std::f32::consts::TAU).sin()]);
assert!(past.iter().any(|part| !(0.0..=1.0).contains(part)), "{past:?}");
assert_eq!((Color::oklch(1.5, 0.2, 0.3), Color::oklch(-1.0, 0.2, 0.3)), (Color::WHITE, Color::BLACK));
}
#[test]
fn colours_are_mixed_lightened_and_darkened_as_the_eye_sees_them() {
let (red, blue) = (Color::rgb(0xff0000), Color::rgb(0x0000ff));
assert_eq!((red.mix(blue, 0.0), red.mix(blue, 1.0), red.mix(red, 0.4)), (red, blue, red));
assert_eq!(Color::BLACK.mix(Color::WHITE, 0.5), Color::rgb(0x636363));
for (one, two, part) in [(0xff0000, 0x0000ff, 0.5), (0x8b93ff, 0xe8b04b, 0.3), (0x123456, 0xfedcba, 0.8)] {
let ([l1, a1, b1], [l2, a2, b2]) = (Color::rgb(one).to_oklab(), Color::rgb(two).to_oklab());
let [l, a, b] = Color::rgb(one).mix(Color::rgb(two), part).to_oklab();
let want = [l1 + (l2 - l1) * part, a1 + (a2 - a1) * part, b1 + (b2 - b1) * part];
assert!((l - want[0]).abs() < 0.004 && (a - want[1]).abs() < 0.004 && (b - want[2]).abs() < 0.004, "{one:06x} {two:06x}");
}
assert_eq!(red.mix(Color::NONE, 0.5), red.with_alpha(0.5));
assert_eq!(Color::NONE.mix(Color::NONE, 0.5), Color::NONE);
let accent = Color::rgb(0x8b93ff);
let ([l, c, h], [lighter, _, hue], [darker, chroma, _]) = (accent.to_oklch(), accent.lighter(0.1).to_oklch(), accent.darker(0.1).to_oklch());
assert!((lighter - l - 0.1).abs() < 0.004 && (l - darker - 0.1).abs() < 0.004 && (hue - h).abs() < 0.01 && (chroma - c).abs() < 0.004);
assert_eq!((Color::WHITE.lighter(0.2), accent.darker(1.0), red.with_alpha(0.5).lighter(0.0)), (Color::WHITE, Color::BLACK, red.with_alpha(0.5)));
}
#[test]
fn colours_come_from_hue_strength_and_lightness() {
assert_eq!(Color::hsl(0.0, 1.0, 0.5), Color::rgb(0xff0000));
assert_eq!(Color::hsl(120.0 / 360.0, 1.0, 0.25), Color::rgb(0x008000));
assert_eq!(Color::hsl(210.0 / 360.0, 0.5, 0.6), Color::rgb(0x6699cc));
assert_eq!((Color::hsl(0.3, 0.7, 0.0), Color::hsl(0.3, 0.7, 1.0), Color::hsl(0.3, 0.0, 0.5)), (Color::BLACK, Color::WHITE, Color::rgb(0x808080)));
}
#[test]
fn contrast_is_counted_as_the_accessibility_guidelines_count_it() {
assert_eq!(Color::BLACK.contrast(Color::WHITE), 21.0);
assert_eq!(Color::WHITE.contrast(Color::BLACK), 21.0);
assert_eq!(Color::rgb(0x8b93ff).contrast(Color::rgb(0x8b93ff)), 1.0);
assert!((Color::rgb(0x767676).contrast(Color::WHITE) - 4.54).abs() < 0.005);
assert!((Color::rgb(0x777777).contrast(Color::WHITE) - 4.48).abs() < 0.005);
assert_eq!(Color::WHITE.with_alpha(0.5).contrast(Color::BLACK), Color::rgb(0x7f7f7f).contrast(Color::BLACK));
assert!((Color::rgb(0x808080).contrast(Color::BLACK) - 5.32).abs() < 0.005);
}
fn seen(color: Color, by: usize) -> [f32; 3] {
let ways = [
[[0.152286, 1.052583, -0.204868], [0.114503, 0.786281, 0.099216], [-0.003882, -0.048116, 1.051998]],
[[0.367322, 0.860646, -0.227968], [0.280085, 0.672501, 0.047413], [-0.011820, 0.042940, 0.968881]],
[[1.255528, -0.076749, -0.178779], [-0.078411, 0.930809, 0.147602], [0.004733, 0.691367, 0.303900]],
];
let linear = color.linear();
ways[by].map(|row| (row[0] * linear[0] + row[1] * linear[1] + row[2] * linear[2]).clamp(0.0, 1.0))
}
#[test]
fn the_rows_of_a_chart_are_told_apart_by_everyone() {
let lab = |linear: [f32; 3]| {
let [l, a, b] = [
0.412_221_47 * linear[0] + 0.536_332_54 * linear[1] + 0.051_445_99 * linear[2],
0.211_903_5 * linear[0] + 0.680_699_5 * linear[1] + 0.107_396_96 * linear[2],
0.088_302_46 * linear[0] + 0.281_718_84 * linear[1] + 0.629_978_7 * linear[2],
]
.map(f32::cbrt);
[0.210_454_26 * l + 0.793_617_8 * a - 0.004_072_047 * b, 1.977_998_5 * l - 2.428_592_2 * a + 0.450_593_7 * b, 0.025_904_037 * l + 0.782_771_77 * a - 0.808_675_77 * b]
};
let apart = |one: [f32; 3], two: [f32; 3]| 100.0 * (lab(one)[0] - lab(two)[0]).hypot(lab(one)[1] - lab(two)[1]).hypot(lab(one)[2] - lab(two)[2]);
for (ground, [least_seen, least]) in [(Color::WHITE, [9.1, 19.6]), (Color::rgb(0x151821), [8.4, 19.3])] {
let rows: Vec<Color> = (0..8).map(|nth| Color::series(nth, ground)).collect();
let pairs = || rows.windows(2).map(|pair| (pair[0], pair[1]));
let worst_seen = pairs().flat_map(|(one, two)| [0, 1].map(|by| apart(seen(one, by), seen(two, by)))).fold(f32::INFINITY, f32::min);
let worst = pairs().map(|(one, two)| apart(one.linear(), two.linear())).fold(f32::INFINITY, f32::min);
assert!((worst_seen - least_seen).abs() < 0.05 && (worst - least).abs() < 0.05, "on {ground}: {worst_seen:.2} and {worst:.2}");
let standing: Vec<bool> = rows.iter().map(|row| row.contrast(ground) >= 3.0).collect();
let want = if ground.is_dark() { [true; 8] } else { [true, true, false, false, false, true, true, true] };
assert_eq!(standing, want, "on {ground}");
}
assert_eq!((Color::series(9, Color::WHITE), Color::series(3, Color::rgb(0xf5f6f8))), (Color::series(1, Color::WHITE), Color::series(3, Color::WHITE)));
assert_ne!(Color::series(0, Color::WHITE), Color::series(0, Color::BLACK));
}
}