1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::errors::IndexedImageError;
use crate::image::IndexedImage;
use crate::prelude::AnimatedIndexedImage;
use crate::IciColor;

#[derive(Debug, Clone, PartialEq)]
pub enum IndexedWrapper {
    Static(IndexedImage),
    Animated(AnimatedIndexedImage),
}

impl From<IndexedImage> for IndexedWrapper {
    fn from(value: IndexedImage) -> Self {
        IndexedWrapper::Static(value)
    }
}

impl From<AnimatedIndexedImage> for IndexedWrapper {
    fn from(value: AnimatedIndexedImage) -> Self {
        IndexedWrapper::Animated(value)
    }
}

impl IndexedWrapper {
    /// Replace palette for image
    /// Will only return an error if the new palette has less colors than the image needs
    pub fn set_palette(&mut self, palette: &[IciColor]) -> Result<(), IndexedImageError> {
        match self {
            IndexedWrapper::Static(img) => img.set_palette(palette),
            IndexedWrapper::Animated(img) => img.set_palette(palette),
        }
    }

    /// Replace palette for image, any pixels outside the new palette will be replaced with `id`
    /// Will only return an error if id is outside the new palette
    pub fn set_palette_replace_id(
        &mut self,
        palette: &[IciColor],
        id: u8,
    ) -> Result<(), IndexedImageError> {
        match self {
            IndexedWrapper::Static(img) => img.set_palette_replace_id(palette, id),
            IndexedWrapper::Animated(img) => img.set_palette_replace_id(palette, id),
        }
    }

    /// Replace palette for image, any color indexes outside the palette will be expanded with `color`
    pub fn set_palette_replace_color<C: Into<IciColor> + Copy>(
        &mut self,
        palette: &[IciColor],
        color: C,
    ) {
        match self {
            IndexedWrapper::Static(img) => img.set_palette_replace_color(palette, color),
            IndexedWrapper::Animated(img) => img.set_palette_replace_color(palette, color),
        }
    }

    pub fn size(&self) -> (u8, u8) {
        match self {
            IndexedWrapper::Static(img) => img.size(),
            IndexedWrapper::Animated(img) => img.size(),
        }
    }

    pub fn get_pixels(&self) -> &[u8] {
        match self {
            IndexedWrapper::Static(img) => img.get_pixels(),
            IndexedWrapper::Animated(img) => img.get_pixels(),
        }
    }

    pub fn get_pixel_index(&self, x: u8, y: u8) -> Result<usize, IndexedImageError> {
        match self {
            IndexedWrapper::Static(img) => img.get_pixel_index(x, y),
            IndexedWrapper::Animated(img) => img.get_pixel_index(x, y),
        }
    }

    pub fn get_color(&self, idx: u8) -> Result<IciColor, IndexedImageError> {
        match self {
            IndexedWrapper::Static(img) => img.get_color(idx),
            IndexedWrapper::Animated(img) => img.get_color(idx),
        }
    }

    pub fn set_color(&mut self, idx: u8, color: IciColor) -> Result<(), IndexedImageError> {
        match self {
            IndexedWrapper::Static(img) => img.set_color(idx, color),
            IndexedWrapper::Animated(img) => img.set_color(idx, color),
        }
    }

    pub fn get_palette(&self) -> &[IciColor] {
        match self {
            IndexedWrapper::Static(img) => img.get_palette(),
            IndexedWrapper::Animated(img) => img.get_palette(),
        }
    }

    pub fn min_palette_size_supported(&self) -> u8 {
        match self {
            IndexedWrapper::Static(img) => img.min_palette_size_supported(),
            IndexedWrapper::Animated(img) => img.min_palette_size_supported(),
        }
    }

    pub fn width(&self) -> u8 {
        match self {
            IndexedWrapper::Static(img) => img.width(),
            IndexedWrapper::Animated(img) => img.width(),
        }
    }

    pub fn height(&self) -> u8 {
        match self {
            IndexedWrapper::Static(img) => img.height(),
            IndexedWrapper::Animated(img) => img.height(),
        }
    }

    pub fn update(&mut self, delta: f64) {
        match self {
            IndexedWrapper::Static(_) => {}
            IndexedWrapper::Animated(img) => img.update(delta),
        }
    }

    pub fn reset(&mut self) {
        match self {
            IndexedWrapper::Static(_) => {}
            IndexedWrapper::Animated(img) => img.reset(),
        }
    }

    #[inline]
    pub fn set_animate(&mut self, animate: bool) {
        match self {
            IndexedWrapper::Static(_) => {}
            IndexedWrapper::Animated(img) => img.set_animate(animate),
        }
    }

    #[inline]
    pub fn animating(&self) -> bool {
        match self {
            IndexedWrapper::Static(_) => false,
            IndexedWrapper::Animated(img) => img.animating(),
        }
    }

    pub fn frame_count(&self) -> u8 {
        match self {
            IndexedWrapper::Static(_) => 1,
            IndexedWrapper::Animated(img) => img.frame_count(),
        }
    }

    pub fn is_animation(&self) -> bool {
        matches!(self, IndexedWrapper::Animated(_))
    }
}