plotpx 0.1.7

Pixel-focused plotting engine that renders magnitude grids, heatmaps, and spectra to RGBA buffers
Documentation
use std::vec::Vec;

pub type Rgba = [u8; 4];

pub const WHITE: Rgba = [255, 255, 255, 255];
pub const BLACK: Rgba = [0, 0, 0, 255];
pub const TRANSPARENT: Rgba = [0, 0, 0, 0];

pub fn get_color_count(colors: &[u8]) -> usize {
    colors.len() / 4
}

pub fn interpolate_color(colors: &mut [u8], c1: &Rgba, c2: &Rgba, steps: usize) {
    for i in 0..steps {
        let ratio = i as f32 / (steps - 1) as f32;
        let color = [
            (c1[0] as f32 + ratio * (c2[0] as f32 - c1[0] as f32)) as u8,
            (c1[1] as f32 + ratio * (c2[1] as f32 - c1[1] as f32)) as u8,
            (c1[2] as f32 + ratio * (c2[2] as f32 - c1[2] as f32)) as u8,
            (c1[3] as f32 + ratio * (c2[3] as f32 - c1[3] as f32)) as u8,
        ];
        let ix = i * 4;
        colors[ix] = color[0];
        colors[ix + 1] = color[1];
        colors[ix + 2] = color[2];
        colors[ix + 3] = color[3];
    }
}

pub fn make_color_scheme(key_colors: &[Rgba], steps_between_keys: usize) -> Vec<u8> {
    if key_colors.len() < 2 {
        return Vec::new();
    }

    let num_segments = key_colors.len() - 1;
    let bytes_per_segment = steps_between_keys * 4;
    let total_bytes = num_segments * bytes_per_segment;

    let mut data = vec![0u8; total_bytes];

    for i in 0..num_segments {
        let start = &key_colors[i];
        let end = &key_colors[i + 1];

        let segment_start = i * bytes_per_segment;
        let segment_end = segment_start + bytes_per_segment;
        interpolate_color(
            &mut data[segment_start..segment_end],
            start,
            end,
            steps_between_keys,
        );
    }

    data
}

pub const RAINBOW_KEY_COLORS: &[Rgba] = &[
    [148, 0, 211, 255], // Violet
    [75, 0, 130, 255],  // Indigo
    [0, 0, 255, 255],   // Blue
    [0, 255, 0, 255],   // Green
    [255, 255, 0, 255], // Yellow
    [255, 127, 0, 255], // Orange
    [255, 0, 0, 255],   // Red
];

pub const VIRIDIS_KEY_COLORS: &[Rgba] = &[
    [68, 1, 84, 255],    // Dark Purple
    [72, 35, 116, 255],  // Purple
    [64, 67, 135, 255],  // Blue
    [52, 94, 141, 255],  // Blue-Green
    [33, 145, 140, 255], // Green
    [94, 201, 98, 255],  // Yellow-Green
    [253, 231, 37, 255], // Yellow
];

pub const JET_KEY_COLORS: &[Rgba] = &[
    [0, 0, 131, 255],   // Dark Blue
    [0, 60, 170, 255],  // Blue
    [5, 255, 255, 255], // Cyan
    [255, 255, 0, 255], // Yellow
    [250, 0, 0, 255],   // Red
    [128, 0, 0, 255],   // Dark Red
];

pub const SOFT_KEY_COLORS: &[Rgba] = &[
    [30, 30, 150, 255],   // Dark Blue
    [50, 50, 200, 255],   // Blue
    [50, 120, 220, 255],  // Blue-Grey
    [180, 180, 180, 255], // Light Grey
    [220, 140, 80, 255],  // Brownish Orange
    [200, 80, 80, 255],   // Dark Red
    [150, 50, 50, 255],   // Very Dark Red
];

pub const INFERNO_KEY_COLORS: &[Rgba] = &[
    [0, 0, 3, 255],       // Black
    [40, 11, 84, 255],    // Dark Purple
    [101, 21, 110, 255],  // Purple
    [159, 42, 99, 255],   // Red-Purple
    [212, 72, 66, 255],   // Red-Orange
    [245, 125, 21, 255],  // Orange
    [250, 193, 39, 255],  // Yellow-Orange
    [252, 254, 164, 255], // Light Yellow
];

pub const TURBO_KEY_COLORS: &[Rgba] = &[
    [48, 18, 59, 255],   // Dark Purple
    [49, 54, 149, 255],  // Blue
    [33, 113, 181, 255], // Blue-Green
    [94, 201, 98, 255],  // Green
    [253, 231, 37, 255], // Yellow
    [224, 163, 0, 255],  // Orange
    [136, 0, 0, 255],    // Dark Red
];

pub const PASTEL_KEY_COLORS: &[Rgba] = &[
    [151, 136, 157, 255], // Pastel Purple
    [152, 154, 202, 255], // Pastel Blue
    [144, 184, 218, 255], // Pastel Blue-Green
    [174, 228, 176, 255], // Pastel Green
    [254, 243, 146, 255], // Pastel Yellow
    [239, 209, 128, 255], // Pastel Orange
    [195, 127, 127, 255], // Pastel Red
];

pub const TEMPERATURE_KEY_COLORS: &[Rgba] = &[
    [48, 18, 59, 255],   // Dark Purple
    [49, 54, 149, 255],  // Blue
    [253, 231, 37, 255], // Yellow
    [224, 163, 0, 255],  // Orange
    [136, 0, 0, 255],    // Dark Red
];

pub fn default_color_scheme_data() -> Vec<u8> {
    make_color_scheme(TEMPERATURE_KEY_COLORS, 128)
}