use gpui_kit::component::{Colorize as _, Theme};
use gpui_kit::{Hsla, Rgba, black, white};
use crate::contrast::{composite, ratio_over};
pub const SWITCH_CONTRAST: f64 = 3.0;
const MIX_STEPS: u16 = 40;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SwitchColors {
pub off: Hsla,
pub on: Hsla,
pub thumb: Hsla,
}
pub fn surfaces(theme: &Theme) -> [(&'static str, Rgba); 7] {
let base: Rgba = theme.background.into();
[
("background", theme.background),
("muted", theme.muted),
("secondary", theme.secondary),
("sidebar", theme.sidebar),
("popover", theme.popover),
("accent", theme.accent),
("list_active", theme.list_active),
]
.map(|(name, color)| (name, composite(color.into(), base)))
}
pub fn tracks_on_surfaces(tracks: [Hsla; 2], surfaces: &[Rgba]) -> Vec<Rgba> {
tracks
.iter()
.flat_map(|track| {
surfaces
.iter()
.map(move |surface| composite((*track).into(), *surface))
})
.collect()
}
pub fn switch_colors(theme: &Theme) -> SwitchColors {
let backgrounds = surfaces(theme).map(|(_, surface)| surface);
let off = legible(theme.switch, theme.foreground, &backgrounds);
let on = legible(theme.primary, theme.foreground, &backgrounds);
let tracks = tracks_on_surfaces([off, on], &backgrounds);
let thumb = legible(theme.switch_thumb, theme.background, &tracks);
SwitchColors { off, on, thumb }
}
pub fn apply(theme: &mut Theme) {
let colors = switch_colors(theme);
write(theme, colors);
}
fn write(theme: &mut Theme, colors: SwitchColors) {
theme.switch = colors.off;
theme.tokens.switch = colors.off.into();
theme.switch_thumb = colors.thumb;
theme.tokens.switch_thumb = colors.thumb.into();
}
fn legible(color: Hsla, target: Hsla, backgrounds: &[Rgba]) -> Hsla {
let candidates = candidates(color, target);
candidates
.iter()
.copied()
.find(|candidate| worst(*candidate, backgrounds) >= SWITCH_CONTRAST)
.unwrap_or_else(|| best(&candidates, backgrounds))
}
fn candidates(color: Hsla, target: Hsla) -> Vec<Hsla> {
std::iter::once(color)
.chain([target, black(), white()].into_iter().flat_map(|goal| {
(1..=MIX_STEPS).map(move |step| {
color.mix_oklab(goal, 1.0 - f32::from(step) / f32::from(MIX_STEPS))
})
}))
.collect()
}
fn worst(color: Hsla, backgrounds: &[Rgba]) -> f64 {
backgrounds
.iter()
.map(|background| ratio_over(color.into(), *background))
.fold(f64::INFINITY, f64::min)
}
fn best(candidates: &[Hsla], backgrounds: &[Rgba]) -> Hsla {
candidates
.iter()
.copied()
.max_by(|a, b| worst(*a, backgrounds).total_cmp(&worst(*b, backgrounds)))
.unwrap_or(black())
}
#[cfg(test)]
mod tests {
use super::{SWITCH_CONTRAST, legible, worst};
use gpui_kit::{Hsla, Rgba, black, rgb, white};
fn solid(hex: u32) -> Hsla {
rgb(hex).into()
}
#[test]
fn a_passing_colour_is_kept_exactly() {
let page: Rgba = white().into();
let track = solid(0x555555);
assert_eq!(legible(track, black(), &[page]), track);
}
#[test]
fn a_faint_track_moves_until_it_passes() {
let page: Rgba = solid(0x292929).into();
let track = legible(page.into(), white(), &[page]);
assert!(worst(track, &[page]) >= SWITCH_CONTRAST);
}
#[test]
fn an_impossible_target_still_moves_to_black_or_white() {
let page: Rgba = white().into();
let track = legible(white(), white(), &[page]);
assert!(worst(track, &[page]) >= SWITCH_CONTRAST);
}
}