viewport-lib 0.13.3

3D viewport rendering library
Documentation
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/// Built-in colormap LUT data (256 RGBA samples each).
///
/// Each function returns a `[[u8; 4]; 256]` array suitable for uploading to a
/// 256×1 GPU texture.  All values are in linear (non-sRGB) space to match the
/// `Rgba8Unorm` texture format used by the LUT sampler.

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

#[inline]
fn clamp01(x: f32) -> f32 {
    x.clamp(0.0, 1.0)
}

#[inline]
fn to_u8(x: f32) -> u8 {
    (clamp01(x) * 255.0 + 0.5) as u8
}

// ---------------------------------------------------------------------------
// Greyscale: linear ramp black -> white
// ---------------------------------------------------------------------------

/// Linear greyscale ramp from black (index 0) to white (index 255).
pub fn greyscale_rgba() -> [[u8; 4]; 256] {
    let mut lut = [[0u8; 4]; 256];
    for i in 0..256 {
        let v = i as u8;
        lut[i] = [v, v, v, 255];
    }
    lut
}

// ---------------------------------------------------------------------------
// Viridis: Smith 2015 polynomial approximation
// ---------------------------------------------------------------------------

/// Viridis colormap : perceptually uniform, colorblind-friendly.
///
/// Uses the Smith 2015 polynomial approximation.
pub fn viridis_rgba() -> [[u8; 4]; 256] {
    let mut lut = [[0u8; 4]; 256];
    for i in 0..256 {
        let t = i as f32 / 255.0;
        // Degree-6 polynomial approximation (Zucker/Smith coefficients).
        // Range: dark purple (t=0) -> blue -> teal -> green -> yellow (t=1).
        let r = 0.277_727_33
            + t * (0.105_093_04
                + t * (-0.330_861_83
                    + t * (-4.634_230_5
                        + t * (6.228_269_9
                            + t * (4.776_385_0 - t * 5.435_455_9)))));
        let g = 0.005_407_345
            + t * (1.404_613_5
                + t * (0.214_847_56
                    + t * (-5.799_101_0
                        + t * (14.179_933
                            + t * (-13.745_145 + t * 4.645_852_6)))));
        let b = 0.334_099_81
            + t * (1.384_590_2
                + t * (0.095_095_16
                    + t * (-19.332_441
                        + t * (56.690_552
                            + t * (-65.353_033 + t * 26.312_435)))));
        lut[i] = [to_u8(r), to_u8(g), to_u8(b), 255];
    }
    lut
}

// ---------------------------------------------------------------------------
// Plasma: polynomial approximation
// ---------------------------------------------------------------------------

/// Plasma colormap : perceptually uniform, colorblind-friendly.
///
/// Uses a polynomial approximation of the Matplotlib plasma colormap.
pub fn plasma_rgba() -> [[u8; 4]; 256] {
    let mut lut = [[0u8; 4]; 256];
    for i in 0..256 {
        let t = i as f32 / 255.0;
        // Degree-6 polynomial approximation (Zucker/Kall coefficients).
        // Range: dark blue (t=0) -> pink (t=0.5) -> orange -> yellow (t=1).
        let r = 0.058_732_344
            + t * (2.176_514_6
                + t * (-2.689_460_5
                    + t * (6.130_348_3
                        + t * (-11.107_436
                            + t * (10.023_066 - t * 3.658_713_8)))));
        let g = 0.023_336_709
            + t * (0.238_383_42
                + t * (-7.455_851
                    + t * (42.346_188
                        + t * (-82.666_31
                            + t * (71.413_62 - t * 22.931_534)))));
        let b = 0.543_340_18
            + t * (0.753_960_46
                + t * (3.110_800
                    + t * (-28.518_854
                        + t * (60.139_847
                            + t * (-54.072_186 + t * 18.191_908)))));
        lut[i] = [to_u8(r), to_u8(g), to_u8(b), 255];
    }
    lut
}

// ---------------------------------------------------------------------------
// Coolwarm: diverging blue -> white -> red (cubic Hermite)
// ---------------------------------------------------------------------------

/// Diverging coolwarm colormap: blue at t=0, white at t=0.5, red at t=1.
pub fn coolwarm_rgba() -> [[u8; 4]; 256] {
    let mut lut = [[0u8; 4]; 256];

    // Control points in linear [0,1] space.
    let cold = [59.0 / 255.0_f32, 76.0 / 255.0, 192.0 / 255.0]; // blue
    let mid = [220.0 / 255.0_f32, 220.0 / 255.0, 220.0 / 255.0]; // near-white
    let warm = [180.0 / 255.0_f32, 4.0 / 255.0, 38.0 / 255.0]; // red

    for i in 0..256 {
        let t = i as f32 / 255.0;
        // Piecewise smooth cubic Hermite blend across two halves.
        let (r, g, b) = if t <= 0.5 {
            let s = t * 2.0; // [0,1] in the cold half
            let h = s * s * (3.0 - 2.0 * s); // smoothstep
            (
                cold[0] + h * (mid[0] - cold[0]),
                cold[1] + h * (mid[1] - cold[1]),
                cold[2] + h * (mid[2] - cold[2]),
            )
        } else {
            let s = (t - 0.5) * 2.0; // [0,1] in the warm half
            let h = s * s * (3.0 - 2.0 * s);
            (
                mid[0] + h * (warm[0] - mid[0]),
                mid[1] + h * (warm[1] - mid[1]),
                mid[2] + h * (warm[2] - mid[2]),
            )
        };
        lut[i] = [to_u8(r), to_u8(g), to_u8(b), 255];
    }
    lut
}

