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
#[derive(Debug, Copy, Clone)]
pub struct RGBColor {
    red: u8,
    green: u8,
    blue: u8,
}

/// Use the 3d nearest neighbor to determine an approximation for RGB colors
pub fn nearest_palette(red: u8, green: u8, blue: u8) -> u8 {
    let mut ifurthest = 0usize;
    let mut furthest = 3 * (255 as i32).pow(2) + 1;
    for (i, color) in COLOR_PALETTE.iter().enumerate() {
        // Exact match
        if red == color.red && green == color.green && blue == color.blue {
            return i as u8;
        }

        let distance = (red as i32 - color.red as i32).pow(2) +
                       (green as i32 - color.green as i32).pow(2) +
                       (blue as i32 - color.blue as i32).pow(2);
        if distance < furthest {
            furthest = distance;
            ifurthest = i;
        }
    }

    return ifurthest as u8;
}


/// palette Table information from http://launchpaddr.com/mk2palette/
pub const COLOR_PALETTE: [RGBColor; 128] = [
    // 0..64
    RGBColor{red: 0x00, green: 0x00, blue: 0x00},
    RGBColor{red: 0x1c, green: 0x1c, blue: 0x1c},
    RGBColor{red: 0x7c, green: 0x7c, blue: 0x7c},
    RGBColor{red: 0xfc, green: 0xfc, blue: 0xfc},
    RGBColor{red: 0xff, green: 0x4e, blue: 0x48},
    RGBColor{red: 0xfe, green: 0x0a, blue: 0x00},
    RGBColor{red: 0x5a, green: 0x00, blue: 0x00},
    RGBColor{red: 0x18, green: 0x00, blue: 0x02},
    RGBColor{red: 0xff, green: 0xbc, blue: 0x63},
    RGBColor{red: 0xff, green: 0x57, blue: 0x00},
    RGBColor{red: 0x5a, green: 0x1d, blue: 0x00},
    RGBColor{red: 0x24, green: 0x18, blue: 0x02},
    RGBColor{red: 0xfd, green: 0xfd, blue: 0x21},
    RGBColor{red: 0xfd, green: 0xfd, blue: 0x00},
    RGBColor{red: 0x58, green: 0x58, blue: 0x00},
    RGBColor{red: 0x18, green: 0x18, blue: 0x00},
    RGBColor{red: 0x81, green: 0xfd, blue: 0x2b},
    RGBColor{red: 0x40, green: 0xfd, blue: 0x01},
    RGBColor{red: 0x16, green: 0x58, blue: 0x00},
    RGBColor{red: 0x13, green: 0x28, blue: 0x01},
    RGBColor{red: 0x35, green: 0xfd, blue: 0x2b},
    RGBColor{red: 0x00, green: 0xfe, blue: 0x00},
    RGBColor{red: 0x00, green: 0x58, blue: 0x01},
    RGBColor{red: 0x00, green: 0x18, blue: 0x00},
    RGBColor{red: 0x35, green: 0xfc, blue: 0x47},
    RGBColor{red: 0x00, green: 0xfe, blue: 0x00},
    RGBColor{red: 0x00, green: 0x58, blue: 0x01},
    RGBColor{red: 0x00, green: 0x18, blue: 0x00},
    RGBColor{red: 0x32, green: 0xfd, blue: 0x7f},
    RGBColor{red: 0x00, green: 0xfd, blue: 0x3a},
    RGBColor{red: 0x01, green: 0x58, blue: 0x14},
    RGBColor{red: 0x00, green: 0x1c, blue: 0x0e},
    RGBColor{red: 0x2f, green: 0xfc, blue: 0xb1},
    RGBColor{red: 0x00, green: 0xfb, blue: 0x91},
    RGBColor{red: 0x01, green: 0x57, blue: 0x32},
    RGBColor{red: 0x01, green: 0x18, blue: 0x10},
    RGBColor{red: 0x39, green: 0xbe, blue: 0xff},
    RGBColor{red: 0x00, green: 0xa7, blue: 0xff},
    RGBColor{red: 0x01, green: 0x40, blue: 0x51},
    RGBColor{red: 0x00, green: 0x10, blue: 0x18},
    RGBColor{red: 0x41, green: 0x86, blue: 0xff},
    RGBColor{red: 0x00, green: 0x50, blue: 0xff},
    RGBColor{red: 0x01, green: 0x1a, blue: 0x5a},
    RGBColor{red: 0x01, green: 0x06, blue: 0x19},
    RGBColor{red: 0x47, green: 0x47, blue: 0xff},
    RGBColor{red: 0x00, green: 0x00, blue: 0xfe},
    RGBColor{red: 0x00, green: 0x00, blue: 0x5a},
    RGBColor{red: 0x00, green: 0x00, blue: 0x18},
    RGBColor{red: 0x83, green: 0x47, blue: 0xff},
    RGBColor{red: 0x50, green: 0x00, blue: 0xff},
    RGBColor{red: 0x16, green: 0x00, blue: 0x67},
    RGBColor{red: 0x0a, green: 0x00, blue: 0x32},
    RGBColor{red: 0xff, green: 0x48, blue: 0xfe},
    RGBColor{red: 0xff, green: 0x00, blue: 0xfe},
    RGBColor{red: 0x5a, green: 0x00, blue: 0x5a},
    RGBColor{red: 0x18, green: 0x00, blue: 0x18},
    RGBColor{red: 0xfb, green: 0x4e, blue: 0x83},
    RGBColor{red: 0xff, green: 0x07, blue: 0x53},
    RGBColor{red: 0x5a, green: 0x02, blue: 0x1b},
    RGBColor{red: 0x21, green: 0x01, blue: 0x10},
    RGBColor{red: 0xff, green: 0x19, blue: 0x01},
    RGBColor{red: 0x9a, green: 0x35, blue: 0x00},
    RGBColor{red: 0x7a, green: 0x51, blue: 0x01},
    RGBColor{red: 0x3e, green: 0x65, blue: 0x00},

    // 64..128
    RGBColor{red: 0x01, green: 0x38, blue: 0x00},
    RGBColor{red: 0x00, green: 0x54, blue: 0x32},
    RGBColor{red: 0x00, green: 0x53, blue: 0x7f},
    RGBColor{red: 0x00, green: 0x00, blue: 0xfe},
    RGBColor{red: 0x01, green: 0x44, blue: 0x4d},
    RGBColor{red: 0x1a, green: 0x00, blue: 0xd1},
    RGBColor{red: 0x7c, green: 0x7c, blue: 0x7c},
    RGBColor{red: 0x20, green: 0x20, blue: 0x20},
    RGBColor{red: 0xff, green: 0x0a, blue: 0x00},
    RGBColor{red: 0xba, green: 0xfd, blue: 0x00},
    RGBColor{red: 0xac, green: 0xec, blue: 0x00},
    RGBColor{red: 0x56, green: 0xfd, blue: 0x00},
    RGBColor{red: 0x00, green: 0x88, blue: 0x00},
    RGBColor{red: 0x01, green: 0xfc, blue: 0x7b},
    RGBColor{red: 0x00, green: 0xa7, blue: 0xff},
    RGBColor{red: 0x02, green: 0x1a, blue: 0xff},
    RGBColor{red: 0x35, green: 0x00, blue: 0xff},
    RGBColor{red: 0x78, green: 0x00, blue: 0xff},
    RGBColor{red: 0xb4, green: 0x17, blue: 0x7e},
    RGBColor{red: 0x41, green: 0x20, blue: 0x00},
    RGBColor{red: 0xff, green: 0x4a, blue: 0x01},
    RGBColor{red: 0x82, green: 0xe1, blue: 0x00},
    RGBColor{red: 0x66, green: 0xfd, blue: 0x00},
    RGBColor{red: 0x00, green: 0xfe, blue: 0x00},
    RGBColor{red: 0x00, green: 0xfe, blue: 0x00},
    RGBColor{red: 0x45, green: 0xfd, blue: 0x61},
    RGBColor{red: 0x01, green: 0xfb, blue: 0xcb},
    RGBColor{red: 0x50, green: 0x86, blue: 0xff},
    RGBColor{red: 0x27, green: 0x4d, blue: 0xc8},
    RGBColor{red: 0x84, green: 0x7a, blue: 0xed},
    RGBColor{red: 0xd3, green: 0x0c, blue: 0xff},
    RGBColor{red: 0xff, green: 0x06, blue: 0x5a},
    RGBColor{red: 0xff, green: 0x7d, blue: 0x01},
    RGBColor{red: 0xb8, green: 0xb1, blue: 0x00},
    RGBColor{red: 0x8a, green: 0xfd, blue: 0x00},
    RGBColor{red: 0x81, green: 0x5d, blue: 0x00},
    RGBColor{red: 0x3a, green: 0x28, blue: 0x02},
    RGBColor{red: 0x0d, green: 0x4c, blue: 0x05},
    RGBColor{red: 0x00, green: 0x50, blue: 0x37},
    RGBColor{red: 0x13, green: 0x14, blue: 0x29},
    RGBColor{red: 0x10, green: 0x1f, blue: 0x5a},
    RGBColor{red: 0x6a, green: 0x3c, blue: 0x18},
    RGBColor{red: 0xac, green: 0x04, blue: 0x01},
    RGBColor{red: 0xe1, green: 0x51, blue: 0x36},
    RGBColor{red: 0xdc, green: 0x69, blue: 0x00},
    RGBColor{red: 0xfe, green: 0xe1, blue: 0x00},
    RGBColor{red: 0x99, green: 0xe1, blue: 0x01},
    RGBColor{red: 0x60, green: 0xb5, blue: 0x00},
    RGBColor{red: 0x1b, green: 0x1c, blue: 0x31},
    RGBColor{red: 0xdc, green: 0xfd, blue: 0x54},
    RGBColor{red: 0x76, green: 0xfb, blue: 0xb9},
    RGBColor{red: 0x96, green: 0x98, blue: 0xff},
    RGBColor{red: 0x8b, green: 0x62, blue: 0xff},
    RGBColor{red: 0x40, green: 0x40, blue: 0x40},
    RGBColor{red: 0x74, green: 0x74, blue: 0x74},
    RGBColor{red: 0xde, green: 0xfc, blue: 0xfc},
    RGBColor{red: 0xa2, green: 0x04, blue: 0x01},
    RGBColor{red: 0x34, green: 0x01, blue: 0x00},
    RGBColor{red: 0x00, green: 0xd2, blue: 0x01},
    RGBColor{red: 0x00, green: 0x41, blue: 0x01},
    RGBColor{red: 0xb8, green: 0xb1, blue: 0x00},
    RGBColor{red: 0x3c, green: 0x30, blue: 0x00},
    RGBColor{red: 0xb4, green: 0x5d, blue: 0x00},
    RGBColor{red: 0x4c, green: 0x13, blue: 0x00},
];