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