material-color-utils 0.1.3

Color libraries for Google's Material You
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
547
548
549
550
551
552
553
use crate::quantize::quantizer::{Quantizer, QuantizerResult};
use crate::quantize::quantizer_map::QuantizerMap;
use crate::utils::color_utils::Argb;
use indexmap::IndexMap;

// A histogram of all the input colors is constructed. It has the shape of a cube. The cube
// would be too large if it contained all 16 million colors: historical best practice is to use
// 5 bits of the 8 in each channel, reducing the histogram to a volume of ~32,000.
const INDEX_BITS: u32 = 5;
const INDEX_COUNT: usize = 33; // ((1 << INDEX_BITS) + 1)
const TOTAL_SIZE: usize = 35937; // INDEX_COUNT * INDEX_COUNT * INDEX_COUNT

/// An image quantizer that divides the image's pixels into clusters by recursively cutting an RGB
/// cube, based on the weight of pixels in each area of the cube.
///
/// The algorithm was described by Xiaolin Wu in Graphic Gems II, published in 1991.
pub struct QuantizerWu {
    weights: Vec<i32>,
    moments_r: Vec<i32>,
    moments_g: Vec<i32>,
    moments_b: Vec<i32>,
    moments: Vec<f64>,
    cubes: Vec<Box>,
}

impl Default for QuantizerWu {
    fn default() -> Self {
        Self {
            weights: vec![0; TOTAL_SIZE],
            moments_r: vec![0; TOTAL_SIZE],
            moments_g: vec![0; TOTAL_SIZE],
            moments_b: vec![0; TOTAL_SIZE],
            moments: vec![0.0; TOTAL_SIZE],
            cubes: Vec::new(),
        }
    }
}

impl QuantizerWu {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    fn construct_histogram(&mut self, pixels: &IndexMap<Argb, u32>) {
        self.weights.fill(0);
        self.moments_r.fill(0);
        self.moments_g.fill(0);
        self.moments_b.fill(0);
        self.moments.fill(0.0);

        for (&pixel, &count) in pixels {
            let red = pixel.red();
            let green = pixel.green();
            let blue = pixel.blue();
            let bits_to_remove = 8 - INDEX_BITS;
            let i_r = (red >> bits_to_remove) + 1;
            let i_g = (green >> bits_to_remove) + 1;
            let i_b = (blue >> bits_to_remove) + 1;
            let index = Self::get_index(i_r as usize, i_g as usize, i_b as usize);

            let count_i = count.cast_signed();
            self.weights[index] += count_i;
            self.moments_r[index] += i32::from(red) * count_i;
            self.moments_g[index] += i32::from(green) * count_i;
            self.moments_b[index] += i32::from(blue) * count_i;
            self.moments[index] += f64::from(count)
                * f64::from(blue).mul_add(
                    f64::from(blue),
                    f64::from(red).mul_add(f64::from(red), f64::from(green) * f64::from(green)),
                );
        }
    }

    fn create_moments(&mut self) {
        for r in 1..INDEX_COUNT {
            let mut area = [0i32; INDEX_COUNT];
            let mut area_r = [0i32; INDEX_COUNT];
            let mut area_g = [0i32; INDEX_COUNT];
            let mut area_b = [0i32; INDEX_COUNT];
            let mut area2 = [0.0f64; INDEX_COUNT];
            for g in 1..INDEX_COUNT {
                let mut line = 0i32;
                let mut line_r = 0i32;
                let mut line_g = 0i32;
                let mut line_b = 0i32;
                let mut line2 = 0.0f64;
                for b in 1..INDEX_COUNT {
                    let index = Self::get_index(r, g, b);
                    line += self.weights[index];
                    line_r += self.moments_r[index];
                    line_g += self.moments_g[index];
                    line_b += self.moments_b[index];
                    line2 += self.moments[index];

                    area[b] += line;
                    area_r[b] += line_r;
                    area_g[b] += line_g;
                    area_b[b] += line_b;
                    area2[b] += line2;

                    let previous_index = Self::get_index(r - 1, g, b);
                    self.weights[index] = self.weights[previous_index] + area[b];
                    self.moments_r[index] = self.moments_r[previous_index] + area_r[b];
                    self.moments_g[index] = self.moments_g[previous_index] + area_g[b];
                    self.moments_b[index] = self.moments_b[previous_index] + area_b[b];
                    self.moments[index] = self.moments[previous_index] + area2[b];
                }
            }
        }
    }