// ---------------------------------------------------------------------------
// Rainbow: HSV hue sweep 240° (blue) -> 0° (red) at full saturation/value
// ---------------------------------------------------------------------------

/// Rainbow colormap: HSV hue sweep from 240° (blue) at t=0 to 0° (red) at t=1.
pub fn rainbow_rgba() -> [[u8; 4]; 256] {
    let mut lut = [[0u8; 4]; 256];
    for i in 0..256 {
        let t = i as f32 / 255.0;
        // Hue sweeps from 240° down to 0° as t goes from 0 to 1.
        let hue = 240.0 * (1.0 - t); // degrees
        let (r, g, b) = hsv_to_rgb(hue, 1.0, 1.0);
        lut[i] = [to_u8(r), to_u8(g), to_u8(b), 255];
    }
    lut
}

/// Convert HSV (hue in degrees [0,360), saturation [0,1], value [0,1]) to RGB.
fn hsv_to_rgb(h: f32, s: f32, v: f32) -> (f32, f32, f32) {
    if s == 0.0 {
        return (v, v, v);
    }
    let h = h % 360.0;
    let sector = (h / 60.0).floor() as i32;
    let frac = h / 60.0 - sector as f32;
    let p = v * (1.0 - s);
    let q = v * (1.0 - s * frac);
    let t = v * (1.0 - s * (1.0 - frac));
    match sector {
        0 => (v, t, p),
        1 => (q, v, p),
        2 => (p, v, t),
        3 => (p, q, v),
        4 => (t, p, v),
        _ => (v, p, q),
    }
}

// ---------------------------------------------------------------------------
// Magma: exact 256-sample LUT sampled from matplotlib
// ---------------------------------------------------------------------------

/// Magma colormap : perceptually uniform. Black/purple -> orange -> near-white.
pub fn magma_rgba() -> [[u8; 4]; 256] {
    [
        [0,0,4,255], [1,0,5,255], [1,1,6,255], [1,1,8,255], [2,1,9,255], [2,2,11,255], [2,2,13,255], [3,3,15,255],
        [3,3,18,255], [4,4,20,255], [5,4,22,255], [6,5,24,255], [6,5,26,255], [7,6,28,255], [8,7,30,255], [9,7,32,255],
        [10,8,34,255], [11,9,36,255], [12,9,38,255], [13,10,41,255], [14,11,43,255], [16,11,45,255], [17,12,47,255], [18,13,49,255],
        [19,13,52,255], [20,14,54,255], [21,14,56,255], [22,15,59,255], [24,15,61,255], [25,16,63,255], [26,16,66,255], [28,16,68,255],
        [29,17,71,255], [30,17,73,255], [32,17,75,255], [33,17,78,255], [34,17,80,255], [36,18,83,255], [37,18,85,255], [39,18,88,255],
        [41,17,90,255], [42,17,92,255], [44,17,95,255], [45,17,97,255], [47,17,99,255], [49,17,101,255], [51,16,103,255], [52,16,105,255],
        [54,16,107,255], [56,16,108,255], [57,15,110,255], [59,15,112,255], [61,15,113,255], [63,15,114,255], [64,15,116,255], [66,15,117,255],
        [68,15,118,255], [69,16,119,255], [71,16,120,255], [73,16,120,255], [74,16,121,255], [76,17,122,255], [78,17,123,255], [79,18,123,255],
        [81,18,124,255], [82,19,124,255], [84,19,125,255], [86,20,125,255], [87,21,126,255], [89,21,126,255], [90,22,126,255], [92,22,127,255],
        [93,23,127,255], [95,24,127,255], [96,24,128,255], [98,25,128,255], [100,26,128,255], [101,26,128,255], [103,27,128,255], [104,28,129,255],
        [106,28,129,255], [107,29,129,255], [109,29,129,255], [110,30,129,255], [112,31,129,255], [114,31,129,255], [115,32,129,255], [117,33,129,255],
        [118,33,129,255], [120,34,129,255], [121,34,130,255], [123,35,130,255], [124,35,130,255], [126,36,130,255], [128,37,130,255], [129,37,129,255],
        [131,38,129,255], [132,38,129,255], [134,39,129,255], [136,39,129,255], [137,40,129,255], [139,41,129,255], [140,41,129,255], [142,42,129,255],
        [144,42,129,255], [145,43,129,255], [147,43,128,255], [148,44,128,255], [150,44,128,255], [152,45,128,255], [153,45,128,255], [155,46,127,255],
        [156,46,127,255], [158,47,127,255], [160,47,127,255], [161,48,126,255], [163,48,126,255], [165,49,126,255], [166,49,125,255], [168,50,125,255],
        [170,51,125,255], [171,51,124,255], [173,52,124,255], [174,52,123,255], [176,53,123,255], [178,53,123,255], [179,54,122,255], [181,54,122,255],
        [183,55,121,255], [184,55,121,255], [186,56,120,255], [188,57,120,255], [189,57,119,255], [191,58,119,255], [192,58,118,255], [194,59,117,255],
        [196,60,117,255], [197,60,116,255], [199,61,115,255], [200,62,115,255], [202,62,114,255], [204,63,113,255], [205,64,113,255], [207,64,112,255],
        [208,65,111,255], [210,66,111,255], [211,67,110,255], [213,68,109,255], [214,69,108,255], [216,69,108,255], [217,70,107,255], [219,71,106,255],
        [220,72,105,255], [222,73,104,255], [223,74,104,255], [224,76,103,255], [226,77,102,255], [227,78,101,255], [228,79,100,255], [229,80,100,255],
        [231,82,99,255], [232,83,98,255], [233,84,98,255], [234,86,97,255], [235,87,96,255], [236,88,96,255], [237,90,95,255], [238,91,94,255],
        [239,93,94,255], [240,95,94,255], [241,96,93,255], [242,98,93,255], [242,100,92,255], [243,101,92,255], [244,103,92,255], [244,105,92,255],
        [245,107,92,255], [246,108,92,255], [246,110,92,255], [247,112,92,255], [247,114,92,255], [248,116,92,255], [248,118,92,255], [249,120,93,255],
        [249,121,93,255], [249,123,93,255], [250,125,94,255], [250,127,94,255], [250,129,95,255], [251,131,95,255], [251,133,96,255], [251,135,97,255],
        [252,137,97,255], [252,138,98,255], [252,140,99,255], [252,142,100,255], [252,144,101,255], [253,146,102,255], [253,148,103,255], [253,150,104,255],
        [253,152,105,255], [253,154,106,255], [253,155,107,255], [254,157,108,255], [254,159,109,255], [254,161,110,255], [254,163,111,255], [254,165,113,255],
        [254,167,114,255], [254,169,115,255], [254,170,116,255], [254,172,118,255], [254,174,119,255], [254,176,120,255], [254,178,122,255], [254,180,123,255],
        [254,182,124,255], [254,183,126,255], [254,185,127,255], [254,187,129,255], [254,189,130,255], [254,191,132,255], [254,193,133,255], [254,194,135,255],
        [254,196,136,255], [254,198,138,255], [254,200,140,255], [254,202,141,255], [254,204,143,255], [254,205,144,255], [254,207,146,255], [254,209,148,255],
        [254,211,149,255], [254,213,151,255], [254,215,153,255], [254,216,154,255], [253,218,156,255], [253,220,158,255], [253,222,160,255], [253,224,161,255],
        [253,226,163,255], [253,227,165,255], [253,229,167,255], [253,231,169,255], [253,233,170,255], [253,235,172,255], [252,236,174,255], [252,238,176,255],
        [252,240,178,255], [252,242,180,255], [252,244,182,255], [252,246,184,255], [252,247,185,255], [252,249,187,255], [252,251,189,255], [252,253,191,255],
    ]
}

