runmat-plot 0.7.1

GPU-accelerated and static plotting for RunMat with WGPU and Plotters
//! Reference colormap tables, sampled at 64 evenly spaced points and interpolated linearly.
//! turbo, viridis, plasma, inferno and magma are sampled from matplotlib 3.10;
//! parula uses its ten published anchor colors.

/// Four-decimal RGB samples are color data, not approximations of angle constants.
const fn rgb_1e4(channels: [u16; 3]) -> [f32; 3] {
    [
        channels[0] as f32 / 10_000.0,
        channels[1] as f32 / 10_000.0,
        channels[2] as f32 / 10_000.0,
    ]
}

pub(crate) const TURBO: [[f32; 3]; 64] = [
    [0.1900, 0.0718, 0.2322],
    [0.2086, 0.1180, 0.3461],
    [0.2250, 0.1635, 0.4510],
    [0.2392, 0.2083, 0.5469],
    [0.2511, 0.2524, 0.6337],
    [0.2607, 0.2957, 0.7116],
    [0.2682, 0.3382, 0.7805],
    [0.2733, 0.3801, 0.8404],
    [0.2763, 0.4212, 0.8912],
    [0.2770, 0.4615, 0.9331],
    [0.2754, 0.5011, 0.9659],
    [0.2711, 0.5402, 0.9893],
    [0.2586, 0.5796, 0.9988],
    [0.2387, 0.6193, 0.9949],
    [0.2138, 0.6589, 0.9796],
    [0.1862, 0.6977, 0.9550],
    [0.1517, 0.7447, 0.9142],
    [0.1270, 0.7804, 0.8759],
    [0.1074, 0.8138, 0.8348],
    [0.0953, 0.8446, 0.7930],
    [0.0932, 0.8721, 0.7524],
    [0.1034, 0.8960, 0.7150],
    [0.1273, 0.9170, 0.6763],
    [0.1632, 0.9361, 0.6314],
    [0.2088, 0.9530, 0.5820],
    [0.2618, 0.9677, 0.5298],
    [0.3201, 0.9797, 0.4765],
    [0.3813, 0.9891, 0.4239],
    [0.4432, 0.9955, 0.3735],
    [0.5036, 0.9988, 0.3270],
    [0.5603, 0.9987, 0.2862],
    [0.6109, 0.9951, 0.2528],
    [0.6643, 0.9852, 0.2237],
    [0.7055, 0.9726, 0.2103],
    [0.7462, 0.9559, 0.2041],
    [0.7856, 0.9358, 0.2034],
    [0.8233, 0.9125, 0.2066],
    [0.8587, 0.8865, 0.2123],
    [0.8911, 0.8583, 0.2188],
    [0.9200, 0.8281, 0.2246],
    [0.9449, 0.7963, 0.2280],
    [0.9651, 0.7635, 0.2275],
    [0.9800, 0.7300, 0.2216],
    [0.9899, 0.6938, 0.2104],
    [0.9953, 0.6534, 0.1958],
    [0.9967, 0.6098, 0.1784],
    [0.9942, 0.5639, 0.1592],
    [0.9880, 0.5167, 0.1388],
    [0.9755, 0.4574, 0.1130],
    [0.9619, 0.4109, 0.0931],
    [0.9454, 0.3664, 0.0746],
    [0.9262, 0.3247, 0.0584],
    [0.9046, 0.2870, 0.0452],
    [0.8807, 0.2533, 0.0352],
    [0.8538, 0.2217, 0.0268],
    [0.8240, 0.1918, 0.0197],
    [0.7913, 0.1637, 0.0139],
    [0.7556, 0.1373, 0.0094],
    [0.7169, 0.1127, 0.0063],
    [0.6754, 0.0898, 0.0045],
    [0.6308, 0.0687, 0.0040],
    [0.5834, 0.0493, 0.0049],
    [0.5330, 0.0317, 0.0070],
    [0.4796, 0.0158, 0.0106],
];

