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
use crate::{Color, ColorF64, ColorI32, SimpleStatBuilder};

/// Simple statistics of color samples
#[derive(Default)]
pub struct ColorStat {
	pub mean: ColorI32,
	pub deviation: ColorF64,
}

/// Compute simple statistics from color samples
#[derive(Default)]
pub struct ColorStatBuilder {
	r: SimpleStatBuilder,
	g: SimpleStatBuilder,
	b: SimpleStatBuilder,
}

impl ColorStatBuilder {
	pub fn new() -> Self {
		Self::default()
	}

	pub fn add(&mut self, color: Color) {
		self.r.add(color.r as i32);
		self.g.add(color.g as i32);
		self.b.add(color.b as i32);
	}

	pub fn build(&self) -> ColorStat {
		let rs = self.r.build();
		let gs = self.g.build();
		let bs = self.b.build();
		ColorStat {
			mean: ColorI32 {
				r: rs.mean as i32,
				g: gs.mean as i32,
				b: bs.mean as i32,
			},
			deviation: ColorF64 {
				r: rs.deviation,
				g: gs.deviation,
				b: bs.deviation,
			}
		}
	}
}