Skip to content

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

Colours: handle non-hex Unicode values without an internal error (#261) (#261)

storybored3793c1bab7cc172
diff --git a/crates/engine/src/filters_ext.rs b/crates/engine/src/filters_ext.rs
index 4fafacf96..7fae4a211 100644
--- a/crates/engine/src/filters_ext.rs
+++ b/crates/engine/src/filters_ext.rs
@@ -43,7 +43,7 @@ fn colour(p: &Value, k: &str, d: [f32; 4]) -> [f32; 4] {
[c(0, 0.0), c(1, 0.0), c(2, 0.0), c(3, 1.0)]
}
Some(Value::String(h)) if h.len() == 7 && h.starts_with('#') => {
- let ch = |j: usize| u8::from_str_radix(&h[j..j + 2], 16).map_or(0.0, |v| v as f32 / 255.0);
+ let ch = |j: usize| h.get(j..j + 2).and_then(|s| u8::from_str_radix(s, 16).ok()).map_or(0.0, |v| v as f32 / 255.0);
[ch(1), ch(3), ch(5), 1.0]
}
_ => d,
diff --git a/crates/engine/src/gallery_cmds.rs b/crates/engine/src/gallery_cmds.rs
index 484bd7a88..a13995697 100644
--- a/crates/engine/src/gallery_cmds.rs
+++ b/crates/engine/src/gallery_cmds.rs
@@ -21,7 +21,7 @@ fn colour(v: Option<&Value>) -> Option<[f32; 4]> {
Some([c(0, 0.0), c(1, 0.0), c(2, 0.0), c(3, 1.0)])
}
Value::String(h) if h.len() == 7 && h.starts_with('#') => {
- let ch = |j: usize| u8::from_str_radix(&h[j..j + 2], 16).map_or(0.0, |v| v as f32 / 255.0);
+ let ch = |j: usize| h.get(j..j + 2).and_then(|s| u8::from_str_radix(s, 16).ok()).map_or(0.0, |v| v as f32 / 255.0);
Some([ch(1), ch(3), ch(5), 1.0])
}
_ => None,
diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs
index 7f72f4cf6..8a2a8b3e2 100644
--- a/crates/engine/src/prefs.rs
+++ b/crates/engine/src/prefs.rs
@@ -925,7 +925,7 @@ pub fn parse_hex(s: &str) -> Option<[u8; 3]> {
if s.len() != 6 {
return None;
}
- let b = |i: usize| u8::from_str_radix(&s[i..i + 2], 16).ok();
+ let b = |i: usize| s.get(i..i + 2).and_then(|s| u8::from_str_radix(s, 16).ok());
Some([b(0)?, b(2)?, b(4)?])
}
diff --git a/crates/engine/src/render_cmds.rs b/crates/engine/src/render_cmds.rs
index 3660d3d77..ab302b11d 100644
--- a/crates/engine/src/render_cmds.rs
+++ b/crates/engine/src/render_cmds.rs
@@ -22,7 +22,7 @@ fn parse_colour(v: &Value) -> Option<[f32; 4]> {
Some([c(0, 0.0), c(1, 0.0), c(2, 0.0), c(3, 1.0)])
}
Value::String(h) if h.len() == 7 && h.starts_with('#') => {
- let ch = |j: usize| u8::from_str_radix(&h[j..j + 2], 16).map_or(0.0, |v| v as f32 / 255.0);
+ let ch = |j: usize| h.get(j..j + 2).and_then(|s| u8::from_str_radix(s, 16).ok()).map_or(0.0, |v| v as f32 / 255.0);
Some([ch(1), ch(3), ch(5), 1.0])
}
_ => None,
diff --git a/crates/engine/tests/unicode_colors.rs b/crates/engine/tests/unicode_colors.rs
new file mode 100644
index 000000000..eeb87399a
--- /dev/null
+++ b/crates/engine/tests/unicode_colors.rs
@@ -0,0 +1,33 @@
+//! Colour strings supplied through the same commands used by CLI/MCP clients.
+use photocraft_engine::{Session, command_specs};
+use serde_json::{Value, json};
+
+fn run(id: &str, params: Value) {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 8, "height": 8})).unwrap();
+ let spec = command_specs().iter().find(|c| c.id == id).unwrap();
+ // Exercise the command directly, without the last-resort shell guard.
+ (spec.run)(&mut s, &params).unwrap();
+}
+
+#[test]
+fn unicode_pixelate_colour_uses_the_existing_fallback() {
+ run("filter.pixelate.pointillize", json!({"background": "#€€"}));
+}
+
+#[test]
+fn unicode_gallery_colour_uses_the_existing_fallback() {
+ run("filter.gallery.photocopy", json!({"foreground": "#€€"}));
+}
+
+#[test]
+fn unicode_render_colour_uses_the_existing_fallback() {
+ run("filter.render.tree", json!({"leavesColor": "#€€"}));
+}
+
+#[test]
+fn unicode_preference_colour_is_invalid() {
+ assert_eq!(photocraft_engine::prefs::parse_hex("#€€"), None);
+ assert_eq!(photocraft_engine::prefs::parse_hex(""), None);
+ assert_eq!(photocraft_engine::prefs::parse_hex("#12aBcD"), Some([0x12, 0xab, 0xcd]));
+}