pub(crate) const VIRIDIS: [[f32; 3]; 64] = [
    [0.2670, 0.0049, 0.3294],
    [0.2726, 0.0256, 0.3531],
    [0.2770, 0.0503, 0.3757],
    [0.2803, 0.0734, 0.3972],
    [0.2823, 0.0950, 0.4173],
    [0.2832, 0.1157, 0.4361],
    [0.2829, 0.1359, 0.4534],
    [0.2814, 0.1558, 0.4692],
    [0.2788, 0.1755, 0.4834],
    [0.2752, 0.1949, 0.4960],
    [0.2706, 0.2141, 0.5071],
    [0.2651, 0.2330, 0.5166],
    [0.2590, 0.2515, 0.5247],
    [0.2522, 0.2698, 0.5316],
    [0.2450, 0.2877, 0.5373],
    [0.2374, 0.3052, 0.5419],
    [0.2278, 0.3266, 0.5465],
    [0.2201, 0.3433, 0.5494],
    [0.2124, 0.3597, 0.5517],
    [0.2049, 0.3757, 0.5535],
    [0.1976, 0.3915, 0.5550],
    [0.1906, 0.4071, 0.5561],
    [0.1839, 0.4224, 0.5569],
    [0.1774, 0.4375, 0.5576],
    [0.1712, 0.4525, 0.5580],
    [0.1651, 0.4674, 0.5581],
    [0.1592, 0.4822, 0.5581],
    [0.1534, 0.4970, 0.5577],
    [0.1476, 0.5117, 0.5570],
    [0.1419, 0.5265, 0.5560],
    [0.1364, 0.5412, 0.5545],
    [0.1312, 0.5559, 0.5525],
    [0.1254, 0.5743, 0.5491],
    [0.1218, 0.5891, 0.5456],
    [0.1197, 0.6038, 0.5414],
    [0.1197, 0.6185, 0.5363],
    [0.1223, 0.6332, 0.5304],
    rgb_1e4([1281, 6477, 5235]),
    [0.1373, 0.6623, 0.5156],
    [0.1501, 0.6766, 0.5066],
    [0.1664, 0.6909, 0.4965],
    [0.1858, 0.7049, 0.4853],
    [0.2080, 0.7187, 0.4729],
    [0.2328, 0.7322, 0.4593],
    [0.2599, 0.7455, 0.4445],
    [0.2889, 0.7584, 0.4284],
    [0.3198, 0.7709, 0.4112],
    rgb_1e4([3524, 7830, 3926]),
    [0.3952, 0.7975, 0.3678],
    [0.4310, 0.8085, 0.3465],
    [0.4681, 0.8189, 0.3240],
    [0.5063, 0.8288, 0.3004],
    [0.5455, 0.8380, 0.2756],
    [0.5857, 0.8467, 0.2499],
    [0.6266, 0.8546, 0.2234],
    [0.6681, 0.8620, 0.1963],
    [0.7099, 0.8688, 0.1693],
    [0.7519, 0.8750, 0.1432],
    [0.7938, 0.8807, 0.1200],
    [0.8353, 0.8860, 0.1026],
    [0.8762, 0.8911, 0.0953],
    [0.9162, 0.8961, 0.1007],
    [0.9553, 0.9011, 0.1181],
    [0.9932, 0.9062, 0.1439],
];

pub(crate) const PLASMA: [[f32; 3]; 64] = [
    [0.0504, 0.0298, 0.5280],
    [0.0964, 0.0252, 0.5471],
    [0.1324, 0.0223, 0.5633],
    [0.1641, 0.0202, 0.5775],
    [0.1934, 0.0184, 0.5903],
    [0.2212, 0.0165, 0.6021],
    [0.2480, 0.0144, 0.6129],
    [0.2742, 0.0121, 0.6227],
    [0.2999, 0.0096, 0.6316],
    [0.3251, 0.0069, 0.6395],
    [0.3502, 0.0044, 0.6463],
    [0.3749, 0.0022, 0.6519],
    [0.3994, 0.0009, 0.6561],
    [0.4237, 0.0006, 0.6590],
    [0.4477, 0.0021, 0.6602],
    [0.4715, 0.0057, 0.6599],
    [0.5007, 0.0141, 0.6571],
    rgb_1e4([5236, 245, 6529]),
    [0.5462, 0.0390, 0.6470],
    [0.5682, 0.0558, 0.6395],
    [0.5897, 0.0729, 0.6304],
    [0.6107, 0.0902, 0.6200],
    [0.6310, 0.1077, 0.6083],
    [0.6507, 0.1253, 0.5956],
    [0.6698, 0.1430, 0.5822],
    [0.6883, 0.1607, 0.5681],
    [0.7062, 0.1784, 0.5537],
    [0.7234, 0.1962, 0.5390],
    [0.7401, 0.2139, 0.5242],
    [0.7563, 0.2316, 0.5095],
    [0.7720, 0.2492, 0.4948],
    [0.7871, 0.2669, 0.4803],
    [0.8055, 0.2891, 0.4624],
    [0.8197, 0.3068, 0.4483],
    [0.8334, 0.3246, 0.4344],
    [0.8468, 0.3426, 0.4206],
    [0.8598, 0.3606, 0.4069],
    [0.8723, 0.3788, 0.3934],
    [0.8844, 0.3971, 0.3799],
    [0.8961, 0.4157, 0.3664],
    [0.9074, 0.4345, 0.3530],
    [0.9181, 0.4536, 0.3395],
    [0.9283, 0.4730, 0.3261],
    [0.9380, 0.4927, 0.3126],
    [0.9471, 0.5127, 0.2990],
    [0.9555, 0.5331, 0.2855],
    [0.9632, 0.5539, 0.2719],
    [0.9702, 0.5750, 0.2583],
    [0.9779, 0.6021, 0.2414],
    [0.9830, 0.6241, 0.2279],
    [0.9873, 0.6466, 0.2146],
    [0.9907, 0.6696, 0.2016],
    [0.9930, 0.6929, 0.1891],
    [0.9943, 0.7167, 0.1772],
    [0.9945, 0.7409, 0.1663],
    [0.9935, 0.7655, 0.1569],
    [0.9912, 0.7905, 0.1494],
    [0.9876, 0.8160, 0.1444],
    [0.9827, 0.8418, 0.1423],
    [0.9763, 0.8680, 0.1434],
    [0.9684, 0.8946, 0.1470],
    [0.9593, 0.9214, 0.1516],
    [0.9492, 0.9484, 0.1522],
    [0.9400, 0.9752, 0.1313],
];

