PhotoCraft: An open-source, photo editor written by scratch in pure Rust
feat(layer-style): expose Bevel & Emboss highlight and shadow shading
Nigel Ihza Firdausy22f852097417
diff --git a/crates/engine/src/layer_style.rs b/crates/engine/src/layer_style.rsindex bf2d55a5a..a4bcc8b94 100644--- a/crates/engine/src/layer_style.rs+++ b/crates/engine/src/layer_style.rs@@ -86,7 +86,9 @@ pub fn effect_defaults(kind: &str) -> Value {}"patternOverlay" => json!({"blend": "Normal", "opacity": 100, "pattern": "", "angle": 0, "scale": 100, "link": true}),"bevelEmboss" => {- json!({"style": "inner", "depth": 100, "direction": "up", "size": 5, "soften": 0, "angle": 120, "useGlobalLight": true, "altitude": 30, "glossContour": "Linear", "contour": false, "contourName": "Linear", "contourRange": 50, "contourAntiAlias": false})+ json!({"style": "inner", "depth": 100, "direction": "up", "size": 5, "soften": 0, "angle": 120, "useGlobalLight": true, "altitude": 30, "glossContour": "Linear", "highlightBlend": "Screen", "highlightColor": "#ffffff", "highlightOpacity": 75,+ "shadowBlend": "Multiply", "shadowColor": "#000000", "shadowOpacity": 75,+ "contour": false, "contourName": "Linear", "contourRange": 50, "contourAntiAlias": false)}"satin" => {json!({"blend": "Multiply", "color": "#000000", "opacity": 50, "angle": 19, "distance": 11, "size": 14, "contour": "Linear", "antiAlias": true, "invert": true})@@ -559,6 +561,21 @@ pub fn overlay_effect(fx: &mut Effect, p: &Value) {bv.use_global_light = x;}set_contour(p, "glossContour", &mut bv.gloss_contour);+ // Photoshop exposes separate shading controls for each side of the bevel.+ if let Some(blend) = p.get("highlightBlend").and_then(Value::as_str).and_then(blend_from_str) {+ bv.highlight.blend = blend;+ }+ if let Some(blend) = p.get("shadowBlend").and_then(Value::as_str).and_then(blend_from_str) {+ bv.shadow.blend = blend;+ }+ set_color(p, "highlightColor", &mut bv.highlight_color);+ set_color(p, "shadowColor", &mut bv.shadow_color);+ if let Some(x) = f_of(p, "highlightOpacity") {+ bv.highlight.opacity = (x / 100.0).clamp(0.0, 1.0);+ }+ if let Some(x) = f_of(p, "shadowOpacity") {+ bv.shadow.opacity = (x / 100.0).clamp(0.0, 1.0);+ }// The Contour element: `contour` turns it on/off; its curve, Range and// Anti-aliased edit the element in place, so an imported curve survives.match b_of(p, "contour") {@@ -910,6 +927,33 @@ mod tests {assert_eq!((t.name.as_str(), t.depth, t.invert), ("Bubbles", -2.0, true));}+ #[test]+ fn bevel_shading_fields_are_independent_and_preserved_on_partial_edits() {+ let mut s = session();+ s.execute("layer.layerStyle.bevelEmboss", json!({+ "highlightBlend": "Normal", "highlightColor": "#ff0000", "highlightOpacity": 35,+ "shadowBlend": "Color Burn", "shadowColor": "#0000ff", "shadowOpacity": 60+ })).unwrap();+ let fx = effects(&s);+ let Effect::BevelEmboss(bevel) = &fx[0] else { panic!("{fx:?}") };+ assert_eq!(bevel.highlight.blend, BlendMode::Normal);+ assert_eq!(bevel.highlight_color, Color::rgb(1.0, 0.0, 0.0));+ assert!((bevel.highlight.opacity - 0.35).abs() < 0.001);+ assert_eq!(bevel.shadow.blend, BlendMode::ColorBurn);+ assert_eq!(bevel.shadow_color, Color::rgb(0.0, 0.0, 1.0));+ assert!((bevel.shadow.opacity - 0.60).abs() < 0.001);+ let mut changed = fx[0].clone();+ overlay_effect(&mut changed, &json!({"size": 12}));+ let Effect::BevelEmboss(partial) = &changed else { panic!("expected bevel") };+ assert_eq!(partial.highlight, bevel.highlight);+ assert_eq!(partial.highlight_color, bevel.highlight_color);+ assert_eq!(partial.shadow, bevel.shadow);+ assert_eq!(partial.shadow_color, bevel.shadow_color);+ overlay_effect(&mut changed, &json!({"highlightOpacity": 120, "shadowOpacity": -30}));+ let Effect::BevelEmboss(limits) = &changed else { panic!("expected bevel") };+ assert_eq!((limits.highlight.opacity, limits.shadow.opacity), (1.0, 0.0));+ }+#[test]fn every_style_command_adds_its_effect() {for kind in ["dropShadow", "innerShadow", "outerGlow", "innerGlow", "stroke", "colorOverlay", "gradientOverlay", "bevelEmboss", "satin"] {diff --git a/crates/ui-egui/src/layer_style.rs b/crates/ui-egui/src/layer_style.rsindex 93f22c479..5412d5c0a 100644--- a/crates/ui-egui/src/layer_style.rs+++ b/crates/ui-egui/src/layer_style.rs@@ -171,6 +171,12 @@ fn spec(kind: &str) -> &'static [(&'static str, &'static str, P)] {("useGlobalLight", "Use Global Light", P::Check),("altitude", "Altitude", P::Slider(0.0, 90.0, "°")),("glossContour", "Gloss Contour", P::Contour(None)),+ ("highlightBlend", "Highlight Mode", P::Blend),+ ("highlightColor", "Highlight Color", P::Color),+ ("highlightOpacity", "Highlight Opacity", P::Slider(0.0, 100.0, "%")),+ ("shadowBlend", "Shadow Mode", P::Blend),+ ("shadowColor", "Shadow Color", P::Color),+ ("shadowOpacity", "Shadow Opacity", P::Slider(0.0, 100.0, "%")),// Bevel & Emboss › Contour element.("contour", "Contour", P::Check),("contourName", "", P::Contour(Some("contourAntiAlias"))),@@ -350,6 +356,12 @@ fn values_of(e: &Effect, light: f32) -> Value {set(&mut v, "altitude", json!(b.altitude));set(&mut v, "direction", json!(if b.up { "up" } else { "down" }));set(&mut v, "glossContour", photocraft_engine::layer_style::contour_param(&b.gloss_contour));+ set(&mut v, "highlightBlend", json!(b.highlight.blend.label()));+ set(&mut v, "highlightColor", json!(hex(&b.highlight_color)));+ set(&mut v, "highlightOpacity", json!((b.highlight.opacity * 100.0).round()));+ set(&mut v, "shadowBlend", json!(b.shadow.blend.label()));+ set(&mut v, "shadowColor", json!(hex(&b.shadow_color)));+ set(&mut v, "shadowOpacity", json!((b.shadow.opacity * 100.0).round()));set(&mut v, "contour", json!(b.contour.is_some()));if let Some(c) = &b.contour {set(&mut v, "contourName", photocraft_engine::layer_style::contour_param(&c.contour));@@ -1889,6 +1901,34 @@ mod tests {}}+ #[test]+ fn bevel_shading_round_trips_through_dialog_preview_and_confirm() {+ let mut s = session();+ s.execute("layer.layerStyle.bevelEmboss", json!({+ "highlightBlend": "Normal", "highlightColor": "#e08020", "highlightOpacity": 25,+ "shadowBlend": "Multiply", "shadowColor": "#204080", "shadowOpacity": 40+ })).unwrap();+ let st = s.active().unwrap();+ let id = st.active_layer.unwrap();+ let mut fields = initial_fields(st.doc.layer(id).unwrap(), Some("bevelEmboss"), st.doc.global_light.angle);+ let values = &entry(&fields, "fx1").unwrap()["params"];+ assert_eq!(values["highlightColor"], "#e08020");+ assert_eq!(values["highlightOpacity"], 25.0);+ assert_eq!(values["shadowColor"], "#204080");+ assert_eq!(values["shadowOpacity"], 40.0);+ set_param(&mut fields, "fx1", "highlightBlend", json!("Screen"));+ set_param(&mut fields, "fx1", "highlightOpacity", json!(80.0));+ set_param(&mut fields, "fx1", "shadowColor", json!("#ff0000"));+ assert!(preview_document(&st.doc, &s.patterns, &fields).is_ok());+ apply(&fields, |cmd, p| s.execute(cmd, p).map_err(|e| e.to_string())).unwrap();+ let effect = &s.active().unwrap().doc.layer(id).unwrap().effects.items[0];+ let Effect::BevelEmboss(bevel) = effect else { panic!("expected bevel") };+ assert_eq!(bevel.highlight.blend, photocraft_doc::BlendMode::Screen);+ assert!((bevel.highlight.opacity - 0.8).abs() < 0.001);+ assert_eq!(hex(&bevel.shadow_color), "#ff0000");+ assert_eq!(hex(&bevel.highlight_color), "#e08020");+ }+#[test]fn preview_off_shows_the_original_until_reenabled() {let mut s = session();