PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Color panel: HSB on sRGB values, like Photoshop (#3323) (#3327)
LloydNicholson539bf7ba14c6
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex 41ba45a34..54df3b024 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -1968,7 +1968,7 @@ fn color_picker(app: &mut PhotocraftApp, ui: &mut egui::Ui) {// saturation of their own, so recomputing them from RGB would reset what was just typed to 0.let key = egui::Id::new("color-picker-hsva");let stored: Option<egui::ecolor::Hsva> = ui.data(|d| d.get_temp(key));- let hsva0 = stored.filter(|h| h.to_srgb() == srgb_bytes(fg)).unwrap_or_else(|| srgb_hsva(fg));+ let hsva0 = stored.filter(|h| srgb_bytes(hsva_srgb(*h)) == srgb_bytes(fg)).unwrap_or_else(|| srgb_hsva(fg));let mut h = hsva0.h * 360.0;let mut s = hsva0.s * 100.0;let mut v = hsva0.v * 100.0;@@ -1977,9 +1977,9 @@ fn color_picker(app: &mut PhotocraftApp, ui: &mut egui::Ui) {let dynamic = app.session.prefs().interface.dynamic_color_sliders;let mut changed = widgets::slider_row(ui, tl!("Hue"), &mut h, 0.0..=360.0, "°", dynamic.then_some(&hue)).changed();let sat_stops =- [egui::ecolor::Hsva::new(hsva0.h, 0.0, hsva0.v.max(0.2), 1.0), egui::ecolor::Hsva::new(hsva0.h, 1.0, hsva0.v.max(0.2), 1.0)].map(Color32::from);+ [egui::ecolor::Hsva::new(hsva0.h, 0.0, hsva0.v.max(0.2), 1.0), egui::ecolor::Hsva::new(hsva0.h, 1.0, hsva0.v.max(0.2), 1.0)].map(hsva_color32);changed |= widgets::slider_row(ui, tl!("Saturation"), &mut s, 0.0..=100.0, "%", dynamic.then_some(&sat_stops)).changed();- let val_stops = [Color32::BLACK, Color32::from(egui::ecolor::Hsva::new(hsva0.h, hsva0.s, 1.0, 1.0))];+ let val_stops = [Color32::BLACK, hsva_color32(egui::ecolor::Hsva::new(hsva0.h, hsva0.s, 1.0, 1.0))];changed |= widgets::slider_row(ui, tl!("Brightness"), &mut v, 0.0..=100.0, "%", dynamic.then_some(&val_stops)).changed();let hsva = egui::ecolor::Hsva::new(h / 360.0, s / 100.0, v / 100.0, 1.0);// Only an edit counts: the h/s/v round trip isn't exact, so comparing values would rewrite@@ -1989,10 +1989,9 @@ fn color_picker(app: &mut PhotocraftApp, ui: &mut egui::Ui) {ui.data_mut(|d| d.insert_temp(key, hsva));crate::type_tool::foreground_changed(app);}- let [r, g, b, _] = hsva.to_srgba_unmultiplied();ui.horizontal(|ui| {let (sw, _) = ui.allocate_exact_size(vec2(26.0, 26.0), Sense::hover());- ui.painter().rect_filled(sw, 6.0, Color32::from_rgb(r, g, b));+ ui.painter().rect_filled(sw, 6.0, hsva_color32(hsva));let mut c = app.session.tools.foreground;if color_readout(ui, ui.id().with("color-panel"), &mut c) {app.session.tools.foreground = c;@@ -3247,7 +3246,7 @@ fn color_field(app: &mut PhotocraftApp, ui: &mut egui::Ui) {for j in 0..=n {for i in 0..=n {let (sx, vy) = (i as f32 / n as f32, j as f32 / n as f32);- let c = Color32::from(egui::ecolor::Hsva::new(hsva.h, sx, 1.0 - vy, 1.0));+ let c = hsva_color32(egui::ecolor::Hsva::new(hsva.h, sx, 1.0 - vy, 1.0));mesh.colored_vertex(pos2(field.left() + sx * field.width(), field.top() + vy * field.height()), c);}}@@ -3278,7 +3277,7 @@ fn color_field(app: &mut PhotocraftApp, ui: &mut egui::Ui) {let steps = 24;for k in 0..=steps {let f = k as f32 / steps as f32;- let c = Color32::from(egui::ecolor::Hsva::new(1.0 - f, 1.0, 1.0, 1.0));+ let c = hsva_color32(egui::ecolor::Hsva::new(1.0 - f, 1.0, 1.0, 1.0));m2.colored_vertex(pos2(strip.left(), strip.top() + f * strip.height()), c);m2.colored_vertex(pos2(strip.right(), strip.top() + f * strip.height()), c);}@@ -3333,13 +3332,23 @@ fn srgb_bytes(c: [f32; 4]) -> [u8; 3] {[q(c[0]), q(c[1]), q(c[2])]}+/// The Color panel's HSB, as Photoshop's and the Color Picker's: on the sRGB-encoded values,+/// so a 50% grey has Brightness 50%. egui's `Hsva` conversions work in linear light, which put+/// that grey at 22% and drew the field's middle as 188 (#3323); `Hsva` only holds the numbers.fn srgb_hsva(c: [f32; 4]) -> egui::ecolor::Hsva {- egui::ecolor::Hsva::from_srgb(srgb_bytes(c))+ let [h, s, v] = crate::color_picker_ui::rgb_to_hsv([c[0], c[1], c[2]].map(|v| v.clamp(0.0, 1.0)));+ egui::ecolor::Hsva::new(h / 360.0, s, v, 1.0)}fn hsva_srgb(h: egui::ecolor::Hsva) -> [f32; 4] {- let [r, g, b] = h.to_srgb();- [r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, 1.0]+ let [r, g, b] = crate::color_picker_ui::hsv_to_rgb(h.h * 360.0, h.s.clamp(0.0, 1.0), h.v.clamp(0.0, 1.0));+ [r, g, b, 1.0]+}++/// The colour to draw for Color panel HSB `h` (see [`srgb_hsva`]).+fn hsva_color32(h: egui::ecolor::Hsva) -> Color32 {+ let [r, g, b] = srgb_bytes(hsva_srgb(h));+ Color32::from_rgb(r, g, b)}/// The Color panel's editable colour readout: a hex field and R, G, B fields, all drawn like the@@ -3772,6 +3781,23 @@ mod symmetry_theme_tests {mod color_tests {use super::*;+ #[test]+ /// #3323: the Color panel's HSB is Photoshop's (and the Color Picker's), on sRGB values, not+ /// egui's linear-light HSV: a 50% grey has Brightness 50%, and the field's left edge halfway+ /// down is that grey (128), not 188.+ fn color_panel_hsb_is_srgb_like_photoshop() {+ let grey = srgb_hsva([128.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0, 1.0]);+ assert!((grey.v - 128.0 / 255.0).abs() < 1e-3 && grey.s < 1e-3, "{grey:?}");+ let mid = hsva_srgb(egui::ecolor::Hsva::new(0.3, 0.0, 0.5, 1.0));+ assert!(mid[..3].iter().all(|v| (v - 0.5).abs() <= 1.0 / 255.0), "{mid:?}");+ // What the field paints there.+ assert_eq!(hsva_color32(egui::ecolor::Hsva::new(0.3, 0.0, 0.5, 1.0)), Color32::from_gray(128));+ // The same numbers as the Color Picker dialog for a saturated colour.+ let c = [0.847, 0.271, 0.180];+ let (ours, dialog) = (srgb_hsva([c[0], c[1], c[2], 1.0]), crate::color_picker_ui::rgb_to_hsv(c));+ assert!((ours.h * 360.0 - dialog[0]).abs() < 0.5 && (ours.s - dialog[1]).abs() < 1e-3 && (ours.v - dialog[2]).abs() < 1e-3, "{ours:?} vs {dialog:?}");+ }+#[test]fn srgb_hsva_roundtrip() {for c in [[0.847, 0.271, 0.180, 1.0], [0.0, 0.0, 0.0, 1.0], [1.0, 1.0, 1.0, 1.0], [0.2, 0.6, 0.4, 1.0]] {