pub(crate) const INFERNO: [[f32; 3]; 64] = [
    [0.0015, 0.0005, 0.0139],
    [0.0060, 0.0047, 0.0386],
    [0.0140, 0.0112, 0.0719],
    [0.0258, 0.0193, 0.1059],
    [0.0423, 0.0281, 0.1411],
    [0.0613, 0.0366, 0.1776],
    [0.0820, 0.0433, 0.2153],
    [0.1046, 0.0470, 0.2534],
    [0.1293, 0.0473, 0.2908],
    [0.1558, 0.0446, 0.3253],
    [0.1834, 0.0403, 0.3550],
    [0.2111, 0.0370, 0.3786],
    [0.2383, 0.0366, 0.3964],
    [0.2648, 0.0396, 0.4093],
    [0.2908, 0.0456, 0.4186],
    [0.3163, 0.0535, 0.4251],
    [0.3478, 0.0646, 0.4302],
    [0.3728, 0.0739, 0.4324],
    [0.3977, 0.0833, 0.4332],
    [0.4225, 0.0925, 0.4327],
    [0.4474, 0.1016, 0.4311],
    [0.4723, 0.1105, 0.4283],
    [0.4973, 0.1194, 0.4245],
    [0.5222, 0.1281, 0.4195],
    [0.5472, 0.1369, 0.4135],
    [0.5721, 0.1458, 0.4064],
    [0.5969, 0.1548, 0.3981],
    [0.6217, 0.1642, 0.3888],
    [0.6463, 0.1739, 0.3784],
    [0.6706, 0.1842, 0.3669],
    [0.6946, 0.1950, 0.3544],
    [0.7183, 0.2066, 0.3409],
    [0.7471, 0.2224, 0.3229],
    [0.7696, 0.2361, 0.3075],
    [0.7913, 0.2509, 0.2914],
    [0.8122, 0.2668, 0.2747],
    [0.8323, 0.2839, 0.2574],
    [0.8514, 0.3023, 0.2396],
    [0.8694, 0.3218, 0.2215],
    [0.8863, 0.3426, 0.2030],
    [0.9020, 0.3645, 0.1841],
    [0.9165, 0.3875, 0.1649],
    [0.9296, 0.4115, 0.1454],
    [0.9415, 0.4364, 0.1254],
    [0.9521, 0.4622, 0.1050],
    [0.9613, 0.4887, 0.0843],
    [0.9692, 0.5159, 0.0635],
    [0.9757, 0.5438, 0.0436],
    [0.9819, 0.5794, 0.0262],
    [0.9853, 0.6084, 0.0242],
    [0.9873, 0.6379, 0.0349],
    [0.9879, 0.6677, 0.0583],
    [0.9871, 0.6979, 0.0877],
    [0.9849, 0.7284, 0.1208],
    [0.9812, 0.7591, 0.1569],
    [0.9761, 0.7900, 0.1960],
    [0.9698, 0.8208, 0.2387],
    [0.9625, 0.8515, 0.2855],
    [0.9550, 0.8816, 0.3375],
    [0.9487, 0.9105, 0.3953],
    [0.9464, 0.9372, 0.4586],
    [0.9517, 0.9606, 0.5242],
    [0.9662, 0.9807, 0.5872],
    [0.9884, 0.9984, 0.6449],
];

