@open-pencil/scene-graph 0.17.0 → 0.18.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/behaviours/index.d.ts +3 -2
- package/dist/behaviours/index.js +2 -1
- package/dist/behaviours/kinds.d.ts.map +1 -1
- package/dist/behaviours/kinds.js +15 -8
- package/dist/behaviours/kinds.js.map +1 -1
- package/dist/behaviours/model.d.ts +2 -2
- package/dist/behaviours/model.d.ts.map +1 -1
- package/dist/behaviours/model.js.map +1 -1
- package/dist/behaviours/schema.d.ts +136 -1
- package/dist/behaviours/schema.d.ts.map +1 -1
- package/dist/behaviours/schema.js +17 -2
- package/dist/behaviours/schema.js.map +1 -1
- package/dist/behaviours/spec.d.ts.map +1 -1
- package/dist/behaviours/spec.js +4 -0
- package/dist/behaviours/spec.js.map +1 -1
- package/dist/behaviours/transition.d.ts +23 -0
- package/dist/behaviours/transition.d.ts.map +1 -0
- package/dist/behaviours/transition.js +36 -0
- package/dist/behaviours/transition.js.map +1 -0
- package/dist/checkpoint.d.ts.map +1 -1
- package/dist/checkpoint.js +2 -0
- package/dist/checkpoint.js.map +1 -1
- package/dist/color/index.d.ts +12 -9
- package/dist/color/index.d.ts.map +1 -1
- package/dist/color/index.js +15 -25
- package/dist/color/index.js.map +1 -1
- package/dist/color/management.d.ts +51 -23
- package/dist/color/management.d.ts.map +1 -1
- package/dist/color/management.js +154 -149
- package/dist/color/management.js.map +1 -1
- package/dist/color/okhcl.d.ts +28 -10
- package/dist/color/okhcl.d.ts.map +1 -1
- package/dist/color/okhcl.js +56 -40
- package/dist/color/okhcl.js.map +1 -1
- package/dist/color/schema.d.ts +6 -1
- package/dist/color/schema.d.ts.map +1 -1
- package/dist/color/schema.js +10 -1
- package/dist/color/schema.js.map +1 -1
- package/dist/constraints.d.ts.map +1 -1
- package/dist/constraints.js +2 -1
- package/dist/constraints.js.map +1 -1
- package/dist/copy.d.ts +7 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +22 -1
- package/dist/copy.js.map +1 -1
- package/dist/corners.d.ts +3 -1
- package/dist/corners.d.ts.map +1 -1
- package/dist/corners.js +3 -1
- package/dist/corners.js.map +1 -1
- package/dist/css/grid.js +1 -1
- package/dist/css/index.d.ts +2 -1
- package/dist/css/index.js +3 -2
- package/dist/css/motion.d.ts +12 -0
- package/dist/css/motion.d.ts.map +1 -0
- package/dist/css/motion.js +60 -0
- package/dist/css/motion.js.map +1 -0
- package/dist/css/shadows.d.ts +4 -3
- package/dist/css/shadows.d.ts.map +1 -1
- package/dist/css/shadows.js +7 -6
- package/dist/css/shadows.js.map +1 -1
- package/dist/css/values.d.ts +3 -2
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +4 -4
- package/dist/css/values.js.map +1 -1
- package/dist/fields/index.d.ts +2 -2
- package/dist/fields/index.js +2 -2
- package/dist/fields/layout.d.ts +3 -1
- package/dist/fields/layout.d.ts.map +1 -1
- package/dist/fields/layout.js +9 -1
- package/dist/fields/layout.js.map +1 -1
- package/dist/figjam/palette.js +1 -1
- package/dist/font-style.d.ts.map +1 -1
- package/dist/font-style.js +7 -3
- package/dist/font-style.js.map +1 -1
- package/dist/generated/index.d.ts +6 -2
- package/dist/generated/index.d.ts.map +1 -1
- package/dist/generated/index.js +14 -4
- package/dist/generated/index.js.map +1 -1
- package/dist/geometry.d.ts +6 -1
- package/dist/geometry.d.ts.map +1 -1
- package/dist/geometry.js +10 -2
- package/dist/geometry.js.map +1 -1
- package/dist/graph.d.ts +29 -4
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +59 -9
- package/dist/graph.js.map +1 -1
- package/dist/group-bounds.d.ts +5 -2
- package/dist/group-bounds.d.ts.map +1 -1
- package/dist/group-bounds.js +38 -8
- package/dist/group-bounds.js.map +1 -1
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +19 -8
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/instances/content-size.js +1 -1
- package/dist/instances/fields.d.ts +2 -2
- package/dist/instances/fields.d.ts.map +1 -1
- package/dist/instances/fields.js +2 -0
- package/dist/instances/fields.js.map +1 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +50 -11
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/sync.js +67 -7
- package/dist/instances/sync.js.map +1 -1
- package/dist/layout/flow.d.ts +16 -0
- package/dist/layout/flow.d.ts.map +1 -0
- package/dist/layout/flow.js +34 -0
- package/dist/layout/flow.js.map +1 -0
- package/dist/layout/index.d.ts +4 -3
- package/dist/layout/index.js +4 -3
- package/dist/layout/keywords.d.ts.map +1 -1
- package/dist/layout/keywords.js +7 -1
- package/dist/layout/keywords.js.map +1 -1
- package/dist/layout/sizing.d.ts +9 -1
- package/dist/layout/sizing.d.ts.map +1 -1
- package/dist/layout/sizing.js +17 -1
- package/dist/layout/sizing.js.map +1 -1
- package/dist/layout/spacing.d.ts +7 -1
- package/dist/layout/spacing.d.ts.map +1 -1
- package/dist/layout/spacing.js +10 -1
- package/dist/layout/spacing.js.map +1 -1
- package/dist/mesh/geometry.d.ts +83 -0
- package/dist/mesh/geometry.d.ts.map +1 -0
- package/dist/mesh/geometry.js +441 -0
- package/dist/mesh/geometry.js.map +1 -0
- package/dist/mesh/index.d.ts +4 -0
- package/dist/mesh/index.js +4 -0
- package/dist/mesh/model.d.ts +28 -0
- package/dist/mesh/model.d.ts.map +1 -0
- package/dist/mesh/model.js +40 -0
- package/dist/mesh/model.js.map +1 -0
- package/dist/mesh/schema.d.ts +191 -0
- package/dist/mesh/schema.d.ts.map +1 -0
- package/dist/mesh/schema.js +56 -0
- package/dist/mesh/schema.js.map +1 -0
- package/dist/mutation-impact.d.ts +2 -0
- package/dist/mutation-impact.d.ts.map +1 -1
- package/dist/mutation-impact.js +18 -7
- package/dist/mutation-impact.js.map +1 -1
- package/dist/node-defaults.d.ts +3 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +22 -6
- package/dist/node-defaults.js.map +1 -1
- package/dist/paints/index.d.ts +5 -3
- package/dist/paints/index.js +5 -3
- package/dist/paints/pattern.d.ts +5 -1
- package/dist/paints/pattern.d.ts.map +1 -1
- package/dist/paints/pattern.js +33 -1
- package/dist/paints/pattern.js.map +1 -1
- package/dist/paints/shows.d.ts +4 -1
- package/dist/paints/shows.d.ts.map +1 -1
- package/dist/paints/shows.js +8 -1
- package/dist/paints/shows.js.map +1 -1
- package/dist/paints/solid.d.ts +11 -0
- package/dist/paints/solid.d.ts.map +1 -0
- package/dist/paints/solid.js +18 -0
- package/dist/paints/solid.js.map +1 -0
- package/dist/paints/stills.d.ts +74 -0
- package/dist/paints/stills.d.ts.map +1 -0
- package/dist/paints/stills.js +113 -0
- package/dist/paints/stills.js.map +1 -0
- package/dist/plugin-data/field.d.ts.map +1 -1
- package/dist/plugin-data/field.js +2 -0
- package/dist/plugin-data/field.js.map +1 -1
- package/dist/plugin-data/fields.d.ts +67 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +12 -7
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/resize.d.ts +10 -3
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +34 -3
- package/dist/resize.js.map +1 -1
- package/dist/scaling/index.d.ts +2 -2
- package/dist/scaling/index.js +2 -2
- package/dist/scaling/node.d.ts +2 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +27 -7
- package/dist/scaling/node.js.map +1 -1
- package/dist/shaders/index.d.ts +2 -3
- package/dist/shaders/index.js +2 -3
- package/dist/shaders/model.d.ts +3 -11
- package/dist/shaders/model.d.ts.map +1 -1
- package/dist/shaders/model.js +13 -36
- package/dist/shaders/model.js.map +1 -1
- package/dist/shaders/schema.d.ts +1 -1
- package/dist/shaders/schema.js +1 -1
- package/dist/shaders/schema.js.map +1 -1
- package/dist/shape-with-text/document.js +276 -0
- package/dist/shape-with-text/document.js.map +1 -0
- package/dist/shape-with-text/geometry.d.ts +13 -1
- package/dist/shape-with-text/geometry.d.ts.map +1 -1
- package/dist/shape-with-text/geometry.js +103 -41
- package/dist/shape-with-text/geometry.js.map +1 -1
- package/dist/shape-with-text/index.d.ts +2 -2
- package/dist/shape-with-text/index.js +2 -2
- package/dist/shared-style-index.d.ts +21 -0
- package/dist/shared-style-index.d.ts.map +1 -0
- package/dist/shared-style-index.js +72 -0
- package/dist/shared-style-index.js.map +1 -0
- package/dist/shared-styles.d.ts +3 -3
- package/dist/shared-styles.d.ts.map +1 -1
- package/dist/shared-styles.js +16 -7
- package/dist/shared-styles.js.map +1 -1
- package/dist/slides/drop.d.ts +32 -0
- package/dist/slides/drop.d.ts.map +1 -0
- package/dist/slides/drop.js +81 -0
- package/dist/slides/drop.js.map +1 -0
- package/dist/slides/index.d.ts +56 -0
- package/dist/slides/index.d.ts.map +1 -0
- package/dist/slides/index.js +134 -0
- package/dist/slides/index.js.map +1 -0
- package/dist/slides/motion.d.ts +55 -0
- package/dist/slides/motion.d.ts.map +1 -0
- package/dist/slides/motion.js +142 -0
- package/dist/slides/motion.js.map +1 -0
- package/dist/slides/playback.d.ts +59 -0
- package/dist/slides/playback.d.ts.map +1 -0
- package/dist/slides/playback.js +188 -0
- package/dist/slides/playback.js.map +1 -0
- package/dist/slides/types.d.ts +62 -0
- package/dist/slides/types.d.ts.map +1 -0
- package/dist/slides/types.js +39 -0
- package/dist/slides/types.js.map +1 -0
- package/dist/statuses/workflow.js +1 -1
- package/dist/text/direction.d.ts +6 -3
- package/dist/text/direction.d.ts.map +1 -1
- package/dist/text/direction.js +12 -12
- package/dist/text/direction.js.map +1 -1
- package/dist/text/index.d.ts +2 -1
- package/dist/text/index.js +3 -2
- package/dist/text/name.d.ts +22 -0
- package/dist/text/name.d.ts.map +1 -0
- package/dist/text/name.js +38 -0
- package/dist/text/name.js.map +1 -0
- package/dist/text/references.d.ts.map +1 -1
- package/dist/text/references.js +1 -2
- package/dist/text/references.js.map +1 -1
- package/dist/thumbnail.d.ts +23 -0
- package/dist/thumbnail.d.ts.map +1 -0
- package/dist/thumbnail.js +47 -0
- package/dist/thumbnail.js.map +1 -0
- package/dist/transfer/apply.d.ts.map +1 -1
- package/dist/transfer/apply.js +14 -3
- package/dist/transfer/apply.js.map +1 -1
- package/dist/transfer/copied.d.ts +6 -1
- package/dist/transfer/copied.d.ts.map +1 -1
- package/dist/transfer/copied.js +2 -2
- package/dist/transfer/copied.js.map +1 -1
- package/dist/transfer/dependencies.d.ts +4 -1
- package/dist/transfer/dependencies.d.ts.map +1 -1
- package/dist/transfer/dependencies.js +10 -1
- package/dist/transfer/dependencies.js.map +1 -1
- package/dist/transfer/history.js +2 -0
- package/dist/transfer/history.js.map +1 -1
- package/dist/transfer/identity.js +18 -3
- package/dist/transfer/identity.js.map +1 -1
- package/dist/transfer/index.d.ts +2 -1
- package/dist/transfer/index.js +4 -3
- package/dist/transfer/library.d.ts +45 -0
- package/dist/transfer/library.d.ts.map +1 -0
- package/dist/transfer/library.js +102 -0
- package/dist/transfer/library.js.map +1 -0
- package/dist/transfer/nodes.d.ts.map +1 -1
- package/dist/transfer/nodes.js +9 -0
- package/dist/transfer/nodes.js.map +1 -1
- package/dist/transfer/plan.d.ts +9 -0
- package/dist/transfer/plan.d.ts.map +1 -1
- package/dist/transfer/plan.js +51 -6
- package/dist/transfer/plan.js.map +1 -1
- package/dist/transfer/remove.js +7 -1
- package/dist/transfer/remove.js.map +1 -1
- package/dist/transfer/validation.js +8 -4
- package/dist/transfer/validation.js.map +1 -1
- package/dist/transfer/variables.d.ts.map +1 -1
- package/dist/transfer/variables.js +2 -1
- package/dist/transfer/variables.js.map +1 -1
- package/dist/types.d.ts +90 -12
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +33 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/bindings.d.ts +11 -6
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +162 -38
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/easing.d.ts +48 -0
- package/dist/variables/easing.d.ts.map +1 -0
- package/dist/variables/easing.js +321 -0
- package/dist/variables/easing.js.map +1 -0
- package/dist/variables/formula/index.d.ts +4 -2
- package/dist/variables/formula/index.d.ts.map +1 -1
- package/dist/variables/formula/index.js +26 -9
- package/dist/variables/formula/index.js.map +1 -1
- package/dist/variables/formula/parse.js +1 -1
- package/dist/variables/formula/schema.js +1 -1
- package/dist/variables/formula/signatures.d.ts +2 -2
- package/dist/variables/formula/signatures.d.ts.map +1 -1
- package/dist/variables/formula/signatures.js.map +1 -1
- package/dist/variables/formula/tree.d.ts +2 -2
- package/dist/variables/formula/tree.d.ts.map +1 -1
- package/dist/variables/formula/tree.js.map +1 -1
- package/dist/variables/index.d.ts +22 -5
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +67 -17
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/lookup.js +1 -1
- package/dist/variables/lookup.js.map +1 -1
- package/dist/variables/motion.d.ts +46 -0
- package/dist/variables/motion.d.ts.map +1 -0
- package/dist/variables/motion.js +107 -0
- package/dist/variables/motion.js.map +1 -0
- package/dist/variables/paints.d.ts +10 -2
- package/dist/variables/paints.d.ts.map +1 -1
- package/dist/variables/paints.js +24 -10
- package/dist/variables/paints.js.map +1 -1
- package/dist/variables/resolution.d.ts.map +1 -1
- package/dist/variables/resolution.js +10 -9
- package/dist/variables/resolution.js.map +1 -1
- package/dist/variables/schema.d.ts +59 -1
- package/dist/variables/schema.d.ts.map +1 -1
- package/dist/variables/schema.js +36 -1
- package/dist/variables/schema.js.map +1 -1
- package/dist/variables/style-bindings.d.ts +23 -0
- package/dist/variables/style-bindings.d.ts.map +1 -0
- package/dist/variables/style-bindings.js +83 -0
- package/dist/variables/style-bindings.js.map +1 -0
- package/package.json +21 -2
- package/src/behaviours/index.ts +1 -0
- package/src/behaviours/kinds.ts +5 -1
- package/src/behaviours/model.ts +2 -2
- package/src/behaviours/schema.ts +19 -2
- package/src/behaviours/spec.ts +5 -0
- package/src/behaviours/transition.ts +46 -0
- package/src/checkpoint.ts +2 -0
- package/src/color/index.ts +15 -24
- package/src/color/management.ts +144 -168
- package/src/color/okhcl.ts +62 -43
- package/src/color/schema.ts +13 -0
- package/src/constraints.ts +2 -1
- package/src/copy.ts +29 -0
- package/src/corners.ts +3 -0
- package/src/css/index.ts +1 -0
- package/src/css/motion.ts +51 -0
- package/src/css/shadows.ts +7 -6
- package/src/css/values.ts +7 -3
- package/src/fields/layout.ts +9 -0
- package/src/font-style.ts +8 -3
- package/src/generated/index.ts +15 -3
- package/src/geometry.ts +13 -1
- package/src/graph.ts +72 -9
- package/src/group-bounds.ts +54 -13
- package/src/hit-test.ts +13 -8
- package/src/instances/fields.ts +2 -0
- package/src/instances/index.ts +71 -12
- package/src/instances/sync.ts +93 -11
- package/src/layout/flow.ts +34 -0
- package/src/layout/index.ts +1 -0
- package/src/layout/keywords.ts +7 -1
- package/src/layout/sizing.ts +30 -0
- package/src/layout/spacing.ts +12 -0
- package/src/mesh/geometry.ts +433 -0
- package/src/mesh/index.ts +3 -0
- package/src/mesh/model.ts +50 -0
- package/src/mesh/schema.ts +81 -0
- package/src/mutation-impact.ts +23 -7
- package/src/node-defaults.ts +22 -5
- package/src/paints/index.ts +2 -0
- package/src/paints/pattern.ts +55 -0
- package/src/paints/shows.ts +9 -0
- package/src/paints/solid.ts +10 -0
- package/src/paints/stills.ts +182 -0
- package/src/plugin-data/field.ts +3 -0
- package/src/plugin-data/fields.ts +15 -2
- package/src/resize.ts +32 -20
- package/src/scaling/index.ts +1 -1
- package/src/scaling/node.ts +43 -6
- package/src/shaders/index.ts +0 -1
- package/src/shaders/model.ts +19 -49
- package/src/shaders/schema.ts +1 -1
- package/src/shape-with-text/document.ts +175 -0
- package/src/shape-with-text/geometry.ts +99 -27
- package/src/shared-style-index.ts +106 -0
- package/src/shared-styles.ts +21 -14
- package/src/slides/drop.ts +94 -0
- package/src/slides/index.ts +125 -0
- package/src/slides/motion.ts +169 -0
- package/src/slides/playback.ts +200 -0
- package/src/slides/types.ts +94 -0
- package/src/text/direction.ts +13 -23
- package/src/text/index.ts +1 -0
- package/src/text/name.ts +39 -0
- package/src/text/references.ts +3 -3
- package/src/thumbnail.ts +52 -0
- package/src/transfer/apply.ts +16 -3
- package/src/transfer/copied.ts +7 -2
- package/src/transfer/dependencies.ts +10 -1
- package/src/transfer/history.ts +2 -0
- package/src/transfer/identity.ts +34 -2
- package/src/transfer/index.ts +6 -0
- package/src/transfer/library.ts +153 -0
- package/src/transfer/nodes.ts +20 -0
- package/src/transfer/plan.ts +96 -10
- package/src/transfer/remove.ts +6 -2
- package/src/transfer/validation.ts +15 -7
- package/src/transfer/variables.ts +8 -2
- package/src/types.ts +114 -10
- package/src/variables/bindings.ts +260 -36
- package/src/variables/easing.ts +282 -0
- package/src/variables/formula/index.ts +33 -14
- package/src/variables/formula/signatures.ts +6 -2
- package/src/variables/formula/tree.ts +3 -3
- package/src/variables/index.ts +90 -37
- package/src/variables/lookup.ts +1 -1
- package/src/variables/motion.ts +151 -0
- package/src/variables/paints.ts +29 -7
- package/src/variables/resolution.ts +11 -5
- package/src/variables/schema.ts +41 -0
- package/src/variables/style-bindings.ts +137 -0
- package/dist/shaders/still.d.ts +0 -18
- package/dist/shaders/still.d.ts.map +0 -1
- package/dist/shaders/still.js +0 -36
- package/dist/shaders/still.js.map +0 -1
- package/src/shaders/still.ts +0 -41
package/dist/color/management.js
CHANGED
|
@@ -1,187 +1,192 @@
|
|
|
1
|
+
import { BLACK } from "../constants.js";
|
|
1
2
|
import { normalizeColor } from "./normalize.js";
|
|
2
|
-
import {
|
|
3
|
-
import { converter, formatCss, formatRgb, inGamut, parse, toGamut } from "culori";
|
|
3
|
+
import { converter, formatCss, formatRgb, parse, toGamut } from "culori";
|
|
4
4
|
//#region src/color/management.ts
|
|
5
|
-
const
|
|
5
|
+
const toLinearSRGB = converter("lrgb");
|
|
6
6
|
const toRGB = converter("rgb");
|
|
7
7
|
const toP3 = converter("p3");
|
|
8
8
|
const toDisplayableRGB = toGamut("rgb", "oklch");
|
|
9
9
|
const toDisplayableP3 = toGamut("p3", "oklch");
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
/** Whether `color`'s components fit 0 to 1, give or take round-off. */
|
|
11
|
+
function fitsGamut(color) {
|
|
12
|
+
return [
|
|
13
|
+
color.r,
|
|
14
|
+
color.g,
|
|
15
|
+
color.b
|
|
16
|
+
].every((channel) => channel >= -1e-6 && channel <= 1.000001);
|
|
17
|
+
}
|
|
18
|
+
/** `color`, a culori color in any space, as components in `profile`, past 0 and 1 if need be. */
|
|
19
|
+
function exactlyInProfile(color, profile) {
|
|
20
|
+
const { r, g, b } = profile === "display-p3" ? toP3(color) : toRGB(color);
|
|
14
21
|
return {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
alpha: Math.max(0, Math.min(1, color.a ?? 1))
|
|
22
|
+
r,
|
|
23
|
+
g,
|
|
24
|
+
b,
|
|
25
|
+
a: color.alpha ?? 1
|
|
20
26
|
};
|
|
21
27
|
}
|
|
22
28
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
29
|
+
* `color`, a culori color in any space, as components in `profile`, brought inside it the way CSS
|
|
30
|
+
* maps out-of-gamut colors, by reducing chroma in OKLCH so lightness and hue hold, and whether it
|
|
31
|
+
* had to be. A color inside already keeps its components, as mapping it would shift them by float
|
|
32
|
+
* noise; one on the gamut's edge, as every saturated primary is, counts as inside despite
|
|
33
|
+
* round-off. A missing (`none`) component reads as zero.
|
|
25
34
|
*/
|
|
26
|
-
function
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
alpha: color.a
|
|
35
|
+
function fitIntoProfile(color, profile) {
|
|
36
|
+
const exact = normalizeColor(exactlyInProfile(color, profile));
|
|
37
|
+
const clipped = !fitsGamut(exact);
|
|
38
|
+
const inside = clipped ? normalizeColor(exactlyInProfile(profile === "display-p3" ? toDisplayableP3(color) : toDisplayableRGB(color), profile)) : exact;
|
|
39
|
+
const clamp = (channel) => Math.min(1, Math.max(0, channel));
|
|
40
|
+
return {
|
|
41
|
+
color: {
|
|
42
|
+
r: clamp(inside.r),
|
|
43
|
+
g: clamp(inside.g),
|
|
44
|
+
b: clamp(inside.b),
|
|
45
|
+
a: color.alpha ?? 1
|
|
46
|
+
},
|
|
47
|
+
clipped
|
|
40
48
|
};
|
|
41
|
-
const converted = to === "display-p3" ? toP3(source) : toRGB(source);
|
|
42
|
-
return normalizeColor({
|
|
43
|
-
r: converted.r,
|
|
44
|
-
g: converted.g,
|
|
45
|
-
b: converted.b,
|
|
46
|
-
a: converted.alpha ?? color.a
|
|
47
|
-
});
|
|
48
49
|
}
|
|
49
|
-
function
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
alpha: color.a
|
|
56
|
-
});
|
|
57
|
-
return !isDisplayableRGB({
|
|
58
|
-
mode: "rgb",
|
|
50
|
+
function insideProfile(color, profile) {
|
|
51
|
+
return fitIntoProfile(color, profile).color;
|
|
52
|
+
}
|
|
53
|
+
function culoriColor(color, profile) {
|
|
54
|
+
return {
|
|
55
|
+
mode: profile === "display-p3" ? "p3" : "rgb",
|
|
59
56
|
r: color.r,
|
|
60
57
|
g: color.g,
|
|
61
58
|
b: color.b,
|
|
62
59
|
alpha: color.a
|
|
63
|
-
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
/** `color`, whose components are in `from`, as the components in `to` that show the same color. */
|
|
63
|
+
function convertColor(color, from, to, gamut = "extend") {
|
|
64
|
+
if (from === to) return color;
|
|
65
|
+
const source = culoriColor(color, from);
|
|
66
|
+
return gamut === "extend" ? exactlyInProfile(source, to) : insideProfile(source, to);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* `color`, whose components are in `profile`, as the sRGB components of the same color: past 0 and
|
|
70
|
+
* 1 where it lies outside sRGB. Skia takes colors this way and converts them into the space of
|
|
71
|
+
* whatever it draws on, so a wide-gamut color reaches it whole.
|
|
72
|
+
*/
|
|
73
|
+
function toExtendedSRGB(color, profile) {
|
|
74
|
+
if (profile === "srgb") return color;
|
|
75
|
+
const m = P3_TO_LINEAR_SRGB;
|
|
76
|
+
const r = decodeSRGB(color.r);
|
|
77
|
+
const g = decodeSRGB(color.g);
|
|
78
|
+
const b = decodeSRGB(color.b);
|
|
79
|
+
return {
|
|
80
|
+
r: encodeSRGB(m[0] * r + m[1] * g + m[2] * b),
|
|
81
|
+
g: encodeSRGB(m[3] * r + m[4] * g + m[5] * b),
|
|
82
|
+
b: encodeSRGB(m[6] * r + m[7] * g + m[8] * b),
|
|
83
|
+
a: color.a
|
|
84
|
+
};
|
|
64
85
|
}
|
|
65
|
-
|
|
66
|
-
|
|
86
|
+
/** The sRGB transfer curve, mirrored below zero as extended sRGB and culori's `lrgb` take it. */
|
|
87
|
+
function decodeSRGB(channel) {
|
|
88
|
+
const magnitude = Math.abs(channel);
|
|
89
|
+
if (magnitude <= .04045) return channel / 12.92;
|
|
90
|
+
return (Math.sign(channel) || 1) * ((magnitude + .055) / 1.055) ** 2.4;
|
|
67
91
|
}
|
|
68
|
-
function
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
92
|
+
function encodeSRGB(channel) {
|
|
93
|
+
const magnitude = Math.abs(channel);
|
|
94
|
+
if (magnitude <= .0031308) return channel * 12.92;
|
|
95
|
+
return (Math.sign(channel) || 1) * (1.055 * magnitude ** (1 / 2.4) - .055);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* `color`, whose components are in `from`, as CSS showing it in `to`: `color(display-p3 …)` for
|
|
99
|
+
* Display P3, `rgb()` for sRGB, where a color outside sRGB clips per channel as a canvas clips it.
|
|
100
|
+
*/
|
|
101
|
+
function colorToProfileCSS(color, from, to) {
|
|
102
|
+
const shown = convertColor(color, from, to);
|
|
103
|
+
if (to === "display-p3") return formatCss({
|
|
104
|
+
mode: "p3",
|
|
105
|
+
r: shown.r,
|
|
106
|
+
g: shown.g,
|
|
107
|
+
b: shown.b,
|
|
108
|
+
alpha: shown.a
|
|
109
|
+
});
|
|
85
110
|
return formatRgb({
|
|
86
111
|
mode: "rgb",
|
|
87
|
-
r:
|
|
88
|
-
g:
|
|
89
|
-
b:
|
|
90
|
-
alpha:
|
|
112
|
+
r: shown.r,
|
|
113
|
+
g: shown.g,
|
|
114
|
+
b: shown.b,
|
|
115
|
+
alpha: shown.a
|
|
91
116
|
});
|
|
92
117
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
a: rgb.alpha ?? oklch.alpha
|
|
125
|
-
});
|
|
126
|
-
return {
|
|
127
|
-
color: resolved,
|
|
128
|
-
cssColor: formatRgb({
|
|
129
|
-
mode: "rgb",
|
|
130
|
-
r: resolved.r,
|
|
131
|
-
g: resolved.g,
|
|
132
|
-
b: resolved.b,
|
|
133
|
-
alpha: resolved.a
|
|
134
|
-
}),
|
|
135
|
-
sourceSpace: "oklch",
|
|
136
|
-
targetSpace,
|
|
137
|
-
clipped
|
|
138
|
-
};
|
|
118
|
+
/**
|
|
119
|
+
* The 3×3 matrix, row by row, taking linear components in `profile` to linear sRGB ones: its
|
|
120
|
+
* columns are the profile's primaries in linear sRGB.
|
|
121
|
+
*/
|
|
122
|
+
function linearSRGBMatrix(profile) {
|
|
123
|
+
const mode = profile === "display-p3" ? "p3" : "rgb";
|
|
124
|
+
const columns = [
|
|
125
|
+
{
|
|
126
|
+
r: 1,
|
|
127
|
+
g: 0,
|
|
128
|
+
b: 0
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
r: 0,
|
|
132
|
+
g: 1,
|
|
133
|
+
b: 0
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
r: 0,
|
|
137
|
+
g: 0,
|
|
138
|
+
b: 1
|
|
139
|
+
}
|
|
140
|
+
].map((primary) => toLinearSRGB({
|
|
141
|
+
mode,
|
|
142
|
+
...primary
|
|
143
|
+
}));
|
|
144
|
+
return [
|
|
145
|
+
"r",
|
|
146
|
+
"g",
|
|
147
|
+
"b"
|
|
148
|
+
].flatMap((row) => columns.map((column) => column[row]));
|
|
139
149
|
}
|
|
150
|
+
/** Display P3's matrix, made once: renderers convert every paint they draw through it. */
|
|
151
|
+
const P3_TO_LINEAR_SRGB = linearSRGBMatrix("display-p3");
|
|
140
152
|
/**
|
|
141
153
|
* A CSS color in any space, mapped into sRGB the way CSS maps out-of-gamut colors: by reducing
|
|
142
154
|
* chroma in OKLCH, so a Display P3 red becomes the nearest sRGB red, not a clipped one. A missing
|
|
143
155
|
* (`none`) component reads as zero. Null when `input` is not a color.
|
|
144
156
|
*/
|
|
145
157
|
function parseDisplayableColor(input) {
|
|
158
|
+
const parsed = parse(input);
|
|
159
|
+
return parsed ? insideProfile(parsed, "srgb") : null;
|
|
160
|
+
}
|
|
161
|
+
/** Syntaxes CSS reads as sRGB numbers: hex, `rgb()`, `hsl()`, `hwb()`, names, `color(srgb …)`. */
|
|
162
|
+
const SRGB_SYNTAX_MODES = /* @__PURE__ */ new Set([
|
|
163
|
+
"rgb",
|
|
164
|
+
"hsl",
|
|
165
|
+
"hwb"
|
|
166
|
+
]);
|
|
167
|
+
/**
|
|
168
|
+
* A CSS color as components in `profile`, or null when `input` is not one. Hex and the other sRGB
|
|
169
|
+
* syntaxes give their numbers as they are, as Figma takes typed or imported hex as the document's
|
|
170
|
+
* own numbers in either profile. A color that names its space, such as `color(display-p3 …)` or
|
|
171
|
+
* `oklch()`, is converted into `profile`, and brought inside it by reducing chroma when the
|
|
172
|
+
* profile cannot show it.
|
|
173
|
+
*/
|
|
174
|
+
function tryParseColor(input, profile = "srgb") {
|
|
146
175
|
const parsed = parse(input);
|
|
147
176
|
if (!parsed) return null;
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
a: parsed.alpha
|
|
177
|
+
if (Object.values(parsed).some((value) => typeof value === "number" && !Number.isFinite(value))) return null;
|
|
178
|
+
const a = parsed.alpha ?? 1;
|
|
179
|
+
if (SRGB_SYNTAX_MODES.has(parsed.mode)) return normalizeColor({
|
|
180
|
+
...toRGB(parsed),
|
|
181
|
+
a
|
|
154
182
|
});
|
|
183
|
+
return insideProfile(parsed, profile);
|
|
155
184
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const resolved = convertComponents(normalizeColor(color), documentColorSpace, targetSpace);
|
|
160
|
-
return {
|
|
161
|
-
color: resolved,
|
|
162
|
-
cssColor: formatCSSForTarget(resolved, targetSpace),
|
|
163
|
-
sourceSpace: "srgb",
|
|
164
|
-
targetSpace,
|
|
165
|
-
clipped: targetSpace !== documentColorSpace && isOutsideGamut(resolved, targetSpace)
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
function resolveNodeFillColor(fill, fillIndex, node, options) {
|
|
169
|
-
const okhcl = getFillOkHCL(node, fillIndex)?.color;
|
|
170
|
-
if (okhcl) return resolveOkHCLForPreview(okhcl, options);
|
|
171
|
-
return resolveRGBAForPreview(fill.color, options);
|
|
172
|
-
}
|
|
173
|
-
function resolveNodeStrokeColor(stroke, strokeIndex, node, options) {
|
|
174
|
-
const okhcl = getStrokeOkHCL(node, strokeIndex)?.color;
|
|
175
|
-
if (okhcl) return resolveOkHCLForPreview(okhcl, options);
|
|
176
|
-
return resolveRGBAForPreview(stroke.color, options);
|
|
177
|
-
}
|
|
178
|
-
function colorToDisplayCSS(color, options) {
|
|
179
|
-
return resolveRGBAForPreview(color, options).cssColor;
|
|
180
|
-
}
|
|
181
|
-
function getDefaultRenderColorSpace() {
|
|
182
|
-
return DEFAULT_COLOR_SPACE;
|
|
185
|
+
/** A CSS color as components in `profile`, or black when `input` is not one. */
|
|
186
|
+
function parseColor(input, profile = "srgb") {
|
|
187
|
+
return tryParseColor(input, profile) ?? { ...BLACK };
|
|
183
188
|
}
|
|
184
189
|
//#endregion
|
|
185
|
-
export {
|
|
190
|
+
export { colorToProfileCSS, convertColor, fitIntoProfile, fitsGamut, linearSRGBMatrix, parseColor, parseDisplayableColor, toExtendedSRGB, tryParseColor };
|
|
186
191
|
|
|
187
192
|
//# sourceMappingURL=management.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"management.js","names":[],"sources":["../../src/color/management.ts"],"sourcesContent":["import { converter, formatCss, formatRgb, inGamut, parse, toGamut } from 'culori'\n\nimport type { Color } from '../primitives'\nimport type { DocumentColorSpace, Fill, SceneNode, Stroke } from '../types'\nimport { normalizeColor } from './normalize'\nimport { getFillOkHCL, getStrokeOkHCL, type OkHCLColor } from './okhcl'\n\nexport type RenderColorSpace = 'srgb' | 'display-p3'\nexport type ColorIntentSpace = 'oklch' | 'srgb'\n\nexport interface ColorPreviewOptions {\n colorSpace?: RenderColorSpace\n documentColorSpace?: DocumentColorSpace\n}\n\nexport interface ResolvedRenderColor {\n color: Color\n cssColor: string\n sourceSpace: ColorIntentSpace\n targetSpace: RenderColorSpace\n clipped: boolean\n}\n\nconst DEFAULT_COLOR_SPACE: RenderColorSpace = 'srgb'\n\nconst toRGB = converter('rgb')\nconst toP3 = converter('p3')\nconst toDisplayableRGB = toGamut('rgb', 'oklch')\nconst toDisplayableP3 = toGamut('p3', 'oklch')\nconst isDisplayableRGB = inGamut('rgb')\nconst isDisplayableP3 = inGamut('p3')\n\nfunction normalizeOkLCH(color: OkHCLColor) {\n const hue = color.h % 360\n return {\n mode: 'oklch' as const,\n l: Math.max(0, Math.min(1, color.l)),\n c: Math.max(0, color.c),\n h: hue < 0 ? hue + 360 : hue,\n alpha: Math.max(0, Math.min(1, color.a ?? 1))\n }\n}\n\n/**\n * Numbers are stored in the document's profile, so painting into a surface with a different\n * profile has to convert them. Same profile means no work, which is the common case.\n */\nfunction convertComponents(color: Color, from: RenderColorSpace, to: RenderColorSpace): Color {\n if (from === to) return color\n const source =\n from === 'display-p3'\n ? { mode: 'p3' as const, r: color.r, g: color.g, b: color.b, alpha: color.a }\n : { mode: 'rgb' as const, r: color.r, g: color.g, b: color.b, alpha: color.a }\n const converted = to === 'display-p3' ? toP3(source) : toRGB(source)\n return normalizeColor({\n r: converted.r,\n g: converted.g,\n b: converted.b,\n a: converted.alpha ?? color.a\n })\n}\n\nfunction isOutsideGamut(color: Color, space: RenderColorSpace): boolean {\n if (space === 'display-p3') {\n return !isDisplayableP3({ mode: 'p3', r: color.r, g: color.g, b: color.b, alpha: color.a })\n }\n return !isDisplayableRGB({ mode: 'rgb', r: color.r, g: color.g, b: color.b, alpha: color.a })\n}\n\nfunction resolveTargetSpace(options?: ColorPreviewOptions): RenderColorSpace {\n return options?.colorSpace ?? options?.documentColorSpace ?? DEFAULT_COLOR_SPACE\n}\n\nfunction formatCSSForTarget(color: Color, targetSpace: RenderColorSpace): string {\n if (targetSpace === 'display-p3') {\n const p3 = toP3({ mode: 'rgb', r: color.r, g: color.g, b: color.b, alpha: color.a })\n return formatCss({\n mode: 'p3',\n r: p3.r,\n g: p3.g,\n b: p3.b,\n alpha: p3.alpha ?? color.a\n })\n }\n\n return formatRgb({\n mode: 'rgb',\n r: color.r,\n g: color.g,\n b: color.b,\n alpha: color.a\n })\n}\n\nexport function resolveOkHCLForPreview(\n color: OkHCLColor,\n options?: ColorPreviewOptions\n): ResolvedRenderColor {\n const oklch = normalizeOkLCH(color)\n const targetSpace = resolveTargetSpace(options)\n\n if (targetSpace === 'display-p3') {\n const clipped = !isDisplayableP3(oklch)\n const p3 = toP3(toDisplayableP3(oklch))\n // P3 coordinates, because the caller asked for a P3 target.\n const resolved = normalizeColor({\n r: p3.r,\n g: p3.g,\n b: p3.b,\n a: p3.alpha ?? oklch.alpha\n })\n\n return {\n color: resolved,\n cssColor: formatCss({\n mode: 'p3',\n r: p3.r,\n g: p3.g,\n b: p3.b,\n alpha: p3.alpha ?? oklch.alpha\n }),\n sourceSpace: 'oklch',\n targetSpace,\n clipped\n }\n }\n\n const clipped = !isDisplayableRGB(oklch)\n const rgb = toRGB(toDisplayableRGB(oklch))\n const resolved = normalizeColor({\n r: rgb.r,\n g: rgb.g,\n b: rgb.b,\n a: rgb.alpha ?? oklch.alpha\n })\n\n return {\n color: resolved,\n cssColor: formatRgb({\n mode: 'rgb',\n r: resolved.r,\n g: resolved.g,\n b: resolved.b,\n alpha: resolved.a\n }),\n sourceSpace: 'oklch',\n targetSpace,\n clipped\n }\n}\n\n/**\n * A CSS color in any space, mapped into sRGB the way CSS maps out-of-gamut colors: by reducing\n * chroma in OKLCH, so a Display P3 red becomes the nearest sRGB red, not a clipped one. A missing\n * (`none`) component reads as zero. Null when `input` is not a color.\n */\nexport function parseDisplayableColor(input: string): Color | null {\n const parsed = parse(input)\n if (!parsed) return null\n // Mapping through OKLCH shifts colors already in sRGB by float noise, so only map the rest.\n const rgb = isDisplayableRGB(parsed) ? toRGB(parsed) : toDisplayableRGB(parsed)\n // culori leaves a `none` channel out, which normalizing reads as zero.\n return normalizeColor({ r: rgb.r, g: rgb.g, b: rgb.b, a: parsed.alpha })\n}\n\nexport function resolveRGBAForPreview(\n color: Color,\n options?: ColorPreviewOptions\n): ResolvedRenderColor {\n const documentColorSpace = options?.documentColorSpace ?? DEFAULT_COLOR_SPACE\n const targetSpace = options?.colorSpace ?? documentColorSpace\n const resolved = convertComponents(normalizeColor(color), documentColorSpace, targetSpace)\n return {\n color: resolved,\n cssColor: formatCSSForTarget(resolved, targetSpace),\n sourceSpace: 'srgb',\n targetSpace,\n clipped: targetSpace !== documentColorSpace && isOutsideGamut(resolved, targetSpace)\n }\n}\n\nexport function resolveNodeFillColor(\n fill: Fill,\n fillIndex: number,\n node: SceneNode,\n options?: ColorPreviewOptions\n): ResolvedRenderColor {\n const okhcl = getFillOkHCL(node, fillIndex)?.color\n if (okhcl) return resolveOkHCLForPreview(okhcl, options)\n return resolveRGBAForPreview(fill.color, options)\n}\n\nexport function resolveNodeStrokeColor(\n stroke: Stroke,\n strokeIndex: number,\n node: SceneNode,\n options?: ColorPreviewOptions\n): ResolvedRenderColor {\n const okhcl = getStrokeOkHCL(node, strokeIndex)?.color\n if (okhcl) return resolveOkHCLForPreview(okhcl, options)\n return resolveRGBAForPreview(stroke.color, options)\n}\n\nexport function colorToDisplayCSS(color: Color, options?: ColorPreviewOptions): string {\n return resolveRGBAForPreview(color, options).cssColor\n}\n\nexport function getDefaultRenderColorSpace(): RenderColorSpace {\n return DEFAULT_COLOR_SPACE\n}\n"],"mappings":";;;;AAuBA,MAAM,sBAAwC;AAE9C,MAAM,QAAQ,UAAU,KAAK;AAC7B,MAAM,OAAO,UAAU,IAAI;AAC3B,MAAM,mBAAmB,QAAQ,OAAO,OAAO;AAC/C,MAAM,kBAAkB,QAAQ,MAAM,OAAO;AAC7C,MAAM,mBAAmB,QAAQ,KAAK;AACtC,MAAM,kBAAkB,QAAQ,IAAI;AAEpC,SAAS,eAAe,OAAmB;CACzC,MAAM,MAAM,MAAM,IAAI;CACtB,OAA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MAAM,iBAAiB,MAAM,IAAI,MAAM,MAAM,IAAI,iBAAiB,MAAM;CAE9E,OAAO,eAAe;EAAE,GAAG,IAAI;EAAG,GAAG,IAAI;EAAG,GAAG,IAAI;EAAG,GAAG,OAAO;CAAM,CAAC;AACzE;AAEA,SAAgB,sBACd,OACA,SACqB;CACrB,MAAM,qBAAqB,SAAS,sBAAsB;CAC1D,MAAM,cAAc,SAAS,cAAc;CAC3C,MAAM,WAAW,kBAAkB,eAAe,KAAK,GAAG,oBAAoB,WAAW;CACzF,OAAO;EACL,OAAO;EACP,UAAU,mBAAmB,UAAU,WAAW;EAClD,aAAa;EACb;EACA,SAAS,gBAAgB,sBAAsB,eAAe,UAAU,WAAW;CACrF;AACF;AAEA,SAAgB,qBACd,MACA,WACA,MACA,SACqB;CACrB,MAAM,QAAQ,aAAa,MAAM,SAAS,CAAC,EAAE;CAC7C,IAAI,OAAO,OAAO,uBAAuB,OAAO,OAAO;CACvD,OAAO,sBAAsB,KAAK,OAAO,OAAO;AAClD;AAEA,SAAgB,uBACd,QACA,aACA,MACA,SACqB;CACrB,MAAM,QAAQ,eAAe,MAAM,WAAW,CAAC,EAAE;CACjD,IAAI,OAAO,OAAO,uBAAuB,OAAO,OAAO;CACvD,OAAO,sBAAsB,OAAO,OAAO,OAAO;AACpD;AAEA,SAAgB,kBAAkB,OAAc,SAAuC;CACrF,OAAO,sBAAsB,OAAO,OAAO,CAAC,CAAC;AAC/C;AAEA,SAAgB,6BAA+C;CAC7D,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"management.js","names":[],"sources":["../../src/color/management.ts"],"sourcesContent":["import { converter, formatCss, formatRgb, parse, toGamut, type Color as CuloriColor } from 'culori'\n\nimport { BLACK } from '../constants'\nimport type { Color } from '../primitives'\nimport type { ColorProfile } from '../types'\nimport { normalizeColor } from './normalize'\n\nconst toLinearSRGB = converter('lrgb')\nconst toRGB = converter('rgb')\nconst toP3 = converter('p3')\nconst toDisplayableRGB = toGamut('rgb', 'oklch')\nconst toDisplayableP3 = toGamut('p3', 'oklch')\n\n/**\n * How far past 0 or 1 a component may land and still count as inside a gamut. Conversions round\n * off: P3 red read through OKLCH comes back with a blue of -1e-15, which is the same color.\n */\nconst GAMUT_TOLERANCE = 1e-6\n\n/** Whether `color`'s components fit 0 to 1, give or take round-off. */\nexport function fitsGamut(color: Pick<Color, 'r' | 'g' | 'b'>): boolean {\n return [color.r, color.g, color.b].every(\n (channel) => channel >= -GAMUT_TOLERANCE && channel <= 1 + GAMUT_TOLERANCE\n )\n}\n\n/** `color`, a culori color in any space, as components in `profile`, past 0 and 1 if need be. */\nfunction exactlyInProfile(color: CuloriColor, profile: ColorProfile): Color {\n const { r, g, b } = profile === 'display-p3' ? toP3(color) : toRGB(color)\n return { r, g, b, a: color.alpha ?? 1 }\n}\n\n/**\n * `color`, a culori color in any space, as components in `profile`, brought inside it the way CSS\n * maps out-of-gamut colors, by reducing chroma in OKLCH so lightness and hue hold, and whether it\n * had to be. A color inside already keeps its components, as mapping it would shift them by float\n * noise; one on the gamut's edge, as every saturated primary is, counts as inside despite\n * round-off. A missing (`none`) component reads as zero.\n */\nexport function fitIntoProfile(\n color: CuloriColor,\n profile: ColorProfile\n): { color: Color; clipped: boolean } {\n const exact = normalizeColor(exactlyInProfile(color, profile))\n const clipped = !fitsGamut(exact)\n const p3 = profile === 'display-p3'\n const inside = clipped\n ? normalizeColor(\n exactlyInProfile(p3 ? toDisplayableP3(color) : toDisplayableRGB(color), profile)\n )\n : exact\n const clamp = (channel: number) => Math.min(1, Math.max(0, channel))\n return {\n color: { r: clamp(inside.r), g: clamp(inside.g), b: clamp(inside.b), a: color.alpha ?? 1 },\n clipped\n }\n}\n\nfunction insideProfile(color: CuloriColor, profile: ColorProfile): Color {\n return fitIntoProfile(color, profile).color\n}\n\nfunction culoriColor(color: Color, profile: ColorProfile): CuloriColor {\n const mode = profile === 'display-p3' ? 'p3' : 'rgb'\n return { mode, r: color.r, g: color.g, b: color.b, alpha: color.a }\n}\n\n/**\n * What to do with a color the target profile cannot show: `extend` keeps its components past 0\n * and 1, for a renderer such as Skia that takes extended sRGB; `map` brings it inside by reducing\n * chroma in OKLCH, as CSS maps out-of-gamut colors, keeping its lightness and hue.\n */\nexport type GamutHandling = 'extend' | 'map'\n\n/** `color`, whose components are in `from`, as the components in `to` that show the same color. */\nexport function convertColor(\n color: Color,\n from: ColorProfile,\n to: ColorProfile,\n gamut: GamutHandling = 'extend'\n): Color {\n if (from === to) return color\n const source = culoriColor(color, from)\n return gamut === 'extend' ? exactlyInProfile(source, to) : insideProfile(source, to)\n}\n\n/**\n * `color`, whose components are in `profile`, as the sRGB components of the same color: past 0 and\n * 1 where it lies outside sRGB. Skia takes colors this way and converts them into the space of\n * whatever it draws on, so a wide-gamut color reaches it whole.\n */\nexport function toExtendedSRGB(color: Color, profile: ColorProfile): Color {\n if (profile === 'srgb') return color\n // Renderers call this for every paint they draw, so it skips culori's color objects: Display P3\n // shares sRGB's transfer curve, leaving one matrix between the linear components.\n // Indexed reads of a plain array: destructuring costs more than the conversion.\n const m = P3_TO_LINEAR_SRGB\n const r = decodeSRGB(color.r)\n const g = decodeSRGB(color.g)\n const b = decodeSRGB(color.b)\n return {\n r: encodeSRGB(m[0] * r + m[1] * g + m[2] * b),\n g: encodeSRGB(m[3] * r + m[4] * g + m[5] * b),\n b: encodeSRGB(m[6] * r + m[7] * g + m[8] * b),\n a: color.a\n }\n}\n\n/** The sRGB transfer curve, mirrored below zero as extended sRGB and culori's `lrgb` take it. */\nfunction decodeSRGB(channel: number): number {\n const magnitude = Math.abs(channel)\n if (magnitude <= 0.04045) return channel / 12.92\n return (Math.sign(channel) || 1) * ((magnitude + 0.055) / 1.055) ** 2.4\n}\n\nfunction encodeSRGB(channel: number): number {\n const magnitude = Math.abs(channel)\n if (magnitude <= 0.0031308) return channel * 12.92\n return (Math.sign(channel) || 1) * (1.055 * magnitude ** (1 / 2.4) - 0.055)\n}\n\n/**\n * `color`, whose components are in `from`, as CSS showing it in `to`: `color(display-p3 …)` for\n * Display P3, `rgb()` for sRGB, where a color outside sRGB clips per channel as a canvas clips it.\n */\nexport function colorToProfileCSS(color: Color, from: ColorProfile, to: ColorProfile): string {\n const shown = convertColor(color, from, to)\n if (to === 'display-p3') {\n return formatCss({ mode: 'p3', r: shown.r, g: shown.g, b: shown.b, alpha: shown.a })\n }\n return formatRgb({ mode: 'rgb', r: shown.r, g: shown.g, b: shown.b, alpha: shown.a })\n}\n\n/**\n * The 3×3 matrix, row by row, taking linear components in `profile` to linear sRGB ones: its\n * columns are the profile's primaries in linear sRGB.\n */\nexport function linearSRGBMatrix(profile: ColorProfile): number[] {\n const mode = profile === 'display-p3' ? 'p3' : 'rgb'\n const columns = [\n { r: 1, g: 0, b: 0 },\n { r: 0, g: 1, b: 0 },\n { r: 0, g: 0, b: 1 }\n ].map((primary) => toLinearSRGB({ mode, ...primary }))\n return (['r', 'g', 'b'] as const).flatMap((row) => columns.map((column) => column[row]))\n}\n\n/** Display P3's matrix, made once: renderers convert every paint they draw through it. */\nconst P3_TO_LINEAR_SRGB = linearSRGBMatrix('display-p3')\n\n/**\n * A CSS color in any space, mapped into sRGB the way CSS maps out-of-gamut colors: by reducing\n * chroma in OKLCH, so a Display P3 red becomes the nearest sRGB red, not a clipped one. A missing\n * (`none`) component reads as zero. Null when `input` is not a color.\n */\nexport function parseDisplayableColor(input: string): Color | null {\n const parsed = parse(input)\n return parsed ? insideProfile(parsed, 'srgb') : null\n}\n\n/** Syntaxes CSS reads as sRGB numbers: hex, `rgb()`, `hsl()`, `hwb()`, names, `color(srgb …)`. */\nconst SRGB_SYNTAX_MODES = new Set(['rgb', 'hsl', 'hwb'])\n\n/**\n * A CSS color as components in `profile`, or null when `input` is not one. Hex and the other sRGB\n * syntaxes give their numbers as they are, as Figma takes typed or imported hex as the document's\n * own numbers in either profile. A color that names its space, such as `color(display-p3 …)` or\n * `oklch()`, is converted into `profile`, and brought inside it by reducing chroma when the\n * profile cannot show it.\n */\nexport function tryParseColor(input: string, profile: ColorProfile = 'srgb'): Color | null {\n const parsed = parse(input)\n if (!parsed) return null\n // A component written past the float range, such as `1e999`, is no color.\n if (Object.values(parsed).some((value) => typeof value === 'number' && !Number.isFinite(value)))\n return null\n const a = parsed.alpha ?? 1\n // culori leaves a `none` channel out, which normalizing reads as zero.\n if (SRGB_SYNTAX_MODES.has(parsed.mode)) return normalizeColor({ ...toRGB(parsed), a })\n return insideProfile(parsed, profile)\n}\n\n/** A CSS color as components in `profile`, or black when `input` is not one. */\nexport function parseColor(input: string, profile: ColorProfile = 'srgb'): Color {\n return tryParseColor(input, profile) ?? { ...BLACK }\n}\n"],"mappings":";;;;AAOA,MAAM,eAAe,UAAU,MAAM;AACrC,MAAM,QAAQ,UAAU,KAAK;AAC7B,MAAM,OAAO,UAAU,IAAI;AAC3B,MAAM,mBAAmB,QAAQ,OAAO,OAAO;AAC/C,MAAM,kBAAkB,QAAQ,MAAM,OAAO;;AAS7C,SAAgB,UAAU,OAA8C;CACtE,OAAO;EAAC,MAAM;EAAG,MAAM;EAAG,MAAM;CAAC,CAAC,CAAC,OAChC,YAAY,WAAW,SAAoB,WAAW,QACzD;AACF;;AAGA,SAAS,iBAAiB,OAAoB,SAA8B;CAC1E,MAAM,EAAE,GAAG,GAAG,MAAM,YAAY,eAAe,KAAK,KAAK,IAAI,MAAM,KAAK;CACxE,OAAO;EAAE;EAAG;EAAG;EAAG,GAAG,MAAM,SAAS;CAAE;AACxC;;;;;;;;AASA,SAAgB,eACd,OACA,SACoC;CACpC,MAAM,QAAQ,eAAe,iBAAiB,OAAO,OAAO,CAAC;CAC7D,MAAM,UAAU,CAAC,UAAU,KAAK;CAEhC,MAAM,SAAS,UACX,eACE,iBAHK,YAAY,eAGK,gBAAgB,KAAK,IAAI,iBAAiB,KAAK,GAAG,OAAO,CACjF,IACA;CACJ,MAAM,SAAS,YAAoB,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,OAAO,CAAC;CACnE,OAAO;EACL,OAAO;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,OAAO,MAAM,CAAC,CAAC,MAAM,UAAU,OAAO,UAAU,YAAY,CAAC,OAAO,SAAS,KAAK,CAAC,GAC5F,OAAO;CACT,MAAM,IAAI,OAAO,SAAS;CAE1B,IAAI,kBAAkB,IAAI,OAAO,IAAI,GAAG,OAAO,eAAe;EAAE,GAAG,MAAM,MAAM;EAAG;CAAE,CAAC;CACrF,OAAO,cAAc,QAAQ,OAAO;AACtC;;AAGA,SAAgB,WAAW,OAAe,UAAwB,QAAe;CAC/E,OAAO,cAAc,OAAO,OAAO,KAAK,EAAE,GAAG,MAAM;AACrD"}
|
package/dist/color/okhcl.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Color } from "../primitives/index.js";
|
|
2
|
-
import { SceneNode } from "../types.js";
|
|
2
|
+
import { ColorProfile, SceneNode } from "../types.js";
|
|
3
3
|
//#region src/color/okhcl.d.ts
|
|
4
4
|
interface OkHCLColor {
|
|
5
5
|
h: number;
|
|
@@ -13,21 +13,39 @@ interface OkHCLPayload {
|
|
|
13
13
|
index: number;
|
|
14
14
|
color: OkHCLColor;
|
|
15
15
|
}
|
|
16
|
-
/**
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
declare function
|
|
16
|
+
/**
|
|
17
|
+
* `color` as components in `profile`, and whether the profile could not show it: a color past the
|
|
18
|
+
* gamut is brought inside by reducing chroma, keeping lightness and hue, as {@link fitIntoProfile}
|
|
19
|
+
* brings any color inside.
|
|
20
|
+
*/
|
|
21
|
+
declare function okhclInProfile(color: OkHCLColor, profile: ColorProfile): {
|
|
22
|
+
color: Color;
|
|
23
|
+
clipped: boolean;
|
|
24
|
+
};
|
|
25
|
+
/** Coordinates in `profile`, so callers can store colours in the document's profile. */
|
|
26
|
+
declare function okhclToRGBA(color: OkHCLColor, profile?: ColorProfile): Color;
|
|
27
|
+
/** Reads `color` as coordinates in `profile`, the inverse of {@link okhclToRGBA}. */
|
|
28
|
+
declare function rgbaToOkHCL(color: Color, profile?: ColorProfile): OkHCLColor;
|
|
22
29
|
declare function serializeOkHCLPayload(payload: OkHCLPayload): string;
|
|
23
30
|
declare function parseOkHCLPayload(value: string): OkHCLPayload | null;
|
|
24
|
-
declare function setNodeFillOkHCL(node: SceneNode, index: number, color: OkHCLColor,
|
|
25
|
-
declare function setNodeStrokeOkHCL(node: SceneNode, index: number, color: OkHCLColor,
|
|
31
|
+
declare function setNodeFillOkHCL(node: SceneNode, index: number, color: OkHCLColor, profile?: ColorProfile): Partial<SceneNode>;
|
|
32
|
+
declare function setNodeStrokeOkHCL(node: SceneNode, index: number, color: OkHCLColor, profile?: ColorProfile): Partial<SceneNode>;
|
|
26
33
|
declare function clearNodeFillOkHCL(node: SceneNode, index: number): Partial<SceneNode>;
|
|
27
34
|
declare function clearNodeStrokeOkHCL(node: SceneNode, index: number): Partial<SceneNode>;
|
|
28
35
|
declare function getNodeOkHCLPayloads(node: SceneNode): OkHCLPayload[];
|
|
29
36
|
declare function getFillOkHCL(node: SceneNode, index: number): OkHCLPayload | null;
|
|
30
37
|
declare function getStrokeOkHCL(node: SceneNode, index: number): OkHCLPayload | null;
|
|
38
|
+
/**
|
|
39
|
+
* The OkHCL color a paint was last edited in, while it still makes the paint's stored color. The
|
|
40
|
+
* stored color is what draws and what files keep; any other edit to it, such as a hex value, the
|
|
41
|
+
* eyedropper, a plugin, or a new color profile, leaves the OkHCL value describing an older color.
|
|
42
|
+
*/
|
|
43
|
+
declare function paintOkHCL(node: SceneNode, kind: 'fill' | 'stroke', index: number, profile: ColorProfile): OkHCLColor | null;
|
|
44
|
+
/**
|
|
45
|
+
* Keeps `color` as the OkHCL color a paint is edited in without changing the paint, for picking
|
|
46
|
+
* the OkHCL format: the paint keeps its exact stored color.
|
|
47
|
+
*/
|
|
48
|
+
declare function rememberPaintOkHCL(node: SceneNode, kind: 'fill' | 'stroke', index: number, color: OkHCLColor): Partial<SceneNode>;
|
|
31
49
|
//#endregion
|
|
32
|
-
export { OkHCLColor,
|
|
50
|
+
export { OkHCLColor, OkHCLPayload, clearNodeFillOkHCL, clearNodeStrokeOkHCL, getFillOkHCL, getNodeOkHCLPayloads, getStrokeOkHCL, okhclInProfile, okhclToRGBA, paintOkHCL, parseOkHCLPayload, rememberPaintOkHCL, rgbaToOkHCL, serializeOkHCLPayload, setNodeFillOkHCL, setNodeStrokeOkHCL };
|
|
33
51
|
//# sourceMappingURL=okhcl.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"okhcl.d.ts","names":[],"sources":["../../src/color/okhcl.ts"],"mappings":";;;UASiB;EACf;EACA;EACA;EACA;;UAGe;EACf;EACA;EACA;EACA,OAAO;;;
|
|
1
|
+
{"version":3,"file":"okhcl.d.ts","names":[],"sources":["../../src/color/okhcl.ts"],"mappings":";;;UASiB;EACf;EACA;EACA;EACA;;UAGe;EACf;EACA;EACA;EACA,OAAO;;;;;;;iBA8BO,eACd,OAAO,YACP,SAAS;EACN,OAAO;EAAO;;;iBAMH,YAAY,OAAO,YAAY,UAAS,eAAwB;;iBAKhE,YAAY,OAAO,OAAO,UAAS,eAAwB;iBAc3D,sBAAsB,SAAS;iBAI/B,kBAAkB,gBAAgB;iBA+BlC,iBACd,MAAM,WACN,eACA,OAAO,YACP,UAAS,eACR,QAAQ;iBAiBK,mBACd,MAAM,WACN,eACA,OAAO,YACP,UAAS,eACR,QAAQ;iBAiBK,mBAAmB,MAAM,WAAW,gBAAgB,QAAQ;iBAI5D,qBAAqB,MAAM,WAAW,gBAAgB,QAAQ;iBAI9D,qBAAqB,MAAM,YAAY;iBAIvC,aAAa,MAAM,WAAW,gBAAgB;iBAQ9C,eAAe,MAAM,WAAW,gBAAgB;;;;;;iBAmBhD,WACd,MAAM,WACN,yBACA,eACA,SAAS,eACR;;;;;iBAWa,mBACd,MAAM,WACN,yBACA,eACA,OAAO,aACN,QAAQ"}
|
package/dist/color/okhcl.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
import { copyFill, copyStroke } from "../copy.js";
|
|
2
|
-
import {
|
|
2
|
+
import { fitIntoProfile } from "./management.js";
|
|
3
3
|
import { readAllPluginData, withAllPluginData } from "../plugin-data/field.js";
|
|
4
4
|
import { OPEN_PENCIL_PLUGIN_DATA } from "../plugin-data/fields.js";
|
|
5
|
-
import { converter
|
|
5
|
+
import { converter } from "culori";
|
|
6
6
|
//#region src/color/okhcl.ts
|
|
7
|
-
const toRGB = converter("rgb");
|
|
8
|
-
const toP3 = converter("p3");
|
|
9
7
|
const toOkHCL = converter("oklch");
|
|
10
|
-
const toDisplayableRGB = toGamut("rgb", "oklch");
|
|
11
|
-
const toDisplayableP3 = toGamut("p3", "oklch");
|
|
12
8
|
function clampUnit(value) {
|
|
13
9
|
if (value < 0) return 0;
|
|
14
10
|
if (value > 1) return 1;
|
|
@@ -26,36 +22,28 @@ function normalizeOkHCLColor(color) {
|
|
|
26
22
|
a: clampUnit(color.a ?? 1)
|
|
27
23
|
};
|
|
28
24
|
}
|
|
29
|
-
/**
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
25
|
+
/**
|
|
26
|
+
* `color` as components in `profile`, and whether the profile could not show it: a color past the
|
|
27
|
+
* gamut is brought inside by reducing chroma, keeping lightness and hue, as {@link fitIntoProfile}
|
|
28
|
+
* brings any color inside.
|
|
29
|
+
*/
|
|
30
|
+
function okhclInProfile(color, profile) {
|
|
31
|
+
const { l, c, h, a } = normalizeOkHCLColor(color);
|
|
32
|
+
return fitIntoProfile({
|
|
33
33
|
mode: "oklch",
|
|
34
|
-
l
|
|
35
|
-
c
|
|
36
|
-
h
|
|
37
|
-
alpha:
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
48
|
-
const rgb = toRGB(toDisplayableRGB(oklch));
|
|
49
|
-
return normalizeColor({
|
|
50
|
-
r: rgb.r,
|
|
51
|
-
g: rgb.g,
|
|
52
|
-
b: rgb.b,
|
|
53
|
-
a: rgb.alpha ?? normalized.a
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
/** Reads `color` as coordinates in `colorSpace`, the inverse of {@link okhclToRGBA}. */
|
|
57
|
-
function rgbaToOkHCL(color, colorSpace = "srgb") {
|
|
58
|
-
const oklch = toOkHCL(colorSpace === "display-p3" ? {
|
|
34
|
+
l,
|
|
35
|
+
c,
|
|
36
|
+
h,
|
|
37
|
+
alpha: a ?? 1
|
|
38
|
+
}, profile);
|
|
39
|
+
}
|
|
40
|
+
/** Coordinates in `profile`, so callers can store colours in the document's profile. */
|
|
41
|
+
function okhclToRGBA(color, profile = "srgb") {
|
|
42
|
+
return okhclInProfile(color, profile).color;
|
|
43
|
+
}
|
|
44
|
+
/** Reads `color` as coordinates in `profile`, the inverse of {@link okhclToRGBA}. */
|
|
45
|
+
function rgbaToOkHCL(color, profile = "srgb") {
|
|
46
|
+
const oklch = toOkHCL(profile === "display-p3" ? {
|
|
59
47
|
mode: "p3",
|
|
60
48
|
r: color.r,
|
|
61
49
|
g: color.g,
|
|
@@ -95,11 +83,11 @@ function withOkHCLPayload(node, kind, index, color) {
|
|
|
95
83
|
if (color) payloads.push(createOkHCLPayload(kind, index, color));
|
|
96
84
|
return withAllPluginData(node.pluginData, OPEN_PENCIL_PLUGIN_DATA.okhcl, payloads);
|
|
97
85
|
}
|
|
98
|
-
function setNodeFillOkHCL(node, index, color,
|
|
86
|
+
function setNodeFillOkHCL(node, index, color, profile = "srgb") {
|
|
99
87
|
const fills = node.fills.map(copyFill);
|
|
100
88
|
if (index < 0 || index >= fills.length) throw new Error(`Fill ${index} not found`);
|
|
101
89
|
const fill = fills[index];
|
|
102
|
-
const rgba = okhclToRGBA(color,
|
|
90
|
+
const rgba = okhclToRGBA(color, profile);
|
|
103
91
|
fills[index] = {
|
|
104
92
|
...fill,
|
|
105
93
|
color: rgba,
|
|
@@ -110,11 +98,11 @@ function setNodeFillOkHCL(node, index, color, colorSpace = "srgb") {
|
|
|
110
98
|
pluginData: withOkHCLPayload(node, "fill", index, color)
|
|
111
99
|
};
|
|
112
100
|
}
|
|
113
|
-
function setNodeStrokeOkHCL(node, index, color,
|
|
101
|
+
function setNodeStrokeOkHCL(node, index, color, profile = "srgb") {
|
|
114
102
|
const strokes = node.strokes.map(copyStroke);
|
|
115
103
|
if (index < 0 || index >= strokes.length) throw new Error(`Stroke ${index} not found`);
|
|
116
104
|
const stroke = strokes[index];
|
|
117
|
-
const rgba = okhclToRGBA(color,
|
|
105
|
+
const rgba = okhclToRGBA(color, profile);
|
|
118
106
|
strokes[index] = {
|
|
119
107
|
...stroke,
|
|
120
108
|
color: rgba,
|
|
@@ -140,7 +128,35 @@ function getFillOkHCL(node, index) {
|
|
|
140
128
|
function getStrokeOkHCL(node, index) {
|
|
141
129
|
return getNodeOkHCLPayloads(node).find((payload) => payload.kind === "stroke" && payload.index === index) ?? null;
|
|
142
130
|
}
|
|
131
|
+
/** Whether two colors differ by less than half a step of 8 bits in every component. */
|
|
132
|
+
function sameColor(left, right) {
|
|
133
|
+
const step = 1 / 510;
|
|
134
|
+
return [
|
|
135
|
+
"r",
|
|
136
|
+
"g",
|
|
137
|
+
"b",
|
|
138
|
+
"a"
|
|
139
|
+
].every((key) => Math.abs(left[key] - right[key]) <= step);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* The OkHCL color a paint was last edited in, while it still makes the paint's stored color. The
|
|
143
|
+
* stored color is what draws and what files keep; any other edit to it, such as a hex value, the
|
|
144
|
+
* eyedropper, a plugin, or a new color profile, leaves the OkHCL value describing an older color.
|
|
145
|
+
*/
|
|
146
|
+
function paintOkHCL(node, kind, index, profile) {
|
|
147
|
+
const payload = kind === "fill" ? getFillOkHCL(node, index) : getStrokeOkHCL(node, index);
|
|
148
|
+
const paint = index < 0 ? void 0 : (kind === "fill" ? node.fills : node.strokes).at(index);
|
|
149
|
+
if (!payload || !paint) return null;
|
|
150
|
+
return sameColor(okhclToRGBA(payload.color, profile), paint.color) ? payload.color : null;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Keeps `color` as the OkHCL color a paint is edited in without changing the paint, for picking
|
|
154
|
+
* the OkHCL format: the paint keeps its exact stored color.
|
|
155
|
+
*/
|
|
156
|
+
function rememberPaintOkHCL(node, kind, index, color) {
|
|
157
|
+
return { pluginData: withOkHCLPayload(node, kind, index, color) };
|
|
158
|
+
}
|
|
143
159
|
//#endregion
|
|
144
|
-
export { clearNodeFillOkHCL, clearNodeStrokeOkHCL, getFillOkHCL, getNodeOkHCLPayloads, getStrokeOkHCL, okhclToRGBA, parseOkHCLPayload, rgbaToOkHCL, serializeOkHCLPayload, setNodeFillOkHCL, setNodeStrokeOkHCL };
|
|
160
|
+
export { clearNodeFillOkHCL, clearNodeStrokeOkHCL, getFillOkHCL, getNodeOkHCLPayloads, getStrokeOkHCL, okhclInProfile, okhclToRGBA, paintOkHCL, parseOkHCLPayload, rememberPaintOkHCL, rgbaToOkHCL, serializeOkHCLPayload, setNodeFillOkHCL, setNodeStrokeOkHCL };
|
|
145
161
|
|
|
146
162
|
//# sourceMappingURL=okhcl.js.map
|