Skip to main content

clairlune_render/
palette.rs

1//! The indexed palette of one rendering (at most 255 colours).
2//!
3//! Layout (by construction, so every pixel lands on an entry):
4//!
5//! | Indices | Entries |
6//! |---|---|
7//! | 0-7 | background bands (vignette 0-3, warm glow 4-7) |
8//! | 8-55 | ink: band x 6 levels (ink in glow bands is tinted toward the accent) |
9//! | 56-103 | ember particles: band x 6 levels |
10//! | 104.. | sprite colours |
11//! | 255 | reserved (GIF transparency) |
12
13use clairlune_scene::graph::BANDS;
14use clairlune_scene::graph::SpriteKind;
15use clairlune_scene::{PaletteFamily, Rgb};
16
17/// Ink levels per band (glyph alpha x shade, quantised).
18pub 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;
22/// Reserved for GIF transparency; never used by a rendered pixel.
23pub 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        for rgb in clairlune_sprites::builtin_character().colors() {
70            if !colors[SPRITE_BASE as usize..].contains(&rgb) {
71                colors.push(rgb);
72            }
73        }
74        assert!(
75            colors.len() <= TRANSPARENT_INDEX as usize,
76            "palette over budget"
77        );
78        Self { colors }
79    }
80
81    pub fn len(&self) -> usize {
82        self.colors.len()
83    }
84
85    pub fn is_empty(&self) -> bool {
86        self.colors.is_empty()
87    }
88
89    pub fn colors(&self) -> &[Rgb] {
90        &self.colors
91    }
92
93    /// `r g b` triplets for a PNG/GIF palette chunk.
94    pub fn rgb_bytes(&self) -> Vec<u8> {
95        self.colors.iter().flat_map(|c| [c.r, c.g, c.b]).collect()
96    }
97
98    /// Ink entry for a background band and level `1..=LEVELS`.
99    #[inline]
100    pub fn ink(band: u8, level: u8) -> u8 {
101        INK_BASE + band * LEVELS + level - 1
102    }
103
104    /// Ember entry for a background band and level `1..=LEVELS`.
105    #[inline]
106    pub fn ember(band: u8, level: u8) -> u8 {
107        EMBER_BASE + band * LEVELS + level - 1
108    }
109
110    /// Palette index of a sprite colour.
111    pub fn sprite_index(&self, rgb: Rgb) -> Option<u8> {
112        self.colors[SPRITE_BASE as usize..]
113            .iter()
114            .position(|&c| c == rgb)
115            .map(|i| SPRITE_BASE + i as u8)
116    }
117}
118
119/// Ink level `0..=LEVELS` from glyph alpha and cell shade (both 0-255).
120#[inline]
121pub fn ink_level(alpha: u8, shade: u8) -> u8 {
122    let v = u32::from(alpha) * u32::from(shade) * u32::from(LEVELS);
123    ((v + 65025 / 2) / 65025) as u8
124}
125
126#[cfg(test)]
127mod tests {
128    use super::*;
129    use clairlune_scene::palette::FAMILIES;
130
131    #[test]
132    fn every_family_fits_the_budget() {
133        for f in &FAMILIES {
134            let p = IndexedPalette::new(f);
135            assert!(p.len() <= 255, "{}: {}", f.id, p.len());
136            assert!(p.sprite_index(Rgb::hex(0xD9785C)).is_some());
137        }
138    }
139
140    #[test]
141    fn ink_level_range() {
142        assert_eq!(ink_level(0, 255), 0);
143        assert_eq!(ink_level(255, 255), LEVELS);
144        assert_eq!(ink_level(255, 0), 0);
145    }
146}