1use byteorder::{ReadBytesExt, WriteBytesExt, LE};
2pub use jascpal::PaletteIndex;
3use std::convert::TryInto;
4use std::io::{Read, Result, Write};
5
6#[derive(Debug, Clone)]
8pub struct ColorTable {
9 pub id: i32,
10 pub base: PaletteIndex,
12 pub unit_outline_color: PaletteIndex,
14 pub unit_selection_colors: (PaletteIndex, PaletteIndex),
15 pub minimap_colors: (PaletteIndex, PaletteIndex, PaletteIndex),
17 pub statistics_text_color: i32,
19}
20
21impl ColorTable {
22 pub fn read_from<R: Read>(input: &mut R) -> Result<Self> {
24 let id = input.read_i32::<LE>()?;
25 let base = input.read_i32::<LE>()?.try_into().unwrap();
26 let unit_outline_color = input.read_i32::<LE>()?.try_into().unwrap();
27 let unit_selection_colors = (
28 input.read_i32::<LE>()?.try_into().unwrap(),
29 input.read_i32::<LE>()?.try_into().unwrap(),
30 );
31 let minimap_colors = (
32 input.read_i32::<LE>()?.try_into().unwrap(),
33 input.read_i32::<LE>()?.try_into().unwrap(),
34 input.read_i32::<LE>()?.try_into().unwrap(),
35 );
36 let statistics_text_color = input.read_i32::<LE>()?;
37
38 Ok(Self {
39 id,
40 base,
41 unit_outline_color,
42 unit_selection_colors,
43 minimap_colors,
44 statistics_text_color,
45 })
46 }
47
48 pub fn write_to<W: Write>(&self, output: &mut W) -> Result<()> {
50 output.write_i32::<LE>(self.id)?;
51 output.write_i32::<LE>(self.base.try_into().unwrap())?;
52 output.write_i32::<LE>(self.unit_outline_color.try_into().unwrap())?;
53 output.write_i32::<LE>(self.unit_selection_colors.0.try_into().unwrap())?;
54 output.write_i32::<LE>(self.unit_selection_colors.1.try_into().unwrap())?;
55 output.write_i32::<LE>(self.minimap_colors.0.try_into().unwrap())?;
56 output.write_i32::<LE>(self.minimap_colors.1.try_into().unwrap())?;
57 output.write_i32::<LE>(self.minimap_colors.2.try_into().unwrap())?;
58 output.write_i32::<LE>(self.statistics_text_color)?;
59 Ok(())
60 }
61}