Hue/Saturation range bar: handle reach counts neighbours across the seam

CodeRabbit (discussion_r4369244320): Reds' fully-on zone crosses the bar's
seam, so its two seam handles saw no neighbour on their open side and kept
the full 10-point reach, swallowing the whole ~19-point zone on a 230-point
bar. The bar is a hue circle: the gap now wraps to the first handle from
the other edge.

Test: hue_range_zones_stay_selectable_on_a_narrow_bar (a Reds case).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Hector VillarrealandClaude Opus 5.5 committed 2026-11-05 22:53:19 -06:00
1 parent f2dcbee218
commit fe19df6077
2 files changed
+20 -12

No files matched your search

+11 -4
View File
@@ -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]
@@ -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<u8> {
/// 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<u8> {
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()