pub(crate) const MAGMA: [[f32; 3]; 64] = [
    [0.0015, 0.0005, 0.0139],
    [0.0060, 0.0048, 0.0371],
    [0.0137, 0.0118, 0.0687],
    [0.0248, 0.0207, 0.1007],
    [0.0396, 0.0311, 0.1335],
    [0.0566, 0.0422, 0.1674],
    [0.0743, 0.0520, 0.2027],
    [0.0929, 0.0599, 0.2392],
    [0.1131, 0.0655, 0.2768],
    [0.1351, 0.0684, 0.3150],
    [0.1590, 0.0684, 0.3527],
    [0.1848, 0.0657, 0.3880],
    [0.2117, 0.0620, 0.4186],
    [0.2388, 0.0595, 0.4433],
    [0.2654, 0.0602, 0.4618],
    [0.2914, 0.0646, 0.4755],
    [0.3229, 0.0738, 0.4874],
    [0.3476, 0.0829, 0.4941],
    [0.3721, 0.0928, 0.4991],
    [0.3965, 0.1029, 0.5027],
    [0.4208, 0.1129, 0.5052],
    [0.4452, 0.1227, 0.5069],
    [0.4696, 0.1322, 0.5078],
    [0.4943, 0.1415, 0.5080],
    [0.5190, 0.1504, 0.5074],
    [0.5440, 0.1590, 0.5062],
    [0.5692, 0.1675, 0.5041],
    [0.5945, 0.1757, 0.5012],
    [0.6200, 0.1838, 0.4975],
    [0.6456, 0.1920, 0.4929],
    [0.6713, 0.2001, 0.4874],
    [0.6971, 0.2085, 0.4808],
    [0.7292, 0.2194, 0.4713],
    [0.7547, 0.2288, 0.4625],
    [0.7800, 0.2389, 0.4528],
    [0.8048, 0.2499, 0.4421],
    [0.8289, 0.2622, 0.4306],
    [0.8521, 0.2761, 0.4186],
    [0.8742, 0.2919, 0.4062],
    [0.8947, 0.3098, 0.3940],
    [0.9134, 0.3301, 0.3826],
    [0.9298, 0.3527, 0.3727],
    [0.9440, 0.3776, 0.3651],
    [0.9558, 0.4044, 0.3606],
    [0.9655, 0.4325, 0.3595],
    [0.9734, 0.4615, 0.3620],
    [0.9796, 0.4910, 0.3678],
    [0.9846, 0.5207, 0.3767],
    [0.9894, 0.5579, 0.3917],
    [0.9922, 0.5875, 0.4063],
    [0.9943, 0.6170, 0.4229],
    [0.9958, 0.6463, 0.4414],
    [0.9968, 0.6755, 0.4613],
    [0.9973, 0.7046, 0.4826],
    [0.9973, 0.7335, 0.5052],
    [0.9970, 0.7624, 0.5288],
    [0.9964, 0.7912, 0.5535],
    [0.9954, 0.8199, 0.5791],
    [0.9942, 0.8485, 0.6057],
    [0.9928, 0.8772, 0.6331],
    [0.9913, 0.9058, 0.6613],
    [0.9898, 0.9343, 0.6902],
    [0.9884, 0.9629, 0.7196],
    [0.9871, 0.9914, 0.7495],
];

pub(crate) const PARULA: [[f32; 3]; 10] = [
    [0.2422, 0.1504, 0.6603],
    [0.2803, 0.2782, 0.9221],
    [0.2440, 0.4358, 0.9988],
    [0.1540, 0.5902, 0.9218],
    [0.0297, 0.7082, 0.8163],
    [0.1938, 0.7758, 0.6251],
    [0.5044, 0.7993, 0.3480],
    [0.8634, 0.7406, 0.1596],
    [0.9892, 0.8136, 0.1885],
    [0.9763, 0.9831, 0.0538],
];

/// Linear interpolation through an evenly spaced table, `t` in [0, 1].
pub(crate) fn sample(table: &[[f32; 3]], t: f32) -> [f32; 3] {
    let last = table.len() - 1;
    let x = if t.is_finite() {
        t.clamp(0.0, 1.0)
    } else {
        0.0
    } * last as f32;
    let i = (x.floor() as usize).min(last.saturating_sub(1));
    let f = x - i as f32;
    let (a, b) = (table[i], table[(i + 1).min(last)]);
    [
        a[0] + (b[0] - a[0]) * f,
        a[1] + (b[1] - a[1]) * f,
        a[2] + (b[2] - a[2]) * f,
    ]
}

#[cfg(test)]
mod tests {
    use super::{PLASMA, VIRIDIS};

    #[test]
    fn fixed_point_samples_preserve_original_channel_bits() {
        for (actual, original) in [
            (VIRIDIS[37], ["0.1281", "0.6477", "0.5235"]),
            (VIRIDIS[47], ["0.3524", "0.7830", "0.3926"]),
            (PLASMA[17], ["0.5236", "0.0245", "0.6529"]),
        ] {
            assert_eq!(
                actual.map(f32::to_bits),
                original.map(|channel| channel.parse::<f32>().unwrap().to_bits())
            );
        }
    }
}