PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Swatches: an RGB group with the exact primaries and secondaries (#4302) (#4409)
FredApps78c61e6df5ae
diff --git a/crates/engine/src/presets/swatches.rs b/crates/engine/src/presets/swatches.rsindex eec8c2d0d..eb666e1ec 100644--- a/crates/engine/src/presets/swatches.rs+++ b/crates/engine/src/presets/swatches.rs@@ -288,7 +288,9 @@ pub fn hex(c: [f32; 3]) -> String {// ------------------------------------------------------------------ defaults-/// The default library: 40 colours in four groups (our own palette).+/// The default library: 46 colours in five groups. "RGB" holds the exact primaries and+/// secondaries (#FF0000, #00FF00, ...) as Photoshop's RGB group does; the other four are our own+/// palette, whose "Pure" hues are vivid but not the pure channel values (#4302).pub fn builtin() -> Vec<Group<Swatch>> {let rgb = |n: &str, c: [u8; 3]| Swatch::new(n, SwatchColor::Rgb(c.map(|v| f32::from(v) / 255.0)));let grays = [@@ -333,8 +335,19 @@ pub fn builtin() -> Vec<Group<Swatch>> {let row = |prefix: &str, cs: [[u8; 3]; 10]| -> Vec<Swatch> {hues.iter().zip(cs).map(|(h, c)| rgb(&if prefix.is_empty() { (*h).to_string() } else { format!("{prefix} {h}") }, c)).collect()};+ // Right after Grays, near the top of the panel (Grays stays first: new swatches land there).+ // Photoshop's order: red, yellow, green, cyan, blue, magenta.+ let exact: [(&str, [u8; 3]); 6] = [+ ("RGB Red", [255, 0, 0]),+ ("RGB Yellow", [255, 255, 0]),+ ("RGB Green", [0, 255, 0]),+ ("RGB Cyan", [0, 255, 255]),+ ("RGB Blue", [0, 0, 255]),+ ("RGB Magenta", [255, 0, 255]),+ ];vec![Group::new("Grays", grays.iter().map(|(n, v)| rgb(n, [*v; 3])).collect()),+ Group::new("RGB", exact.iter().map(|(n, c)| rgb(n, *c)).collect()),Group::new("Pastel", row("Pastel", pastel)),Group::new("Pure", row("", pure)),Group::new("Dark", row("Dark", dark)),@@ -346,15 +359,27 @@ mod tests {use super::*;#[test]- fn default_library_has_the_forty_colours() {+ fn default_library_has_the_forty_six_colours() {let b = builtin();- assert_eq!(b.iter().map(|g| g.items.len()).sum::<usize>(), 40);+ assert_eq!(b.iter().map(|g| g.items.len()).sum::<usize>(), 46);let names: Vec<&str> = b.iter().flat_map(|g| g.items.iter().map(|s| s.name.as_str())).collect();let mut uniq = names.clone();uniq.sort_unstable();uniq.dedup();- assert_eq!(uniq.len(), 40, "names are unique: {names:?}");- assert_eq!(b[2].items[0].color, SwatchColor::Rgb([230.0 / 255.0, 40.0 / 255.0, 40.0 / 255.0]));+ assert_eq!(uniq.len(), 46, "names are unique: {names:?}");+ assert_eq!(b[3].name, "Pure");+ assert_eq!(b[3].items[0].color, SwatchColor::Rgb([230.0 / 255.0, 40.0 / 255.0, 40.0 / 255.0]));+ }++ #[test]+ fn the_rgb_group_holds_exact_primaries_and_secondaries() {+ // #4302: picking green should give #00FF00, as in Photoshop's RGB group.+ let b = builtin();+ let g = &b[1];+ assert_eq!(g.name, "RGB");+ let conv = SwatchConv::builtin();+ let hexes: Vec<String> = g.items.iter().map(|s| hex(conv.rgb(&s.color))).collect();+ assert_eq!(hexes, ["#ff0000", "#ffff00", "#00ff00", "#00ffff", "#0000ff", "#ff00ff"]);}#[test]diff --git a/crates/engine/src/swatch_cmds/tests.rs b/crates/engine/src/swatch_cmds/tests.rsindex cb121a183..836b84397 100644--- a/crates/engine/src/swatch_cmds/tests.rs+++ b/crates/engine/src/swatch_cmds/tests.rs@@ -14,10 +14,11 @@ fn export_bytes(s: &mut Session, p: Value) -> Vec<u8> {fn defaults_and_listing() {let mut s = Session::new();let r = s.execute("swatches.list", json!({})).unwrap();- assert_eq!(r["count"], 40);+ assert_eq!(r["count"], 46);assert_eq!(r["groups"][0]["name"], "Grays");assert_eq!(r["groups"][0]["swatches"][0], json!({"name": "Black", "model": "rgb", "values": [0.0, 0.0, 0.0], "hex": "#000000"}));- assert_eq!(r["groups"][2]["swatches"][0]["hex"], "#e62828");+ assert_eq!(r["groups"][1]["swatches"][2]["hex"], "#00ff00", "RGB Green is exact (#4302)");+ assert_eq!(r["groups"][3]["swatches"][0]["hex"], "#e62828");}#[test]@@ -67,7 +68,7 @@ fn rename_delete_move_and_groups() {assert_eq!(s.presets.swatches[0].items[0].name, "White");let r = s.execute("swatches.move", json!({"swatch": "White", "to": "Dark", "position": 1})).unwrap();assert_eq!(r, json!({"group": "Dark", "position": 1}));- assert_eq!(s.presets.swatches[3].items[1].name, "White");+ assert_eq!(s.presets.swatches[4].items[1].name, "White");// Out-of-range positions clamp.let r = s.execute("swatches.move", json!({"swatch": "White", "position": 999_999})).unwrap();assert_eq!(r["position"], 10);@@ -80,21 +81,21 @@ fn rename_delete_move_and_groups() {assert!(s.execute("swatches.renameGroup", json!({"group": "Favourites", "name": "Dark"})).is_err());let r = s.execute("swatches.deleteGroup", json!({"group": "Favourites", "keepSwatches": true})).unwrap();assert_eq!(r["kept"], 2);- assert!(s.presets.swatches[3].items.iter().any(|i| i.name == "Red"), "kept in the group above");+ assert!(s.presets.swatches[4].items.iter().any(|i| i.name == "Red"), "kept in the group above");s.execute("swatches.deleteGroup", json!({"group": "Mine 1"})).unwrap();// Delete several by name, and one by index.let r = s.execute("swatches.delete", json!({"swatch": ["Red", "Blue", "Red"]})).unwrap();assert_eq!(r["deleted"].as_array().unwrap().len(), 2);let r = s.execute("swatches.delete", json!({"index": 0})).unwrap();assert_eq!(r["deleted"], json!(["Ink Black"]));- assert_eq!(names(&s).len(), 37);+ assert_eq!(names(&s).len(), 43);// Reset restores the defaults; append adds them again with unique names.s.execute("swatches.reset", json!({})).unwrap();- assert_eq!(names(&s).len(), 40);+ assert_eq!(names(&s).len(), 46);s.execute("swatches.reset", json!({"append": true})).unwrap();- assert_eq!(names(&s).len(), 80);+ assert_eq!(names(&s).len(), 92);assert!(names(&s).contains(&"Black 2".to_string()));- assert_eq!(s.presets.swatches[4].name, "Grays 1");+ assert_eq!(s.presets.swatches[5].name, "Grays 1");}#[test]@@ -220,7 +221,7 @@ fn bad_params_fail_gracefully() {("swatches.delete", json!({"swatch": "Nope"})),("swatches.delete", json!({"swatch": ["Red", 5]})),("swatches.delete", json!({"swatch": ["Red", "Nope"]})),- ("swatches.delete", json!({"index": 40})),+ ("swatches.delete", json!({"index": 46})),("swatches.delete", json!({"index": "0"})),("swatches.delete", json!({"swatch": "Red", "group": "Grays"})),("swatches.rename", json!({"swatch": "Red"})),@@ -264,7 +265,7 @@ fn bad_params_fail_gracefully() {assert_eq!(s.presets.swatches, before, "failed commands change nothing");// Empty library: the swatch commands are disabled, not crashing.s.execute("swatches.delete", json!({"swatch": names(&s)})).unwrap();- for g in ["Grays", "Pastel", "Pure", "Dark"] {+ for g in ["Grays", "RGB", "Pastel", "Pure", "Dark"] {s.execute("swatches.deleteGroup", json!({"group": g})).unwrap();}for id in ["swatches.delete", "swatches.rename", "swatches.move", "swatches.use", "swatches.export", "swatches.renameGroup", "swatches.deleteGroup"] {