dotzuki_engine/palette.rs
1use std::fmt::Debug;
2use std::hash::Hash;
3
4// ============================================================================
5// SGB Color Types (shared between engine, data, and renderer)
6// ============================================================================
7
8/// A single SGB color in 5-bit-per-channel RGB format.
9/// Stored as the original 5-bit values (0-31 per channel).
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub struct SgbColor {
12 pub r: u8,
13 pub g: u8,
14 pub b: u8,
15}
16
17impl SgbColor {
18 pub const fn new(r: u8, g: u8, b: u8) -> Self {
19 Self { r, g, b }
20 }
21}
22
23/// An SGB palette entry: 4 colors (color0..color3).
24/// color0 is typically the lightest, color3 the darkest.
25pub type SgbPaletteEntry = [SgbColor; 4];
26
27// ============================================================================
28// Palette Trait & Provider
29// ============================================================================
30
31/// Marker trait for palette identifiers.
32///
33/// Implementations are typically lightweight enums or numeric IDs
34/// that uniquely identify a colour palette used for rendering
35/// backgrounds, sprites, or UI elements.
36pub trait PaletteTrait: Copy + Eq + Hash + Debug + 'static {}
37
38/// Provider trait that supplies palette data to the engine.
39///
40/// The renderer queries this provider to obtain the correct colour
41/// palette for the current scene, overworld map, or monster sprite.
42pub trait PaletteProvider<P: PaletteTrait> {
43 /// Returns the 4-colour background palette as an array of colour indices.
44 fn bg_palette(&self, palette: P) -> [u8; 4];
45
46 /// Returns the first 4-colour object (sprite) palette.
47 fn obj_palette0(&self, palette: P) -> [u8; 4];
48
49 /// Returns the second 4-colour object (sprite) palette.
50 fn obj_palette1(&self, palette: P) -> [u8; 4];
51
52 /// Returns the overworld palette for a given tileset and map combination.
53 ///
54 /// The `last_map` parameter is used for palette transition smoothing
55 /// when the player moves between maps with different palettes.
56 fn overworld_palette_for(&self, tileset_id: u8, map_id: u8, last_map: u8) -> P;
57
58 /// Returns the palette used to colour a monster sprite.
59 fn monster_palette(&self, species_index: u8) -> P;
60
61 /// Look up the SGB palette entry (4 SGB colors) for the given palette ID.
62 ///
63 /// Used by SGB rendering mode to convert palette IDs to actual colors.
64 /// Default returns a black fallback.
65 fn sgb_palette_data(&self, _id: P, _is_red: bool) -> SgbPaletteEntry {
66 [SgbColor::new(0, 0, 0); 4]
67 }
68
69 /// Convert HP bar color index (0=green, 1=yellow, 2=red) to a palette ID.
70 fn hp_bar_to_palette_id(&self, hp_bar_color: u8) -> P;
71}