src/render/ansi/sgr/palette.rs

//! Color palette used for source tagging.
//!
//! We stick to the 256-color cube so every color is legible on both dark and
//! light terminals. Colors are chosen from a pre-picked set with reasonable
//! contrast. See `hash::source_color` for the deterministic mapping from
//! origin path to palette index.

/// 256-color palette index.
pub type Color = u8;

pub const PALETTE: &[Color] = &[
    39,  // light blue
    208, // orange
    70,  // green
    141, // violet
    203, // coral
    81,  // cyan
    220, // yellow
    135, // magenta
    112, // lime
    174, // salmon
    45,  // teal
    215, // peach
    150, // pale green
    177, // orchid
    186, // khaki
    147, // sky
];

pub fn len() -> usize {
    PALETTE.len()
}

pub fn nth(i: usize) -> Color {
    PALETTE[i % PALETTE.len()]
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn palette_is_non_empty_and_bounded() {
        assert!(!PALETTE.is_empty());
        for c in PALETTE {
            assert!(*c < 232, "palette should stay out of greyscale range");
        }
    }

    #[test]
    fn nth_wraps() {
        assert_eq!(nth(0), nth(PALETTE.len()));
    }
}