diff --git a/crates/ui-egui/src/dialogs/adjust_editors.rs b/crates/ui-egui/src/dialogs/adjust_editors.rs index 78ad692a8c..998aa2b4f4 100644 --- a/crates/ui-egui/src/dialogs/adjust_editors.rs +++ b/crates/ui-egui/src/dialogs/adjust_editors.rs @@ -271,14 +271,21 @@ mod tests { let w = 230.0; let xs = [75.0f32, 105.0, 135.0, 165.0].map(|d| d / 360.0 * w); for (a, z) in [(0, 1), (1, 2), (2, 3)] { - assert_eq!(hue_range_handle_at(xs, (xs[a] + xs[z]) / 2.0), None, "the middle of zone {a}..{z} is the zone's"); + assert_eq!(hue_range_handle_at(xs, (xs[a] + xs[z]) / 2.0, w), None, "the middle of zone {a}..{z} is the zone's"); } for (k, x) in xs.iter().enumerate() { - assert_eq!(hue_range_handle_at(xs, *x + 1.0), Some(k as u8), "handle {k}"); + assert_eq!(hue_range_handle_at(xs, *x + 1.0, w), Some(k as u8), "handle {k}"); } - assert_eq!(hue_range_handle_at(xs, xs[0] - 9.0), Some(0), "the full reach outside the range"); + assert_eq!(hue_range_handle_at(xs, xs[0] - 9.0, w), Some(0), "the full reach outside the range"); let stacked = [50.0, 50.0, 80.0, 120.0]; - assert!(hue_range_handle_at(stacked, 51.0).is_some(), "coinciding handles stay grabbable"); + assert!(hue_range_handle_at(stacked, 51.0, w).is_some(), "coinciding handles stay grabbable"); + // Reds (315/345…15\45): its fully-on zone crosses the seam, ~9.6 points from each edge. + let reds = [315.0f32, 345.0, 15.0, 45.0].map(|d| d / 360.0 * w); + for x in [w - 1.0, w - 3.0, 0.5, 2.5] { + assert_eq!(hue_range_handle_at(reds, x, w), None, "x {x}: Reds' fully-on zone across the seam"); + } + assert_eq!(hue_range_handle_at(reds, reds[1] + 1.0, w), Some(1)); + assert_eq!(hue_range_handle_at(reds, reds[2] - 1.0, w), Some(2)); } #[test] diff --git a/crates/ui-egui/src/dialogs/adjust_editors/hue_saturation.rs b/crates/ui-egui/src/dialogs/adjust_editors/hue_saturation.rs index 27221ebb43..777ccf4a39 100644 --- a/crates/ui-egui/src/dialogs/adjust_editors/hue_saturation.rs +++ b/crates/ui-egui/src/dialogs/adjust_editors/hue_saturation.rs @@ -38,17 +38,18 @@ pub(super) fn move_hue_range_handle(bounds: [f32; 4], k: usize, deg: f32) -> [f3 b } -/// The range handle a press at `x` grabs, given the four handles' x positions: the nearest -/// within 10 points, or within a third of the gap to the next handle on that side, so the middle -/// third of a narrow zone still drags the zone (at least 3 points, for handles that coincide). -pub(super) fn hue_range_handle_at(xs: [f32; 4], x: f32) -> Option { +/// The range handle a press at `x` grabs, given the four handles' x positions on a bar `w` wide: +/// the nearest within 10 points, or within a third of the gap to the next handle on that side, so +/// the middle third of a narrow zone still drags the zone (at least 3 points, for handles that +/// coincide). The bar is a hue circle: the next handle past an edge is the first from the other. +pub(super) fn hue_range_handle_at(xs: [f32; 4], x: f32, w: f32) -> Option { let k = nearest(&xs, x)?; - let d = x - xs[k as usize]; + let (xk, d) = (xs[k as usize], x - xs[k as usize]); let gap = xs .iter() .enumerate() - .filter(|&(j, &o)| j != k as usize && (o - xs[k as usize]) * d.signum() >= 0.0) - .map(|(_, &o)| (o - xs[k as usize]).abs()) + .filter(|&(j, _)| j != k as usize) + .map(|(_, &o)| if d >= 0.0 { (o - xk).rem_euclid(w) } else { (xk - o).rem_euclid(w) }) .fold(f32::INFINITY, f32::min); (d.abs() < (gap / 3.0).clamp(3.0, 10.0)).then_some(k) } @@ -242,7 +243,7 @@ pub(super) fn hue_saturation(ui: &mut egui::Ui, v: &mut Value, cx: &EditorCx) -> && ui.input(|i| i.pointer.primary_pressed()) && let Some(pos) = ui.input(|i| i.pointer.press_origin()) { - which = hue_range_handle_at(b.map(xof), pos.x); + which = hue_range_handle_at(b.map(xof), pos.x, w); zone = if which.is_none() { hue_range_zone(b, deg_of(pos.x)).map(|z| (z, b, pos.x)) } else { None }; } if bresp.is_pointer_button_down_on()