use std::cell::Cell;
use image::DynamicImage;
use ratatui::style::Color;
type Rgb = [u8; 3];
pub const THEMES: [(&str, Rgb, Rgb); 6] = [
("spotify", [30, 215, 96], [0, 175, 235]),
("ocean", [0, 180, 255], [130, 100, 255]),
("violet", [185, 120, 255], [255, 95, 175]),
("sunset", [255, 145, 55], [255, 75, 115]),
("rose", [255, 115, 165], [255, 180, 95]),
("mono", [225, 225, 225], [130, 130, 140]),
];
pub const COVER: usize = THEMES.len();
pub fn name(i: usize) -> &'static str {
THEMES.get(i).map(|t| t.0).unwrap_or("cover")
}
pub fn by_name(n: &str) -> usize {
if n == "cover" { COVER } else { THEMES.iter().position(|t| t.0 == n).unwrap_or(0) }
}
pub fn colors(i: usize, tint: Option<Rgb>) -> (Rgb, Rgb) {
match (THEMES.get(i), tint) {
(Some(t), _) => (t.1, t.2),
(None, Some(c)) => {
let (h, s, v) = to_hsv(c);
(c, if s < 0.2 { from_hsv(h, s, v * 0.6) } else { from_hsv(h + 40.0, s, v) })
}
(None, None) => (THEMES[0].1, THEMES[0].2),
}
}
thread_local! {
static CURRENT: Cell<(Rgb, Rgb)> = const { Cell::new(([30, 215, 96], [0, 175, 235])) };
}
pub fn set(c: (Rgb, Rgb)) {
CURRENT.with(|t| t.set(c));
}
pub fn accent() -> Color {
let [r, g, b] = CURRENT.with(Cell::get).0;
Color::Rgb(r, g, b)
}
pub fn on_accent() -> Color {
if luma(CURRENT.with(Cell::get).0) > 110.0 { Color::Black } else { Color::White }
}
pub fn grad(t: f32) -> Color {
let (a, b) = CURRENT.with(Cell::get);
let t = t.clamp(0.0, 1.0);
let mix = |i: usize| (a[i] as f32 + (b[i] as f32 - a[i] as f32) * t) as u8;
Color::Rgb(mix(0), mix(1), mix(2))
}
pub fn cover_tint(img: &DynamicImage) -> Rgb {
const BINS: usize = 24; let small = img.thumbnail(48, 48).to_rgb8();
let n = small.len() as f32 / 3.0;
let mut bins = [[0f32; 4]; BINS];
let mut avg = [0f32; 3];
let mut colourful = 0f32;
for p in small.pixels() {
let [r, g, b] = p.0.map(|c| c as f32);
avg = [avg[0] + r, avg[1] + g, avg[2] + b];
let (h, _, v) = to_hsv(p.0);
let chroma = (r.max(g).max(b) - r.min(g).min(b)) / 255.0;
if chroma < 0.06 || v < 0.12 {
continue;
}
colourful += chroma;
let w = chroma * chroma;
let bin = &mut bins[(h / (360.0 / BINS as f32)) as usize % BINS];
bin[0] += w;
bin[1] += r * w;
bin[2] += g * w;
bin[3] += b * w;
}
if colourful < n * 0.012 {
let (h, s, _) = to_hsv(avg.map(|c| (c / n) as u8));
return from_hsv(h, s.min(0.12), 0.86);
}
let at = |i: isize| bins[i.rem_euclid(BINS as isize) as usize];
let best = (0..BINS as isize)
.max_by(|&a, &b| {
let score = |i| at(i - 1)[0] * 0.5 + at(i)[0] + at(i + 1)[0] * 0.5;
score(a).total_cmp(&score(b))
})
.unwrap_or(0);
let sum = [at(best - 1), at(best), at(best + 1)].iter().fold([0f32; 4], |acc, b| std::array::from_fn(|i| acc[i] + b[i]));
let (h, s, v) = to_hsv([sum[1] / sum[0], sum[2] / sum[0], sum[3] / sum[0]].map(|c| c as u8));
let (mut s, v) = (s.clamp(0.35, 0.85), v.max(0.88));
while luma(from_hsv(h, s, v)) < 125.0 && s > 0.2 {
s -= 0.05;
}
from_hsv(h, s, v)
}
fn luma([r, g, b]: Rgb) -> f32 {
0.2126 * r as f32 + 0.7152 * g as f32 + 0.0722 * b as f32
}
fn to_hsv([r, g, b]: Rgb) -> (f32, f32, f32) {
let [r, g, b] = [r, g, b].map(|c| c as f32 / 255.0);
let (max, min) = (r.max(g).max(b), r.min(g).min(b));
let d = max - min;
let h = if d == 0.0 {
0.0
} else if max == r {
60.0 * ((g - b) / d).rem_euclid(6.0)
} else if max == g {
60.0 * ((b - r) / d + 2.0)
} else {
60.0 * ((r - g) / d + 4.0)
};
(h, if max > 0.0 { d / max } else { 0.0 }, max)
}
fn from_hsv(h: f32, s: f32, v: f32) -> Rgb {
let h = h.rem_euclid(360.0) / 60.0;
let c = v * s;
let x = c * (1.0 - (h % 2.0 - 1.0).abs());
let (r, g, b) = match h as u32 {
0 => (c, x, 0.0),
1 => (x, c, 0.0),
2 => (0.0, c, x),
3 => (0.0, x, c),
4 => (x, 0.0, c),
_ => (c, 0.0, x),
};
let m = v - c;
[r, g, b].map(|u| ((u + m) * 255.0).round() as u8)
}
#[cfg(test)]
mod tests {
use super::*;
use image::{Rgb as Px, RgbImage};
fn tint(f: impl Fn(u32, u32) -> [u8; 3]) -> (f32, f32, f32) {
to_hsv(cover_tint(&DynamicImage::ImageRgb8(RgbImage::from_fn(48, 48, |x, y| Px(f(x, y))))))
}
#[test]
fn picks_cover_colours() {
let (h, s, v) = tint(|x, _| if x < 12 { [140, 20, 20] } else { [30, 30, 30] });
assert!((h < 15.0 || h > 345.0) && s >= 0.45 && v >= 0.87, "{h} {s} {v}");
let (h, s, _) = tint(|_, _| [250, 228, 236]);
assert!((h > 300.0 || h < 20.0) && s >= 0.34, "{h} {s}");
let (h, ..) = tint(|_, y| if y < 6 { [20, 90, 230] } else { [245, 245, 245] });
assert!((200.0..=240.0).contains(&h), "{h}");
let (_, s, v) = tint(|x, _| if x < 24 { [0, 0, 0] } else { [255, 255, 255] });
assert!(s <= 0.13 && v > 0.8, "{s} {v}");
assert!(luma(cover_tint(&DynamicImage::ImageRgb8(RgbImage::from_fn(48, 48, |_, _| Px([40, 20, 200]))))) >= 125.0);
assert_eq!(from_hsv(120.0, 1.0, 1.0), [0, 255, 0]);
assert_eq!(by_name(name(COVER)), COVER);
}
}