// ---------------------------------------------------------------------------
// Inferno: exact 256-sample LUT sampled from matplotlib
// ---------------------------------------------------------------------------

/// Inferno colormap : perceptually uniform. Black -> deep red -> orange -> light yellow.
pub fn inferno_rgba() -> [[u8; 4]; 256] {
    [
        [0,0,4,255], [1,0,5,255], [1,1,6,255], [1,1,8,255], [2,1,10,255], [2,2,12,255], [2,2,14,255], [3,2,16,255],
        [4,3,18,255], [4,3,20,255], [5,4,23,255], [6,4,25,255], [7,5,27,255], [8,5,29,255], [9,6,31,255], [10,7,34,255],
        [11,7,36,255], [12,8,38,255], [13,8,41,255], [14,9,43,255], [16,9,45,255], [17,10,48,255], [18,10,50,255], [20,11,52,255],
        [21,11,55,255], [22,11,57,255], [24,12,60,255], [25,12,62,255], [27,12,65,255], [28,12,67,255], [30,12,69,255], [31,12,72,255],
        [33,12,74,255], [35,12,76,255], [36,12,79,255], [38,12,81,255], [40,11,83,255], [41,11,85,255], [43,11,87,255], [45,11,89,255],
        [47,10,91,255], [49,10,92,255], [50,10,94,255], [52,10,95,255], [54,9,97,255], [56,9,98,255], [57,9,99,255], [59,9,100,255],
        [61,9,101,255], [62,9,102,255], [64,10,103,255], [66,10,104,255], [68,10,104,255], [69,10,105,255], [71,11,106,255], [73,11,106,255],
        [74,12,107,255], [76,12,107,255], [77,13,108,255], [79,13,108,255], [81,14,108,255], [82,14,109,255], [84,15,109,255], [85,15,109,255],
        [87,16,110,255], [89,16,110,255], [90,17,110,255], [92,18,110,255], [93,18,110,255], [95,19,110,255], [97,19,110,255], [98,20,110,255],
        [100,21,110,255], [101,21,110,255], [103,22,110,255], [105,22,110,255], [106,23,110,255], [108,24,110,255], [109,24,110,255], [111,25,110,255],
        [113,25,110,255], [114,26,110,255], [116,26,110,255], [117,27,110,255], [119,28,109,255], [120,28,109,255], [122,29,109,255], [124,29,109,255],
        [125,30,109,255], [127,30,108,255], [128,31,108,255], [130,32,108,255], [132,32,107,255], [133,33,107,255], [135,33,107,255], [136,34,106,255],
        [138,34,106,255], [140,35,105,255], [141,35,105,255], [143,36,105,255], [144,37,104,255], [146,37,104,255], [147,38,103,255], [149,38,103,255],
        [151,39,102,255], [152,39,102,255], [154,40,101,255], [155,41,100,255], [157,41,100,255], [159,42,99,255], [160,42,99,255], [162,43,98,255],
        [163,44,97,255], [165,44,96,255], [166,45,96,255], [168,46,95,255], [169,46,94,255], [171,47,94,255], [173,48,93,255], [174,48,92,255],
        [176,49,91,255], [177,50,90,255], [179,50,90,255], [180,51,89,255], [182,52,88,255], [183,53,87,255], [185,53,86,255], [186,54,85,255],
        [188,55,84,255], [189,56,83,255], [191,57,82,255], [192,58,81,255], [193,58,80,255], [195,59,79,255], [196,60,78,255], [198,61,77,255],
        [199,62,76,255], [200,63,75,255], [202,64,74,255], [203,65,73,255], [204,66,72,255], [206,67,71,255], [207,68,70,255], [208,69,69,255],
        [210,70,68,255], [211,71,67,255], [212,72,66,255], [213,74,65,255], [215,75,63,255], [216,76,62,255], [217,77,61,255], [218,78,60,255],
        [219,80,59,255], [221,81,58,255], [222,82,56,255], [223,83,55,255], [224,85,54,255], [225,86,53,255], [226,87,52,255], [227,89,51,255],
        [228,90,49,255], [229,92,48,255], [230,93,47,255], [231,94,46,255], [232,96,45,255], [233,97,43,255], [234,99,42,255], [235,100,41,255],
        [235,102,40,255], [236,103,38,255], [237,105,37,255], [238,106,36,255], [239,108,35,255], [239,110,33,255], [240,111,32,255], [241,113,31,255],
        [241,115,29,255], [242,116,28,255], [243,118,27,255], [243,120,25,255], [244,121,24,255], [245,123,23,255], [245,125,21,255], [246,126,20,255],
        [246,128,19,255], [247,130,18,255], [247,132,16,255], [248,133,15,255], [248,135,14,255], [248,137,12,255], [249,139,11,255], [249,140,10,255],
        [249,142,9,255], [250,144,8,255], [250,146,7,255], [250,148,7,255], [251,150,6,255], [251,151,6,255], [251,153,6,255], [251,155,6,255],
        [251,157,7,255], [252,159,7,255], [252,161,8,255], [252,163,9,255], [252,165,10,255], [252,166,12,255], [252,168,13,255], [252,170,15,255],
        [252,172,17,255], [252,174,18,255], [252,176,20,255], [252,178,22,255], [252,180,24,255], [251,182,26,255], [251,184,29,255], [251,186,31,255],
        [251,188,33,255], [251,190,35,255], [250,192,38,255], [250,194,40,255], [250,196,42,255], [250,198,45,255], [249,199,47,255], [249,201,50,255],
        [249,203,53,255], [248,205,55,255], [248,207,58,255], [247,209,61,255], [247,211,64,255], [246,213,67,255], [246,215,70,255], [245,217,73,255],
        [245,219,76,255], [244,221,79,255], [244,223,83,255], [244,225,86,255], [243,227,90,255], [243,229,93,255], [242,230,97,255], [242,232,101,255],
        [242,234,105,255], [241,236,109,255], [241,237,113,255], [241,239,117,255], [241,241,121,255], [242,242,125,255], [242,244,130,255], [243,245,134,255],
        [243,246,138,255], [244,248,142,255], [245,249,146,255], [246,250,150,255], [248,251,154,255], [249,252,157,255], [250,253,161,255], [252,255,164,255],
    ]
}