    fn create_boxes(&mut self, max_color_count: usize) -> CreateBoxesResult {
        self.cubes = vec![Box::default(); max_color_count];
        let mut volume_variance = vec![0.0; max_color_count];

        let first_box = &mut self.cubes[0];
        first_box.r1 = (INDEX_COUNT - 1) as i32;
        first_box.g1 = (INDEX_COUNT - 1) as i32;
        first_box.b1 = (INDEX_COUNT - 1) as i32;

        let mut generated_color_count = max_color_count;
        let mut next = 0;
        let mut i = 1;
        while i < max_color_count {
            // We need to split the cubes vector to get two mutable references
            let (one, two) = if next < i {
                let (left, right) = self.cubes.split_at_mut(i);
                (&mut left[next], &mut right[0])
            } else {
                let (left, right) = self.cubes.split_at_mut(next);
                (&mut right[0], &mut left[i])
            };

            if Self::cut(
                one,
                two,
                &self.moments_r,
                &self.moments_g,
                &self.moments_b,
                &self.weights,
            ) {
                volume_variance[next] = if one.vol > 1 {
                    Self::variance(
                        one,
                        &self.moments,
                        &self.moments_r,
                        &self.moments_g,
                        &self.moments_b,
                        &self.weights,
                    )
                } else {
                    0.0
                };
                volume_variance[i] = if two.vol > 1 {
                    Self::variance(
                        two,
                        &self.moments,
                        &self.moments_r,
                        &self.moments_g,
                        &self.moments_b,
                        &self.weights,
                    )
                } else {
                    0.0
                };
            } else {
                volume_variance[next] = 0.0;
                i -= 1;
            }

            next = 0;
            let mut temp = volume_variance[0];

            for (j, &variance) in volume_variance.iter().enumerate().take(i + 1).skip(1) {
                if variance > temp {
                    temp = variance;
                    next = j;
                }
            }

            if temp <= 0.0 {
                generated_color_count = i + 1;
                break;
            }
            i += 1;
        }

        CreateBoxesResult {
            requested_count: max_color_count as i32,
            result_count: generated_color_count as i32,
        }
    }

    fn create_result(&self, color_count: usize) -> Vec<Argb> {
        let mut colors = Vec::new();
        for i in 0..color_count {
            let cube = &self.cubes[i];
            let weight = Self::volume(cube, &self.weights);
            if weight > 0 {
                let r = Self::volume(cube, &self.moments_r) / weight;
                let g = Self::volume(cube, &self.moments_g) / weight;
                let b = Self::volume(cube, &self.moments_b) / weight;
                let color = Argb::from_rgb((r & 0xFF) as u8, (g & 0xFF) as u8, (b & 0xFF) as u8);
                colors.push(color);
            }
        }
        colors
    }

