#[rustfmt::skip]
pub const DEFAULT_PALETTE: [[u8; 3]; 256] = [
[255, 255, 255], [0, 0, 255], [0, 255, 0], [255, 0, 0], [255, 255, 0], [255, 0, 255],
[255, 127, 0], [0, 255, 255], [64, 64, 64], [192, 192, 192], [254, 0, 96], [160, 224, 0],
[0, 254, 160], [128, 0, 160], [176, 176, 176], [0, 240, 240], [240, 240, 240], [0, 0, 240],
[0, 240, 0], [240, 0, 0], [240, 240, 0], [240, 0, 240], [240, 122, 0], [0, 240, 240],
[240, 240, 240], [0, 0, 240], [0, 240, 0], [240, 0, 0], [240, 240, 0], [240, 0, 240],
[240, 122, 0], [0, 225, 225], [225, 225, 225], [0, 0, 225], [0, 225, 0], [225, 0, 0],
[225, 225, 0], [225, 0, 225], [225, 117, 0], [0, 225, 225], [225, 225, 225], [0, 0, 225],
[0, 225, 0], [225, 0, 0], [225, 225, 0], [225, 0, 225], [225, 117, 0], [0, 210, 210],
[210, 210, 210], [0, 0, 210], [0, 210, 0], [210, 0, 0], [210, 210, 0], [210, 0, 210],
[210, 112, 0], [0, 210, 210], [210, 210, 210], [0, 0, 210], [0, 210, 0], [210, 0, 0],
[210, 210, 0], [210, 0, 210], [210, 112, 0], [0, 195, 195], [195, 195, 195], [0, 0, 195],
[0, 195, 0], [195, 0, 0], [195, 195, 0], [195, 0, 195], [195, 107, 0], [0, 195, 195],
[195, 195, 195], [0, 0, 195], [0, 195, 0], [195, 0, 0], [195, 195, 0], [195, 0, 195],
[195, 107, 0], [0, 180, 180], [180, 180, 180], [0, 0, 180], [0, 180, 0], [180, 0, 0],
[180, 180, 0], [180, 0, 180], [180, 102, 0], [0, 180, 180], [180, 180, 180], [0, 0, 180],
[0, 180, 0], [180, 0, 0], [180, 180, 0], [180, 0, 180], [180, 102, 0], [0, 165, 165],
[165, 165, 165], [0, 0, 165], [0, 165, 0], [165, 0, 0], [165, 165, 0], [165, 0, 165],
[165, 97, 0], [0, 165, 165], [165, 165, 165], [0, 0, 165], [0, 165, 0], [165, 0, 0],
[165, 165, 0], [165, 0, 165], [165, 97, 0], [0, 150, 150], [150, 150, 150], [0, 0, 150],
[0, 150, 0], [150, 0, 0], [150, 150, 0], [150, 0, 150], [150, 92, 0], [0, 150, 150],
[150, 150, 150], [0, 0, 150], [0, 150, 0], [150, 0, 0], [150, 150, 0], [150, 0, 150],
[150, 92, 0], [0, 135, 135], [135, 135, 135], [0, 0, 135], [0, 135, 0], [135, 0, 0],
[135, 135, 0], [135, 0, 135], [135, 87, 0], [0, 135, 135], [135, 135, 135], [0, 0, 135],
[0, 135, 0], [135, 0, 0], [135, 135, 0], [135, 0, 135], [135, 87, 0], [0, 120, 120],
[120, 120, 120], [0, 0, 120], [0, 120, 0], [120, 0, 0], [120, 120, 0], [120, 0, 120],
[120, 82, 0], [0, 120, 120], [120, 120, 120], [0, 0, 120], [0, 120, 0], [120, 0, 0],
[120, 120, 0], [120, 0, 120], [120, 82, 0], [0, 105, 105], [105, 105, 105], [0, 0, 105],
[0, 105, 0], [105, 0, 0], [105, 105, 0], [105, 0, 105], [105, 77, 0], [0, 105, 105],
[105, 105, 105], [0, 0, 105], [0, 105, 0], [105, 0, 0], [105, 105, 0], [105, 0, 105],
[105, 77, 0], [0, 90, 90], [90, 90, 90], [0, 0, 90], [0, 90, 0], [90, 0, 0],
[90, 90, 0], [90, 0, 90], [90, 72, 0], [0, 90, 90], [90, 90, 90], [0, 0, 90],
[0, 90, 0], [90, 0, 0], [90, 90, 0], [90, 0, 90], [90, 72, 0], [0, 75, 75],
[75, 75, 75], [0, 0, 75], [0, 75, 0], [75, 0, 0], [75, 75, 0], [75, 0, 75],
[75, 67, 0], [0, 75, 75], [75, 75, 75], [0, 0, 75], [0, 75, 0], [75, 0, 0],
[75, 75, 0], [75, 0, 75], [75, 67, 0], [0, 60, 60], [60, 60, 60], [0, 0, 60],
[0, 60, 0], [60, 0, 0], [60, 60, 0], [60, 0, 60], [60, 62, 0], [0, 60, 60],
[60, 60, 60], [0, 0, 60], [0, 60, 0], [60, 0, 0], [60, 60, 0], [60, 0, 60],
[60, 62, 0], [0, 45, 45], [45, 45, 45], [0, 0, 45], [0, 45, 0], [45, 0, 0],
[45, 45, 0], [45, 0, 45], [45, 57, 0], [0, 45, 45], [45, 45, 45], [0, 0, 45],
[0, 45, 0], [45, 0, 0], [45, 45, 0], [45, 0, 45], [45, 57, 0], [0, 30, 30],
[30, 30, 30], [0, 0, 30], [0, 30, 0], [30, 0, 0], [30, 30, 0], [30, 0, 30],
[30, 52, 0], [0, 30, 30], [30, 30, 30], [0, 0, 30], [0, 30, 0], [30, 0, 0],
[30, 30, 0], [30, 0, 30], [192, 192, 192], [28, 0, 100],
];
pub fn exact_index(table: &[[u8; 3]; 256], rgb: [u8; 3]) -> Option<u8> {
table
.iter()
.position(|c| *c == rgb)
.and_then(|i| u8::try_from(i).ok())
}
pub fn nearest_index(table: &[[u8; 3]; 256], rgb: [u8; 3]) -> u8 {
let mut best = (u32::MAX, 0u8);
for (i, c) in (0..=u8::MAX).zip(table.iter()) {
let dist: u32 = c
.iter()
.zip(rgb)
.map(|(&a, b)| u32::from(a.abs_diff(b)).pow(2))
.sum();
if dist < best.0 {
best = (dist, i);
if dist == 0 {
break;
}
}
}
best.1
}
pub(crate) fn lookup(table: &[[u8; 3]; 256], index: u32) -> Option<[u8; 3]> {
usize::try_from(index)
.ok()
.and_then(|i| table.get(i))
.copied()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn known_entries() {
assert_eq!(DEFAULT_PALETTE[0], [255, 255, 255]);
assert_eq!(DEFAULT_PALETTE[3], [255, 0, 0]);
assert_eq!(DEFAULT_PALETTE[255], [28, 0, 100]);
assert_eq!(lookup(&DEFAULT_PALETTE, 256), None);
}
#[test]
fn exact_and_nearest_indexes() {
assert_eq!(exact_index(&DEFAULT_PALETTE, [255, 255, 255]), Some(0));
assert_eq!(exact_index(&DEFAULT_PALETTE, [0, 0, 240]), Some(17));
assert_eq!(exact_index(&DEFAULT_PALETTE, [1, 2, 3]), None);
assert_eq!(nearest_index(&DEFAULT_PALETTE, [255, 0, 0]), 3);
assert_eq!(nearest_index(&DEFAULT_PALETTE, [250, 12, 18]), 3);
let mut table = [[0u8; 3]; 256];
table[7] = [10, 10, 10];
table[9] = [10, 10, 10];
assert_eq!(nearest_index(&table, [9, 10, 11]), 7);
assert_eq!(exact_index(&table, [10, 10, 10]), Some(7));
assert_eq!(nearest_index(&table, [0, 0, 0]), 0);
}
}