// ---------------------------------------------------------------------------
// Turbo: exact 256-sample LUT sampled from matplotlib
// ---------------------------------------------------------------------------

/// Turbo colormap (Google 2019) : improved rainbow. Deep purple -> cyan -> green -> yellow -> red.
pub fn turbo_rgba() -> [[u8; 4]; 256] {
    [
        [48,18,59,255], [50,21,67,255], [51,24,74,255], [52,27,81,255], [53,30,88,255], [54,33,95,255], [55,36,102,255], [56,39,109,255],
        [57,42,115,255], [58,45,121,255], [59,47,128,255], [60,50,134,255], [61,53,139,255], [62,56,145,255], [63,59,151,255], [63,62,156,255],
        [64,64,162,255], [65,67,167,255], [65,70,172,255], [66,73,177,255], [66,75,181,255], [67,78,186,255], [68,81,191,255], [68,84,195,255],
        [68,86,199,255], [69,89,203,255], [69,92,207,255], [69,94,211,255], [70,97,214,255], [70,100,218,255], [70,102,221,255], [70,105,224,255],
        [70,107,227,255], [71,110,230,255], [71,113,233,255], [71,115,235,255], [71,118,238,255], [71,120,240,255], [71,123,242,255], [70,125,244,255],
        [70,128,246,255], [70,130,248,255], [70,133,250,255], [70,135,251,255], [69,138,252,255], [69,140,253,255], [68,143,254,255], [67,145,254,255],
        [66,148,255,255], [65,150,255,255], [64,153,255,255], [62,155,254,255], [61,158,254,255], [59,160,253,255], [58,163,252,255], [56,165,251,255],
        [55,168,250,255], [53,171,248,255], [51,173,247,255], [49,175,245,255], [47,178,244,255], [46,180,242,255], [44,183,240,255], [42,185,238,255],
        [40,188,235,255], [39,190,233,255], [37,192,231,255], [35,195,228,255], [34,197,226,255], [32,199,223,255], [31,201,221,255], [30,203,218,255],
        [28,205,216,255], [27,208,213,255], [26,210,210,255], [26,212,208,255], [25,213,205,255], [24,215,202,255], [24,217,200,255], [24,219,197,255],
        [24,221,194,255], [24,222,192,255], [24,224,189,255], [25,226,187,255], [25,227,185,255], [26,228,182,255], [28,230,180,255], [29,231,178,255],
        [31,233,175,255], [32,234,172,255], [34,235,170,255], [37,236,167,255], [39,238,164,255], [42,239,161,255], [44,240,158,255], [47,241,155,255],
        [50,242,152,255], [53,243,148,255], [56,244,145,255], [60,245,142,255], [63,246,138,255], [67,247,135,255], [70,248,132,255], [74,248,128,255],
        [78,249,125,255], [82,250,122,255], [85,250,118,255], [89,251,115,255], [93,252,111,255], [97,252,108,255], [101,253,105,255], [105,253,102,255],
        [109,254,98,255], [113,254,95,255], [117,254,92,255], [121,254,89,255], [125,255,86,255], [128,255,83,255], [132,255,81,255], [136,255,78,255],
        [139,255,75,255], [143,255,73,255], [146,255,71,255], [150,254,68,255], [153,254,66,255], [156,254,64,255], [159,253,63,255], [161,253,61,255],
        [164,252,60,255], [167,252,58,255], [169,251,57,255], [172,251,56,255], [175,250,55,255], [177,249,54,255], [180,248,54,255], [183,247,53,255],
        [185,246,53,255], [188,245,52,255], [190,244,52,255], [193,243,52,255], [195,241,52,255], [198,240,52,255], [200,239,52,255], [203,237,52,255],
        [205,236,52,255], [208,234,52,255], [210,233,53,255], [212,231,53,255], [215,229,53,255], [217,228,54,255], [219,226,54,255], [221,224,55,255],
        [223,223,55,255], [225,221,55,255], [227,219,56,255], [229,217,56,255], [231,215,57,255], [233,213,57,255], [235,211,57,255], [236,209,58,255],
        [238,207,58,255], [239,205,58,255], [241,203,58,255], [242,201,58,255], [244,199,58,255], [245,197,58,255], [246,195,58,255], [247,193,58,255],
        [248,190,57,255], [249,188,57,255], [250,186,57,255], [251,184,56,255], [251,182,55,255], [252,179,54,255], [252,177,54,255], [253,174,53,255],
        [253,172,52,255], [254,169,51,255], [254,167,50,255], [254,164,49,255], [254,161,48,255], [254,158,47,255], [254,155,45,255], [254,153,44,255],
        [254,150,43,255], [254,147,42,255], [254,144,41,255], [253,141,39,255], [253,138,38,255], [252,135,37,255], [252,132,35,255], [251,129,34,255],
        [251,126,33,255], [250,123,31,255], [249,120,30,255], [249,117,29,255], [248,114,28,255], [247,111,26,255], [246,108,25,255], [245,105,24,255],
        [244,102,23,255], [243,99,21,255], [242,96,20,255], [241,93,19,255], [240,91,18,255], [239,88,17,255], [237,85,16,255], [236,83,15,255],
        [235,80,14,255], [234,78,13,255], [232,75,12,255], [231,73,12,255], [229,71,11,255], [228,69,10,255], [226,67,10,255], [225,65,9,255],
        [223,63,8,255], [221,61,8,255], [220,59,7,255], [218,57,7,255], [216,55,6,255], [214,53,6,255], [212,51,5,255], [210,49,5,255],
        [208,47,5,255], [206,45,4,255], [204,43,4,255], [202,42,4,255], [200,40,3,255], [197,38,3,255], [195,37,3,255], [193,35,2,255],
        [190,33,2,255], [188,32,2,255], [185,30,2,255], [183,29,2,255], [180,27,1,255], [178,26,1,255], [175,24,1,255], [172,23,1,255],
        [169,22,1,255], [167,20,1,255], [164,19,1,255], [161,18,1,255], [158,16,1,255], [155,15,1,255], [152,14,1,255], [149,13,1,255],
        [146,11,1,255], [142,10,1,255], [139,9,2,255], [136,8,2,255], [133,7,2,255], [129,6,2,255], [126,5,2,255], [122,4,3,255],
    ]
}