    fn variance(
        cube: &Box,
        moments: &[f64],
        moments_r: &[i32],
        moments_g: &[i32],
        moments_b: &[i32],
        weights: &[i32],
    ) -> f64 {
        let dr = Self::volume(cube, moments_r);
        let dg = Self::volume(cube, moments_g);
        let db = Self::volume(cube, moments_b);
        let xx = moments[Self::get_index(cube.r1 as usize, cube.g1 as usize, cube.b1 as usize)]
            - moments[Self::get_index(cube.r1 as usize, cube.g1 as usize, cube.b0 as usize)]
            - moments[Self::get_index(cube.r1 as usize, cube.g0 as usize, cube.b1 as usize)]
            + moments[Self::get_index(cube.r1 as usize, cube.g0 as usize, cube.b0 as usize)]
            - moments[Self::get_index(cube.r0 as usize, cube.g1 as usize, cube.b1 as usize)]
            + moments[Self::get_index(cube.r0 as usize, cube.g1 as usize, cube.b0 as usize)]
            + moments[Self::get_index(cube.r0 as usize, cube.g0 as usize, cube.b1 as usize)]
            - moments[Self::get_index(cube.r0 as usize, cube.g0 as usize, cube.b0 as usize)];

        let hypotenuse = f64::from(db).mul_add(
            f64::from(db),
            f64::from(dr).mul_add(f64::from(dr), f64::from(dg) * f64::from(dg)),
        );
        let volume = Self::volume(cube, weights);
        xx - (hypotenuse / f64::from(volume))
    }

    fn cut(
        one: &mut Box,
        two: &mut Box,
        moments_r: &[i32],
        moments_g: &[i32],
        moments_b: &[i32],
        weights: &[i32],
    ) -> bool {
        let whole_r = Self::volume(one, moments_r);
        let whole_g = Self::volume(one, moments_g);
        let whole_b = Self::volume(one, moments_b);
        let whole_w = Self::volume(one, weights);

        let max_r_result = Self::maximize(
            one,
            Direction::Red,
            one.r0 + 1,
            one.r1,
            whole_r,
            whole_g,
            whole_b,
            whole_w,
            moments_r,
            moments_g,
            moments_b,
            weights,
        );
        let max_g_result = Self::maximize(
            one,
            Direction::Green,
            one.g0 + 1,
            one.g1,
            whole_r,
            whole_g,
            whole_b,
            whole_w,
            moments_r,
            moments_g,
            moments_b,
            weights,
        );
        let max_b_result = Self::maximize(
            one,
            Direction::Blue,
            one.b0 + 1,
            one.b1,
            whole_r,
            whole_g,
            whole_b,
            whole_w,
            moments_r,
            moments_g,
            moments_b,
            weights,
        );

        let max_r = max_r_result.maximum;
        let max_g = max_g_result.maximum;
        let max_b = max_b_result.maximum;

        let cut_direction = if max_r >= max_g && max_r >= max_b {
            if max_r_result.cut_location < 0 {
                return false;
            }
            Direction::Red
        } else if max_g >= max_r && max_g >= max_b {
            Direction::Green
        } else {
            Direction::Blue
        };

        two.r1 = one.r1;
        two.g1 = one.g1;
        two.b1 = one.b1;

        match cut_direction {
            Direction::Red => {
                one.r1 = max_r_result.cut_location;
                two.r0 = one.r1;
                two.g0 = one.g0;
                two.b0 = one.b0;
            }
            Direction::Green => {
                one.g1 = max_g_result.cut_location;
                two.r0 = one.r0;
                two.g0 = one.g1;
                two.b0 = one.b0;
            }
            Direction::Blue => {
                one.b1 = max_b_result.cut_location;
                two.r0 = one.r0;
                two.g0 = one.g0;
                two.b0 = one.b1;
            }
        }

        one.vol = (one.r1 - one.r0) * (one.g1 - one.g0) * (one.b1 - one.b0);
        two.vol = (two.r1 - two.r0) * (two.g1 - two.g0) * (two.b1 - two.b0);

        true
    }

