Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(color-panel): non-finite pointer no longer panics the wheel; ramp drag keeps its ends

Khawar Zamman Wanib2c5056a088a
diff --git a/crates/ui-egui/src/color_panel_tests.rs b/crates/ui-egui/src/color_panel_tests.rs
index 956d07ad4..5922fa900 100644
--- a/crates/ui-egui/src/color_panel_tests.rs
+++ b/crates/ui-egui/src/color_panel_tests.rs
@@ -247,6 +247,44 @@ fn ramp_picks_and_shift_click_cycles_and_alt_picks_the_other_colour() {
assert!(bytes(h.state().session.tools.foreground).iter().all(|v| *v > 245));
}
+/// A drag on the Current Colors ramp picks from the ends it began with: holding still keeps the
+/// colour under the pointer instead of drifting toward the background.
+#[test]
+fn current_colors_ramp_drag_keeps_its_ends() {
+ let mut h = color_tab(ThemeKind::BreezeDark, ColorPanelMode::RgbSliders, [0.0, 0.0, 0.0, 1.0]);
+ h.state_mut().session.tools.background = [1.0, 1.0, 1.0, 1.0];
+ h.state_mut().ui.color_panel.ramp = ColorRamp::CurrentColors;
+ h.run_steps(2);
+ let ramp: Rect = h.get_by_label("Color Ramp").rect();
+ let p = ramp.center();
+ h.hover_at(p);
+ h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
+ h.run_steps(1);
+ // Move away and back to start the drag, then hold still for a while.
+ for q in [p + vec2(12.0, 0.0), p] {
+ h.event(egui::Event::PointerMoved(q));
+ h.run_steps(1);
+ }
+ h.run_steps(20);
+ h.event(egui::Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE });
+ h.run_steps(2);
+ let expected = ramp_color(ColorRamp::CurrentColors, false, [0.0; 3], [1.0; 3], (p.x - ramp.left()) / ramp.width(), 0.5);
+ let got = bytes(h.state().session.tools.foreground);
+ for (g, e) in got.iter().zip(expected) {
+ assert!(i32::from(*g).abs_diff((e * 255.0).round() as i32) <= 3, "{got:?} vs {expected:?}");
+ }
+}
+
+/// A non-finite pointer (a huge synthetic `ui.move`) misses the triangle instead of panicking.
+#[test]
+fn triangle_ignores_non_finite_points() {
+ let tri = [pos2(100.0, 0.0), pos2(0.0, 0.0), pos2(0.0, 100.0)];
+ for p in [pos2(f32::INFINITY, f32::INFINITY), pos2(f32::NAN, 1.0), pos2(f32::NEG_INFINITY, f32::INFINITY)] {
+ assert_eq!(crate::color_panel_ui::triangle_sv(tri, p), None, "{p:?}");
+ }
+ assert!(crate::color_panel_ui::triangle_sv(tri, pos2(10.0, 10.0)).is_some());
+}
+
#[test]
fn color_wheel_ring_sets_hue_and_triangle_sets_saturation_and_brightness() {
let mut h = color_tab(ThemeKind::Pro, ColorPanelMode::ColorWheel, [0.0, 0.0, 1.0, 1.0]);
diff --git a/crates/ui-egui/src/color_panel_ui.rs b/crates/ui-egui/src/color_panel_ui.rs
index 35b6593f4..e015be052 100644
--- a/crates/ui-egui/src/color_panel_ui.rs
+++ b/crates/ui-egui/src/color_panel_ui.rs
@@ -471,8 +471,17 @@ fn ramp(app: &mut PhotocraftApp, ui: &mut egui::Ui, height: f32) {
let bar = Rect::from_min_max(all.min, pos2(all.right() - BOXES_W - 2.0, all.bottom()));
let boxes = Rect::from_min_max(pos2(all.right() - BOXES_W, all.top()), all.max);
let cp = app.ui.color_panel;
- let fg = rgb3(app.session.tools.foreground);
- let bg = rgb3(app.session.tools.background);
+ let ramp_id = ui.id().with("color-panel-ramp");
+ // A pick changes the foreground or background, so a drag keeps the Current Colors ends it began
+ // with; otherwise the ramp would slide under the pointer and a held pick would drift.
+ let ends_id = ramp_id.with("ends");
+ let live = (rgb3(app.session.tools.foreground), rgb3(app.session.tools.background));
+ let (fg, bg) = if ui.ctx().is_being_dragged(ramp_id) {
+ ui.data(|d| d.get_temp::<([f32; 3], [f32; 3])>(ends_id)).unwrap_or(live)
+ } else {
+ ui.data_mut(|d| d.insert_temp(ends_id, live));
+ live
+ };
let colors = ramp_colors(ui, cp.ramp, cp.web_safe_ramp, fg, bg);
ui.painter().add(ramp_mesh(bar, &colors, cp.web_safe_ramp));
ui.painter().rect_stroke(bar, 0.0, Stroke::new(1.0, t.field_border), StrokeKind::Outside);
@@ -481,7 +490,7 @@ fn ramp(app: &mut PhotocraftApp, ui: &mut egui::Ui, height: f32) {
ui.painter().rect_filled(white, 0.0, Color32::WHITE);
ui.painter().rect_filled(black, 0.0, Color32::BLACK);
ui.painter().rect_stroke(boxes, 0.0, Stroke::new(1.0, t.field_border), StrokeKind::Outside);
- let resp = ui.interact(bar, ui.id().with("color-panel-ramp"), Sense::click_and_drag());
+ let resp = ui.interact(bar, ramp_id, Sense::click_and_drag());
resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Other, ui.is_enabled(), "Color Ramp"));
let mods = ui.input(|i| i.modifiers);
let target = cp.background != mods.alt;
@@ -643,15 +652,21 @@ fn triangle_point([hue, white, black]: [Pos2; 3], s: f32, b: f32) -> Pos2 {
black + (white - black) * b + (hue - white) * (b * s)
}
-/// Saturation and brightness at `p`, clamped into the triangle; `None` for a degenerate one.
-fn triangle_sv([hue, white, black]: [Pos2; 3], p: Pos2) -> Option<(f32, f32)> {
+/// Saturation and brightness at `p`, clamped into the triangle; `None` for a degenerate one or a
+/// non-finite `p`.
+pub(crate) fn triangle_sv([hue, white, black]: [Pos2; 3], p: Pos2) -> Option<(f32, f32)> {
let (a, c, d) = (white - black, hue - white, p - black);
let det = a.x * c.y - a.y * c.x;
if det.abs() < 1e-6 {
return None;
}
- let b = ((d.x * c.y - d.y * c.x) / det).clamp(0.0, 1.0);
- let bs = ((a.x * d.y - a.y * d.x) / det).clamp(0.0, b);
+ let (b, bs) = ((d.x * c.y - d.y * c.x) / det, (a.x * d.y - a.y * d.x) / det);
+ // A NaN `b` would panic as a clamp bound.
+ if !b.is_finite() || !bs.is_finite() {
+ return None;
+ }
+ let b = b.clamp(0.0, 1.0);
+ let bs = bs.clamp(0.0, b);
Some((if b > 0.0 { bs / b } else { 0.0 }, b))
}