// ---------------------------------------------------------------------------
// Jet: exact 256-sample LUT sampled from matplotlib
// ---------------------------------------------------------------------------

/// Jet colormap : classic blue-cyan-green-yellow-red. Not perceptually uniform.
pub fn jet_rgba() -> [[u8; 4]; 256] {
    [
        [0,0,128,255], [0,0,132,255], [0,0,137,255], [0,0,141,255], [0,0,146,255], [0,0,150,255], [0,0,155,255], [0,0,159,255],
        [0,0,164,255], [0,0,168,255], [0,0,173,255], [0,0,178,255], [0,0,182,255], [0,0,187,255], [0,0,191,255], [0,0,196,255],
        [0,0,200,255], [0,0,205,255], [0,0,209,255], [0,0,214,255], [0,0,218,255], [0,0,223,255], [0,0,227,255], [0,0,232,255],
        [0,0,237,255], [0,0,241,255], [0,0,246,255], [0,0,250,255], [0,0,255,255], [0,0,255,255], [0,0,255,255], [0,0,255,255],
        [0,1,255,255], [0,4,255,255], [0,9,255,255], [0,13,255,255], [0,17,255,255], [0,20,255,255], [0,25,255,255], [0,29,255,255],
        [0,33,255,255], [0,36,255,255], [0,41,255,255], [0,45,255,255], [0,49,255,255], [0,52,255,255], [0,57,255,255], [0,61,255,255],
        [0,65,255,255], [0,68,255,255], [0,73,255,255], [0,77,255,255], [0,81,255,255], [0,84,255,255], [0,89,255,255], [0,93,255,255],
        [0,97,255,255], [0,100,255,255], [0,105,255,255], [0,109,255,255], [0,113,255,255], [0,116,255,255], [0,121,255,255], [0,125,255,255],
        [0,129,255,255], [0,133,255,255], [0,136,255,255], [0,141,255,255], [0,145,255,255], [0,149,255,255], [0,153,255,255], [0,157,255,255],
        [0,161,255,255], [0,165,255,255], [0,168,255,255], [0,173,255,255], [0,177,255,255], [0,181,255,255], [0,185,255,255], [0,189,255,255],
        [0,193,255,255], [0,197,255,255], [0,200,255,255], [0,205,255,255], [0,209,255,255], [0,213,255,255], [0,217,255,255], [0,221,254,255],
        [0,225,251,255], [0,229,248,255], [2,232,244,255], [6,237,241,255], [9,241,238,255], [12,245,235,255], [15,249,231,255], [19,253,228,255],
        [22,255,225,255], [25,255,222,255], [28,255,219,255], [31,255,215,255], [35,255,212,255], [38,255,209,255], [41,255,206,255], [44,255,202,255],
        [48,255,199,255], [51,255,196,255], [54,255,193,255], [57,255,190,255], [60,255,186,255], [64,255,183,255], [67,255,180,255], [70,255,177,255],
        [73,255,173,255], [77,255,170,255], [80,255,167,255], [83,255,164,255], [86,255,160,255], [90,255,157,255], [93,255,154,255], [96,255,151,255],
        [99,255,148,255], [102,255,144,255], [106,255,141,255], [109,255,138,255], [112,255,135,255], [115,255,131,255], [119,255,128,255], [122,255,125,255],
        [125,255,122,255], [128,255,119,255], [131,255,115,255], [135,255,112,255], [138,255,109,255], [141,255,106,255], [144,255,102,255], [148,255,99,255],
        [151,255,96,255], [154,255,93,255], [157,255,90,255], [160,255,86,255], [164,255,83,255], [167,255,80,255], [170,255,77,255], [173,255,73,255],
        [177,255,70,255], [180,255,67,255], [183,255,64,255], [186,255,60,255], [190,255,57,255], [193,255,54,255], [196,255,51,255], [199,255,48,255],
        [202,255,44,255], [206,255,41,255], [209,255,38,255], [212,255,35,255], [215,255,31,255], [219,255,28,255], [222,255,25,255], [225,255,22,255],
        [228,255,19,255], [231,255,15,255], [235,255,12,255], [238,255,9,255], [241,252,6,255], [244,248,2,255], [248,245,0,255], [251,241,0,255],
        [254,237,0,255], [255,234,0,255], [255,230,0,255], [255,226,0,255], [255,222,0,255], [255,219,0,255], [255,215,0,255], [255,211,0,255],
        [255,208,0,255], [255,204,0,255], [255,200,0,255], [255,196,0,255], [255,193,0,255], [255,189,0,255], [255,185,0,255], [255,182,0,255],
        [255,178,0,255], [255,174,0,255], [255,171,0,255], [255,167,0,255], [255,163,0,255], [255,159,0,255], [255,156,0,255], [255,152,0,255],
        [255,148,0,255], [255,145,0,255], [255,141,0,255], [255,137,0,255], [255,134,0,255], [255,130,0,255], [255,126,0,255], [255,122,0,255],
        [255,119,0,255], [255,115,0,255], [255,111,0,255], [255,108,0,255], [255,104,0,255], [255,100,0,255], [255,96,0,255], [255,93,0,255],
        [255,89,0,255], [255,85,0,255], [255,82,0,255], [255,78,0,255], [255,74,0,255], [255,71,0,255], [255,67,0,255], [255,63,0,255],
        [255,59,0,255], [255,56,0,255], [255,52,0,255], [255,48,0,255], [255,45,0,255], [255,41,0,255], [255,37,0,255], [255,34,0,255],
        [255,30,0,255], [255,26,0,255], [255,22,0,255], [255,19,0,255], [250,15,0,255], [246,11,0,255], [241,8,0,255], [237,4,0,255],
        [232,0,0,255], [228,0,0,255], [223,0,0,255], [218,0,0,255], [214,0,0,255], [209,0,0,255], [205,0,0,255], [200,0,0,255],
        [196,0,0,255], [191,0,0,255], [187,0,0,255], [182,0,0,255], [178,0,0,255], [173,0,0,255], [168,0,0,255], [164,0,0,255],
        [159,0,0,255], [155,0,0,255], [150,0,0,255], [146,0,0,255], [141,0,0,255], [137,0,0,255], [132,0,0,255], [128,0,0,255],
    ]
}

