mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 01:34:29 +01:00
Plug-ins: returned effect colors keep their components from 0 to 1
A returned effect's `color` given as [r, g, b] now needs each of the first three components from 0 to 1, and a color object needs its components from 0 to 1 (a Lab color's lightness from 0 to 100), as the effects document them. [2, -1, 0] was taken before.
This commit is contained in:
1 parent
c1f74eeeb1
commit
1e644a8251
3 files changed
+23
-5
No files matched your search
@@ -219,7 +219,7 @@ fn check_returned_values(id: &str, given: &Map<String, Value>) -> Result<(), Str
|
||||
}
|
||||
let takes_color = catalog_index().get(id).is_some_and(|info| info.defaults.get("color").is_some_and(Value::is_string));
|
||||
if takes_color && given.get("color").is_some_and(|v| !is_color(v)) {
|
||||
return Err("`color` must be a #rrggbb color".into());
|
||||
return Err("`color` must be a #rrggbb color, [r, g, b] from 0 to 1 or a color object".into());
|
||||
}
|
||||
let takes_blend_mode = catalog_index().get(id).is_some_and(|info| info.params.contains("mode: blend mode"));
|
||||
if takes_blend_mode && given.get("mode").is_some_and(|v| v.as_str().and_then(crate::raster::named_blend_mode).is_none()) {
|
||||
@@ -250,15 +250,23 @@ fn check_returned_values(id: &str, given: &Map<String, Value>) -> Result<(), Str
|
||||
}
|
||||
|
||||
/// Can an effect read `v` as its `color`: a hex color or name [`Color::from_hex`] reads, `[r, g, b]`
|
||||
/// with three numbers first, or a color object?
|
||||
/// with three numbers from 0 to 1 first, or a color object with its components from 0 to 1 (a Lab
|
||||
/// color's lightness from 0 to 100, and finite a and b)?
|
||||
///
|
||||
/// [`Color::from_hex`]: vectorcraft_doc::color::Color::from_hex
|
||||
fn is_color(v: &Value) -> bool {
|
||||
use vectorcraft_doc::color::Color;
|
||||
let unit = |x: f32| (0.0..=1.0).contains(&x);
|
||||
match v {
|
||||
Value::String(s) => Color::from_hex(s).is_some(),
|
||||
Value::Array(a) => a.len() >= 3 && a.iter().take(3).all(Value::is_number),
|
||||
Value::Object(_) => serde_json::from_value::<Color>(v.clone()).is_ok(),
|
||||
Value::Array(a) => a.len() >= 3 && a.iter().take(3).all(|c| c.as_f64().is_some_and(|c| (0.0..=1.0).contains(&c))),
|
||||
Value::Object(_) => match serde_json::from_value::<Color>(v.clone()) {
|
||||
Ok(Color::Rgb { r, g, b }) => [r, g, b].into_iter().all(unit),
|
||||
Ok(Color::Cmyk { c, m, y, k }) => [c, m, y, k].into_iter().all(unit),
|
||||
Ok(Color::Gray { k }) => unit(k),
|
||||
Ok(Color::Lab { l, a, b }) => (0.0..=100.0).contains(&l) && a.is_finite() && b.is_finite(),
|
||||
Err(_) => false,
|
||||
},
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -296,7 +296,10 @@ fn returned_effects_are_built_in_effects_with_their_own_parameters() {
|
||||
("stylize.outerGlow", json!({"mode": "Normal"})),
|
||||
// A `color` is also taken as [r, g, b] 0..1 or a color object, as the effects draw it.
|
||||
("stylize.dropShadow", json!({"color": [1.0, 0.0, 0.0]})),
|
||||
("stylize.dropShadow", json!({"color": [1, 0, 0]})),
|
||||
("stylize.outerGlow", json!({"color": {"model": "gray", "k": 0.5}})),
|
||||
("stylize.innerGlow", json!({"color": {"model": "cmyk", "c": 0, "m": 1, "y": 0, "k": 0}})),
|
||||
("stylize.innerGlow", json!({"color": {"model": "lab", "l": 100, "a": -128, "b": 127}})),
|
||||
] {
|
||||
assert_eq!(check(&e(id, params.clone()), false), Ok(()), "{id} {params}");
|
||||
}
|
||||
@@ -334,6 +337,12 @@ fn returned_effects_are_built_in_effects_with_their_own_parameters() {
|
||||
("stylize.dropShadow", json!({"color": [1.0, "0", 0.0]}), "color"),
|
||||
("stylize.innerGlow", json!({"color": {"model": "rgb"}}), "color"),
|
||||
("stylize.outerGlow", json!({"color": true}), "color"),
|
||||
// Color components are from 0 to 1, Lab's lightness from 0 to 100.
|
||||
("stylize.dropShadow", json!({"color": [2, -1, 0]}), "color"),
|
||||
("stylize.dropShadow", json!({"color": [0.5, 0.5, 1.01]}), "color"),
|
||||
("stylize.outerGlow", json!({"color": {"model": "rgb", "r": 2.0, "g": 0.0, "b": 0.0}}), "color"),
|
||||
("stylize.innerGlow", json!({"color": {"model": "gray", "k": -0.5}}), "color"),
|
||||
("stylize.innerGlow", json!({"color": {"model": "lab", "l": 101, "a": 0, "b": 0}}), "color"),
|
||||
("adjust.levels", json!({"channel": "cyan"}), "channel"),
|
||||
("blur.radial", json!({"centerX": 1.5}), "centerX"),
|
||||
("blur.gaussian", json!({"linear": 1}), "linear"),
|
||||
|
||||
+2
-1
@@ -878,7 +878,8 @@ fill and stroke the piece returns, three more keys:
|
||||
type of its default, within the range `effect.list` gives for it, where it gives one, and one of the choices it
|
||||
lists for it, where it lists them (Roughen's `points` is `"smooth"` or `"corner"`). The values `effect.apply`
|
||||
checks for one effect are checked the same way: an SVG filter's `xml`, `scale`, `origin`, `axes` and `name`, a
|
||||
Warp's `version`, a `color`, a Levels or Curves `channel` and a Radial Blur's center. A Drop Shadow's, Inner
|
||||
Warp's `version`, a `color` (`"#rrggbb"`, `[r, g, b]` with each component from 0 to 1, or a color object with
|
||||
components from 0 to 1 and a Lab lightness from 0 to 100), a Levels or Curves `channel` and a Radial Blur's center. A Drop Shadow's, Inner
|
||||
Glow's or Outer Glow's `mode` must name a blend mode (`"multiply"`, `"color-burn"`). Omitted parameters take the
|
||||
effect's defaults. On a fill or stroke only raster effects are taken, and only from an effect in the object's stack:
|
||||
the fills or strokes that an effect on a fill or a stroke returns take `opacity` and `blend` but no `effects` (its
|
||||
|
||||
Reference in new issue
Block a user