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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
extern crate serde;
#[macro_use] extern crate serde_derive;

pub enum Error {
    RangeError(u8),
}
pub type Result<T> = ::std::result::Result<T, Error>;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum NormalColour {
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    Grey,
}

impl NormalColour {
    pub fn code(self) -> u8 {
        use self::NormalColour::*;
        use self::raw::normal::*;
        match self {
            Black => BLACK,
            Red => RED,
            Green => GREEN,
            Yellow => YELLOW,
            Blue => BLUE,
            Magenta => MAGENTA,
            Cyan => CYAN,
            Grey => GREY,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BrightColour {
    DarkGrey,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,
}

impl BrightColour {
    pub fn code(self) -> u8 {
        use self::BrightColour::*;
        use self::raw::bright::*;
        match self {
            DarkGrey => DARK_GREY,
            Red => RED,
            Green => GREEN,
            Yellow => YELLOW,
            Blue => BLUE,
            Magenta => MAGENTA,
            Cyan => CYAN,
            White => WHITE,
        }

    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RgbColour {
    red: u8,
    green: u8,
    blue: u8,
}

const RGB_START: u8 = 16;
const RGB_MAX_FIELD: u8 = 5;
const RGB_FIELD_RANGE: u8 = RGB_MAX_FIELD + 1;
const RGB_COUNT: u8 = RGB_FIELD_RANGE * RGB_FIELD_RANGE * RGB_FIELD_RANGE;
const RGB_END: u8 = RGB_START + RGB_COUNT - 1;

const GREYSCALE_START: u8 = RGB_END + 1;
const GREYSCALE_MAX_LEVEL: u8 = 23;

impl RgbColour {
    pub fn red(self) -> u8 { self.red }
    pub fn green(self) -> u8 { self.green }
    pub fn blue(self) -> u8 { self.blue }
    pub fn new(red: u8, green: u8, blue: u8) -> Result<Self> {
        if red > RGB_MAX_FIELD {
            return Err(Error::RangeError(red));
        }
        if green > RGB_MAX_FIELD {
            return Err(Error::RangeError(green));
        }
        if blue > RGB_MAX_FIELD {
            return Err(Error::RangeError(blue));
        }
        Ok(Self { red, green, blue })
    }
    pub fn code(self) -> u8 {
        RGB_START +
            (RGB_FIELD_RANGE * RGB_FIELD_RANGE) *
            self.red + RGB_FIELD_RANGE * self.green + self.blue
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct GreyScaleColour(u8);

impl GreyScaleColour {
    pub fn level(self) -> u8 { self.0 }
    pub fn new(level: u8) -> Result<Self> {
        if level > GREYSCALE_MAX_LEVEL {
            return Err(Error::RangeError(level));
        }
        Ok(GreyScaleColour(level))
    }
    pub fn code(self) -> u8 {
        GREYSCALE_START + self.0
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ColourVariant {
    Normal(NormalColour),
    Bright(BrightColour),
    Rgb(RgbColour),
    GreyScale(GreyScaleColour),
}

impl ColourVariant {
    pub fn code(self) -> u8 {
        use self::ColourVariant::*;
        match self {
            Normal(c) => c.code(),
            Bright(c) => c.code(),
            Rgb(c) => c.code(),
            GreyScale(c) => c.code(),
        }
    }
    pub fn colour(self) -> Colour {
        Colour(self.code())
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Colour(u8);

impl Colour {
    pub fn greyscale(level: u8) -> Result<Self> {
        Ok(Colour(GreyScaleColour::new(level)?.code()))
    }
    pub fn rgb(red: u8, green: u8, blue: u8) -> Result<Self> {
        Ok(Colour(RgbColour::new(red, green, blue)?.code()))
    }
    pub fn normal(normal_colour: NormalColour) -> Self {
        Colour(normal_colour.code())
    }
    pub fn bright(bright_colour: BrightColour) -> Self {
        Colour(bright_colour.code())
    }
    pub fn code(self) -> u8 { self.0 }
    pub fn from_code(code: u8) -> Self { Colour(code) }
    pub fn typ(self) -> ColourVariant {
        use self::raw::*;
        match self.0 {
            normal::BLACK => ColourVariant::Normal(NormalColour::Black),
            normal::RED => ColourVariant::Normal(NormalColour::Red),
            normal::GREEN => ColourVariant::Normal(NormalColour::Green),
            normal::YELLOW => ColourVariant::Normal(NormalColour::Yellow),
            normal::BLUE => ColourVariant::Normal(NormalColour::Blue),
            normal::MAGENTA => ColourVariant::Normal(NormalColour::Magenta),
            normal::CYAN => ColourVariant::Normal(NormalColour::Cyan),
            normal::GREY => ColourVariant::Normal(NormalColour::Grey),
            bright::DARK_GREY => ColourVariant::Bright(BrightColour::DarkGrey),
            bright::RED => ColourVariant::Bright(BrightColour::Red),
            bright::GREEN => ColourVariant::Bright(BrightColour::Green),
            bright::YELLOW => ColourVariant::Bright(BrightColour::Yellow),
            bright::BLUE => ColourVariant::Bright(BrightColour::Blue),
            bright::MAGENTA => ColourVariant::Bright(BrightColour::Magenta),
            bright::CYAN => ColourVariant::Bright(BrightColour::Cyan),
            bright::WHITE => ColourVariant::Bright(BrightColour::White),
            RGB_START...RGB_END => {
                let zero_based = self.0 - RGB_START;
                let blue = zero_based % RGB_FIELD_RANGE;
                let zero_based = zero_based / RGB_FIELD_RANGE;
                let green = zero_based % RGB_FIELD_RANGE;
                let zero_based = zero_based / RGB_FIELD_RANGE;
                let red = zero_based % RGB_FIELD_RANGE;
                ColourVariant::Rgb(RgbColour{ red, green, blue })
            }
            GREYSCALE_START...255 => {
                let zero_based = self.0 - GREYSCALE_START;
                ColourVariant::GreyScale(GreyScaleColour(zero_based))
            }
            _ => unreachable!(),
        }
    }
    pub fn all() -> ColourIter {
        ColourIter::new()
    }
}

impl From<ColourVariant> for Colour {
    fn from(t: ColourVariant) -> Self {
        t.colour()
    }
}

impl From<Colour> for ColourVariant {
    fn from(c: Colour) -> Self {
        c.typ()
    }
}

impl From<Colour> for u8 {
    fn from(c: Colour) -> Self {
        c.code()
    }
}

impl From<u8> for Colour {
    fn from(u: u8) -> Self {
        Colour(u)
    }
}

pub struct ColourIter(::std::ops::Range<u16>);

impl ColourIter {
    fn new() -> Self {
        ColourIter(0..256)
    }
}

impl Iterator for ColourIter {
    type Item = Colour;
    fn next(&mut self) -> Option<Self::Item> {
        self.0.next().map(|i| Colour(i as u8))
    }
}

pub struct AllColours;

impl IntoIterator for AllColours {
    type Item = Colour;
    type IntoIter = ColourIter;
    fn into_iter(self) -> Self::IntoIter {
        ColourIter::new()
    }
}

mod raw {
    pub mod normal {
        pub const BLACK: u8 = 0;
        pub const RED: u8 = 1;
        pub const GREEN: u8 = 2;
        pub const YELLOW: u8 = 3;
        pub const BLUE: u8 = 4;
        pub const MAGENTA: u8 = 5;
        pub const CYAN: u8 = 6;
        pub const GREY: u8 = 7;
    }
    pub mod bright {
        pub const DARK_GREY: u8 = 8;
        pub const RED: u8 = 9;
        pub const GREEN: u8 = 10;
        pub const YELLOW: u8 = 11;
        pub const BLUE: u8 = 12;
        pub const MAGENTA: u8 = 13;
        pub const CYAN: u8 = 14;
        pub const WHITE: u8 = 15;
    }
}

pub mod colours {

    use Colour;
    use raw;

    pub const BLACK: Colour = Colour(self::raw::normal::BLACK);
    pub const RED: Colour = Colour(self::raw::normal::RED);
    pub const GREEN: Colour = Colour(self::raw::normal::GREEN);
    pub const YELLOW: Colour = Colour(self::raw::normal::YELLOW);
    pub const BLUE: Colour = Colour(self::raw::normal::BLUE);
    pub const MAGENTA: Colour = Colour(self::raw::normal::MAGENTA);
    pub const CYAN: Colour = Colour(self::raw::normal::CYAN);
    pub const GREY: Colour = Colour(self::raw::normal::GREY);

    pub const DARK_GREY: Colour = Colour(self::raw::bright::DARK_GREY);
    pub const BRIGHT_RED: Colour = Colour(self::raw::bright::RED);
    pub const BRIGHT_GREEN: Colour = Colour(self::raw::bright::GREEN);
    pub const BRIGHT_YELLOW: Colour = Colour(self::raw::bright::YELLOW);
    pub const BRIGHT_BLUE: Colour = Colour(self::raw::bright::BLUE);
    pub const BRIGHT_MAGENTA: Colour = Colour(self::raw::bright::MAGENTA);
    pub const BRIGHT_CYAN: Colour = Colour(self::raw::bright::CYAN);
    pub const WHITE: Colour = Colour(self::raw::bright::WHITE);
}

#[cfg(test)]
mod tests {

    use AllColours;

    #[test]
    fn from_colour() {
        for c in AllColours {
            c.typ();
        }
    }

    #[test]
    fn reflexive() {
        for orig_c in AllColours {
            let orig_t = orig_c.typ();
            let new_c = orig_t.colour();
            let new_t = new_c.typ();

            assert_eq!(orig_t, new_t);
            assert_eq!(orig_c, new_c);
        }
    }
}