@open-pencil/scene-graph 0.15.0 → 0.16.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/LICENSE +21 -0
- package/README.md +1 -1
- package/dist/chunks/behaviours/kinds.js +273 -0
- package/dist/chunks/behaviours/kinds.js.map +1 -0
- package/dist/chunks/behaviours/layers.js +596 -0
- package/dist/chunks/behaviours/layers.js.map +1 -0
- package/dist/chunks/behaviours/spec.js +181 -0
- package/dist/chunks/behaviours/spec.js.map +1 -0
- package/dist/chunks/buffered-events.js +57 -0
- package/dist/chunks/buffered-events.js.map +1 -0
- package/dist/chunks/color/contrast.js +57 -0
- package/dist/chunks/color/contrast.js.map +1 -0
- package/dist/chunks/{components/properties.js → color/index.js} +573 -521
- package/dist/chunks/color/index.js.map +1 -0
- package/dist/chunks/comments/document.js +32 -0
- package/dist/chunks/comments/document.js.map +1 -0
- package/dist/chunks/comments/list.js +182 -0
- package/dist/chunks/comments/list.js.map +1 -0
- package/dist/chunks/comments/merge.js +58 -0
- package/dist/chunks/comments/merge.js.map +1 -0
- package/dist/chunks/components/exposure.js +2021 -0
- package/dist/chunks/components/exposure.js.map +1 -0
- package/dist/chunks/components/variants.js +32 -0
- package/dist/chunks/components/variants.js.map +1 -0
- package/dist/chunks/coordinate.js +93 -55
- package/dist/chunks/coordinate.js.map +1 -1
- package/dist/chunks/css/grid.js +94 -0
- package/dist/chunks/css/grid.js.map +1 -0
- package/dist/chunks/css/index.js +160 -0
- package/dist/chunks/css/index.js.map +1 -0
- package/dist/chunks/fields/geometry.js +14 -0
- package/dist/chunks/fields/geometry.js.map +1 -0
- package/dist/chunks/fields/text.js +36 -0
- package/dist/chunks/fields/text.js.map +1 -0
- package/dist/chunks/group-bounds.js +104 -0
- package/dist/chunks/group-bounds.js.map +1 -0
- package/dist/chunks/icons/glyph.js +147 -0
- package/dist/chunks/icons/glyph.js.map +1 -0
- package/dist/chunks/index.js +1712 -662
- package/dist/chunks/index.js.map +1 -1
- package/dist/color.d.ts +72 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +3 -0
- package/dist/constants.d.ts +2 -2
- package/dist/constants.js +2 -2
- package/dist/constants2.d.ts +3 -1
- package/dist/constants2.d.ts.map +1 -1
- package/dist/coordinate.d.ts +2 -448
- package/dist/coordinate.js +2 -2
- package/dist/copy.d.ts +1 -1
- package/dist/copy.js +1 -1
- package/dist/css.d.ts +39 -0
- package/dist/css.d.ts.map +1 -0
- package/dist/css.js +3 -0
- package/dist/geometry.d.ts +1 -1
- package/dist/geometry.js +1 -1
- package/dist/gradient.d.ts +51 -0
- package/dist/gradient.d.ts.map +1 -0
- package/dist/gradient.js +132 -0
- package/dist/gradient.js.map +1 -0
- package/dist/guides.d.ts +2 -2
- package/dist/guides.js +39 -0
- package/dist/guides.js.map +1 -0
- package/dist/hit-test.d.ts +2 -9
- package/dist/hit-test.js +154 -18
- package/dist/hit-test.js.map +1 -1
- package/dist/index.d.ts +4 -8
- package/dist/index.js +20 -5
- package/dist/instances.d.ts +2 -2
- package/dist/instances.js +2 -2
- package/dist/layout.d.ts +2 -0
- package/dist/layout.js +4 -0
- package/dist/node-defaults.d.ts +2 -8
- package/dist/node-defaults.js +2 -2
- package/dist/order-keys.d.ts +39 -0
- package/dist/order-keys.d.ts.map +1 -0
- package/dist/order-keys.js +136 -0
- package/dist/order-keys.js.map +1 -0
- package/dist/parse-path.d.ts +7 -1
- package/dist/parse-path.d.ts.map +1 -1
- package/dist/parse-path.js +6 -2
- package/dist/parse-path.js.map +1 -1
- package/dist/polygon.d.ts +53 -0
- package/dist/polygon.d.ts.map +1 -0
- package/dist/polygon.js +205 -0
- package/dist/polygon.js.map +1 -0
- package/dist/preview.d.ts +1 -1
- package/dist/random.d.ts +7 -0
- package/dist/random.d.ts.map +1 -0
- package/dist/random.js +2 -0
- package/dist/resize.d.ts +11 -2
- package/dist/resize.d.ts.map +1 -1
- package/dist/resize.js +2 -184
- package/dist/snap.d.ts +1 -1
- package/dist/text-direction.d.ts +13 -0
- package/dist/text-direction.d.ts.map +1 -0
- package/dist/text-direction.js +31 -0
- package/dist/text-direction.js.map +1 -0
- package/dist/text-picture.d.ts +1 -1
- package/dist/text-picture.d.ts.map +1 -1
- package/dist/text.d.ts +2 -0
- package/dist/text.js +3 -0
- package/dist/types.d.ts +2063 -13
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +2 -1
- package/dist/types2.d.ts +2 -2
- package/dist/undo.d.ts +2 -2
- package/dist/undo.js +2 -2
- package/dist/undo2.d.ts +29 -1
- package/dist/undo2.d.ts.map +1 -1
- package/dist/variables.d.ts +2 -29
- package/dist/variables.js +2 -2
- package/dist/variant-properties.d.ts +9 -1
- package/dist/variant-properties.d.ts.map +1 -1
- package/dist/variant-properties.js +36 -1
- package/dist/variant-properties.js.map +1 -1
- package/dist/vector-network.d.ts +1 -1
- package/dist/vector-network.js +1 -1
- package/package.json +60 -9
- package/src/behaviours/kinds.ts +185 -0
- package/src/behaviours/layers.ts +51 -0
- package/src/behaviours/model.ts +192 -0
- package/src/behaviours/schema.ts +58 -0
- package/src/behaviours/spec.ts +223 -0
- package/src/buffered-events.ts +64 -0
- package/src/color/contrast.ts +31 -0
- package/src/color/index.ts +88 -0
- package/src/color/management.ts +210 -0
- package/src/color/normalize.ts +7 -0
- package/src/color/okhcl.ts +201 -0
- package/src/comments/document.ts +51 -0
- package/src/comments/list.ts +52 -0
- package/src/comments/merge.ts +63 -0
- package/src/comments/threads.ts +156 -0
- package/src/components/exposure.ts +61 -0
- package/src/components/properties.ts +18 -15
- package/src/components/variants.ts +53 -0
- package/src/constants.ts +2 -0
- package/src/coordinate.ts +100 -43
- package/src/copy.ts +13 -5
- package/src/css/grid.ts +70 -0
- package/src/css/index.ts +9 -0
- package/src/css/layout.ts +53 -0
- package/src/css/shadows.ts +68 -0
- package/src/css/values.ts +42 -0
- package/src/export-format.ts +12 -0
- package/src/fields/geometry.ts +11 -0
- package/src/fields/text.ts +35 -0
- package/src/gradient.ts +166 -0
- package/src/group-bounds.ts +131 -0
- package/src/guides.ts +39 -0
- package/src/hit-test.ts +219 -25
- package/src/icons/glyph.ts +128 -0
- package/src/icons/model.ts +54 -0
- package/src/icons/schema.ts +18 -0
- package/src/index.ts +344 -56
- package/src/instance-overrides.ts +27 -0
- package/src/instances/copies.ts +239 -0
- package/src/instances/cycles.ts +73 -0
- package/src/instances/fields.ts +143 -0
- package/src/instances/main-component.ts +19 -0
- package/src/instances/placement.ts +29 -0
- package/src/instances/source-walk.ts +41 -0
- package/src/instances/sync.ts +599 -0
- package/src/instances.ts +122 -330
- package/src/layout/aspect-ratio.ts +51 -0
- package/src/{layout-guides.ts → layout/guides.ts} +1 -1
- package/src/layout/index.ts +4 -0
- package/src/layout/keywords.ts +73 -0
- package/src/layout/sizing.ts +196 -0
- package/src/mutation-impact.ts +25 -10
- package/src/node-defaults.ts +50 -2
- package/src/order-keys.ts +162 -0
- package/src/parse-path.ts +10 -2
- package/src/plugin-data/field.ts +131 -0
- package/src/plugin-data/fields.ts +246 -0
- package/src/polygon.ts +211 -0
- package/src/preview.ts +1 -1
- package/src/random.ts +28 -0
- package/src/resize.ts +99 -12
- package/src/scaling/index.ts +2 -0
- package/src/scaling/node.ts +164 -0
- package/src/scaling/tree.ts +50 -0
- package/src/shaders/model.ts +98 -0
- package/src/shaders/schema.ts +48 -0
- package/src/slots/authoring.ts +101 -0
- package/src/slots/content.ts +124 -0
- package/src/slots/frames.ts +160 -0
- package/src/slots/limits.ts +49 -0
- package/src/text/direction.ts +55 -0
- package/src/text/index.ts +2 -0
- package/src/text/lists.ts +138 -0
- package/src/text/paragraphs.ts +241 -0
- package/src/{text-picture.ts → text/picture.ts} +21 -29
- package/src/transfer/apply.ts +84 -0
- package/src/transfer/history.ts +49 -0
- package/src/transfer/identity.ts +16 -0
- package/src/transfer/index.ts +6 -0
- package/src/transfer/nodes.ts +108 -0
- package/src/transfer/plan.ts +107 -0
- package/src/transfer/references.ts +9 -0
- package/src/transfer/remove.ts +39 -0
- package/src/transfer/validation.ts +54 -0
- package/src/transfer/variables.ts +82 -0
- package/src/types.ts +221 -11
- package/src/undo.ts +54 -2
- package/src/variables/bindings.ts +186 -0
- package/src/variables/fields.ts +43 -0
- package/src/variables/token.ts +14 -0
- package/src/variables/units.ts +15 -0
- package/src/variables.ts +118 -69
- package/src/variant-properties.ts +50 -0
- package/dist/chunks/components/properties.js.map +0 -1
- package/dist/chunks/export-scale.js +0 -19
- package/dist/chunks/export-scale.js.map +0 -1
- package/dist/coordinate.d.ts.map +0 -1
- package/dist/coordinate2.d.ts +0 -2
- package/dist/guides2.d.ts +0 -11
- package/dist/guides2.d.ts.map +0 -1
- package/dist/hit-test.d.ts.map +0 -1
- package/dist/layout-guides.d.ts +0 -2
- package/dist/layout-guides.js +0 -2
- package/dist/layout-guides2.d.ts +0 -16
- package/dist/layout-guides2.d.ts.map +0 -1
- package/dist/node-defaults.d.ts.map +0 -1
- package/dist/preview2.d.ts +0 -12
- package/dist/preview2.d.ts.map +0 -1
- package/dist/resize.js.map +0 -1
- package/dist/variables.d.ts.map +0 -1
- package/dist/vector-network2.d.ts +0 -32
- package/dist/vector-network2.d.ts.map +0 -1
- package/src/instance-overrides.test.ts +0 -67
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
export * from './contrast'
|
|
2
|
+
export * from './management'
|
|
3
|
+
export { normalizeColor } from './normalize'
|
|
4
|
+
export * from './okhcl'
|
|
5
|
+
|
|
6
|
+
import { parse, formatHex, formatHex8, formatRgb, converter, differenceEuclidean } from 'culori'
|
|
7
|
+
|
|
8
|
+
import { BLACK } from '../constants'
|
|
9
|
+
import type { Color } from '../primitives'
|
|
10
|
+
|
|
11
|
+
const toRGB = converter('rgb')
|
|
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
|
+
}
|
|
30
|
+
|
|
31
|
+
export function colorToHex(color: Color): string {
|
|
32
|
+
return formatHex({ mode: 'rgb', r: color.r, g: color.g, b: color.b }).toUpperCase()
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function colorToHex8(color: Color, alpha?: number): string {
|
|
36
|
+
const a = alpha ?? color.a
|
|
37
|
+
if (a >= 1) return colorToHex(color)
|
|
38
|
+
return formatHex8({ mode: 'rgb', r: color.r, g: color.g, b: color.b, alpha: a }).toUpperCase()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function colorToHexRaw(color: Color): string {
|
|
42
|
+
return colorToHex(color).slice(1)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function colorToRgba255(color: Color) {
|
|
46
|
+
return {
|
|
47
|
+
r: Math.round(color.r * 255),
|
|
48
|
+
g: Math.round(color.g * 255),
|
|
49
|
+
b: Math.round(color.b * 255),
|
|
50
|
+
a: color.a
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function colorToCSS(color: Color): string {
|
|
55
|
+
return formatRgb({ mode: 'rgb', r: color.r, g: color.g, b: color.b, alpha: color.a })
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function colorToCSSCompact(color: Color): string {
|
|
59
|
+
const { r, g, b } = colorToRgba255(color)
|
|
60
|
+
const a = Number(color.a.toFixed(3))
|
|
61
|
+
if (a >= 1) return `rgb(${r},${g},${b})`
|
|
62
|
+
return `rgba(${r},${g},${b},${a})`
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function rgba255ToColor(r: number, g: number, b: number, a = 1): Color {
|
|
66
|
+
return { r: r / 255, g: g / 255, b: b / 255, a }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function colorToFill(color: string | Color) {
|
|
70
|
+
const rgba = typeof color === 'string' ? parseColor(color) : color
|
|
71
|
+
return {
|
|
72
|
+
type: 'SOLID' as const,
|
|
73
|
+
color: { r: rgba.r, g: rgba.g, b: rgba.b, a: rgba.a },
|
|
74
|
+
opacity: rgba.a,
|
|
75
|
+
visible: true
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const euclideanRGB255 = differenceEuclidean('rgb')
|
|
80
|
+
|
|
81
|
+
export function colorDistance(c1: Color, c2: Color): number {
|
|
82
|
+
return (
|
|
83
|
+
euclideanRGB255(
|
|
84
|
+
{ mode: 'rgb', r: c1.r, g: c1.g, b: c1.b },
|
|
85
|
+
{ mode: 'rgb', r: c2.r, g: c2.g, b: c2.b }
|
|
86
|
+
) * 255
|
|
87
|
+
)
|
|
88
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { converter, formatCss, formatRgb, inGamut, parse, toGamut } from 'culori'
|
|
2
|
+
|
|
3
|
+
import type { Color } from '../primitives'
|
|
4
|
+
import type { DocumentColorSpace, Fill, SceneNode, Stroke } from '../types'
|
|
5
|
+
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
|
+
|
|
26
|
+
const toRGB = converter('rgb')
|
|
27
|
+
const toP3 = converter('p3')
|
|
28
|
+
const toDisplayableRGB = toGamut('rgb', 'oklch')
|
|
29
|
+
const toDisplayableP3 = toGamut('p3', 'oklch')
|
|
30
|
+
const isDisplayableRGB = inGamut('rgb')
|
|
31
|
+
const isDisplayableP3 = inGamut('p3')
|
|
32
|
+
|
|
33
|
+
function normalizeOkLCH(color: OkHCLColor) {
|
|
34
|
+
const hue = color.h % 360
|
|
35
|
+
return {
|
|
36
|
+
mode: 'oklch' as const,
|
|
37
|
+
l: Math.max(0, Math.min(1, color.l)),
|
|
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))
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Numbers are stored in the document's profile, so painting into a surface with a different
|
|
46
|
+
* profile has to convert them. Same profile means no work, which is the common case.
|
|
47
|
+
*/
|
|
48
|
+
function convertComponents(color: Color, from: RenderColorSpace, to: RenderColorSpace): Color {
|
|
49
|
+
if (from === to) return color
|
|
50
|
+
const source =
|
|
51
|
+
from === 'display-p3'
|
|
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
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function isOutsideGamut(color: Color, space: RenderColorSpace): boolean {
|
|
64
|
+
if (space === 'display-p3') {
|
|
65
|
+
return !isDisplayableP3({ mode: 'p3', r: color.r, g: color.g, b: color.b, alpha: color.a })
|
|
66
|
+
}
|
|
67
|
+
return !isDisplayableRGB({ mode: 'rgb', r: color.r, g: color.g, b: color.b, alpha: color.a })
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function resolveTargetSpace(options?: ColorPreviewOptions): RenderColorSpace {
|
|
71
|
+
return options?.colorSpace ?? options?.documentColorSpace ?? DEFAULT_COLOR_SPACE
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function formatCSSForTarget(color: Color, targetSpace: RenderColorSpace): string {
|
|
75
|
+
if (targetSpace === 'display-p3') {
|
|
76
|
+
const p3 = toP3({ mode: 'rgb', r: color.r, g: color.g, b: color.b, alpha: color.a })
|
|
77
|
+
return formatCss({
|
|
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
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function resolveOkHCLForPreview(
|
|
96
|
+
color: OkHCLColor,
|
|
97
|
+
options?: ColorPreviewOptions
|
|
98
|
+
): ResolvedRenderColor {
|
|
99
|
+
const oklch = normalizeOkLCH(color)
|
|
100
|
+
const targetSpace = resolveTargetSpace(options)
|
|
101
|
+
|
|
102
|
+
if (targetSpace === 'display-p3') {
|
|
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
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const clipped = !isDisplayableRGB(oklch)
|
|
129
|
+
const rgb = toRGB(toDisplayableRGB(oklch))
|
|
130
|
+
const resolved = normalizeColor({
|
|
131
|
+
r: rgb.r,
|
|
132
|
+
g: rgb.g,
|
|
133
|
+
b: rgb.b,
|
|
134
|
+
a: rgb.alpha ?? oklch.alpha
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
color: resolved,
|
|
139
|
+
cssColor: formatRgb({
|
|
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
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* A CSS color in any space, mapped into sRGB the way CSS maps out-of-gamut colors: by reducing
|
|
154
|
+
* chroma in OKLCH, so a Display P3 red becomes the nearest sRGB red, not a clipped one. A missing
|
|
155
|
+
* (`none`) component reads as zero. Null when `input` is not a color.
|
|
156
|
+
*/
|
|
157
|
+
export function parseDisplayableColor(input: string): Color | null {
|
|
158
|
+
const parsed = parse(input)
|
|
159
|
+
if (!parsed) return null
|
|
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 })
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export function resolveRGBAForPreview(
|
|
167
|
+
color: Color,
|
|
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
|
+
}
|
|
181
|
+
|
|
182
|
+
export function resolveNodeFillColor(
|
|
183
|
+
fill: Fill,
|
|
184
|
+
fillIndex: number,
|
|
185
|
+
node: SceneNode,
|
|
186
|
+
options?: ColorPreviewOptions
|
|
187
|
+
): ResolvedRenderColor {
|
|
188
|
+
const okhcl = getFillOkHCL(node, fillIndex)?.color
|
|
189
|
+
if (okhcl) return resolveOkHCLForPreview(okhcl, options)
|
|
190
|
+
return resolveRGBAForPreview(fill.color, options)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export function resolveNodeStrokeColor(
|
|
194
|
+
stroke: Stroke,
|
|
195
|
+
strokeIndex: number,
|
|
196
|
+
node: SceneNode,
|
|
197
|
+
options?: ColorPreviewOptions
|
|
198
|
+
): ResolvedRenderColor {
|
|
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
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function getDefaultRenderColorSpace(): RenderColorSpace {
|
|
209
|
+
return DEFAULT_COLOR_SPACE
|
|
210
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { converter, toGamut } from 'culori'
|
|
2
|
+
|
|
3
|
+
import { copyFill, copyStroke } from '../copy'
|
|
4
|
+
import { readAllPluginData, withAllPluginData } from '../plugin-data/field'
|
|
5
|
+
import { OPEN_PENCIL_PLUGIN_DATA } from '../plugin-data/fields'
|
|
6
|
+
import type { Color } from '../primitives'
|
|
7
|
+
import type { PluginDataEntry, SceneNode } from '../types'
|
|
8
|
+
import { normalizeColor } from './normalize'
|
|
9
|
+
|
|
10
|
+
export interface OkHCLColor {
|
|
11
|
+
h: number
|
|
12
|
+
c: number
|
|
13
|
+
l: number
|
|
14
|
+
a?: number
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface OkHCLPayload {
|
|
18
|
+
version: 1
|
|
19
|
+
kind: 'fill' | 'stroke'
|
|
20
|
+
index: number
|
|
21
|
+
color: OkHCLColor
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const toRGB = converter('rgb')
|
|
25
|
+
const toP3 = converter('p3')
|
|
26
|
+
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
|
+
|
|
33
|
+
function clampUnit(value: number): number {
|
|
34
|
+
if (value < 0) return 0
|
|
35
|
+
if (value > 1) return 1
|
|
36
|
+
return value
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function normalizeHue(value: number): number {
|
|
40
|
+
const hue = value % 360
|
|
41
|
+
return hue < 0 ? hue + 360 : hue
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function normalizeOkHCLColor(color: OkHCLColor): OkHCLColor {
|
|
45
|
+
return {
|
|
46
|
+
h: normalizeHue(color.h),
|
|
47
|
+
c: Math.max(0, color.c),
|
|
48
|
+
l: clampUnit(color.l),
|
|
49
|
+
a: clampUnit(color.a ?? 1)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Coordinates in `colorSpace`, so callers can store colours in the document's profile. */
|
|
54
|
+
export function okhclToRGBA(color: OkHCLColor, colorSpace: OkHCLColorSpace = 'srgb'): Color {
|
|
55
|
+
const normalized = normalizeOkHCLColor(color)
|
|
56
|
+
const oklch = {
|
|
57
|
+
mode: 'oklch' as const,
|
|
58
|
+
l: normalized.l,
|
|
59
|
+
c: normalized.c,
|
|
60
|
+
h: normalized.h,
|
|
61
|
+
alpha: normalized.a
|
|
62
|
+
}
|
|
63
|
+
if (colorSpace === 'display-p3') {
|
|
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
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Reads `color` as coordinates in `colorSpace`, the inverse of {@link okhclToRGBA}. */
|
|
82
|
+
export function rgbaToOkHCL(color: Color, colorSpace: OkHCLColorSpace = 'srgb'): OkHCLColor {
|
|
83
|
+
const oklch = toOkHCL(
|
|
84
|
+
colorSpace === 'display-p3'
|
|
85
|
+
? { mode: 'p3', r: color.r, g: color.g, b: color.b, alpha: color.a }
|
|
86
|
+
: { mode: 'rgb', r: color.r, g: color.g, b: color.b, alpha: color.a }
|
|
87
|
+
)
|
|
88
|
+
return normalizeOkHCLColor({
|
|
89
|
+
h: oklch.h ?? 0,
|
|
90
|
+
c: oklch.c,
|
|
91
|
+
l: oklch.l,
|
|
92
|
+
a: oklch.alpha ?? color.a
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function serializeOkHCLPayload(payload: OkHCLPayload): string {
|
|
97
|
+
return OPEN_PENCIL_PLUGIN_DATA.okhcl.encode(payload)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function parseOkHCLPayload(value: string): OkHCLPayload | null {
|
|
101
|
+
return OPEN_PENCIL_PLUGIN_DATA.okhcl.decode(value) ?? null
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function createOkHCLPayload(
|
|
105
|
+
kind: 'fill' | 'stroke',
|
|
106
|
+
index: number,
|
|
107
|
+
color: OkHCLColor
|
|
108
|
+
): OkHCLPayload {
|
|
109
|
+
return {
|
|
110
|
+
version: 1,
|
|
111
|
+
kind,
|
|
112
|
+
index,
|
|
113
|
+
color: normalizeOkHCLColor(color)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The node's plugin data with the payload for one paint replaced, or removed without `color`. */
|
|
118
|
+
function withOkHCLPayload(
|
|
119
|
+
node: SceneNode,
|
|
120
|
+
kind: 'fill' | 'stroke',
|
|
121
|
+
index: number,
|
|
122
|
+
color?: OkHCLColor
|
|
123
|
+
): PluginDataEntry[] {
|
|
124
|
+
const payloads = getNodeOkHCLPayloads(node).filter(
|
|
125
|
+
(payload) => payload.kind !== kind || payload.index !== index
|
|
126
|
+
)
|
|
127
|
+
if (color) payloads.push(createOkHCLPayload(kind, index, color))
|
|
128
|
+
return withAllPluginData(node.pluginData, OPEN_PENCIL_PLUGIN_DATA.okhcl, payloads)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function setNodeFillOkHCL(
|
|
132
|
+
node: SceneNode,
|
|
133
|
+
index: number,
|
|
134
|
+
color: OkHCLColor,
|
|
135
|
+
colorSpace: OkHCLColorSpace = 'srgb'
|
|
136
|
+
): Partial<SceneNode> {
|
|
137
|
+
const fills = node.fills.map(copyFill)
|
|
138
|
+
if (index < 0 || index >= fills.length) throw new Error(`Fill ${index} not found`)
|
|
139
|
+
const fill = fills[index]
|
|
140
|
+
const rgba = okhclToRGBA(color, colorSpace)
|
|
141
|
+
fills[index] = {
|
|
142
|
+
...fill,
|
|
143
|
+
color: rgba,
|
|
144
|
+
opacity: rgba.a
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
fills,
|
|
149
|
+
pluginData: withOkHCLPayload(node, 'fill', index, color)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function setNodeStrokeOkHCL(
|
|
154
|
+
node: SceneNode,
|
|
155
|
+
index: number,
|
|
156
|
+
color: OkHCLColor,
|
|
157
|
+
colorSpace: OkHCLColorSpace = 'srgb'
|
|
158
|
+
): Partial<SceneNode> {
|
|
159
|
+
const strokes = node.strokes.map(copyStroke)
|
|
160
|
+
if (index < 0 || index >= strokes.length) throw new Error(`Stroke ${index} not found`)
|
|
161
|
+
const stroke = strokes[index]
|
|
162
|
+
const rgba = okhclToRGBA(color, colorSpace)
|
|
163
|
+
strokes[index] = {
|
|
164
|
+
...stroke,
|
|
165
|
+
color: rgba,
|
|
166
|
+
opacity: rgba.a
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
strokes,
|
|
171
|
+
pluginData: withOkHCLPayload(node, 'stroke', index, color)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function clearNodeFillOkHCL(node: SceneNode, index: number): Partial<SceneNode> {
|
|
176
|
+
return { pluginData: withOkHCLPayload(node, 'fill', index) }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function clearNodeStrokeOkHCL(node: SceneNode, index: number): Partial<SceneNode> {
|
|
180
|
+
return { pluginData: withOkHCLPayload(node, 'stroke', index) }
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function getNodeOkHCLPayloads(node: SceneNode): OkHCLPayload[] {
|
|
184
|
+
return readAllPluginData(node.pluginData, OPEN_PENCIL_PLUGIN_DATA.okhcl)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function getFillOkHCL(node: SceneNode, index: number): OkHCLPayload | null {
|
|
188
|
+
return (
|
|
189
|
+
getNodeOkHCLPayloads(node).find(
|
|
190
|
+
(payload) => payload.kind === 'fill' && payload.index === index
|
|
191
|
+
) ?? null
|
|
192
|
+
)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function getStrokeOkHCL(node: SceneNode, index: number): OkHCLPayload | null {
|
|
196
|
+
return (
|
|
197
|
+
getNodeOkHCLPayloads(node).find(
|
|
198
|
+
(payload) => payload.kind === 'stroke' && payload.index === index
|
|
199
|
+
) ?? null
|
|
200
|
+
)
|
|
201
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { SceneGraph } from '../index'
|
|
2
|
+
import {
|
|
3
|
+
isPluginDataEntry,
|
|
4
|
+
readPluginData,
|
|
5
|
+
withoutPluginData,
|
|
6
|
+
withPluginData
|
|
7
|
+
} from '../plugin-data/field'
|
|
8
|
+
import { OPEN_PENCIL_PLUGIN_DATA } from '../plugin-data/fields'
|
|
9
|
+
import type { CommentThread, PluginDataEntry } from '../types'
|
|
10
|
+
|
|
11
|
+
const { comments } = OPEN_PENCIL_PLUGIN_DATA
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A document's comment threads, deleted ones included. Comments are part of the document: they
|
|
15
|
+
* live in OpenPencil's plugin data on the document node, so they travel with the `.fig` file,
|
|
16
|
+
* its storage copy and a collaboration room, and Figma or an older OpenPencil open the file as
|
|
17
|
+
* before.
|
|
18
|
+
*/
|
|
19
|
+
export function readComments(graph: SceneGraph): CommentThread[] {
|
|
20
|
+
return readPluginData(graph.getNode(graph.rootId)?.pluginData, comments) ?? []
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Stores the threads on the document node. This is a document change, so the document asks to
|
|
25
|
+
* be saved, but not an undo step: undoing a layer edit never takes back a comment.
|
|
26
|
+
*/
|
|
27
|
+
export function writeComments(graph: SceneGraph, threads: readonly CommentThread[]): void {
|
|
28
|
+
const root = graph.getNode(graph.rootId)
|
|
29
|
+
if (!root) return
|
|
30
|
+
graph.updateNode(root.id, {
|
|
31
|
+
pluginData: withPluginData(
|
|
32
|
+
root.pluginData,
|
|
33
|
+
comments,
|
|
34
|
+
threads.length > 0 ? [...threads] : undefined
|
|
35
|
+
)
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* `entries` holding the comments of `live` instead of their own. Undo and redo restore the
|
|
41
|
+
* document node with this, so they leave comments as they are now.
|
|
42
|
+
*/
|
|
43
|
+
export function withLiveComments(
|
|
44
|
+
entries: readonly PluginDataEntry[],
|
|
45
|
+
live: readonly PluginDataEntry[]
|
|
46
|
+
): PluginDataEntry[] {
|
|
47
|
+
return [
|
|
48
|
+
...withoutPluginData(entries, [comments]),
|
|
49
|
+
...live.filter((entry) => isPluginDataEntry(entry, comments))
|
|
50
|
+
]
|
|
51
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { CommentThread } from '../types'
|
|
2
|
+
import { byCreatedAt, liveReplies, takesPartInThread } from './threads'
|
|
3
|
+
|
|
4
|
+
export const COMMENT_SORTS = ['newest', 'oldest'] as const
|
|
5
|
+
export type CommentSort = (typeof COMMENT_SORTS)[number]
|
|
6
|
+
|
|
7
|
+
export interface CommentListOptions {
|
|
8
|
+
query: string
|
|
9
|
+
showResolved: boolean
|
|
10
|
+
/** Only threads on `pageId`. */
|
|
11
|
+
onlyPage: boolean
|
|
12
|
+
pageId: string
|
|
13
|
+
/** Only threads `author` started or replied to. */
|
|
14
|
+
onlyMine: boolean
|
|
15
|
+
author: string
|
|
16
|
+
sort: CommentSort
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function matches(thread: CommentThread, query: string): boolean {
|
|
20
|
+
const words = [thread.text, thread.author, thread.nodeName ?? '', thread.pageName ?? '']
|
|
21
|
+
for (const entry of liveReplies(thread)) words.push(entry.text, entry.author)
|
|
22
|
+
return words.some((word) => word.toLocaleLowerCase().includes(query))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Each thread's number, counted in the order threads were started, as Figma numbers them in its
|
|
27
|
+
* list. Deleted threads keep their place so the numbers people refer to do not shift.
|
|
28
|
+
*/
|
|
29
|
+
export function commentThreadNumbers(threads: readonly CommentThread[]): Map<string, number> {
|
|
30
|
+
const started = threads.toSorted(byCreatedAt)
|
|
31
|
+
return new Map(started.map((thread, index) => [thread.id, index + 1]))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The threads the comments list shows for its search, filters and order. */
|
|
35
|
+
export function listCommentThreads(
|
|
36
|
+
threads: readonly CommentThread[],
|
|
37
|
+
options: CommentListOptions
|
|
38
|
+
): CommentThread[] {
|
|
39
|
+
const query = options.query.trim().toLocaleLowerCase()
|
|
40
|
+
const author = options.author.trim()
|
|
41
|
+
const listed = threads.filter(
|
|
42
|
+
(thread) =>
|
|
43
|
+
!thread.deleted &&
|
|
44
|
+
(options.showResolved || !thread.resolved) &&
|
|
45
|
+
(!options.onlyPage || thread.pageId === options.pageId) &&
|
|
46
|
+
(!options.onlyMine || (author !== '' && takesPartInThread(thread, author))) &&
|
|
47
|
+
(query === '' || matches(thread, query))
|
|
48
|
+
)
|
|
49
|
+
// Ordered by when each thread was started, as Figma's "Sort by date" is.
|
|
50
|
+
const direction = options.sort === 'newest' ? -1 : 1
|
|
51
|
+
return listed.sort((a, b) => direction * byCreatedAt(a, b))
|
|
52
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { CommentReply, CommentThread } from '../types'
|
|
2
|
+
import { byCreatedAt } from './threads'
|
|
3
|
+
|
|
4
|
+
function mergeReplies(first: CommentReply[], second: CommentReply[]): CommentReply[] {
|
|
5
|
+
const byId = new Map<string, CommentReply>()
|
|
6
|
+
for (const reply of [...first, ...second]) {
|
|
7
|
+
const existing = byId.get(reply.id)
|
|
8
|
+
// A deletion is never undone by an older copy of the same reply.
|
|
9
|
+
byId.set(
|
|
10
|
+
reply.id,
|
|
11
|
+
existing ? { ...existing, ...reply, deleted: existing.deleted || reply.deleted } : reply
|
|
12
|
+
)
|
|
13
|
+
}
|
|
14
|
+
return [...byId.values()].sort(byCreatedAt)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Two copies of a document's comments become one. Nothing is lost: threads and replies are
|
|
19
|
+
* joined by id, and for a thread both copies changed, the newer edit wins while replies from
|
|
20
|
+
* both are kept. Deleted threads and replies stay deleted.
|
|
21
|
+
*/
|
|
22
|
+
export function mergeCommentThreads(
|
|
23
|
+
local: readonly CommentThread[],
|
|
24
|
+
remote: readonly CommentThread[]
|
|
25
|
+
): CommentThread[] {
|
|
26
|
+
const byId = new Map<string, CommentThread>()
|
|
27
|
+
for (const thread of remote) byId.set(thread.id, thread)
|
|
28
|
+
for (const thread of local) {
|
|
29
|
+
const other = byId.get(thread.id)
|
|
30
|
+
if (!other) {
|
|
31
|
+
byId.set(thread.id, thread)
|
|
32
|
+
continue
|
|
33
|
+
}
|
|
34
|
+
const newer = thread.updatedAt >= other.updatedAt ? thread : other
|
|
35
|
+
byId.set(thread.id, {
|
|
36
|
+
...newer,
|
|
37
|
+
deleted: thread.deleted || other.deleted,
|
|
38
|
+
replies: mergeReplies(other.replies, thread.replies)
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
return [...byId.values()].sort(byCreatedAt)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Whether `local` holds anything `remote` lacks: a thread or reply it does not have, a newer edit,
|
|
46
|
+
* or a deletion. Merging is then needed; otherwise `remote` already says everything.
|
|
47
|
+
*/
|
|
48
|
+
export function hasNewerComments(
|
|
49
|
+
local: readonly CommentThread[],
|
|
50
|
+
remote: readonly CommentThread[]
|
|
51
|
+
): boolean {
|
|
52
|
+
const remoteById = new Map(remote.map((thread) => [thread.id, thread]))
|
|
53
|
+
return local.some((thread) => {
|
|
54
|
+
const other = remoteById.get(thread.id)
|
|
55
|
+
if (!other) return true
|
|
56
|
+
if (thread.updatedAt > other.updatedAt || (thread.deleted && !other.deleted)) return true
|
|
57
|
+
const replies = new Map(other.replies.map((entry) => [entry.id, entry]))
|
|
58
|
+
return thread.replies.some((entry) => {
|
|
59
|
+
const known = replies.get(entry.id)
|
|
60
|
+
return !known || (!!entry.deleted && !known.deleted)
|
|
61
|
+
})
|
|
62
|
+
})
|
|
63
|
+
}
|