// ---------------------------------------------------------------------------
// RdBu (reversed): exact 256-sample LUT sampled from matplotlib
// ---------------------------------------------------------------------------

/// RdBu diverging colormap (red -> white -> blue, reversed so blue is at t=0).
/// Suitable for signed quantities (negative = blue, zero = white, positive = red).
pub fn rdbu_r_rgba() -> [[u8; 4]; 256] {
    [
        [5,48,97,255], [6,50,100,255], [7,52,103,255], [8,54,106,255], [9,56,109,255], [10,59,112,255], [12,61,115,255], [13,63,118,255],
        [14,65,121,255], [15,67,123,255], [16,69,126,255], [17,71,129,255], [18,73,132,255], [19,76,135,255], [20,78,138,255], [21,80,141,255],
        [23,82,144,255], [24,84,147,255], [25,86,150,255], [26,88,153,255], [27,90,156,255], [28,92,159,255], [29,95,162,255], [30,97,165,255],
        [31,99,168,255], [32,101,171,255], [34,103,172,255], [35,105,173,255], [36,106,174,255], [38,108,175,255], [39,110,176,255], [40,112,177,255],
        [42,113,178,255], [43,115,179,255], [44,117,180,255], [46,119,181,255], [47,121,181,255], [48,122,182,255], [50,124,183,255], [51,126,184,255],
        [52,128,185,255], [54,129,186,255], [55,131,187,255], [56,133,188,255], [58,135,189,255], [59,136,190,255], [60,138,190,255], [62,140,191,255],
        [63,142,192,255], [64,143,193,255], [66,145,194,255], [67,147,195,255], [70,149,196,255], [73,151,197,255], [76,153,198,255], [79,155,199,255],
        [82,157,200,255], [86,159,201,255], [89,161,202,255], [92,163,203,255], [95,165,205,255], [98,167,206,255], [101,169,207,255], [104,171,208,255],
        [107,172,209,255], [110,174,210,255], [113,176,211,255], [117,178,212,255], [120,180,213,255], [123,182,214,255], [126,184,215,255], [129,186,216,255],
        [132,188,217,255], [135,190,218,255], [138,192,219,255], [141,194,220,255], [144,196,221,255], [147,198,222,255], [150,199,223,255], [152,200,224,255],
        [155,201,224,255], [157,203,225,255], [160,204,226,255], [162,205,227,255], [165,206,227,255], [167,208,228,255], [169,209,229,255], [172,210,229,255],
        [174,211,230,255], [177,213,231,255], [179,214,232,255], [182,215,232,255], [184,216,233,255], [187,218,234,255], [189,219,234,255], [192,220,235,255],
        [194,221,236,255], [197,223,236,255], [199,224,237,255], [202,225,238,255], [204,226,239,255], [207,228,239,255], [209,229,240,255], [210,230,240,255],
        [212,230,241,255], [213,231,241,255], [215,232,241,255], [216,233,241,255], [218,233,242,255], [219,234,242,255], [221,235,242,255], [222,235,242,255],
        [224,236,243,255], [225,237,243,255], [227,237,243,255], [228,238,244,255], [230,239,244,255], [231,240,244,255], [233,240,244,255], [234,241,245,255],
        [236,242,245,255], [237,242,245,255], [239,243,245,255], [240,244,246,255], [242,245,246,255], [243,245,246,255], [245,246,247,255], [246,247,247,255],
        [247,246,246,255], [247,245,244,255], [248,244,242,255], [248,243,240,255], [248,242,239,255], [248,241,237,255], [249,240,235,255], [249,239,233,255],
        [249,238,231,255], [249,237,229,255], [249,235,227,255], [250,234,225,255], [250,233,223,255], [250,232,222,255], [250,231,220,255], [251,230,218,255],
        [251,229,216,255], [251,228,214,255], [251,227,212,255], [252,226,210,255], [252,224,208,255], [252,223,207,255], [252,222,205,255], [253,221,203,255],
        [253,220,201,255], [253,219,199,255], [253,217,196,255], [252,215,194,255], [252,213,191,255], [252,211,188,255], [251,208,185,255], [251,206,183,255],
        [251,204,180,255], [250,202,177,255], [250,200,175,255], [249,198,172,255], [249,196,169,255], [249,194,167,255], [248,191,164,255], [248,189,161,255],
        [248,187,158,255], [247,185,156,255], [247,183,153,255], [247,181,150,255], [246,179,148,255], [246,177,145,255], [246,175,142,255], [245,172,139,255],
        [245,170,137,255], [245,168,134,255], [244,166,131,255], [243,164,129,255], [242,161,127,255], [241,158,125,255], [240,156,123,255], [239,153,121,255],
        [238,150,119,255], [236,147,116,255], [235,145,114,255], [234,142,112,255], [233,139,110,255], [232,137,108,255], [230,134,106,255], [229,131,104,255],
        [228,128,102,255], [227,126,100,255], [226,123,98,255], [225,120,96,255], [223,118,94,255], [222,115,92,255], [221,112,89,255], [220,110,87,255],
        [219,107,85,255], [218,104,83,255], [216,101,81,255], [215,99,79,255], [214,96,77,255], [213,93,76,255], [211,90,74,255], [210,88,73,255],
        [208,85,72,255], [207,82,70,255], [206,79,69,255], [204,76,68,255], [203,73,66,255], [201,71,65,255], [200,68,64,255], [198,65,62,255],
        [197,62,61,255], [196,59,60,255], [194,56,58,255], [193,54,57,255], [191,51,56,255], [190,48,54,255], [189,45,53,255], [187,42,52,255],
        [186,40,50,255], [184,37,49,255], [183,34,48,255], [182,31,46,255], [180,28,45,255], [179,25,44,255], [177,24,43,255], [174,23,42,255],
        [171,22,42,255], [168,21,41,255], [165,20,41,255], [162,19,40,255], [159,18,40,255], [156,17,39,255], [153,16,39,255], [150,15,39,255],
        [147,14,38,255], [144,13,38,255], [141,12,37,255], [138,11,37,255], [135,10,36,255], [132,9,36,255], [129,8,35,255], [127,8,35,255],
        [124,7,34,255], [121,6,34,255], [118,5,33,255], [115,4,33,255], [112,3,32,255], [109,2,32,255], [106,1,31,255], [103,0,31,255],
    ]
}

