DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Color Picker: InDesign's layout at the app's density, its own OK / Cancel / Add CMYK Swatch at the top right
moppeeer7cf1e30acb7b
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rsindex 262a48b60..6e8fe34b5 100644--- a/crates/ui-egui/src/dialogs.rs+++ b/crates/ui-egui/src/dialogs.rs@@ -1538,6 +1538,7 @@ pub fn show(app: &mut DesignApp, ctx: &egui::Context) {let preferred_width: f32 = match d.id.as_str() {"alert" => 440.0,"closeDocument" | "updateLink" => 380.0,+ "colorPicker" => 580.0,"newDocument" if crate::i18n::is_rtl(&app.ui.language) => 380.0,_ => 640.0,};@@ -2201,6 +2202,15 @@ pub fn show(app: &mut DesignApp, ctx: &egui::Context) {}_ => {}});+ // The Color Picker has its own OK and Cancel, at the top right as InDesign's.+ if d.id == "colorPicker" {+ match d.fields.remove("_close").as_ref().and_then(Value::as_str) {+ Some("ok") => result = Some(true),+ Some("cancel") => result = Some(false),+ _ => {}+ }+ return;+ }ui.add_space(12.0);let ok_label = match d.id.as_str() {"closeDocument" => " Save ",@@ -4899,10 +4909,63 @@ fn store_picked(d: &mut Dialog, p: &Picked) {d.fields.insert("_lab".into(), json!(p.lab));}-/// The Color Picker, laid out as InDesign's: the field of two components and the slider of the-/// third (the one whose radio button is on, R at first), the new colour over the one it replaces,-/// H/S/B, L/a/b, R/G/B and C/M/Y/K values, the hex code, and Add CMYK Swatch.+/// The Color Picker's place in the dialog, as InDesign's (points from the body's top-left).+mod picker_layout {+ use egui::{Rect, pos2, vec2};+ // InDesign's proportions at this app's density (its 30 px fields are 21 pt here).+ pub const SIZE: egui::Vec2 = vec2(566.0, 334.0);+ pub fn field() -> Rect {+ Rect::from_min_size(pos2(2.0, 31.0), vec2(265.0, 287.0))+ }+ pub fn slider() -> Rect {+ Rect::from_min_size(pos2(285.0, 32.0), vec2(19.0, 282.0))+ }+ pub fn swatch() -> Rect {+ Rect::from_min_size(pos2(319.0, 0.0), vec2(71.0, 90.0))+ }+ /// OK, Cancel, Add … Swatch.+ pub fn button(i: usize) -> Rect {+ Rect::from_min_size(pos2(433.0, [0.0, 39.0, 78.0][i.min(2)]), vec2(132.0, 25.0))+ }+ /// Row centres: H/L, S/a, B/b, (gap), R/C, G/M, B/Y, K, #.+ pub const ROWS: [f32; 8] = [129.0, 157.0, 185.0, 224.0, 252.0, 280.0, 308.0, 320.0];+ /// The radio buttons' columns.+ pub const LEFT: f32 = 326.0;+ pub const RIGHT: f32 = 457.0;+}++/// A value field: the text typed stays while it has the focus; a number that parses applies.+fn picker_field(ui: &mut egui::Ui, d: &mut Dialog, r: egui::Rect, key: &str, value: f32, range: (f32, f32)) -> Option<f32> {+ let id = egui::Id::new(("color_picker_field", key));+ let editing = ui.memory(|m| m.has_focus(id));+ let shown = format!("{}", value.round() as i64);+ let edit_key = format!("_edit_{key}");+ let mut text = if editing { d.fields.get(&edit_key).and_then(Value::as_str).map(str::to_string).unwrap_or(shown) } else { shown };+ let resp = ui.put(+ r,+ egui::TextEdit::singleline(&mut text)+ .id(id)+ .font(egui::FontId::proportional(11.5))+ .vertical_align(egui::Align::Center)+ .margin(egui::Margin::symmetric(4, 2)),+ );+ let mut out = None;+ if resp.changed()+ && let Ok(v) = text.trim().parse::<f32>()+ && v.is_finite()+ {+ out = Some(v.clamp(range.0, range.1));+ }+ d.fields.insert(edit_key, json!(text));+ out+}++/// The Color Picker, laid out as InDesign's: the colour space view (a field of two components,+/// a slider for the third: the one whose radio button is on, R at first), the new colour over the+/// one it replaces, OK / Cancel / Add CMYK Swatch at the top right, H/S/B beside L/a/b, R/G/B+/// beside C/M/Y/K, and the hex code. Its buttons replace the dialog's own.fn color_picker(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialog) {+ use picker_layout as at;let lang = app.ui.language.clone();let tr = |s: &str| crate::i18n::tr(&lang, s).to_string();if !d.fields.contains_key("old") {@@ -4913,151 +4976,196 @@ fn color_picker(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialog) {let mut p = picked_of(d);let before = p;let t = crate::theme::Tokens::get(ui.ctx());- ui.horizontal_top(|ui| {- // The field and the slider.- ui.vertical(|ui| {- crate::rtl::label(ui, egui::RichText::new(tr(if comp.lab() { "Lab Color Space View" } else { "RGB Color Space View" })).size(11.5));- ui.add_space(4.0);- ui.horizontal_top(|ui| {- let (fr, fresp) = ui.allocate_exact_size(egui::vec2(190.0, 200.0), egui::Sense::click_and_drag());- let (tv, _, _) = p.coords(comp);- let cells = if matches!(comp, PickComp::R | PickComp::G | PickComp::Bl | PickComp::H) { 2 } else { 24 };- gradient_mesh(ui.painter(), fr, cells, cells, |x, y| p.at(comp, tv, x, y).rgb);- ui.painter().rect_stroke(fr, 0.0, egui::Stroke::new(1.0, t.field_border), egui::StrokeKind::Outside);- fresp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Other, true, "Color field"));- if let Some(pos) = fresp.interact_pointer_pos().filter(|_| fresp.is_pointer_button_down_on() || fresp.clicked()) {- let u = ((pos.x - fr.left()) / fr.width()).clamp(0.0, 1.0);- let v = ((fr.bottom() - pos.y) / fr.height()).clamp(0.0, 1.0);- p = p.at(comp, tv, u, v);- }- let (_, u, v) = p.coords(comp);- let mark = egui::pos2(fr.left() + u * fr.width(), fr.bottom() - v * fr.height());- ui.painter().circle_stroke(mark, 5.0, egui::Stroke::new(1.5, egui::Color32::WHITE));- ui.painter().circle_stroke(mark, 6.0, egui::Stroke::new(1.0, egui::Color32::BLACK));- ui.add_space(6.0);- let (sr, sresp) = ui.allocate_exact_size(egui::vec2(14.0, 200.0), egui::Sense::click_and_drag());- gradient_mesh(ui.painter(), sr, 1, 48, |_, y| p.slider_at(comp, y));- ui.painter().rect_stroke(sr, 0.0, egui::Stroke::new(1.0, t.field_border), egui::StrokeKind::Outside);- sresp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Slider, true, "Color slider"));- if let Some(pos) = sresp.interact_pointer_pos().filter(|_| sresp.is_pointer_button_down_on() || sresp.clicked()) {- let tn = ((sr.bottom() - pos.y) / sr.height()).clamp(0.0, 1.0);- let (_, u, v) = p.coords(comp);- p = p.at(comp, tn, u, v);- }- let (tn, _, _) = p.coords(comp);- let y = sr.bottom() - tn * sr.height();- for (x, dir) in [(sr.left() - 1.0, 1.0), (sr.right() + 1.0, -1.0)] {- ui.painter().add(egui::Shape::convex_polygon(- vec![egui::pos2(x, y), egui::pos2(x - 5.0 * dir, y - 4.0), egui::pos2(x - 5.0 * dir, y + 4.0)],- t.text_strong,- egui::Stroke::NONE,- ));- }- });- });- ui.add_space(10.0);- ui.vertical(|ui| {- // The new colour over the one it replaces.- let (pr, _) = ui.allocate_exact_size(egui::vec2(60.0, 60.0), egui::Sense::hover());- let old = designcraft_color::Color::from_hex(&d.s("old")).map_or([0.0; 3], |c| c.to_rgb_uncalibrated());- ui.painter().rect_filled(egui::Rect::from_min_max(pr.min, egui::pos2(pr.max.x, pr.center().y)), 0.0, color32(p.rgb));- ui.painter().rect_filled(egui::Rect::from_min_max(egui::pos2(pr.min.x, pr.center().y), pr.max), 0.0, color32(old));- ui.painter().rect_stroke(pr, 0.0, egui::Stroke::new(1.0, t.field_border), egui::StrokeKind::Outside);- ui.add_space(10.0);- let mut comp_now = comp;- let mut edited: Option<Picked> = None;- let [cc, cm, cy, ck] = designcraft_color::Color::Rgb { r: p.rgb[0], g: p.rgb[1], b: p.rgb[2] }- .to_cmyk_managed(designcraft_color::cms::active_settings().intent)- .map(|v| v * 100.0);- let mut cmyk = [cc, cm, cy, ck];- egui::Grid::new("cp_values").num_columns(6).spacing([4.0, 4.0]).show(ui, |ui| {- let mut field =- |ui: &mut egui::Ui, c: Option<PickComp>, label: &str, v: &mut f32, range: std::ops::RangeInclusive<f32>, suffix: &str| -> bool {- match c {- Some(c) => {- ui.radio_value(&mut comp_now, c, "");- }- None => {- ui.label("");- }- }- ui.label(label);- ui.add(egui::DragValue::new(v).range(range).max_decimals(0).suffix(suffix)).changed()- };- let (mut h, mut s, mut b) = (p.hsb[0], p.hsb[1] * 100.0, p.hsb[2] * 100.0);- let (mut l, mut la, mut lb) = (p.lab[0], p.lab[1], p.lab[2]);- let (mut r, mut g, mut bl) = (p.rgb[0] * 255.0, p.rgb[1] * 255.0, p.rgb[2] * 255.0);- // Rows as InDesign's: H S B beside L a b, R G B beside C M Y K.- let hsb_changed =- field(ui, Some(PickComp::H), "H:", &mut h, 0.0..=360.0, "°") | field(ui, Some(PickComp::L), "L:", &mut l, 0.0..=100.0, "");- ui.end_row();- let hsb_changed2 =- field(ui, Some(PickComp::S), "S:", &mut s, 0.0..=100.0, "%") | field(ui, Some(PickComp::A), "a:", &mut la, -128.0..=127.0, "");- ui.end_row();- let hsb_changed3 =- field(ui, Some(PickComp::Br), "B:", &mut b, 0.0..=100.0, "%") | field(ui, Some(PickComp::Lb), "b:", &mut lb, -128.0..=127.0, "");- ui.end_row();- if hsb_changed || hsb_changed2 || hsb_changed3 {- // Whichever space changed wins: HSB if its values moved, else Lab.- edited = Some(if (h, s, b) != (p.hsb[0], p.hsb[1] * 100.0, p.hsb[2] * 100.0) {- Picked::from_hsb([h, s / 100.0, b / 100.0])- } else {- Picked::from_lab([l, la, lb], p.hsb[0])- });- }- let rgb_changed = field(ui, Some(PickComp::R), "R:", &mut r, 0.0..=255.0, "");- let c1 = field(ui, None, "C:", &mut cmyk[0], 0.0..=100.0, "%");- ui.end_row();- let rgb_changed2 = field(ui, Some(PickComp::G), "G:", &mut g, 0.0..=255.0, "");- let c2 = field(ui, None, "M:", &mut cmyk[1], 0.0..=100.0, "%");- ui.end_row();- let rgb_changed3 = field(ui, Some(PickComp::Bl), "B:", &mut bl, 0.0..=255.0, "");- let c3 = field(ui, None, "Y:", &mut cmyk[2], 0.0..=100.0, "%");- ui.end_row();- ui.label("");- ui.label("");- ui.label("");- let c4 = field(ui, None, "K:", &mut cmyk[3], 0.0..=100.0, "%");- ui.end_row();- if rgb_changed || rgb_changed2 || rgb_changed3 {- edited = Some(Picked::from_rgb([r / 255.0, g / 255.0, bl / 255.0], p.hsb[0]));- }- if c1 || c2 || c3 || c4 {- let [c, m, y, k] = cmyk.map(|v| v / 100.0);- edited = Some(Picked::from_rgb(designcraft_color::Color::Cmyk { c, m, y, k }.to_rgb(), p.hsb[0]));- }- });- if let Some(e) = edited {- p = e;- }- if comp_now != comp {- d.fields.insert("component".into(), json!(comp_now.key()));+ let (area, _) = ui.allocate_exact_size(at::SIZE, egui::Sense::hover());+ let o = area.min.to_vec2();+ let put = |r: egui::Rect| r.translate(o);+ let font = egui::FontId::proportional(11.5);+ let text = |ui: &egui::Ui, pos: egui::Pos2, align: egui::Align2, s: &str| {+ ui.painter().text(pos, align, s, egui::FontId::proportional(11.5), t.text_strong);+ };+ // The colour space view (a label, so screen readers hear which).+ ui.scope_builder(+ egui::UiBuilder::new()+ .max_rect(egui::Rect::from_min_size(area.min, egui::vec2(260.0, 24.0)))+ .layout(egui::Layout::left_to_right(egui::Align::Center)),+ |ui| {+ ui.label(+ egui::RichText::new(tr(if comp.lab() { "Lab Color Space View" } else { "RGB Color Space View" })).size(11.5).color(t.text_strong),+ );+ },+ );+ let fr = put(at::field());+ let fresp = ui.interact(fr, ui.id().with("cp_field"), egui::Sense::click_and_drag());+ let (tv, _, _) = p.coords(comp);+ let cells = if matches!(comp, PickComp::R | PickComp::G | PickComp::Bl | PickComp::H) { 2 } else { 24 };+ gradient_mesh(ui.painter(), fr, cells, cells, |x, y| p.at(comp, tv, x, y).rgb);+ fresp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Other, true, "Color field"));+ if let Some(pos) = fresp.interact_pointer_pos().filter(|_| fresp.is_pointer_button_down_on() || fresp.clicked()) {+ let u = ((pos.x - fr.left()) / fr.width()).clamp(0.0, 1.0);+ let v = ((fr.bottom() - pos.y) / fr.height()).clamp(0.0, 1.0);+ p = p.at(comp, tv, u, v);+ }+ // The marker: a cross with an open centre, white over black.+ let (_, u, v) = p.coords(comp);+ let m = egui::pos2(fr.left() + u * fr.width(), fr.bottom() - v * fr.height());+ let painter = ui.painter().with_clip_rect(fr);+ for (dx, dy) in [(1.0, 0.0), (-1.0, 0.0), (0.0, 1.0), (0.0, -1.0)] {+ let (a, b) = (m + egui::vec2(dx * 3.0, dy * 3.0), m + egui::vec2(dx * 10.0, dy * 10.0));+ let side = egui::vec2(dy, dx) * 1.2;+ painter.line_segment([a + side, b + side], egui::Stroke::new(1.0, egui::Color32::BLACK));+ painter.line_segment([a - side, b - side], egui::Stroke::new(1.0, egui::Color32::BLACK));+ painter.line_segment([a, b], egui::Stroke::new(1.5, egui::Color32::WHITE));+ }+ // The slider, with a marker on each side.+ let sr = put(at::slider());+ let sresp = ui.interact(sr.expand2(egui::vec2(6.0, 0.0)), ui.id().with("cp_slider"), egui::Sense::click_and_drag());+ gradient_mesh(ui.painter(), sr, 1, 48, |_, y| p.slider_at(comp, y));+ sresp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Slider, true, "Color slider"));+ if let Some(pos) = sresp.interact_pointer_pos().filter(|_| sresp.is_pointer_button_down_on() || sresp.clicked()) {+ let tn = ((sr.bottom() - pos.y) / sr.height()).clamp(0.0, 1.0);+ let (_, u, v) = p.coords(comp);+ p = p.at(comp, tn, u, v);+ }+ let (tn, _, _) = p.coords(comp);+ let y = sr.bottom() - tn * sr.height();+ for (x, dir) in [(sr.left(), 1.0), (sr.right(), -1.0)] {+ let pts = vec![+ egui::pos2(x - 5.0 * dir, y - 4.5),+ egui::pos2(x, y - 4.5),+ egui::pos2(x + 3.5 * dir, y),+ egui::pos2(x, y + 4.5),+ egui::pos2(x - 5.0 * dir, y + 4.5),+ ];+ ui.painter().add(egui::Shape::convex_polygon(pts, egui::Color32::WHITE, egui::Stroke::new(1.0, egui::Color32::from_gray(40))));+ }+ // The new colour over the one it replaces.+ let pr = put(at::swatch());+ let old = designcraft_color::Color::from_hex(&d.s("old")).map_or([0.0; 3], |c| c.to_rgb_uncalibrated());+ ui.painter().rect_filled(egui::Rect::from_min_max(pr.min, egui::pos2(pr.max.x, pr.center().y)), 0.0, color32(p.rgb));+ ui.painter().rect_filled(egui::Rect::from_min_max(egui::pos2(pr.min.x, pr.center().y), pr.max), 0.0, color32(old));+ ui.painter().rect_stroke(pr, 0.0, egui::Stroke::new(1.0, egui::Color32::from_gray(80)), egui::StrokeKind::Inside);+ // OK, Cancel, Add CMYK Swatch: rounded, OK filled.+ let pill = |ui: &mut egui::Ui, i: usize, label: &str, primary: bool| -> bool {+ let r = put(at::button(i));+ let resp = ui.interact(r, ui.id().with(("cp_button", i)), egui::Sense::click());+ let (fill, fg, stroke) = if primary {+ (t.accent_strong, egui::Color32::WHITE, egui::Stroke::NONE)+ } else {+ (if resp.hovered() { t.hover } else { egui::Color32::TRANSPARENT }, t.text_strong, egui::Stroke::new(1.0, t.text_dim))+ };+ ui.painter().rect(r, r.height() / 2.0, fill, stroke, egui::StrokeKind::Inside);+ ui.painter().text(r.center(), egui::Align2::CENTER_CENTER, label, egui::FontId::proportional(12.0), fg);+ resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, label));+ resp.clicked()+ };+ if pill(ui, 0, &tr("OK"), true) || ui.input(|i| i.key_pressed(egui::Key::Enter)) {+ d.fields.insert("_close".into(), json!("ok"));+ }+ if pill(ui, 1, &tr("Cancel"), false) || ui.input(|i| i.key_pressed(egui::Key::Escape)) {+ d.fields.insert("_close".into(), json!("cancel"));+ }+ if pill(ui, 2, &tr("Add CMYK Swatch"), false) {+ let [c, m, y, k] = designcraft_color::Color::Rgb { r: p.rgb[0], g: p.rgb[1], b: p.rgb[2] }+ .to_cmyk_managed(designcraft_color::cms::active_settings().intent)+ .map(|v| (v * 100.0).round());+ match app.run("swatch.create", json!({"color": {"c": c, "m": m, "y": y, "k": k}})) {+ Ok(r) => app.status(format!("{} {}", tr("Added swatch"), r["name"].as_str().unwrap_or(""))),+ Err(e) => app.status(e),+ }+ }+ // The values: a radio button, a label, the field, a unit.+ let mut comp_now = comp;+ let mut edited: Option<Picked> = None;+ let cmyk = designcraft_color::Color::Rgb { r: p.rgb[0], g: p.rgb[1], b: p.rgb[2] }+ .to_cmyk_managed(designcraft_color::cms::active_settings().intent)+ .map(|v| v * 100.0);+ // (column x of the radio, row, component, label, key, value, range, unit)+ let rows: [(f32, usize, Option<PickComp>, &str, &str, f32, (f32, f32), &str); 13] = [+ (at::LEFT, 0, Some(PickComp::H), "H:", "h", p.hsb[0], (0.0, 360.0), "°"),+ (at::LEFT, 1, Some(PickComp::S), "S:", "s", p.hsb[1] * 100.0, (0.0, 100.0), "%"),+ (at::LEFT, 2, Some(PickComp::Br), "B:", "b", p.hsb[2] * 100.0, (0.0, 100.0), "%"),+ (at::RIGHT, 0, Some(PickComp::L), "L:", "l", p.lab[0], (0.0, 100.0), ""),+ (at::RIGHT, 1, Some(PickComp::A), "a:", "la", p.lab[1], (-128.0, 127.0), ""),+ (at::RIGHT, 2, Some(PickComp::Lb), "b:", "lb", p.lab[2], (-128.0, 127.0), ""),+ (at::LEFT, 3, Some(PickComp::R), "R:", "r", p.rgb[0] * 255.0, (0.0, 255.0), ""),+ (at::LEFT, 4, Some(PickComp::G), "G:", "g", p.rgb[1] * 255.0, (0.0, 255.0), ""),+ (at::LEFT, 5, Some(PickComp::Bl), "B:", "bl", p.rgb[2] * 255.0, (0.0, 255.0), ""),+ (at::RIGHT, 3, None, "C:", "c", cmyk[0], (0.0, 100.0), "%"),+ (at::RIGHT, 4, None, "M:", "m", cmyk[1], (0.0, 100.0), "%"),+ (at::RIGHT, 5, None, "Y:", "y", cmyk[2], (0.0, 100.0), "%"),+ (at::RIGHT, 6, None, "K:", "k", cmyk[3], (0.0, 100.0), "%"),+ ];+ for (x, row, c, label, key, value, range, unit) in rows {+ let cy = area.min.y + at::ROWS[row];+ let x = area.min.x + x;+ match c {+ Some(c) => {+ let rr = egui::Rect::from_center_size(egui::pos2(x, cy), egui::vec2(14.0, 14.0));+ let resp = ui.interact(rr, ui.id().with(("cp_radio", key)), egui::Sense::click());+ ui.painter().circle(rr.center(), 6.0, t.input, egui::Stroke::new(1.0, t.text_dim));+ if comp_now == c {+ ui.painter().circle_filled(rr.center(), 2.6, t.text_strong);+ }+ resp.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::RadioButton, true, comp_now == c, label));+ if resp.clicked() {+ comp_now = c;+ }+ text(ui, egui::pos2(x + 11.0, cy), egui::Align2::LEFT_CENTER, label);}- ui.add_space(4.0);- ui.horizontal(|ui| {- ui.label("#");- let id = egui::Id::new("color_picker_hex");- let editing = ui.memory(|m| m.has_focus(id));- let mut h = if editing { d.s("_hexEdit") } else { p.hex().trim_start_matches('#').to_string() };- if ui.add(egui::TextEdit::singleline(&mut h).id(id).desired_width(70.0)).changed()- && let Some(c) = designcraft_color::Color::from_hex(&h)- {- p = Picked::from_rgb(c.to_rgb_uncalibrated(), p.hsb[0]);+ // C/M/Y/K have no radio button: their labels end at the field.+ None => text(ui, egui::pos2(x + 28.0, cy), egui::Align2::RIGHT_CENTER, label),+ }+ let fr = egui::Rect::from_min_size(egui::pos2(x + 35.0, cy - 10.5), egui::vec2(44.0, 21.0));+ if let Some(v) = picker_field(ui, d, fr, key, value, range) {+ edited = Some(match key {+ "h" => Picked::from_hsb([v, p.hsb[1], p.hsb[2]]),+ "s" => Picked::from_hsb([p.hsb[0], v / 100.0, p.hsb[2]]),+ "b" => Picked::from_hsb([p.hsb[0], p.hsb[1], v / 100.0]),+ "l" => Picked::from_lab([v, p.lab[1], p.lab[2]], p.hsb[0]),+ "la" => Picked::from_lab([p.lab[0], v, p.lab[2]], p.hsb[0]),+ "lb" => Picked::from_lab([p.lab[0], p.lab[1], v], p.hsb[0]),+ "r" => Picked::from_rgb([v / 255.0, p.rgb[1], p.rgb[2]], p.hsb[0]),+ "g" => Picked::from_rgb([p.rgb[0], v / 255.0, p.rgb[2]], p.hsb[0]),+ "bl" => Picked::from_rgb([p.rgb[0], p.rgb[1], v / 255.0], p.hsb[0]),+ _ => {+ let mut c = cmyk;+ c[["c", "m", "y", "k"].iter().position(|k| *k == key).unwrap_or(0)] = v;+ let [c, m, y, k] = c.map(|x| x / 100.0);+ Picked::from_rgb(designcraft_color::Color::Cmyk { c, m, y, k }.to_rgb(), p.hsb[0])}- d.fields.insert("_hexEdit".into(), json!(h));});- ui.add_space(8.0);- if ui.button(crate::rtl::widget(ui, tr("Add CMYK Swatch"))).clicked() {- let [c, m, y, k] = designcraft_color::Color::Rgb { r: p.rgb[0], g: p.rgb[1], b: p.rgb[2] }- .to_cmyk_managed(designcraft_color::cms::active_settings().intent)- .map(|v| (v * 100.0).round());- match app.run("swatch.create", json!({"color": {"c": c, "m": m, "y": y, "k": k}})) {- Ok(r) => app.status(format!("{} {}", tr("Added swatch"), r["name"].as_str().unwrap_or(""))),- Err(e) => app.status(e),- }- }- });- });+ }+ if !unit.is_empty() {+ text(ui, egui::pos2(fr.right() + 8.0, cy), egui::Align2::LEFT_CENTER, unit);+ }+ }+ if let Some(e) = edited {+ p = e;+ }+ if comp_now != comp {+ d.fields.insert("component".into(), json!(comp_now.key()));+ }+ // The hex code, and the eyedropper's mark beside it.+ let cy = area.min.y + at::ROWS[7];+ text(ui, egui::pos2(area.min.x + 321.0, cy), egui::Align2::LEFT_CENTER, "#:");+ let hr = egui::Rect::from_min_size(egui::pos2(area.min.x + 341.0, cy - 10.5), egui::vec2(64.0, 21.0));+ let id = egui::Id::new("color_picker_hex");+ let editing = ui.memory(|m| m.has_focus(id));+ let mut h = if editing { d.s("_hexEdit") } else { p.hex().trim_start_matches('#').to_string() };+ if ui+ .put(hr, egui::TextEdit::singleline(&mut h).id(id).font(font).vertical_align(egui::Align::Center).margin(egui::Margin::symmetric(4, 2)))+ .changed()+ && let Some(c) = designcraft_color::Color::from_hex(&h)+ {+ p = Picked::from_rgb(c.to_rgb_uncalibrated(), p.hsb[0]);+ }+ d.fields.insert("_hexEdit".into(), json!(h));+ crate::icons::paint(+ ui.painter(),+ egui::Rect::from_center_size(egui::pos2(hr.right() + 16.0, cy), egui::vec2(15.0, 15.0)),+ "tool-eyedropper",+ t.icon,+ );if p != before {store_picked(d, &p);}@@ -7154,7 +7262,10 @@ mod tests {doc.swatches.last().unwrap().value,designcraft_color::swatch::SwatchValue::Color { color: designcraft_color::Color::Cmyk { .. }, .. }));- confirm(&mut h.state_mut().app).unwrap();+ // Its own OK, at the top right, applies it.+ h.get_by_label("OK").click();+ h.run_steps(3);+ assert!(h.state().app.ui.dialog.is_none(), "OK closes it");let st = h.state().app.session.active().unwrap();let id = st.selection.items[0];let fill = st.doc.item(id).unwrap().fill.swatch.clone();