Colors: a color with a component too large to store is refused

A color given as {"c": 1e300, ...}, [1e300, 0, 0], {"gray": 1e300}, a Lab
color or a color object with a number past f32::MAX was stored with an
infinite component. paint.setFill, artboard.setProps, swatch.new,
text.create and the other commands that read colors through
cmd::color_value took it.

- color_value refuses a color whose components are not finite, so the
  commands that read a color through it answer an error and change
  nothing.
- An effect's color (Drop Shadow, Inner Glow, Outer Glow, Shift to Color)
  with such a number is refused with "`color` must be a #rrggbb color".
- ui.colorWheel's contrastBackground refuses it as well.

Docs: docs/mcp.md.
This commit is contained in:
dotCooCoo committed 2026-11-29 07:11:18 -08:00
1 parent d4d8da4544
commit 8f1edd0644
7 files changed
+93 -15

No files matched your search

+17 -1
View File
@@ -672,8 +672,24 @@ pub fn matrix_param(p: &Value, key: &str) -> Option<Affine> {
}
/// A colour from `"#rrggbb"`, a colour key (`"cmyk 0 100 100 0"`, `"lab 55 60 40"`), `[r,g,b]` (0..1),
/// `{"c":..,"m":..,"y":..,"k":..}` (percent when any component is above 1, else 0..1), `{"gray":..}` or `{"l":..,"a":..,"b":..}` (CIE Lab: L 0..100, a and b about
/// −128..127). `None` for a hex color with an alpha part (`#rrggbbaa` or `#rgba`, with or without the `#`).
/// −128..127). `None` for a hex color with an alpha part (`#rrggbbaa` or `#rgba`, with or without the `#`),
/// and for a color with a component too large to store ([`color_is_finite`]).
pub fn color_value(v: &Value) -> Option<Color> {
given_color(v).filter(color_is_finite)
}
/// Is every component of `c` finite? A number past `f32::MAX`, such as `{"c": 1e300}`, is stored
/// as an infinity.
pub fn color_is_finite(c: &Color) -> bool {
match *c {
Color::Rgb { r, g, b } | Color::Lab { l: r, a: g, b } => [r, g, b].iter().all(|x| x.is_finite()),
Color::Cmyk { c, m, y, k } => [c, m, y, k].iter().all(|x| x.is_finite()),
Color::Gray { k } => k.is_finite(),
}
}
/// The color `v` gives, as [`color_value`] reads it, with its components unchecked.
fn given_color(v: &Value) -> Option<Color> {
match v {
Value::String(s) if hex_with_alpha(s) => None,
// A hex, or a colour key ("cmyk 0 100 100 0", as Recolor Artwork lists colours).
+6 -6
View File
@@ -43,7 +43,7 @@ use std::sync::OnceLock;
use serde_json::Value;
use vectorcraft_apischema::shown_key;
use super::{bad, command_specs, find_command};
use super::{bad, color_is_finite, command_specs, find_command};
use crate::Result;
/// What a params description says about the keys of the params object.
@@ -608,16 +608,16 @@ pub(crate) fn undefaulted_type(effect: &str, key: &str) -> Option<Value> {
/// Can an effect read `v` as its `color`: a string [`Color::from_hex`] reads (`#rrggbb`, `#rgb`,
/// `#rrggbbaa`, black, white, red, green, blue), `[r, g, b]` whose first three entries are numbers
/// (read on a 0..1 scale) or a color object (`{"model": "rgb", "r": …}`)? The effect draws its
/// default color for any other value, except that an array of three or more entries draws an
/// entry that isn't a number as 0.
/// (read on a 0..1 scale) or a color object (`{"model": "rgb", "r": …}`), with no component too
/// large to store ([`color_is_finite`])? The effect draws its default color for any other value,
/// except that an array of three or more entries draws an entry that isn't a number as 0.
///
/// [`Color::from_hex`]: vectorcraft_color::Color::from_hex
fn is_effect_color(v: &Value) -> bool {
match v {
Value::String(s) => vectorcraft_color::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::<vectorcraft_color::Color>(v.clone()).is_ok(),
Value::Array(a) => a.len() >= 3 && a.iter().take(3).all(|n| n.as_f64().is_some_and(|n| (n as f32).is_finite())),
Value::Object(_) => serde_json::from_value::<vectorcraft_color::Color>(v.clone()).is_ok_and(|c| color_is_finite(&c)),
_ => false,
}
}
+46
View File
@@ -266,6 +266,52 @@ fn a_color_with_an_alpha_part_is_refused() {
s.execute("object.rasterize", &json!({"ppi": 72, "background": "#ff0000"})).unwrap();
}
/// A color param with a component too large to store (past `f32::MAX`, which would be stored as an
/// infinity) is refused and changes nothing, in every form a color is given in: CMYK, `[r, g, b]`,
/// Gray, Lab and a color object. The ends of each range still set the color.
#[test]
fn a_color_with_a_component_too_large_to_store_is_refused() {
let mut s = session();
let id = rect(&mut s);
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
let before = s.doc().unwrap().doc.clone();
let undo = s.doc().unwrap().history.undo.len();
for bad in [
json!({"c": f64::MAX, "m": 0, "y": 0, "k": 0}),
json!({"c": 0, "m": 0, "y": 0, "k": 1e308}),
json!([1e300, 0, 0]),
json!([0, 0, -1e300]),
json!({"gray": 1e300}),
json!({"l": 1e300, "a": 0, "b": 0}),
json!({"l": 50, "a": 0, "b": -1e300}),
json!({"model": "rgb", "r": 1e300, "g": 0, "b": 0}),
json!({"model": "cmyk", "c": 0, "m": 1e300, "y": 0, "k": 0}),
] {
for (cmd, p) in [
("artboard.setProps", json!({"index": 0, "color": bad})),
("artboard.new", json!({"color": bad})),
("paint.setFill", json!({"ids": [id.0], "color": bad})),
("paint.setStroke", json!({"ids": [id.0], "color": bad})),
("swatch.new", json!({"name": "Far", "color": bad})),
("swatch.new", json!({"colors": ["#00ff00", bad]})),
("text.create", json!({"x": 10, "y": 10, "text": "a", "color": bad})),
] {
assert!(s.execute(cmd, &p).is_err(), "{cmd} {p}");
}
}
assert_eq!(*s.doc().unwrap().doc, *before, "a color too large to store changes nothing");
assert_eq!(s.doc().unwrap().history.undo.len(), undo, "and records no undo step");
let fill = |s: &Session| node(s, id).appearance.fill_paint().color();
s.execute("paint.setFill", &json!({"ids": [id.0], "color": {"c": 100, "m": 0, "y": 0, "k": 0}})).unwrap();
assert_eq!(fill(&s), Some(Color::cmyk(1.0, 0.0, 0.0, 0.0)));
s.execute("paint.setFill", &json!({"ids": [id.0], "color": [1, 0, 0]})).unwrap();
assert_eq!(fill(&s).map(|c| c.to_hex()).as_deref(), Some("#ff0000"));
s.execute("paint.setFill", &json!({"ids": [id.0], "color": {"gray": 100}})).unwrap();
assert_eq!(fill(&s), Some(Color::gray(1.0)));
s.execute("paint.setFill", &json!({"ids": [id.0], "color": {"l": 100, "a": -128, "b": 127}})).unwrap();
assert_eq!(fill(&s), Some(Color::lab(100.0, -128.0, 127.0)));
}
/// The native format writes an artboard's background color and reads it back. An artboard without
/// one is written as before, with no `color` key.
#[test]
+12 -2
View File
@@ -170,7 +170,8 @@ fn effect_params_no_longer_taken_still_replay_and_open() {
}
/// An effect's `color` that the effect can't read (a string `Color::from_hex` doesn't read, an
/// `[r, g, b]` without three numbers, an object that isn't a color, `null`) is refused by
/// `[r, g, b]` without three numbers, an object that isn't a color, `null`, a color with a component
/// too large to store) is refused by
/// `effect.apply`, `appearance.addEffect` and `effect.setParams` themselves, not only by the check,
/// and the document and its undo history stay as they were. The forms the effect reads pass. A
/// document saved with an unreadable color still opens with it.
@@ -186,7 +187,16 @@ fn an_effect_color_it_cannot_draw_is_refused() {
};
let before = state(&s);
let why = "`color` must be a #rrggbb color";
for bad in [json!("not-a-color"), json!("#ff00"), json!([1, 0]), json!(["#ff0000", 0, 0]), json!({"gray": 50}), json!(null)] {
for bad in [
json!("not-a-color"),
json!("#ff00"),
json!([1, 0]),
json!(["#ff0000", 0, 0]),
json!({"gray": 50}),
json!(null),
json!([1e300, 0, 0]),
json!({"model": "rgb", "r": 0, "g": 1e300, "b": 0}),
] {
for (cmd, params) in [
("effect.setParams", json!({"index": 0, "params": {"color": bad}})),
("effect.apply", json!({"effect": "stylize.outerGlow", "params": {"color": bad}})),
+4 -2
View File
@@ -334,9 +334,11 @@ fn deserialize_category<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Catego
}
/// A pinned background as saved (`{model, …}`) or as `ui.colorWheel` takes it (any color the
/// paint commands take); anything else reads as no pin.
/// paint commands take); anything else, a color with a component too large to store included,
/// reads as no pin.
fn background_value(v: &Value) -> Option<Color> {
serde_json::from_value(v.clone()).ok().or_else(|| vectorcraft_engine::cmd::color_value(v))
let saved = serde_json::from_value(v.clone()).ok().filter(vectorcraft_engine::cmd::color_is_finite);
saved.or_else(|| vectorcraft_engine::cmd::color_value(v))
}
fn deserialize_background<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Option<Color>, D::Error> {
@@ -347,6 +347,8 @@ fn ui_color_wheel_sets_the_keys_given_and_refuses_bad_values() {
(json!({"contrastFor": 3}), "`contrastFor` must be one of normalText, largeText, graphics"),
(json!({"contrastBackground": "nope"}), "`contrastBackground` must be a color or null"),
(json!({"contrast": false, "contrastBackground": 5}), "`contrastBackground` must be a color or null"),
(json!({"contrastBackground": {"model": "rgb", "r": 1e300, "g": 0, "b": 0}}), "`contrastBackground` must be a color or null"),
(json!({"contrastBackground": {"c": 0, "m": 0, "y": 0, "k": 1e300}}), "`contrastBackground` must be a color or null"),
] {
assert_eq!(app.run("ui.colorWheel", p.clone()).unwrap_err(), e, "{p}");
}
+6 -4
View File
@@ -642,7 +642,8 @@ Paint values (`fill`, `stroke`) accept `"#rrggbb"`, `"none"`, `[r,g,b]` (0..1),
or a full `paint.setFill` params object (`{"gradient": …}`, `{"swatch": "name"}`).
A color read this way (a fill or stroke color, a swatch's, an artboard's or new type's color, and the other colors
commands read in these forms) that is given as a hex color with an alpha part (`#rrggbbaa` or `#rgba`, with or without
the `#`) is an error and changes nothing, as VectorCraft's own rule. The `color` of Drop Shadow, Inner Glow, Outer Glow
the `#`) is an error and changes nothing, as VectorCraft's own rule. So is a color with a component too large to store
as a 32-bit float, such as `{"c": 1e300, "m": 0, "y": 0, "k": 0}`. The `color` of Drop Shadow, Inner Glow, Outer Glow
and Shift to Color is read differently and takes `#rrggbbaa`, as described below.
For `draw_shape` and `draw_path`, omitted paint fields inherit the current [new-art appearance](#new-art). Both
also style the new object in the same call: `dash` (lengths, or null for solid), `cap`
@@ -2231,9 +2232,10 @@ in a replayed journal or action too, and nothing changes.
The `color` of Drop Shadow, Inner Glow, Outer Glow and Shift to Color must be one the effect can read: a hex color
(`#rrggbb`, `#rgb` or `#rrggbbaa`, with or without the `#`), `black`, `white`, `red`, `green` or `blue`, `[r, g, b]`
(three numbers, each read on a 0 to 1 scale), or a color object (`{"model": "rgb", "r": 1, "g": 0, "b": 0}`).
`effect.apply` and `effect.setParams` refuse any other value themselves (where the parameter check runs first, a number
or a boolean is refused as not a string), so the app's effect dialogs and Appearance panel, a step of
(three numbers, each read on a 0 to 1 scale), or a color object (`{"model": "rgb", "r": 1, "g": 0, "b": 0}`), with no
number too large to store as a 32-bit float (`[1e300, 0, 0]` is refused). `effect.apply` and `effect.setParams`
refuse any other value themselves (where the parameter check runs first, a number or a boolean is refused as not a
string), so the app's effect dialogs and Appearance panel, a step of
`command.batch` and a replayed journal get the same error, and the document and its undo history stay as they were:
```json