Skip to main content

ferric_alpha_render/
theme.rs

1use crate::RenderTheme;
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub struct Color {
5    pub red: u8,
6    pub green: u8,
7    pub blue: u8,
8}
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub struct Theme {
12    pub background: Color,
13    pub surface: Color,
14    pub alternate_row: Color,
15    pub grid: Color,
16    pub text: Color,
17    pub muted_text: Color,
18    pub accent: Color,
19}
20
21const SERIES: [Color; 7] = [
22    Color::new(0x25, 0x63, 0xeb),
23    Color::new(0xdc, 0x26, 0x26),
24    Color::new(0x16, 0xa3, 0x4a),
25    Color::new(0x93, 0x33, 0xea),
26    Color::new(0xea, 0x58, 0x0c),
27    Color::new(0x08, 0x94, 0x94),
28    Color::new(0xbe, 0x12, 0x3c),
29];
30
31impl Color {
32    pub const fn new(red: u8, green: u8, blue: u8) -> Self {
33        Self { red, green, blue }
34    }
35
36    pub fn hex(self) -> String {
37        format!("#{:02x}{:02x}{:02x}", self.red, self.green, self.blue)
38    }
39
40    pub(crate) fn rgb(self) -> plotters::style::RGBColor {
41        plotters::style::RGBColor(self.red, self.green, self.blue)
42    }
43}
44
45impl Theme {
46    pub fn series_color_for_key(key: &str) -> Color {
47        let index = stable_index(key, SERIES.len());
48        SERIES[index]
49    }
50}
51
52pub fn theme_for(theme: RenderTheme) -> Theme {
53    match theme {
54        RenderTheme::Light => Theme {
55            background: Color::new(0xff, 0xff, 0xff),
56            surface: Color::new(0xff, 0xff, 0xff),
57            alternate_row: Color::new(0xf8, 0xfa, 0xfc),
58            grid: Color::new(0xe5, 0xe7, 0xeb),
59            text: Color::new(0x1f, 0x29, 0x37),
60            muted_text: Color::new(0x6b, 0x72, 0x80),
61            accent: Color::new(0x25, 0x63, 0xeb),
62        },
63        RenderTheme::Dark => Theme {
64            background: Color::new(0x11, 0x18, 0x27),
65            surface: Color::new(0x1f, 0x29, 0x37),
66            alternate_row: Color::new(0x17, 0x24, 0x36),
67            grid: Color::new(0x37, 0x41, 0x51),
68            text: Color::new(0xf9, 0xfa, 0xfb),
69            muted_text: Color::new(0xd1, 0xd5, 0xdb),
70            accent: Color::new(0x60, 0xa5, 0xfa),
71        },
72    }
73}
74
75pub fn series_color(index: usize) -> Color {
76    SERIES[index % SERIES.len()]
77}
78
79pub fn contrast_ratio(left: Color, right: Color) -> f64 {
80    let left = relative_luminance(left);
81    let right = relative_luminance(right);
82    let lighter = left.max(right);
83    let darker = left.min(right);
84    (lighter + 0.05) / (darker + 0.05)
85}
86
87fn relative_luminance(color: Color) -> f64 {
88    fn channel(value: u8) -> f64 {
89        let value = f64::from(value) / 255.0;
90        if value <= 0.03928 {
91            value / 12.92
92        } else {
93            ((value + 0.055) / 1.055).powf(2.4)
94        }
95    }
96    0.2126 * channel(color.red) + 0.7152 * channel(color.green) + 0.0722 * channel(color.blue)
97}
98
99fn stable_index(value: &str, modulo: usize) -> usize {
100    let hash = value.bytes().fold(0xcbf29ce484222325_u64, |hash, byte| {
101        (hash ^ u64::from(byte)).wrapping_mul(0x100000001b3)
102    });
103    hash as usize % modulo
104}