    fn maximize(
        cube: &Box,
        direction: Direction,
        first: i32,
        last: i32,
        whole_r: i32,
        whole_g: i32,
        whole_b: i32,
        whole_w: i32,
        moments_r: &[i32],
        moments_g: &[i32],
        moments_b: &[i32],
        weights: &[i32],
    ) -> MaximizeResult {
        let bottom_r = Self::bottom(cube, direction, moments_r);
        let bottom_g = Self::bottom(cube, direction, moments_g);
        let bottom_b = Self::bottom(cube, direction, moments_b);
        let bottom_w = Self::bottom(cube, direction, weights);

        let mut max = 0.0;
        let mut cut = -1;

        for i in first..last {
            let mut half_r = bottom_r + Self::top(cube, direction, i, moments_r);
            let mut half_g = bottom_g + Self::top(cube, direction, i, moments_g);
            let mut half_b = bottom_b + Self::top(cube, direction, i, moments_b);
            let mut half_w = bottom_w + Self::top(cube, direction, i, weights);

            if half_w == 0 {
                continue;
            }

            let mut temp_numerator = f64::from(half_b).mul_add(
                f64::from(half_b),
                f64::from(half_r).mul_add(f64::from(half_r), f64::from(half_g) * f64::from(half_g)),
            );
            let mut temp_denominator = f64::from(half_w);
            let mut temp = temp_numerator / temp_denominator;

            half_r = whole_r - half_r;
            half_g = whole_g - half_g;
            half_b = whole_b - half_b;
            half_w = whole_w - half_w;

            if half_w == 0 {
                continue;
            }

            temp_numerator = f64::from(half_b).mul_add(
                f64::from(half_b),
                f64::from(half_r).mul_add(f64::from(half_r), f64::from(half_g) * f64::from(half_g)),
            );
            temp_denominator = f64::from(half_w);
            temp += temp_numerator / temp_denominator;

            if temp > max {
                max = temp;
                cut = i;
            }
        }

        MaximizeResult {
            cut_location: cut,
            maximum: max,
        }
    }

    const fn get_index(r: usize, g: usize, b: usize) -> usize {
        (r << (INDEX_BITS * 2)) + (r << (INDEX_BITS + 1)) + r + (g << INDEX_BITS) + g + b
    }

    fn volume(cube: &Box, moment: &[i32]) -> i32 {
        moment[Self::get_index(cube.r1 as usize, cube.g1 as usize, cube.b1 as usize)]
            - moment[Self::get_index(cube.r1 as usize, cube.g1 as usize, cube.b0 as usize)]
            - moment[Self::get_index(cube.r1 as usize, cube.g0 as usize, cube.b1 as usize)]
            + moment[Self::get_index(cube.r1 as usize, cube.g0 as usize, cube.b0 as usize)]
            - moment[Self::get_index(cube.r0 as usize, cube.g1 as usize, cube.b1 as usize)]
            + moment[Self::get_index(cube.r0 as usize, cube.g1 as usize, cube.b0 as usize)]
            + moment[Self::get_index(cube.r0 as usize, cube.g0 as usize, cube.b1 as usize)]
            - moment[Self::get_index(cube.r0 as usize, cube.g0 as usize, cube.b0 as usize)]
    }

    fn bottom(cube: &Box, direction: Direction, moment: &[i32]) -> i32 {
        match direction {
            Direction::Red => {
                -moment[Self::get_index(cube.r0 as usize, cube.g1 as usize, cube.b1 as usize)]
                    + moment[Self::get_index(cube.r0 as usize, cube.g1 as usize, cube.b0 as usize)]
                    + moment[Self::get_index(cube.r0 as usize, cube.g0 as usize, cube.b1 as usize)]
                    - moment[Self::get_index(cube.r0 as usize, cube.g0 as usize, cube.b0 as usize)]
            }
            Direction::Green => {
                -moment[Self::get_index(cube.r1 as usize, cube.g0 as usize, cube.b1 as usize)]
                    + moment[Self::get_index(cube.r1 as usize, cube.g0 as usize, cube.b0 as usize)]
                    + moment[Self::get_index(cube.r0 as usize, cube.g0 as usize, cube.b1 as usize)]
                    - moment[Self::get_index(cube.r0 as usize, cube.g0 as usize, cube.b0 as usize)]
            }
            Direction::Blue => {
                -moment[Self::get_index(cube.r1 as usize, cube.g1 as usize, cube.b0 as usize)]
                    + moment[Self::get_index(cube.r1 as usize, cube.g0 as usize, cube.b0 as usize)]
                    + moment[Self::get_index(cube.r0 as usize, cube.g1 as usize, cube.b0 as usize)]
                    - moment[Self::get_index(cube.r0 as usize, cube.g0 as usize, cube.b0 as usize)]
            }
        }
    }