// ---------------------------------------------------------------------------
// Custom colormap LUT generation
// ---------------------------------------------------------------------------

/// Linearly interpolate between colour stops to produce a 256-sample LUT.
///
/// `stops` is a slice of `(position, rgba)` pairs where `position` is in `[0, 1]`.
/// Stops are sorted by position before interpolation.
/// The result is clamped: the first stop colour fills `t < stops[0].0` and the last
/// stop colour fills `t > stops.last().0`.
///
/// Returns a `[[u8; 4]; 256]` suitable for uploading to a 256×1 GPU texture.
pub fn lerp_colormap_lut(stops: &[(f32, [u8; 4])]) -> [[u8; 4]; 256] {
    let mut lut = [[0u8; 4]; 256];
    if stops.is_empty() {
        return lut;
    }
    // Sort by position.
    let mut sorted: Vec<(f32, [u8; 4])> = stops.to_vec();
    sorted.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));

    for i in 0..256 {
        let t = i as f32 / 255.0;

        // Find the surrounding stops.
        let first = sorted[0];
        let last = *sorted.last().unwrap();

        if t <= first.0 {
            lut[i] = first.1;
            continue;
        }
        if t >= last.0 {
            lut[i] = last.1;
            continue;
        }

        // Binary search for the lower stop.
        let pos = sorted.partition_point(|s| s.0 <= t);
        let lo = sorted[pos - 1];
        let hi = sorted[pos];

        let range = hi.0 - lo.0;
        let frac = if range > 1.0e-7 {
            (t - lo.0) / range
        } else {
            0.0
        };
        lut[i] = [
            (lo.1[0] as f32 + (hi.1[0] as f32 - lo.1[0] as f32) * frac + 0.5) as u8,
            (lo.1[1] as f32 + (hi.1[1] as f32 - lo.1[1] as f32) * frac + 0.5) as u8,
            (lo.1[2] as f32 + (hi.1[2] as f32 - lo.1[2] as f32) * frac + 0.5) as u8,
            (lo.1[3] as f32 + (hi.1[3] as f32 - lo.1[3] as f32) * frac + 0.5) as u8,
        ];
    }
    lut
}

