Fixed mirrored crosshair color picker
Moorly CI / linux-build (push) Successful in 5m4s
Moorly CI / windows-build (push) Successful in 6m14s

This commit is contained in:
Squid committed 2026-11-17 20:33:25 +01:00
1 parent 66bafe0524
commit 403cdf8174
2 files changed
+123 -18

No files matched your search

+1
View File
@@ -165,6 +165,7 @@ impl State {
/// Resets the color of the state. /// Resets the color of the state.
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.overlay_state.color = Color::from_rgb(0.5, 0.25, 0.25); self.overlay_state.color = Color::from_rgb(0.5, 0.25, 0.25);
self.overlay_state.sync_hue();
self.overlay_state.color_bar_dragged = ColorBarDragged::None; self.overlay_state.color_bar_dragged = ColorBarDragged::None;
} }
+122 -18
View File
@@ -43,6 +43,25 @@ const HUE_STEP: i32 = 1;
/// The step value of the keyboard change of the RGBA color values. /// The step value of the keyboard change of the RGBA color values.
const RGBA_STEP: i16 = 1; const RGBA_STEP: i16 = 1;
/// Converts a vertical offset inside the saturation/value map to an HSV value.
/// The map is bright (value 1.0) at the top and black (0.0) at the bottom.
fn value_from_offset(offset: f32, height: f32) -> f32 {
1.0 - (offset / height).clamp(0.0, 1.0)
}
/// Converts an HSV value to a vertical offset inside the saturation/value map.
fn offset_from_value(value: f32) -> f32 {
1.0 - value.clamp(0.0, 1.0)
}
/// Converts a horizontal offset inside the hue bar to a hue in `0..360`.
///
/// The upper bound stays below 360 so the right end does not wrap back to the
/// same red as the left end and make the marker jump across the bar.
fn hue_from_offset(offset: f32, width: f32) -> u16 {
((offset / width).clamp(0.0, 1.0) * 360.0).min(359.0) as u16
}
/// The overlay of the [`ColorPicker`](crate::widget::ColorPicker). /// The overlay of the [`ColorPicker`](crate::widget::ColorPicker).
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct ColorPickerOverlay<'a, 'b, Message, Theme> pub struct ColorPickerOverlay<'a, 'b, Message, Theme>
@@ -139,7 +158,7 @@ where
) -> event::Status { ) -> event::Status {
let mut hsv_color_children = layout.children(); let mut hsv_color_children = layout.children();
let hsv_color: Hsv = self.state.color.into(); let hsv_color: Hsv = self.state.hsv();
let mut color_changed = false; let mut color_changed = false;
let sat_value_bounds = hsv_color_children let sat_value_bounds = hsv_color_children
@@ -193,11 +212,11 @@ where
}; };
let calc_percentage_value = |position: Point| { let calc_percentage_value = |position: Point| {
((position.y - sat_value_bounds.y) / sat_value_bounds.height).clamp(0.0, 1.0) value_from_offset(position.y - sat_value_bounds.y, sat_value_bounds.height)
}; };
let calc_hue = |position: Point| { let calc_hue = |position: Point| {
(((position.x - hue_bounds.x) / hue_bounds.width).clamp(0.0, 1.0) * 360.0) as u16 hue_from_offset(position.x - hue_bounds.x, hue_bounds.width)
}; };
match self.state.color_bar_dragged { match self.state.color_bar_dragged {
@@ -220,10 +239,12 @@ where
color_changed = true; color_changed = true;
} }
ColorBarDragged::Hue => { ColorBarDragged::Hue => {
let hue = cursor.position().map(calc_hue).unwrap_or_default();
self.state.hue = hue;
self.state.color = Color { self.state.color = Color {
a: self.state.color.a, a: self.state.color.a,
..Hsv { ..Hsv {
hue: cursor.position().map(calc_hue).unwrap_or_default(), hue,
..hsv_color ..hsv_color
} }
.into() .into()
@@ -408,6 +429,7 @@ where
} }
if color_changed { if color_changed {
self.state.sync_hue();
// Call on_color_change callback for real-time updates // Call on_color_change callback for real-time updates
if let Some(on_color_change) = self.on_color_change { if let Some(on_color_change) = self.on_color_change {
shell.publish(on_color_change(self.state.color)); shell.publish(on_color_change(self.state.color));
@@ -521,6 +543,7 @@ where
if let Some(color) = parse_hex_color(&self.state.hex_text) { if let Some(color) = parse_hex_color(&self.state.hex_text) {
self.state.color = color; self.state.color = color;
self.state.sync_hue();
self.clear_cache(); self.clear_cache();
if let Some(on_color_change) = self.on_color_change { if let Some(on_color_change) = self.on_color_change {
@@ -564,8 +587,8 @@ where
// TODO: maybe place this better // TODO: maybe place this better
self.clear_cache(); self.clear_cache();
} else { } else {
let sat_value_handle = |key_code: &keyboard::Key, color: &mut Color| { let sat_value_handle = |key_code: &keyboard::Key, state: &mut State| {
let mut hsv_color: Hsv = (*color).into(); let mut hsv_color: Hsv = state.hsv();
let mut status = event::Status::Ignored; let mut status = event::Status::Ignored;
match key_code { match key_code {
@@ -578,11 +601,11 @@ where
status = event::Status::Captured; status = event::Status::Captured;
} }
keyboard::Key::Named(keyboard::key::Named::ArrowUp) => { keyboard::Key::Named(keyboard::key::Named::ArrowUp) => {
hsv_color.value -= SAT_VALUE_STEP; hsv_color.value += SAT_VALUE_STEP;
status = event::Status::Captured; status = event::Status::Captured;
} }
keyboard::Key::Named(keyboard::key::Named::ArrowDown) => { keyboard::Key::Named(keyboard::key::Named::ArrowDown) => {
hsv_color.value += SAT_VALUE_STEP; hsv_color.value -= SAT_VALUE_STEP;
status = event::Status::Captured; status = event::Status::Captured;
} }
_ => {} _ => {}
@@ -591,15 +614,15 @@ where
hsv_color.saturation = hsv_color.saturation.clamp(0.0, 1.0); hsv_color.saturation = hsv_color.saturation.clamp(0.0, 1.0);
hsv_color.value = hsv_color.value.clamp(0.0, 1.0); hsv_color.value = hsv_color.value.clamp(0.0, 1.0);
*color = Color { state.color = Color {
a: color.a, a: state.color.a,
..hsv_color.into() ..hsv_color.into()
}; };
status status
}; };
let hue_handle = |key_code: &keyboard::Key, color: &mut Color| { let hue_handle = |key_code: &keyboard::Key, state: &mut State| {
let mut hsv_color: Hsv = (*color).into(); let mut hsv_color: Hsv = state.hsv();
let mut status = event::Status::Ignored; let mut status = event::Status::Ignored;
let mut value = i32::from(hsv_color.hue); let mut value = i32::from(hsv_color.hue);
@@ -621,9 +644,10 @@ where
} }
hsv_color.hue = value.rem_euclid(360) as u16; hsv_color.hue = value.rem_euclid(360) as u16;
state.hue = hsv_color.hue;
*color = Color { state.color = Color {
a: color.a, a: state.color.a,
..hsv_color.into() ..hsv_color.into()
}; };
@@ -655,8 +679,8 @@ where
}; };
match self.state.focus { match self.state.focus {
Focus::SatValue => status = sat_value_handle(key, &mut self.state.color), Focus::SatValue => status = sat_value_handle(key, &mut self.state),
Focus::Hue => status = hue_handle(key, &mut self.state.color), Focus::Hue => status = hue_handle(key, &mut self.state),
Focus::Red => status = rgba_bar_handle(key, &mut self.state.color.r), Focus::Red => status = rgba_bar_handle(key, &mut self.state.color.r),
Focus::Green => status = rgba_bar_handle(key, &mut self.state.color.g), Focus::Green => status = rgba_bar_handle(key, &mut self.state.color.g),
Focus::Blue => status = rgba_bar_handle(key, &mut self.state.color.b), Focus::Blue => status = rgba_bar_handle(key, &mut self.state.color.b),
@@ -664,6 +688,15 @@ where
_ => {} _ => {}
} }
if status == event::Status::Captured
&& matches!(
self.state.focus,
Focus::Red | Focus::Green | Focus::Blue | Focus::Alpha
)
{
self.state.sync_hue();
}
// If color changed via keyboard, call on_color_change callback // If color changed via keyboard, call on_color_change callback
if status == event::Status::Captured if status == event::Status::Captured
&& let Some(on_color_change) = self.on_color_change && let Some(on_color_change) = self.on_color_change
@@ -1515,7 +1548,7 @@ fn hsv_color<Message, Theme>(
Theme: style::color_picker::Catalog + iced_widget::button::Catalog + iced_widget::text::Catalog, Theme: style::color_picker::Catalog + iced_widget::button::Catalog + iced_widget::text::Catalog,
{ {
let mut hsv_color_children = layout.children(); let mut hsv_color_children = layout.children();
let hsv_color: Hsv = color_picker.state.color.into(); let hsv_color: Hsv = color_picker.state.hsv();
let sat_value_layout = hsv_color_children let sat_value_layout = hsv_color_children
.next() .next()
@@ -1566,7 +1599,7 @@ fn hsv_color<Message, Theme>(
let inset = 5.0; let inset = 5.0;
let saturation = (hsv_color.saturation * sat_value_bounds.width) let saturation = (hsv_color.saturation * sat_value_bounds.width)
.clamp(inset, (sat_value_bounds.width - inset).max(inset)); .clamp(inset, (sat_value_bounds.width - inset).max(inset));
let value = (hsv_color.value * sat_value_bounds.height) let value = (offset_from_value(hsv_color.value) * sat_value_bounds.height)
.clamp(inset, (sat_value_bounds.height - inset).max(inset)); .clamp(inset, (sat_value_bounds.height - inset).max(inset));
let crosshair_color = Color::from(Hsv { let crosshair_color = Color::from(Hsv {
hue: 0, hue: 0,
@@ -2163,6 +2196,12 @@ pub struct State {
pub(crate) color: Color, pub(crate) color: Color,
/// The color used to initialize [`ColorPickerOverlay`]. /// The color used to initialize [`ColorPickerOverlay`].
pub(crate) initial_color: Color, pub(crate) initial_color: Color,
/// The last hue that was actually visible.
///
/// Kept separately because the hue of a grayscale or black color is
/// undefined; deriving it from the RGB color would snap the hue marker to
/// red whenever the map is moved to a fully desaturated or dark area.
pub(crate) hue: u16,
/// The cache of the hue canvas of the [`ColorPickerOverlay`]. /// The cache of the hue canvas of the [`ColorPickerOverlay`].
pub(crate) hue_canvas_cache: canvas::Cache, pub(crate) hue_canvas_cache: canvas::Cache,
/// The dragged color bar of the [`ColorPickerOverlay`]. /// The dragged color bar of the [`ColorPickerOverlay`].
@@ -2188,12 +2227,31 @@ impl State {
Self { Self {
color, color,
initial_color: color, initial_color: color,
hue: Hsv::from(color).hue,
hex_text: color.as_hex_string(), hex_text: color.as_hex_string(),
hex_caret: color.as_hex_string().chars().count(), hex_caret: color.as_hex_string().chars().count(),
..Self::default() ..Self::default()
} }
} }
/// The current color as HSV, using the stored hue rather than deriving it
/// from the RGB color.
///
/// Re-deriving it drifts as saturation drops toward white, where tiny RGB
/// differences make the computed hue wander and move the hue marker.
fn hsv(&self) -> Hsv {
let rgb = Hsv::from(self.color);
Hsv::from_hsv(self.hue, rgb.saturation, rgb.value)
}
/// Remember the hue of the current color if it has one.
pub(crate) fn sync_hue(&mut self) {
let hsv = Hsv::from(self.color);
if hsv.saturation > 0.0 && hsv.value > 0.0 {
self.hue = hsv.hue;
}
}
/// Reset cached canvas when internal state is modified. /// Reset cached canvas when internal state is modified.
/// ///
/// If the color has changed, empty the hue canvas cache /// If the color has changed, empty the hue canvas cache
@@ -2281,6 +2339,7 @@ impl State {
self.hex_caret = self.hex_text.chars().count(); self.hex_caret = self.hex_text.chars().count();
self.hex_selection = None; self.hex_selection = None;
self.hex_drag_anchor = None; self.hex_drag_anchor = None;
self.sync_hue();
self.clear_cache(); self.clear_cache();
} }
} }
@@ -2291,6 +2350,7 @@ impl Default for State {
Self { Self {
color: default_color, color: default_color,
initial_color: default_color, initial_color: default_color,
hue: Hsv::from(default_color).hue,
hue_canvas_cache: canvas::Cache::default(), hue_canvas_cache: canvas::Cache::default(),
color_bar_dragged: ColorBarDragged::None, color_bar_dragged: ColorBarDragged::None,
focus: Focus::default(), focus: Focus::default(),
@@ -2552,6 +2612,50 @@ mod tests {
assert_eq!(position, Point::new(100.0, 500.0)); assert_eq!(position, Point::new(100.0, 500.0));
} }
#[test]
fn sat_value_map_is_bright_at_the_top() {
assert_eq!(value_from_offset(0.0, 100.0), 1.0);
assert_eq!(value_from_offset(100.0, 100.0), 0.0);
assert_eq!(offset_from_value(1.0), 0.0);
assert_eq!(offset_from_value(0.0), 1.0);
}
#[test]
fn hue_bar_does_not_wrap_at_the_right_end() {
assert_eq!(hue_from_offset(0.0, 100.0), 0);
assert_eq!(hue_from_offset(50.0, 100.0), 180);
assert_eq!(hue_from_offset(100.0, 100.0), 359);
}
#[test]
fn saturation_changes_keep_the_hue() {
let mut state = State::new(Color::from_rgb(0.0, 0.0, 1.0));
// Near-white blue has almost no chroma; hue must stay put instead of
// being re-derived from the RGB color.
state.color = Color::from(Hsv::from_hsv(240, 0.001, 1.0));
assert_eq!(state.hsv().hue, 240);
state.color = Color::WHITE;
assert_eq!(state.hsv().hue, 240);
}
#[test]
fn hue_survives_desaturated_and_black_colors() {
let mut state = State::new(Color::from_rgb(0.0, 0.0, 1.0));
assert_eq!(state.hsv().hue, 240);
state.color = Color::WHITE;
assert_eq!(state.hsv().hue, 240);
state.color = Color::BLACK;
assert_eq!(state.hsv().hue, 240);
state.color = Color::from_rgb(0.0, 1.0, 0.0);
state.sync_hue();
assert_eq!(state.hsv().hue, 120);
}
#[test] #[test]
fn node_extent_includes_children_outside_the_node() { fn node_extent_includes_children_outside_the_node() {
let child = Node::new(Size::new(50.0, 20.0)).move_to(Point::new(-10.0, 5.0)); let child = Node::new(Size::new(50.0, 20.0)).move_to(Point::new(-10.0, 5.0));