    fn top(cube: &Box, direction: Direction, position: i32, moment: &[i32]) -> i32 {
        match direction {
            Direction::Red => {
                moment[Self::get_index(position as usize, cube.g1 as usize, cube.b1 as usize)]
                    - moment[Self::get_index(position as usize, cube.g1 as usize, cube.b0 as usize)]
                    - moment[Self::get_index(position as usize, cube.g0 as usize, cube.b1 as usize)]
                    + moment[Self::get_index(position as usize, cube.g0 as usize, cube.b0 as usize)]
            }
            Direction::Green => {
                moment[Self::get_index(cube.r1 as usize, position as usize, cube.b1 as usize)]
                    - moment[Self::get_index(cube.r1 as usize, position as usize, cube.b0 as usize)]
                    - moment[Self::get_index(cube.r0 as usize, position as usize, cube.b1 as usize)]
                    + moment[Self::get_index(cube.r0 as usize, position as usize, cube.b0 as usize)]
            }
            Direction::Blue => {
                moment[Self::get_index(cube.r1 as usize, cube.g1 as usize, position as usize)]
                    - moment[Self::get_index(cube.r1 as usize, cube.g0 as usize, position as usize)]
                    - moment[Self::get_index(cube.r0 as usize, cube.g1 as usize, position as usize)]
                    + moment[Self::get_index(cube.r0 as usize, cube.g0 as usize, position as usize)]
            }
        }
    }
}

impl Quantizer for QuantizerWu {
    fn quantize(&mut self, pixels: &[Argb], max_colors: usize) -> QuantizerResult {
        let mut map_quantizer = QuantizerMap::new();
        let map_result = map_quantizer.quantize(pixels, max_colors);

        self.construct_histogram(&map_result.color_to_count);
        self.create_moments();
        let create_boxes_result = self.create_boxes(max_colors);

        let colors = self.create_result(create_boxes_result.result_count as usize);

        let mut result_map = IndexMap::new();
        for color in colors {
            result_map.insert(color, 0);
        }

        QuantizerResult::new(result_map)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::utils::color_utils::Argb;

    #[test]
    fn test_quantize_wu_basic() {
        let mut quantizer = QuantizerWu::new();
        let pixels = vec![
            Argb::from_rgb(255, 0, 0),
            Argb::from_rgb(0, 255, 0),
            Argb::from_rgb(0, 0, 255),
            Argb::from_rgb(255, 255, 255),
            Argb::from_rgb(0, 0, 0),
        ];
        let result = quantizer.quantize(&pixels, 2);
        assert!(!result.color_to_count.is_empty());
        assert!(result.color_to_count.len() <= 2);
    }

    #[test]
    fn test_quantize_wu_red() {
        let mut quantizer = QuantizerWu::new();
        let pixels = vec![Argb::from_rgb(255, 0, 0); 100];
        let result = quantizer.quantize(&pixels, 10);
        assert_eq!(result.color_to_count.len(), 1);
        let color = result.color_to_count.keys().next().expect("Next fail");
        // Wu quantizer might slightly shift the color due to index mapping
        assert!(color.red() > 240);
        assert!(color.green() < 10);
        assert!(color.blue() < 10);
    }
}

#[derive(Clone, Copy, Debug)]
enum Direction {
    Red,
    Green,
    Blue,
}

#[derive(Clone, Copy, Debug)]
struct MaximizeResult {
    cut_location: i32,
    maximum: f64,
}

#[derive(Clone, Copy, Debug)]
struct CreateBoxesResult {
    #[allow(dead_code)]
    requested_count: i32,
    result_count: i32,
}

#[derive(Default, Clone, Copy, Debug)]
struct Box {
    r0: i32,
    r1: i32,
    g0: i32,
    g1: i32,
    b0: i32,
    b1: i32,
    vol: i32,
}