/// Parse a ParaView XML colormap file.
///
/// Supports the standard format:
/// ```xml
/// <ColorMaps>
///   <ColorMap name="MyMap" space="RGB">
///     <Point x="0.0" r="0.0" g="0.0" b="0.0" o="1.0"/>
///     <Point x="1.0" r="1.0" g="1.0" b="1.0" o="1.0"/>
///   </ColorMap>
/// </ColorMaps>
/// ```
///
/// Returns a list of `(name, stops)` pairs, where each stop is `(position_0_1, [r, g, b, a])`.
/// Uses simple string parsing without any XML library dependency.
pub fn parse_paraview_xml_colormap(xml: &str) -> Vec<(String, Vec<(f32, [u8; 4])>)> {
    let mut result = Vec::new();
    let mut current_name: Option<String> = None;
    let mut current_stops: Vec<(f32, [u8; 4])> = Vec::new();

    for line in xml.lines() {
        let trimmed = line.trim();

        if trimmed.starts_with("<ColorMap") {
            // Extract name attribute.
            current_name = attr_value(trimmed, "name").map(|s| s.to_string());
            current_stops.clear();
        } else if trimmed.starts_with("</ColorMap>") {
            if let Some(name) = current_name.take() {
                if !current_stops.is_empty() {
                    result.push((name, current_stops.clone()));
                }
            }
            current_stops.clear();
        } else if trimmed.starts_with("<Point") {
            // Parse x, r, g, b, o attributes.
            if let (Some(x), Some(r), Some(g), Some(b)) = (
                attr_f32(trimmed, "x"),
                attr_f32(trimmed, "r"),
                attr_f32(trimmed, "g"),
                attr_f32(trimmed, "b"),
            ) {
                let a = attr_f32(trimmed, "o").unwrap_or(1.0);
                let stop = (
                    x,
                    [
                        (r.clamp(0.0, 1.0) * 255.0 + 0.5) as u8,
                        (g.clamp(0.0, 1.0) * 255.0 + 0.5) as u8,
                        (b.clamp(0.0, 1.0) * 255.0 + 0.5) as u8,
                        (a.clamp(0.0, 1.0) * 255.0 + 0.5) as u8,
                    ],
                );
                current_stops.push(stop);
            }
        }
    }

    result
}

/// Serialize a colormap to the ParaView XML colormap format.
pub fn export_paraview_xml_colormap(name: &str, stops: &[(f32, [u8; 4])]) -> String {
    let mut out = String::new();
    out.push_str("<ColorMaps>\n");
    out.push_str(&format!("  <ColorMap name=\"{}\" space=\"RGB\">\n", name));
    for &(pos, rgba) in stops {
        let r = rgba[0] as f32 / 255.0;
        let g = rgba[1] as f32 / 255.0;
        let b = rgba[2] as f32 / 255.0;
        let o = rgba[3] as f32 / 255.0;
        out.push_str(&format!(
            "    <Point x=\"{:.6}\" r=\"{:.6}\" g=\"{:.6}\" b=\"{:.6}\" o=\"{:.6}\"/>\n",
            pos, r, g, b, o
        ));
    }
    out.push_str("  </ColorMap>\n");
    out.push_str("</ColorMaps>\n");
    out
}

// ---------------------------------------------------------------------------
// XML parsing helpers
// ---------------------------------------------------------------------------

/// Extract the string value of a named XML attribute from a tag line.
/// e.g. `attr_value(r#"<Point x="0.5"/>"#, "x")` returns `Some("0.5")`.
fn attr_value<'a>(tag: &'a str, name: &str) -> Option<&'a str> {
    let search = format!("{}=\"", name);
    let start = tag.find(search.as_str())? + search.len();
    let end = tag[start..].find('"')? + start;
    Some(&tag[start..end])
}

fn attr_f32(tag: &str, name: &str) -> Option<f32> {
    attr_value(tag, name)?.parse().ok()
}