use egui::Color32;
use facett_core::Theme;
const LIGHT: [(u8, u8, u8); 8] = [
(0x2a, 0x78, 0xd6), (0x1b, 0xaf, 0x7a), (0xed, 0xa1, 0x00), (0x00, 0x83, 0x00), (0x4a, 0x3a, 0xa7), (0xe3, 0x49, 0x48), (0xe8, 0x7b, 0xa4), (0xeb, 0x68, 0x34), ];
const DARK: [(u8, u8, u8); 8] = [
(0x39, 0x87, 0xe5), (0x19, 0x9e, 0x70), (0xc9, 0x85, 0x00), (0x00, 0x83, 0x00), (0x90, 0x85, 0xe9), (0xe6, 0x67, 0x67), (0xd5, 0x51, 0x81), (0xd9, 0x59, 0x26), ];
pub const SLOTS: usize = 8;
fn surface_is_dark(th: &Theme) -> bool {
let c = th.bg;
let luma = 0.299 * c.r() as f32 + 0.587 * c.g() as f32 + 0.114 * c.b() as f32;
luma < 128.0
}
pub fn series_palette(th: &Theme) -> [Color32; SLOTS] {
let src = if surface_is_dark(th) { &DARK } else { &LIGHT };
std::array::from_fn(|i| {
let (r, g, b) = src[i];
Color32::from_rgb(r, g, b)
})
}
pub fn series_color(th: &Theme, i: usize) -> Color32 {
series_palette(th)[i % SLOTS]
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn palette_picks_dark_steps_for_dark_theme() {
let p = series_palette(&Theme::default());
assert_eq!(p[0], Color32::from_rgb(0x39, 0x87, 0xe5), "slot 0 = dark blue");
}
#[test]
fn palette_picks_light_steps_for_light_surface() {
let th = Theme { bg: Color32::from_rgb(252, 252, 251), ..Default::default() };
let p = series_palette(&th);
assert_eq!(p[0], Color32::from_rgb(0x2a, 0x78, 0xd6), "slot 0 = light blue");
}
#[test]
fn colours_are_assigned_in_fixed_order_and_cycle() {
let th = Theme::default();
assert_eq!(series_color(&th, 0), series_color(&th, 0));
assert_ne!(series_color(&th, 0), series_color(&th, 1));
assert_eq!(series_color(&th, 0), series_color(&th, SLOTS));
assert_eq!(series_color(&th, 3), series_color(&th, SLOTS + 3));
}
#[test]
fn light_and_dark_are_the_same_eight_hues() {
assert_eq!(LIGHT.len(), SLOTS);
assert_eq!(DARK.len(), SLOTS);
}
}