Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image color select 2: Histogram strikes back #1

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions tools/twix/src/panels/image_color_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use eframe::{
epaint::Vec2,
};

use egui_plot::{Bar, BarChart};
use linear_algebra::{point, vector, Point2};
use log::error;

Expand Down Expand Up @@ -49,6 +50,12 @@ struct PixelColor {
blue: f32,
}

struct ColorArray {
red: Vec<f64>,
green: Vec<f64>,
blue: Vec<f64>,
}

impl Panel for ImageColorSelectPanel {
const NAME: &'static str = "Image Color Select";

Expand Down Expand Up @@ -159,6 +166,11 @@ impl Widget for &mut ImageColorSelectPanel {
blue: 0.0,
};
let mut pixel_count: usize = 0;
let mut color_distribtion = ColorArray {
red: vec![0.0; 100],
green: vec![0.0; 100],
blue: vec![0.0; 100],
};

for i in (pixel_pos.x() as isize - self.brush_size as isize)
..(pixel_pos.x() as isize + self.brush_size as isize + 1)
Expand Down Expand Up @@ -186,6 +198,10 @@ impl Widget for &mut ImageColorSelectPanel {
average.green += color.green;
average.blue += color.blue;
pixel_count += 1;

color_distribtion.red[(color.red * 90.0) as usize] += 1.0;
color_distribtion.green[(color.green * 90.0) as usize] += 1.0;
color_distribtion.blue[(color.blue * 90.0) as usize] += 1.0;
}
}
}
Expand All @@ -206,7 +222,7 @@ impl Widget for &mut ImageColorSelectPanel {
ui.colored_label(Color32::RED, format!("r: {:.3}", max.red));
ui.colored_label(Color32::GREEN, format!("g: {:.3}", max.green));
ui.colored_label(
Color32::from_rgb(50, 150, 255),
Color32::from_rgb(50, 100, 255),
format!("b: {:.3}", max.blue),
);
ui.end_row();
Expand All @@ -215,7 +231,7 @@ impl Widget for &mut ImageColorSelectPanel {
ui.colored_label(Color32::RED, format!(" r: {:.3}", min.red));
ui.colored_label(Color32::GREEN, format!("g: {:.3}", min.green));
ui.colored_label(
Color32::from_rgb(50, 150, 255),
Color32::from_rgb(50, 100, 255),
format!("b: {:.3}", min.blue),
);
ui.end_row();
Expand All @@ -224,12 +240,24 @@ impl Widget for &mut ImageColorSelectPanel {
ui.colored_label(Color32::RED, format!(" r: {:.3}", average.red));
ui.colored_label(Color32::GREEN, format!("g: {:.3}", average.green));
ui.colored_label(
Color32::from_rgb(50, 150, 255),
Color32::from_rgb(50, 100, 255),
format!("b: {:.3}", average.blue),
);
ui.end_row();
});

ui.separator();

let red_chart = create_chart(color_distribtion.red, Color32::RED, -0.002);
let green_chart = create_chart(color_distribtion.green, Color32::GREEN, 0.0);
let blue_chart = create_chart(color_distribtion.blue, Color32::BLUE, 0.002);

egui_plot::Plot::new("karsten").show(ui, |plot_ui| {
plot_ui.bar_chart(red_chart);
plot_ui.bar_chart(green_chart);
plot_ui.bar_chart(blue_chart);
});

painter.circle(
pixel_pos,
self.brush_size,
Expand Down Expand Up @@ -280,3 +308,15 @@ fn get_pixel_chromaticity(image: &ColorImage, pixel_pos: Point2<Pixel>) -> Pixel
}
pixel
}

fn create_chart(vector: Vec<f64>, color: Color32, offset: f64) -> BarChart {
BarChart::new(
vector
.iter()
.enumerate()
.map(|(index, &value)| Bar::new(index as f64 * 0.01 + offset, value))
.collect(),
)
.color(color)
.width(0.002)
}
Loading