@open-pencil/scene-graph 0.17.0-beta.7 → 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 +3 -2
- 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 +8 -18
- package/dist/css/shadows.js.map +1 -1
- package/dist/css/values.d.ts +6 -3
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +23 -5
- 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 +91 -13
- 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 +2 -0
- package/src/css/motion.ts +51 -0
- package/src/css/shadows.ts +8 -23
- package/src/css/values.ts +34 -4
- 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 +115 -11
- 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/src/color/index.ts
CHANGED
|
@@ -1,37 +1,27 @@
|
|
|
1
1
|
export * from './contrast'
|
|
2
2
|
export * from './management'
|
|
3
3
|
export { normalizeColor } from './normalize'
|
|
4
|
+
export { colorProfileSchema, EXPORT_COLOR_PROFILE_NAMES, exportColorProfileSchema } from './schema'
|
|
4
5
|
export * from './okhcl'
|
|
5
6
|
|
|
6
|
-
import {
|
|
7
|
+
import { formatHex, formatHex8, formatRgb, differenceEuclidean } from 'culori'
|
|
7
8
|
|
|
8
|
-
import { BLACK } from '../constants'
|
|
9
9
|
import type { Color } from '../primitives'
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
/** A CSS color, or null when `input` is not one. */
|
|
14
|
-
export function tryParseColor(input: string): Color | null {
|
|
15
|
-
const parsed = parse(input)
|
|
16
|
-
if (!parsed) return null
|
|
17
|
-
const rgb = toRGB(parsed)
|
|
18
|
-
return {
|
|
19
|
-
r: rgb.r,
|
|
20
|
-
g: rgb.g,
|
|
21
|
-
b: rgb.b,
|
|
22
|
-
a: parsed.alpha ?? 1
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** A CSS color, or black when `input` is not one. */
|
|
27
|
-
export function parseColor(input: string): Color {
|
|
28
|
-
return tryParseColor(input) ?? { ...BLACK }
|
|
29
|
-
}
|
|
10
|
+
import type { ColorProfile } from '../types'
|
|
11
|
+
import { convertColor, parseColor } from './management'
|
|
30
12
|
|
|
31
13
|
export function colorToHex(color: Color): string {
|
|
32
14
|
return formatHex({ mode: 'rgb', r: color.r, g: color.g, b: color.b }).toUpperCase()
|
|
33
15
|
}
|
|
34
16
|
|
|
17
|
+
/**
|
|
18
|
+
* `color`, whose components are in `profile`, as the sRGB hex code tools display: hex holds sRGB
|
|
19
|
+
* only, so a color outside it is brought inside by reducing chroma, not clipped.
|
|
20
|
+
*/
|
|
21
|
+
export function colorToSRGBHex(color: Color, profile: ColorProfile): string {
|
|
22
|
+
return colorToHex(convertColor(color, profile, 'srgb', 'map'))
|
|
23
|
+
}
|
|
24
|
+
|
|
35
25
|
export function colorToHex8(color: Color, alpha?: number): string {
|
|
36
26
|
const a = alpha ?? color.a
|
|
37
27
|
if (a >= 1) return colorToHex(color)
|
|
@@ -66,8 +56,9 @@ export function rgba255ToColor(r: number, g: number, b: number, a = 1): Color {
|
|
|
66
56
|
return { r: r / 255, g: g / 255, b: b / 255, a }
|
|
67
57
|
}
|
|
68
58
|
|
|
69
|
-
|
|
70
|
-
|
|
59
|
+
/** A solid paint of `color`, a CSS color read into `profile` or components already in it. */
|
|
60
|
+
export function colorToFill(color: string | Color, profile: ColorProfile = 'srgb') {
|
|
61
|
+
const rgba = typeof color === 'string' ? parseColor(color, profile) : color
|
|
71
62
|
return {
|
|
72
63
|
type: 'SOLID' as const,
|
|
73
64
|
color: { r: rgba.r, g: rgba.g, b: rgba.b, a: rgba.a },
|
package/src/color/management.ts
CHANGED
|
@@ -1,154 +1,153 @@
|
|
|
1
|
-
import { converter, formatCss, formatRgb,
|
|
1
|
+
import { converter, formatCss, formatRgb, parse, toGamut, type Color as CuloriColor } from 'culori'
|
|
2
2
|
|
|
3
|
+
import { BLACK } from '../constants'
|
|
3
4
|
import type { Color } from '../primitives'
|
|
4
|
-
import type {
|
|
5
|
+
import type { ColorProfile } from '../types'
|
|
5
6
|
import { normalizeColor } from './normalize'
|
|
6
|
-
import { getFillOkHCL, getStrokeOkHCL, type OkHCLColor } from './okhcl'
|
|
7
|
-
|
|
8
|
-
export type RenderColorSpace = 'srgb' | 'display-p3'
|
|
9
|
-
export type ColorIntentSpace = 'oklch' | 'srgb'
|
|
10
|
-
|
|
11
|
-
export interface ColorPreviewOptions {
|
|
12
|
-
colorSpace?: RenderColorSpace
|
|
13
|
-
documentColorSpace?: DocumentColorSpace
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface ResolvedRenderColor {
|
|
17
|
-
color: Color
|
|
18
|
-
cssColor: string
|
|
19
|
-
sourceSpace: ColorIntentSpace
|
|
20
|
-
targetSpace: RenderColorSpace
|
|
21
|
-
clipped: boolean
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const DEFAULT_COLOR_SPACE: RenderColorSpace = 'srgb'
|
|
25
7
|
|
|
8
|
+
const toLinearSRGB = converter('lrgb')
|
|
26
9
|
const toRGB = converter('rgb')
|
|
27
10
|
const toP3 = converter('p3')
|
|
28
11
|
const toDisplayableRGB = toGamut('rgb', 'oklch')
|
|
29
12
|
const toDisplayableP3 = toGamut('p3', 'oklch')
|
|
30
|
-
const isDisplayableRGB = inGamut('rgb')
|
|
31
|
-
const isDisplayableP3 = inGamut('p3')
|
|
32
13
|
|
|
33
|
-
|
|
34
|
-
|
|
14
|
+
/**
|
|
15
|
+
* How far past 0 or 1 a component may land and still count as inside a gamut. Conversions round
|
|
16
|
+
* off: P3 red read through OKLCH comes back with a blue of -1e-15, which is the same color.
|
|
17
|
+
*/
|
|
18
|
+
const GAMUT_TOLERANCE = 1e-6
|
|
19
|
+
|
|
20
|
+
/** Whether `color`'s components fit 0 to 1, give or take round-off. */
|
|
21
|
+
export function fitsGamut(color: Pick<Color, 'r' | 'g' | 'b'>): boolean {
|
|
22
|
+
return [color.r, color.g, color.b].every(
|
|
23
|
+
(channel) => channel >= -GAMUT_TOLERANCE && channel <= 1 + GAMUT_TOLERANCE
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** `color`, a culori color in any space, as components in `profile`, past 0 and 1 if need be. */
|
|
28
|
+
function exactlyInProfile(color: CuloriColor, profile: ColorProfile): Color {
|
|
29
|
+
const { r, g, b } = profile === 'display-p3' ? toP3(color) : toRGB(color)
|
|
30
|
+
return { r, g, b, a: color.alpha ?? 1 }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* `color`, a culori color in any space, as components in `profile`, brought inside it the way CSS
|
|
35
|
+
* maps out-of-gamut colors, by reducing chroma in OKLCH so lightness and hue hold, and whether it
|
|
36
|
+
* had to be. A color inside already keeps its components, as mapping it would shift them by float
|
|
37
|
+
* noise; one on the gamut's edge, as every saturated primary is, counts as inside despite
|
|
38
|
+
* round-off. A missing (`none`) component reads as zero.
|
|
39
|
+
*/
|
|
40
|
+
export function fitIntoProfile(
|
|
41
|
+
color: CuloriColor,
|
|
42
|
+
profile: ColorProfile
|
|
43
|
+
): { color: Color; clipped: boolean } {
|
|
44
|
+
const exact = normalizeColor(exactlyInProfile(color, profile))
|
|
45
|
+
const clipped = !fitsGamut(exact)
|
|
46
|
+
const p3 = profile === 'display-p3'
|
|
47
|
+
const inside = clipped
|
|
48
|
+
? normalizeColor(
|
|
49
|
+
exactlyInProfile(p3 ? toDisplayableP3(color) : toDisplayableRGB(color), profile)
|
|
50
|
+
)
|
|
51
|
+
: exact
|
|
52
|
+
const clamp = (channel: number) => Math.min(1, Math.max(0, channel))
|
|
35
53
|
return {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
c: Math.max(0, color.c),
|
|
39
|
-
h: hue < 0 ? hue + 360 : hue,
|
|
40
|
-
alpha: Math.max(0, Math.min(1, color.a ?? 1))
|
|
54
|
+
color: { r: clamp(inside.r), g: clamp(inside.g), b: clamp(inside.b), a: color.alpha ?? 1 },
|
|
55
|
+
clipped
|
|
41
56
|
}
|
|
42
57
|
}
|
|
43
58
|
|
|
59
|
+
function insideProfile(color: CuloriColor, profile: ColorProfile): Color {
|
|
60
|
+
return fitIntoProfile(color, profile).color
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function culoriColor(color: Color, profile: ColorProfile): CuloriColor {
|
|
64
|
+
const mode = profile === 'display-p3' ? 'p3' : 'rgb'
|
|
65
|
+
return { mode, r: color.r, g: color.g, b: color.b, alpha: color.a }
|
|
66
|
+
}
|
|
67
|
+
|
|
44
68
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
69
|
+
* What to do with a color the target profile cannot show: `extend` keeps its components past 0
|
|
70
|
+
* and 1, for a renderer such as Skia that takes extended sRGB; `map` brings it inside by reducing
|
|
71
|
+
* chroma in OKLCH, as CSS maps out-of-gamut colors, keeping its lightness and hue.
|
|
47
72
|
*/
|
|
48
|
-
|
|
73
|
+
export type GamutHandling = 'extend' | 'map'
|
|
74
|
+
|
|
75
|
+
/** `color`, whose components are in `from`, as the components in `to` that show the same color. */
|
|
76
|
+
export function convertColor(
|
|
77
|
+
color: Color,
|
|
78
|
+
from: ColorProfile,
|
|
79
|
+
to: ColorProfile,
|
|
80
|
+
gamut: GamutHandling = 'extend'
|
|
81
|
+
): Color {
|
|
49
82
|
if (from === to) return color
|
|
50
|
-
const source =
|
|
51
|
-
|
|
52
|
-
? { mode: 'p3' as const, r: color.r, g: color.g, b: color.b, alpha: color.a }
|
|
53
|
-
: { mode: 'rgb' as const, r: color.r, g: color.g, b: color.b, alpha: color.a }
|
|
54
|
-
const converted = to === 'display-p3' ? toP3(source) : toRGB(source)
|
|
55
|
-
return normalizeColor({
|
|
56
|
-
r: converted.r,
|
|
57
|
-
g: converted.g,
|
|
58
|
-
b: converted.b,
|
|
59
|
-
a: converted.alpha ?? color.a
|
|
60
|
-
})
|
|
83
|
+
const source = culoriColor(color, from)
|
|
84
|
+
return gamut === 'extend' ? exactlyInProfile(source, to) : insideProfile(source, to)
|
|
61
85
|
}
|
|
62
86
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
87
|
+
/**
|
|
88
|
+
* `color`, whose components are in `profile`, as the sRGB components of the same color: past 0 and
|
|
89
|
+
* 1 where it lies outside sRGB. Skia takes colors this way and converts them into the space of
|
|
90
|
+
* whatever it draws on, so a wide-gamut color reaches it whole.
|
|
91
|
+
*/
|
|
92
|
+
export function toExtendedSRGB(color: Color, profile: ColorProfile): Color {
|
|
93
|
+
if (profile === 'srgb') return color
|
|
94
|
+
// Renderers call this for every paint they draw, so it skips culori's color objects: Display P3
|
|
95
|
+
// shares sRGB's transfer curve, leaving one matrix between the linear components.
|
|
96
|
+
// Indexed reads of a plain array: destructuring costs more than the conversion.
|
|
97
|
+
const m = P3_TO_LINEAR_SRGB
|
|
98
|
+
const r = decodeSRGB(color.r)
|
|
99
|
+
const g = decodeSRGB(color.g)
|
|
100
|
+
const b = decodeSRGB(color.b)
|
|
101
|
+
return {
|
|
102
|
+
r: encodeSRGB(m[0] * r + m[1] * g + m[2] * b),
|
|
103
|
+
g: encodeSRGB(m[3] * r + m[4] * g + m[5] * b),
|
|
104
|
+
b: encodeSRGB(m[6] * r + m[7] * g + m[8] * b),
|
|
105
|
+
a: color.a
|
|
66
106
|
}
|
|
67
|
-
return !isDisplayableRGB({ mode: 'rgb', r: color.r, g: color.g, b: color.b, alpha: color.a })
|
|
68
107
|
}
|
|
69
108
|
|
|
70
|
-
|
|
71
|
-
|
|
109
|
+
/** The sRGB transfer curve, mirrored below zero as extended sRGB and culori's `lrgb` take it. */
|
|
110
|
+
function decodeSRGB(channel: number): number {
|
|
111
|
+
const magnitude = Math.abs(channel)
|
|
112
|
+
if (magnitude <= 0.04045) return channel / 12.92
|
|
113
|
+
return (Math.sign(channel) || 1) * ((magnitude + 0.055) / 1.055) ** 2.4
|
|
72
114
|
}
|
|
73
115
|
|
|
74
|
-
function
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
mode: 'p3',
|
|
79
|
-
r: p3.r,
|
|
80
|
-
g: p3.g,
|
|
81
|
-
b: p3.b,
|
|
82
|
-
alpha: p3.alpha ?? color.a
|
|
83
|
-
})
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
return formatRgb({
|
|
87
|
-
mode: 'rgb',
|
|
88
|
-
r: color.r,
|
|
89
|
-
g: color.g,
|
|
90
|
-
b: color.b,
|
|
91
|
-
alpha: color.a
|
|
92
|
-
})
|
|
116
|
+
function encodeSRGB(channel: number): number {
|
|
117
|
+
const magnitude = Math.abs(channel)
|
|
118
|
+
if (magnitude <= 0.0031308) return channel * 12.92
|
|
119
|
+
return (Math.sign(channel) || 1) * (1.055 * magnitude ** (1 / 2.4) - 0.055)
|
|
93
120
|
}
|
|
94
121
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const clipped = !isDisplayableP3(oklch)
|
|
104
|
-
const p3 = toP3(toDisplayableP3(oklch))
|
|
105
|
-
// P3 coordinates, because the caller asked for a P3 target.
|
|
106
|
-
const resolved = normalizeColor({
|
|
107
|
-
r: p3.r,
|
|
108
|
-
g: p3.g,
|
|
109
|
-
b: p3.b,
|
|
110
|
-
a: p3.alpha ?? oklch.alpha
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
return {
|
|
114
|
-
color: resolved,
|
|
115
|
-
cssColor: formatCss({
|
|
116
|
-
mode: 'p3',
|
|
117
|
-
r: p3.r,
|
|
118
|
-
g: p3.g,
|
|
119
|
-
b: p3.b,
|
|
120
|
-
alpha: p3.alpha ?? oklch.alpha
|
|
121
|
-
}),
|
|
122
|
-
sourceSpace: 'oklch',
|
|
123
|
-
targetSpace,
|
|
124
|
-
clipped
|
|
125
|
-
}
|
|
122
|
+
/**
|
|
123
|
+
* `color`, whose components are in `from`, as CSS showing it in `to`: `color(display-p3 …)` for
|
|
124
|
+
* Display P3, `rgb()` for sRGB, where a color outside sRGB clips per channel as a canvas clips it.
|
|
125
|
+
*/
|
|
126
|
+
export function colorToProfileCSS(color: Color, from: ColorProfile, to: ColorProfile): string {
|
|
127
|
+
const shown = convertColor(color, from, to)
|
|
128
|
+
if (to === 'display-p3') {
|
|
129
|
+
return formatCss({ mode: 'p3', r: shown.r, g: shown.g, b: shown.b, alpha: shown.a })
|
|
126
130
|
}
|
|
131
|
+
return formatRgb({ mode: 'rgb', r: shown.r, g: shown.g, b: shown.b, alpha: shown.a })
|
|
132
|
+
}
|
|
127
133
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
mode: 'rgb',
|
|
141
|
-
r: resolved.r,
|
|
142
|
-
g: resolved.g,
|
|
143
|
-
b: resolved.b,
|
|
144
|
-
alpha: resolved.a
|
|
145
|
-
}),
|
|
146
|
-
sourceSpace: 'oklch',
|
|
147
|
-
targetSpace,
|
|
148
|
-
clipped
|
|
149
|
-
}
|
|
134
|
+
/**
|
|
135
|
+
* The 3×3 matrix, row by row, taking linear components in `profile` to linear sRGB ones: its
|
|
136
|
+
* columns are the profile's primaries in linear sRGB.
|
|
137
|
+
*/
|
|
138
|
+
export function linearSRGBMatrix(profile: ColorProfile): number[] {
|
|
139
|
+
const mode = profile === 'display-p3' ? 'p3' : 'rgb'
|
|
140
|
+
const columns = [
|
|
141
|
+
{ r: 1, g: 0, b: 0 },
|
|
142
|
+
{ r: 0, g: 1, b: 0 },
|
|
143
|
+
{ r: 0, g: 0, b: 1 }
|
|
144
|
+
].map((primary) => toLinearSRGB({ mode, ...primary }))
|
|
145
|
+
return (['r', 'g', 'b'] as const).flatMap((row) => columns.map((column) => column[row]))
|
|
150
146
|
}
|
|
151
147
|
|
|
148
|
+
/** Display P3's matrix, made once: renderers convert every paint they draw through it. */
|
|
149
|
+
const P3_TO_LINEAR_SRGB = linearSRGBMatrix('display-p3')
|
|
150
|
+
|
|
152
151
|
/**
|
|
153
152
|
* A CSS color in any space, mapped into sRGB the way CSS maps out-of-gamut colors: by reducing
|
|
154
153
|
* chroma in OKLCH, so a Display P3 red becomes the nearest sRGB red, not a clipped one. A missing
|
|
@@ -156,55 +155,32 @@ export function resolveOkHCLForPreview(
|
|
|
156
155
|
*/
|
|
157
156
|
export function parseDisplayableColor(input: string): Color | null {
|
|
158
157
|
const parsed = parse(input)
|
|
159
|
-
|
|
160
|
-
// Mapping through OKLCH shifts colors already in sRGB by float noise, so only map the rest.
|
|
161
|
-
const rgb = isDisplayableRGB(parsed) ? toRGB(parsed) : toDisplayableRGB(parsed)
|
|
162
|
-
// culori leaves a `none` channel out, which normalizing reads as zero.
|
|
163
|
-
return normalizeColor({ r: rgb.r, g: rgb.g, b: rgb.b, a: parsed.alpha })
|
|
158
|
+
return parsed ? insideProfile(parsed, 'srgb') : null
|
|
164
159
|
}
|
|
165
160
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
options?: ColorPreviewOptions
|
|
169
|
-
): ResolvedRenderColor {
|
|
170
|
-
const documentColorSpace = options?.documentColorSpace ?? DEFAULT_COLOR_SPACE
|
|
171
|
-
const targetSpace = options?.colorSpace ?? documentColorSpace
|
|
172
|
-
const resolved = convertComponents(normalizeColor(color), documentColorSpace, targetSpace)
|
|
173
|
-
return {
|
|
174
|
-
color: resolved,
|
|
175
|
-
cssColor: formatCSSForTarget(resolved, targetSpace),
|
|
176
|
-
sourceSpace: 'srgb',
|
|
177
|
-
targetSpace,
|
|
178
|
-
clipped: targetSpace !== documentColorSpace && isOutsideGamut(resolved, targetSpace)
|
|
179
|
-
}
|
|
180
|
-
}
|
|
161
|
+
/** Syntaxes CSS reads as sRGB numbers: hex, `rgb()`, `hsl()`, `hwb()`, names, `color(srgb …)`. */
|
|
162
|
+
const SRGB_SYNTAX_MODES = new Set(['rgb', 'hsl', 'hwb'])
|
|
181
163
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
const okhcl = getStrokeOkHCL(node, strokeIndex)?.color
|
|
200
|
-
if (okhcl) return resolveOkHCLForPreview(okhcl, options)
|
|
201
|
-
return resolveRGBAForPreview(stroke.color, options)
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
export function colorToDisplayCSS(color: Color, options?: ColorPreviewOptions): string {
|
|
205
|
-
return resolveRGBAForPreview(color, options).cssColor
|
|
164
|
+
/**
|
|
165
|
+
* A CSS color as components in `profile`, or null when `input` is not one. Hex and the other sRGB
|
|
166
|
+
* syntaxes give their numbers as they are, as Figma takes typed or imported hex as the document's
|
|
167
|
+
* own numbers in either profile. A color that names its space, such as `color(display-p3 …)` or
|
|
168
|
+
* `oklch()`, is converted into `profile`, and brought inside it by reducing chroma when the
|
|
169
|
+
* profile cannot show it.
|
|
170
|
+
*/
|
|
171
|
+
export function tryParseColor(input: string, profile: ColorProfile = 'srgb'): Color | null {
|
|
172
|
+
const parsed = parse(input)
|
|
173
|
+
if (!parsed) return null
|
|
174
|
+
// A component written past the float range, such as `1e999`, is no color.
|
|
175
|
+
if (Object.values(parsed).some((value) => typeof value === 'number' && !Number.isFinite(value)))
|
|
176
|
+
return null
|
|
177
|
+
const a = parsed.alpha ?? 1
|
|
178
|
+
// culori leaves a `none` channel out, which normalizing reads as zero.
|
|
179
|
+
if (SRGB_SYNTAX_MODES.has(parsed.mode)) return normalizeColor({ ...toRGB(parsed), a })
|
|
180
|
+
return insideProfile(parsed, profile)
|
|
206
181
|
}
|
|
207
182
|
|
|
208
|
-
|
|
209
|
-
|
|
183
|
+
/** A CSS color as components in `profile`, or black when `input` is not one. */
|
|
184
|
+
export function parseColor(input: string, profile: ColorProfile = 'srgb'): Color {
|
|
185
|
+
return tryParseColor(input, profile) ?? { ...BLACK }
|
|
210
186
|
}
|
package/src/color/okhcl.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { converter
|
|
1
|
+
import { converter } from 'culori'
|
|
2
2
|
|
|
3
3
|
import { copyFill, copyStroke } from '../copy'
|
|
4
4
|
import { readAllPluginData, withAllPluginData } from '../plugin-data/field'
|
|
5
5
|
import { OPEN_PENCIL_PLUGIN_DATA } from '../plugin-data/fields'
|
|
6
6
|
import type { Color } from '../primitives'
|
|
7
|
-
import type { PluginDataEntry, SceneNode } from '../types'
|
|
8
|
-
import {
|
|
7
|
+
import type { ColorProfile, PluginDataEntry, SceneNode } from '../types'
|
|
8
|
+
import { fitIntoProfile } from './management'
|
|
9
9
|
|
|
10
10
|
export interface OkHCLColor {
|
|
11
11
|
h: number
|
|
@@ -21,14 +21,7 @@ export interface OkHCLPayload {
|
|
|
21
21
|
color: OkHCLColor
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
const toRGB = converter('rgb')
|
|
25
|
-
const toP3 = converter('p3')
|
|
26
24
|
const toOkHCL = converter('oklch')
|
|
27
|
-
const toDisplayableRGB = toGamut('rgb', 'oklch')
|
|
28
|
-
const toDisplayableP3 = toGamut('p3', 'oklch')
|
|
29
|
-
|
|
30
|
-
/** Colour spaces a document's stored numbers can be in. */
|
|
31
|
-
export type OkHCLColorSpace = 'srgb' | 'display-p3'
|
|
32
25
|
|
|
33
26
|
function clampUnit(value: number): number {
|
|
34
27
|
if (value < 0) return 0
|
|
@@ -50,38 +43,28 @@ function normalizeOkHCLColor(color: OkHCLColor): OkHCLColor {
|
|
|
50
43
|
}
|
|
51
44
|
}
|
|
52
45
|
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const p3 = toP3(toDisplayableP3(oklch))
|
|
65
|
-
return normalizeColor({
|
|
66
|
-
r: p3.r,
|
|
67
|
-
g: p3.g,
|
|
68
|
-
b: p3.b,
|
|
69
|
-
a: p3.alpha ?? normalized.a
|
|
70
|
-
})
|
|
71
|
-
}
|
|
72
|
-
const rgb = toRGB(toDisplayableRGB(oklch))
|
|
73
|
-
return normalizeColor({
|
|
74
|
-
r: rgb.r,
|
|
75
|
-
g: rgb.g,
|
|
76
|
-
b: rgb.b,
|
|
77
|
-
a: rgb.alpha ?? normalized.a
|
|
78
|
-
})
|
|
46
|
+
/**
|
|
47
|
+
* `color` as components in `profile`, and whether the profile could not show it: a color past the
|
|
48
|
+
* gamut is brought inside by reducing chroma, keeping lightness and hue, as {@link fitIntoProfile}
|
|
49
|
+
* brings any color inside.
|
|
50
|
+
*/
|
|
51
|
+
export function okhclInProfile(
|
|
52
|
+
color: OkHCLColor,
|
|
53
|
+
profile: ColorProfile
|
|
54
|
+
): { color: Color; clipped: boolean } {
|
|
55
|
+
const { l, c, h, a } = normalizeOkHCLColor(color)
|
|
56
|
+
return fitIntoProfile({ mode: 'oklch', l, c, h, alpha: a ?? 1 }, profile)
|
|
79
57
|
}
|
|
80
58
|
|
|
81
|
-
/**
|
|
82
|
-
export function
|
|
59
|
+
/** Coordinates in `profile`, so callers can store colours in the document's profile. */
|
|
60
|
+
export function okhclToRGBA(color: OkHCLColor, profile: ColorProfile = 'srgb'): Color {
|
|
61
|
+
return okhclInProfile(color, profile).color
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Reads `color` as coordinates in `profile`, the inverse of {@link okhclToRGBA}. */
|
|
65
|
+
export function rgbaToOkHCL(color: Color, profile: ColorProfile = 'srgb'): OkHCLColor {
|
|
83
66
|
const oklch = toOkHCL(
|
|
84
|
-
|
|
67
|
+
profile === 'display-p3'
|
|
85
68
|
? { mode: 'p3', r: color.r, g: color.g, b: color.b, alpha: color.a }
|
|
86
69
|
: { mode: 'rgb', r: color.r, g: color.g, b: color.b, alpha: color.a }
|
|
87
70
|
)
|
|
@@ -132,12 +115,12 @@ export function setNodeFillOkHCL(
|
|
|
132
115
|
node: SceneNode,
|
|
133
116
|
index: number,
|
|
134
117
|
color: OkHCLColor,
|
|
135
|
-
|
|
118
|
+
profile: ColorProfile = 'srgb'
|
|
136
119
|
): Partial<SceneNode> {
|
|
137
120
|
const fills = node.fills.map(copyFill)
|
|
138
121
|
if (index < 0 || index >= fills.length) throw new Error(`Fill ${index} not found`)
|
|
139
122
|
const fill = fills[index]
|
|
140
|
-
const rgba = okhclToRGBA(color,
|
|
123
|
+
const rgba = okhclToRGBA(color, profile)
|
|
141
124
|
fills[index] = {
|
|
142
125
|
...fill,
|
|
143
126
|
color: rgba,
|
|
@@ -154,12 +137,12 @@ export function setNodeStrokeOkHCL(
|
|
|
154
137
|
node: SceneNode,
|
|
155
138
|
index: number,
|
|
156
139
|
color: OkHCLColor,
|
|
157
|
-
|
|
140
|
+
profile: ColorProfile = 'srgb'
|
|
158
141
|
): Partial<SceneNode> {
|
|
159
142
|
const strokes = node.strokes.map(copyStroke)
|
|
160
143
|
if (index < 0 || index >= strokes.length) throw new Error(`Stroke ${index} not found`)
|
|
161
144
|
const stroke = strokes[index]
|
|
162
|
-
const rgba = okhclToRGBA(color,
|
|
145
|
+
const rgba = okhclToRGBA(color, profile)
|
|
163
146
|
strokes[index] = {
|
|
164
147
|
...stroke,
|
|
165
148
|
color: rgba,
|
|
@@ -199,3 +182,39 @@ export function getStrokeOkHCL(node: SceneNode, index: number): OkHCLPayload | n
|
|
|
199
182
|
) ?? null
|
|
200
183
|
)
|
|
201
184
|
}
|
|
185
|
+
|
|
186
|
+
/** Whether two colors differ by less than half a step of 8 bits in every component. */
|
|
187
|
+
function sameColor(left: Color, right: Color): boolean {
|
|
188
|
+
const step = 1 / 510
|
|
189
|
+
return (['r', 'g', 'b', 'a'] as const).every((key) => Math.abs(left[key] - right[key]) <= step)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* The OkHCL color a paint was last edited in, while it still makes the paint's stored color. The
|
|
194
|
+
* stored color is what draws and what files keep; any other edit to it, such as a hex value, the
|
|
195
|
+
* eyedropper, a plugin, or a new color profile, leaves the OkHCL value describing an older color.
|
|
196
|
+
*/
|
|
197
|
+
export function paintOkHCL(
|
|
198
|
+
node: SceneNode,
|
|
199
|
+
kind: 'fill' | 'stroke',
|
|
200
|
+
index: number,
|
|
201
|
+
profile: ColorProfile
|
|
202
|
+
): OkHCLColor | null {
|
|
203
|
+
const payload = kind === 'fill' ? getFillOkHCL(node, index) : getStrokeOkHCL(node, index)
|
|
204
|
+
const paint = index < 0 ? undefined : (kind === 'fill' ? node.fills : node.strokes).at(index)
|
|
205
|
+
if (!payload || !paint) return null
|
|
206
|
+
return sameColor(okhclToRGBA(payload.color, profile), paint.color) ? payload.color : null
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Keeps `color` as the OkHCL color a paint is edited in without changing the paint, for picking
|
|
211
|
+
* the OkHCL format: the paint keeps its exact stored color.
|
|
212
|
+
*/
|
|
213
|
+
export function rememberPaintOkHCL(
|
|
214
|
+
node: SceneNode,
|
|
215
|
+
kind: 'fill' | 'stroke',
|
|
216
|
+
index: number,
|
|
217
|
+
color: OkHCLColor
|
|
218
|
+
): Partial<SceneNode> {
|
|
219
|
+
return { pluginData: withOkHCLPayload(node, kind, index, color) }
|
|
220
|
+
}
|
package/src/color/schema.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as v from 'valibot'
|
|
2
2
|
|
|
3
3
|
import { finiteNumberSchema } from '../primitives/schema'
|
|
4
|
+
import type { ColorProfile, ExportColorProfile } from '../types'
|
|
4
5
|
|
|
5
6
|
export const colorSchema = v.object({
|
|
6
7
|
r: finiteNumberSchema,
|
|
@@ -8,3 +9,15 @@ export const colorSchema = v.object({
|
|
|
8
9
|
b: finiteNumberSchema,
|
|
9
10
|
a: finiteNumberSchema
|
|
10
11
|
})
|
|
12
|
+
|
|
13
|
+
/** A document's color profile, as files, libraries, and collaboration record it. */
|
|
14
|
+
export const colorProfileSchema = v.picklist(['srgb', 'display-p3'] satisfies ColorProfile[])
|
|
15
|
+
|
|
16
|
+
/** The profiles an export can ask for, as Figma names them. */
|
|
17
|
+
export const EXPORT_COLOR_PROFILE_NAMES = [
|
|
18
|
+
'DOCUMENT',
|
|
19
|
+
'SRGB',
|
|
20
|
+
'DISPLAY_P3_V4'
|
|
21
|
+
] as const satisfies readonly ExportColorProfile[]
|
|
22
|
+
|
|
23
|
+
export const exportColorProfileSchema = v.picklist(EXPORT_COLOR_PROFILE_NAMES)
|
package/src/constraints.ts
CHANGED
package/src/copy.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
import { readGeometryBlob } from './geometry'
|
|
12
12
|
import { cloneInstanceOverrideState } from './instances/overrides'
|
|
13
13
|
import { createDefaultSourceMetadata } from './node-defaults'
|
|
14
|
+
import type { Color } from './primitives'
|
|
14
15
|
import type {
|
|
15
16
|
ArcData,
|
|
16
17
|
ComponentPropertyDefinition,
|
|
@@ -39,6 +40,12 @@ export function copyFill<T extends Fill>(f: T): T {
|
|
|
39
40
|
if (f.filters) copy.filters = { ...f.filters }
|
|
40
41
|
if (f.patternSpacing) copy.patternSpacing = { ...f.patternSpacing }
|
|
41
42
|
if (f.noiseSize) copy.noiseSize = { ...f.noiseSize }
|
|
43
|
+
if (f.customEffectAssetRef) {
|
|
44
|
+
copy.customEffectAssetRef = { ...f.customEffectAssetRef }
|
|
45
|
+
}
|
|
46
|
+
if (f.componentPropAssignments) {
|
|
47
|
+
copy.componentPropAssignments = structuredClone(f.componentPropAssignments)
|
|
48
|
+
}
|
|
42
49
|
return copy
|
|
43
50
|
}
|
|
44
51
|
|
|
@@ -115,6 +122,26 @@ export function copyEffects(effects: Effect[]): Effect[] {
|
|
|
115
122
|
return effects.map(copyEffect)
|
|
116
123
|
}
|
|
117
124
|
|
|
125
|
+
/**
|
|
126
|
+
* Copies of `effects` with every color they hold passed through `map`: each effect's own and a
|
|
127
|
+
* duotone noise's second color. `map` is told where each sits, `0` or `0.noise` for the first.
|
|
128
|
+
*/
|
|
129
|
+
export function mapEffectColors(
|
|
130
|
+
effects: Effect[],
|
|
131
|
+
map: (color: Color, slot: string) => Color
|
|
132
|
+
): Effect[] {
|
|
133
|
+
return copyEffects(effects).map((effect, index) => {
|
|
134
|
+
const next = { ...effect, color: map(effect.color, `${index}`) }
|
|
135
|
+
if (effect.noise?.secondaryColor) {
|
|
136
|
+
next.noise = {
|
|
137
|
+
...effect.noise,
|
|
138
|
+
secondaryColor: map(effect.noise.secondaryColor, `${index}.noise`)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return next
|
|
142
|
+
})
|
|
143
|
+
}
|
|
144
|
+
|
|
118
145
|
export function copyLayoutGrids(grids: LayoutGrid[]): LayoutGrid[] {
|
|
119
146
|
return grids.map((grid) => ({ ...grid, color: grid.color ? { ...grid.color } : undefined }))
|
|
120
147
|
}
|
|
@@ -344,6 +371,8 @@ export function cloneNodeProps(
|
|
|
344
371
|
textPathBox: src.textPathBox ? { ...src.textPathBox } : null,
|
|
345
372
|
gridPosition: src.gridPosition ? { ...src.gridPosition } : null,
|
|
346
373
|
targetAspectRatio: src.targetAspectRatio ? { ...src.targetAspectRatio } : null,
|
|
374
|
+
slide: structuredClone(src.slide),
|
|
375
|
+
animation: structuredClone(src.animation),
|
|
347
376
|
connectorStart: src.connectorStart ? copyConnectorEndpoint(src.connectorStart) : null,
|
|
348
377
|
connectorEnd: src.connectorEnd ? copyConnectorEndpoint(src.connectorEnd) : null,
|
|
349
378
|
connectorControlPoints: structuredClone(src.connectorControlPoints),
|