use oxiui_core::{Color, Palette};
pub fn cooljapan_high_contrast() -> Palette {
Palette {
background: Color(0, 0, 0, 255), surface: Color(10, 10, 26, 255), primary: Color(255, 255, 0, 255), on_primary: Color(0, 0, 0, 255), text: Color(255, 255, 255, 255), muted: Color(200, 200, 200, 255), }
}
pub fn cooljapan_high_contrast_light() -> Palette {
Palette {
background: Color(250, 250, 250, 255), surface: Color(255, 255, 255, 255), primary: Color(0, 0, 139, 255), on_primary: Color(255, 255, 255, 255), text: Color(0, 0, 0, 255), muted: Color(26, 26, 26, 255), }
}
pub fn wcag_luminance(r: u8, g: u8, b: u8) -> f64 {
let linearize = |c: u8| {
let v = c as f64 / 255.0;
if v <= 0.03928 {
v / 12.92
} else {
((v + 0.055) / 1.055).powf(2.4)
}
};
0.2126 * linearize(r) + 0.7152 * linearize(g) + 0.0722 * linearize(b)
}
pub fn wcag_contrast(fg: (u8, u8, u8), bg: (u8, u8, u8)) -> f64 {
let l1 = wcag_luminance(fg.0, fg.1, fg.2);
let l2 = wcag_luminance(bg.0, bg.1, bg.2);
let (lighter, darker) = if l1 > l2 { (l1, l2) } else { (l2, l1) };
(lighter + 0.05) / (darker + 0.05)
}
#[cfg(test)]
mod tests {
use super::*;
fn contrast(fg: Color, bg: Color) -> f64 {
wcag_contrast((fg.0, fg.1, fg.2), (bg.0, bg.1, bg.2))
}
#[test]
fn hc_light_text_on_background_is_aaa() {
let p = cooljapan_high_contrast_light();
let ratio = contrast(p.text, p.background);
assert!(ratio >= 7.0, "text on background: {ratio:.2} < 7.0");
}
#[test]
fn hc_light_all_text_bg_pairs_are_aaa() {
let p = cooljapan_high_contrast_light();
let bg_rgb = (p.background.0, p.background.1, p.background.2);
let r_text = wcag_contrast((p.text.0, p.text.1, p.text.2), bg_rgb);
assert!(r_text >= 7.0, "text on bg: {r_text:.2}");
let r_muted = wcag_contrast((p.muted.0, p.muted.1, p.muted.2), bg_rgb);
assert!(r_muted >= 7.0, "muted on bg: {r_muted:.2}");
let r_primary = wcag_contrast((p.primary.0, p.primary.1, p.primary.2), bg_rgb);
assert!(r_primary >= 7.0, "primary on bg: {r_primary:.2}");
let primary_rgb = (p.primary.0, p.primary.1, p.primary.2);
let r_on_primary = wcag_contrast(
(p.on_primary.0, p.on_primary.1, p.on_primary.2),
primary_rgb,
);
assert!(
r_on_primary >= 7.0,
"on_primary on primary: {r_on_primary:.2}"
);
}
}