clairlune_render/
palette.rs1use clairlune_scene::graph::BANDS;
14use clairlune_scene::graph::SpriteKind;
15use clairlune_scene::{PaletteFamily, Rgb};
16
17pub const LEVELS: u8 = 6;
19pub const INK_BASE: u8 = BANDS;
20pub const EMBER_BASE: u8 = INK_BASE + BANDS * LEVELS;
21pub const SPRITE_BASE: u8 = EMBER_BASE + BANDS * LEVELS;
22pub const TRANSPARENT_INDEX: u8 = 255;
24
25#[derive(Clone, Debug, PartialEq, Eq)]
26pub struct IndexedPalette {
27 colors: Vec<Rgb>,
28}
29
30impl IndexedPalette {
31 pub fn new(family: &PaletteFamily) -> Self {
32 let mut colors = Vec::with_capacity(128);
33 let band = |b: u8| -> Rgb {
34 if b < 4 {
35 family.bg_edge.mix(family.bg_center, f64::from(b) / 3.0)
36 } else {
37 let t = family.glow_strength * f64::from(b - 3) / 4.0;
38 family.bg_center.mix(family.accent, t)
39 }
40 };
41 for b in 0..BANDS {
42 colors.push(band(b));
43 }
44 for b in 0..BANDS {
45 let ink = if b < 4 {
46 family.ink
47 } else {
48 family
49 .ink
50 .mix(family.accent, family.tint_strength * f64::from(b - 3) / 4.0)
51 };
52 for l in 1..=LEVELS {
53 colors.push(band(b).mix(ink, f64::from(l) / f64::from(LEVELS)));
54 }
55 }
56 for b in 0..BANDS {
57 for l in 1..=LEVELS {
58 colors.push(band(b).mix(family.ember, f64::from(l) / f64::from(LEVELS)));
59 }
60 }
61 debug_assert_eq!(colors.len(), SPRITE_BASE as usize);
62 for kind in SpriteKind::ALL {
63 for rgb in kind.map().palette() {
64 if !colors[SPRITE_BASE as usize..].contains(&rgb) {
65 colors.push(rgb);
66 }
67 }
68 }
69 assert!(
70 colors.len() <= TRANSPARENT_INDEX as usize,
71 "palette over budget"
72 );
73 Self { colors }
74 }
75
76 pub fn len(&self) -> usize {
77 self.colors.len()
78 }
79
80 pub fn is_empty(&self) -> bool {
81 self.colors.is_empty()
82 }
83
84 pub fn colors(&self) -> &[Rgb] {
85 &self.colors
86 }
87
88 pub fn rgb_bytes(&self) -> Vec<u8> {
90 self.colors.iter().flat_map(|c| [c.r, c.g, c.b]).collect()
91 }
92
93 #[inline]
95 pub fn ink(band: u8, level: u8) -> u8 {
96 INK_BASE + band * LEVELS + level - 1
97 }
98
99 #[inline]
101 pub fn ember(band: u8, level: u8) -> u8 {
102 EMBER_BASE + band * LEVELS + level - 1
103 }
104
105 pub fn sprite_index(&self, rgb: Rgb) -> Option<u8> {
107 self.colors[SPRITE_BASE as usize..]
108 .iter()
109 .position(|&c| c == rgb)
110 .map(|i| SPRITE_BASE + i as u8)
111 }
112}
113
114#[inline]
116pub fn ink_level(alpha: u8, shade: u8) -> u8 {
117 let v = u32::from(alpha) * u32::from(shade) * u32::from(LEVELS);
118 ((v + 65025 / 2) / 65025) as u8
119}
120
121#[cfg(test)]
122mod tests {
123 use super::*;
124 use clairlune_scene::palette::FAMILIES;
125
126 #[test]
127 fn every_family_fits_the_budget() {
128 for f in &FAMILIES {
129 let p = IndexedPalette::new(f);
130 assert!(p.len() <= 255, "{}: {}", f.id, p.len());
131 assert!(p.sprite_index(Rgb::hex(0xD9785C)).is_some());
132 }
133 }
134
135 #[test]
136 fn ink_level_range() {
137 assert_eq!(ink_level(0, 255), 0);
138 assert_eq!(ink_level(255, 255), LEVELS);
139 assert_eq!(ink_level(255, 0), 0);
140 }
141}