Add saturation, value, brightness and color blind views
This commit is contained in:
+66
-5
@@ -1,5 +1,24 @@
|
||||
//! Analysis always uses source pixels, never the scaled preview.
|
||||
use std::collections::HashMap;
|
||||
pub mod vision;
|
||||
pub const LEVEL_BINS: usize = 20;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Distribution {
|
||||
pub pixels: [u64; LEVEL_BINS],
|
||||
pub colors: [u64; LEVEL_BINS],
|
||||
pub pixel_sum: f64,
|
||||
pub color_sum: f64,
|
||||
}
|
||||
impl Distribution {
|
||||
fn add(&mut self, value: f64, count: u64) {
|
||||
let bin = ((value.clamp(0.0, 1.0) * LEVEL_BINS as f64) as usize).min(LEVEL_BINS - 1);
|
||||
self.pixels[bin] += count;
|
||||
self.colors[bin] += 1;
|
||||
self.pixel_sum += value * count as f64;
|
||||
self.color_sum += value;
|
||||
}
|
||||
}
|
||||
|
||||
use crate::capture::Frame;
|
||||
|
||||
@@ -42,9 +61,14 @@ impl Region {
|
||||
pub struct Swatch {
|
||||
pub rgb: [u8; 3],
|
||||
pub count: u64,
|
||||
pub opaque_count: u64,
|
||||
}
|
||||
|
||||
pub struct Analysis {
|
||||
pub saturation: Distribution,
|
||||
pub brightness: Distribution,
|
||||
pub luminance: Distribution,
|
||||
pub vision: vision::Report,
|
||||
pub palette: Vec<Swatch>,
|
||||
pub pixels: u64,
|
||||
pub neutral_pixels: u64,
|
||||
@@ -56,6 +80,10 @@ pub struct Analysis {
|
||||
impl Default for Analysis {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
saturation: Distribution::default(),
|
||||
brightness: Distribution::default(),
|
||||
luminance: Distribution::default(),
|
||||
vision: vision::Report::default(),
|
||||
palette: Vec::new(),
|
||||
pixels: 0,
|
||||
neutral_pixels: 0,
|
||||
@@ -86,17 +114,22 @@ pub fn hsv(rgb: [u8; 3]) -> [f32; 3] {
|
||||
}
|
||||
|
||||
pub fn analyze(rgba: &[u8]) -> Analysis {
|
||||
let mut counts = HashMap::<[u8; 3], u64>::new();
|
||||
let mut counts = HashMap::<[u8; 3], (u64, u64)>::new();
|
||||
for pixel in rgba.as_chunks::<4>().0 {
|
||||
// Ignore fully transparent pixels; count other captured RGB values exactly.
|
||||
if pixel[3] != 0 {
|
||||
*counts.entry([pixel[0], pixel[1], pixel[2]]).or_default() += 1;
|
||||
let entry = counts.entry([pixel[0], pixel[1], pixel[2]]).or_default();
|
||||
entry.0 += 1;
|
||||
entry.1 += u64::from(pixel[3] == 255);
|
||||
}
|
||||
}
|
||||
let mut result = Analysis::default();
|
||||
for (rgb, count) in counts {
|
||||
for (rgb, (count, opaque_count)) in counts {
|
||||
result.pixels += count;
|
||||
let [h, s, _] = hsv(rgb);
|
||||
let [h, s, v] = hsv(rgb);
|
||||
result.saturation.add(s as f64, count);
|
||||
result.brightness.add(v as f64, count);
|
||||
result.luminance.add(vision::luminance(rgb), count);
|
||||
if s < NEUTRAL_SATURATION {
|
||||
result.neutral_pixels += count;
|
||||
result.neutral_colors += 1;
|
||||
@@ -105,11 +138,16 @@ pub fn analyze(rgba: &[u8]) -> Analysis {
|
||||
result.hue_pixels[bin] += count;
|
||||
result.hue_colors[bin] += 1;
|
||||
}
|
||||
result.palette.push(Swatch { rgb, count });
|
||||
result.palette.push(Swatch {
|
||||
rgb,
|
||||
count,
|
||||
opaque_count,
|
||||
});
|
||||
}
|
||||
result
|
||||
.palette
|
||||
.sort_unstable_by(|a, b| b.count.cmp(&a.count).then(a.rgb.cmp(&b.rgb)));
|
||||
result.vision = vision::check(&result.palette);
|
||||
result
|
||||
}
|
||||
|
||||
@@ -145,6 +183,29 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn saturation_value_and_luminance_include_endpoints_with_both_weights() {
|
||||
let a = analyze(&[
|
||||
0, 0, 0, 255, 255, 255, 255, 255, 255, 0, 0, 255, 255, 0, 0, 255, 128, 128, 128, 255,
|
||||
0, 255, 0, 0,
|
||||
]);
|
||||
for d in [&a.saturation, &a.brightness, &a.luminance] {
|
||||
assert_eq!(d.pixels.iter().sum::<u64>(), 5);
|
||||
assert_eq!(d.colors.iter().sum::<u64>(), 4);
|
||||
}
|
||||
assert_eq!(a.saturation.pixels[0], 3);
|
||||
assert_eq!(a.saturation.pixels[19], 2);
|
||||
assert_eq!(a.saturation.colors[19], 1);
|
||||
assert_eq!(a.brightness.pixels[0], 1);
|
||||
assert_eq!(a.brightness.pixels[19], 3);
|
||||
assert_eq!(a.luminance.pixels[19], 1);
|
||||
assert!((a.saturation.pixel_sum / 5.0 - 0.4).abs() < 1e-9);
|
||||
assert!((a.saturation.color_sum / 4.0 - 0.25).abs() < 1e-9);
|
||||
let empty = analyze(&[]);
|
||||
assert_eq!(empty.brightness.pixel_sum, 0.0);
|
||||
assert_eq!(empty.vision.colors_checked, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn crop_uses_source_coordinates_and_clamps() {
|
||||
let frame = Frame {
|
||||
|
||||
Reference in New Issue
Block a user