Skip to content

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

Integration: Color panel menu in custom workspaces, web-safe ramp pick (#6055)

David Mann690387911152
diff --git a/crates/ui-egui/src/color_panel_tests.rs b/crates/ui-egui/src/color_panel_tests.rs
index 5922fa900..7adc0183b 100644
--- a/crates/ui-egui/src/color_panel_tests.rs
+++ b/crates/ui-egui/src/color_panel_tests.rs
@@ -373,3 +373,36 @@ fn web_only_reads_its_old_snake_case_key() {
let new = serde_json::to_value(old).unwrap();
assert_eq!((new.get("webOnly"), new.get("web_only")), (Some(&json!(true)), None));
}
+
+/// Make Ramp Web Safe: a click picks the cell drawn under the pointer, even near its edge. On
+/// the Grayscale Ramp the fifth cell is drawn white, though its right edge rounds to CC.
+#[test]
+fn web_safe_ramp_picks_the_cell_it_draws() {
+ let mut h = color_tab(ThemeKind::Pro, ColorPanelMode::RgbSliders, [0.0, 0.0, 0.0, 1.0]);
+ h.state_mut().ui.color_panel.ramp = ColorRamp::GrayscaleRamp;
+ h.state_mut().ui.color_panel.web_safe_ramp = true;
+ h.run_steps(2);
+ let ramp: Rect = h.get_by_label("Color Ramp").rect();
+ press(&mut h, pos2(ramp.left() + ramp.width() * 4.95 / 48.0, ramp.center().y), Modifiers::NONE);
+ assert_eq!(bytes(h.state().session.tools.foreground), [255, 255, 255]);
+}
+
+/// A Web Color field takes two hex digits only: "+F" is not 0F.
+#[test]
+fn web_field_ignores_a_sign() {
+ let mut h = color_tab(ThemeKind::Pro, ColorPanelMode::WebColorSliders, [0.0, 0.0, 0.0, 1.0]);
+ type_into(&mut h, egui::accesskit::Role::TextInput, 0, "+F");
+ assert_eq!(bytes(h.state().session.tools.foreground), [0, 0, 0]);
+}
+
+/// A click beside the Color Wheel (its area spans the panel) leaves the colour alone.
+#[test]
+fn color_wheel_ignores_clicks_beside_it() {
+ let mut h = color_tab(ThemeKind::Pro, ColorPanelMode::ColorWheel, [0.0, 0.0, 1.0, 1.0]);
+ let area = h.get_by_label("Color Wheel").rect();
+ let (center, _, outer) = crate::color_panel_ui::wheel_geometry(area);
+ let corner = area.left_top() + vec2(3.0, 3.0);
+ assert!(corner.distance(center) > outer + 20.0, "the corner is beside the wheel");
+ press(&mut h, corner, Modifiers::NONE);
+ assert_eq!(bytes(h.state().session.tools.foreground), [0, 0, 255]);
+}
diff --git a/crates/ui-egui/src/color_panel_ui.rs b/crates/ui-egui/src/color_panel_ui.rs
index e015be052..60f9c728e 100644
--- a/crates/ui-egui/src/color_panel_ui.rs
+++ b/crates/ui-egui/src/color_panel_ui.rs
@@ -301,8 +301,10 @@ fn hex_pair(ui: &mut egui::Ui, id: egui::Id, value: &mut f32) -> bool {
} else {
ui.data_mut(|d| d.remove::<String>(id));
}
+ // Two hex digits only: `from_str_radix` would also take a sign ("+F").
if resp.changed()
&& text.len() == 2
+ && text.bytes().all(|b| b.is_ascii_hexdigit())
&& let Ok(b) = u8::from_str_radix(&text, 16)
{
*value = f32::from(b);
@@ -502,7 +504,13 @@ fn ramp(app: &mut PhotocraftApp, ui: &mut egui::Ui, height: f32) {
&& bar.width() > 0.0
&& bar.height() > 0.0
{
- let c = ramp_color(cp.ramp, cp.web_safe_ramp, fg, bg, (p.x - bar.left()) / bar.width(), (p.y - bar.top()) / bar.height());
+ let (mut x, mut y) = ((p.x - bar.left()) / bar.width(), (p.y - bar.top()) / bar.height());
+ if cp.web_safe_ramp {
+ // Pick the cell drawn under the pointer: web-safe cells show their centre's colour.
+ let centre = |f: f32, n: usize| ((f * n as f32).floor().clamp(0.0, (n - 1) as f32) + 0.5) / n as f32;
+ (x, y) = (centre(x, RAMP_NX), centre(y, RAMP_NY));
+ }
+ let c = ramp_color(cp.ramp, cp.web_safe_ramp, fg, bg, x, y);
set_color(app, target, c);
}
resp.context_menu(|ui| {
@@ -566,22 +574,32 @@ fn wheel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
{
// A click is where it landed; a drag goes by where its press began.
let origin = if resp.clicked() { Some(p) } else { ui.input(|i| i.pointer.press_origin()) };
- let on_ring = match origin {
+ // `None`: the press began beside the wheel (the area spans the panel), which picks nothing.
+ let part = match origin {
Some(origin) => {
- let on_ring = origin.distance(center) >= inner - 2.0;
- ui.data_mut(|d| d.insert_temp(drag_key, on_ring));
- on_ring
+ let d = origin.distance(center);
+ // The hue knob reaches past the ring's edge on a small wheel.
+ let part = (d <= outer + 7.0).then_some(d >= inner - 2.0);
+ ui.data_mut(|data| data.insert_temp(drag_key, part));
+ part
}
- None => ui.data(|d| d.get_temp::<bool>(drag_key)).unwrap_or(false),
+ None => ui.data(|d| d.get_temp::<Option<bool>>(drag_key)).unwrap_or(Some(false)),
};
- if on_ring {
- let a = (center.y - p.y).atan2(p.x - center.x).rem_euclid(TAU);
- v[0] = (a / TAU * 360.0).round().rem_euclid(360.0);
- } else if let Some((s2, b2)) = triangle_sv(tri, p) {
- v[1] = (s2 * 100.0).round();
- v[2] = (b2 * 100.0).round();
+ match part {
+ Some(true) => {
+ let a = (center.y - p.y).atan2(p.x - center.x).rem_euclid(TAU);
+ v[0] = (a / TAU * 360.0).round().rem_euclid(360.0);
+ changed = true;
+ }
+ Some(false) => {
+ if let Some((s2, b2)) = triangle_sv(tri, p) {
+ v[1] = (s2 * 100.0).round();
+ v[2] = (b2 * 100.0).round();
+ }
+ changed = true;
+ }
+ None => {}
}
- changed = true;
}
// Knobs: on the ring at the hue, in the triangle at the saturation and brightness.
let a = h * TAU;
@@ -724,10 +742,8 @@ fn ramp_menu(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
/// The Color panel's panel menu, Photoshop's: the modes, the copy commands, the ramps.
pub(crate) fn panel_menu(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
- for (i, mode) in ColorPanelMode::ALL.into_iter().enumerate() {
- if i == 3 {
- ui.separator();
- }
+ // One section, as Photoshop's: no separator between Color Wheel and Grayscale Slider.
+ for mode in ColorPanelMode::ALL {
if check_item(ui, app.ui.color_panel.mode == mode, mode.label()) {
app.ui.color_panel.mode = mode;
ui.close();
diff --git a/crates/ui-egui/src/panel_docking.rs b/crates/ui-egui/src/panel_docking.rs
index f27d1c2c8..ff093f688 100644
--- a/crates/ui-egui/src/panel_docking.rs
+++ b/crates/ui-egui/src/panel_docking.rs
@@ -855,6 +855,10 @@ fn panel_menu(app: &mut PhotocraftApp, ui: &mut egui::Ui, panel: &str) {
crate::swatches_ui::panel_menu(app, ui);
ui.separator();
}
+ "color" => {
+ crate::color_panel_ui::panel_menu(app, ui);
+ ui.separator();
+ }
"history" => {
crate::history_menu_ui::panel_menu(app, ui);
ui.separator();
diff --git a/crates/ui-egui/src/panel_docking_tests.rs b/crates/ui-egui/src/panel_docking_tests.rs
index 193ff2e6b..e710d9942 100644
--- a/crates/ui-egui/src/panel_docking_tests.rs
+++ b/crates/ui-egui/src/panel_docking_tests.rs
@@ -673,6 +673,26 @@ fn customized_panel_menus_and_rail_remain_reachable_after_float_and_restore() {
assert!(menu.query_all_by_label("New Swatch…").count() > 0);
}
+/// A floated Color panel's menu still picks its mode (#6000): the shared docking renderer has
+/// its own panel menu, separate from the legacy dock's.
+#[test]
+fn floated_color_panel_menu_picks_the_mode() {
+ let mut app = app();
+ app.run("window.panel.float", json!({"panel":"color","x":20,"y":40})).unwrap();
+ let mut h = harness(app, vec2(1280.0, 800.0));
+ let floating = h.ctx.memory(|memory| memory.area_rect(egui::Id::new("photocraft-panel-docking").with(("floating", &"color".to_string())))).unwrap();
+ let button = h.query_all_by_label("Panel menu").find(|node| floating.contains_rect(node.rect())).unwrap().rect().center();
+ h.hover_at(button);
+ for pressed in [true, false] {
+ h.event(Event::PointerButton { pos: button, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE });
+ h.run_steps(1);
+ }
+ h.run_steps(2);
+ h.get_by_label(" RGB Sliders").click();
+ h.run_steps(2);
+ assert_eq!(h.state().ui.color_panel.mode, crate::state::ColorPanelMode::RgbSliders);
+}
+
#[test]
fn custom_workspace_saves_shared_layout_and_timeline_without_embedding_flags() {
let mut app = app();