@pairdraw/scene-graph 0.19.2 → 0.21.0
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/color/index.d.ts +28 -7
- package/dist/color/index.d.ts.map +1 -1
- package/dist/color/index.js +40 -15
- package/dist/color/index.js.map +1 -1
- package/dist/color/management.d.ts +19 -6
- package/dist/color/management.d.ts.map +1 -1
- package/dist/color/management.js +56 -1
- package/dist/color/management.js.map +1 -1
- package/dist/color/profile.d.ts +32 -0
- package/dist/color/profile.d.ts.map +1 -0
- package/dist/color/profile.js +140 -0
- package/dist/color/profile.js.map +1 -0
- package/dist/comments/index.d.ts +2 -2
- package/dist/comments/index.js +2 -2
- package/dist/comments/merge.d.ts.map +1 -1
- package/dist/comments/merge.js +1 -0
- package/dist/comments/merge.js.map +1 -1
- package/dist/comments/schema.d.ts +3 -1
- package/dist/comments/schema.d.ts.map +1 -1
- package/dist/comments/schema.js +19 -2
- package/dist/comments/schema.js.map +1 -1
- package/dist/comments/threads.d.ts +3 -1
- package/dist/comments/threads.d.ts.map +1 -1
- package/dist/comments/threads.js +2 -1
- package/dist/comments/threads.js.map +1 -1
- package/dist/components/index.d.ts +2 -2
- package/dist/components/index.js +2 -2
- package/dist/components/properties.d.ts.map +1 -1
- package/dist/components/properties.js +7 -7
- package/dist/components/properties.js.map +1 -1
- package/dist/components/variants.d.ts +12 -1
- package/dist/components/variants.d.ts.map +1 -1
- package/dist/components/variants.js +30 -1
- package/dist/components/variants.js.map +1 -1
- package/dist/constants.d.ts +25 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +94 -1
- package/dist/constants.js.map +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +17 -1
- package/dist/copy.js.map +1 -1
- package/dist/css/index.d.ts +2 -2
- package/dist/css/index.js +2 -2
- package/dist/css/shadows.d.ts +2 -2
- package/dist/css/shadows.d.ts.map +1 -1
- package/dist/css/shadows.js +4 -4
- package/dist/css/shadows.js.map +1 -1
- package/dist/css/text.d.ts +17 -1
- package/dist/css/text.d.ts.map +1 -1
- package/dist/css/text.js +62 -1
- package/dist/css/text.js.map +1 -1
- package/dist/css/values.d.ts +7 -2
- package/dist/css/values.d.ts.map +1 -1
- package/dist/css/values.js +8 -4
- package/dist/css/values.js.map +1 -1
- package/dist/design-memory/index.d.ts +3 -0
- package/dist/design-memory/index.js +3 -0
- package/dist/design-memory/model.d.ts +33 -0
- package/dist/design-memory/model.d.ts.map +1 -0
- package/dist/design-memory/model.js +142 -0
- package/dist/design-memory/model.js.map +1 -0
- package/dist/design-memory/schema.d.ts +76 -0
- package/dist/design-memory/schema.d.ts.map +1 -0
- package/dist/design-memory/schema.js +72 -0
- package/dist/design-memory/schema.js.map +1 -0
- package/dist/effects.d.ts +7 -4
- package/dist/effects.d.ts.map +1 -1
- package/dist/effects.js +9 -4
- package/dist/effects.js.map +1 -1
- package/dist/empty-values.js +102 -0
- package/dist/empty-values.js.map +1 -0
- package/dist/fields/components.d.ts +9 -0
- package/dist/fields/components.d.ts.map +1 -0
- package/dist/fields/components.js +24 -0
- package/dist/fields/components.js.map +1 -0
- package/dist/fields/index.d.ts +3 -2
- package/dist/fields/index.js +3 -2
- package/dist/fields/layout.d.ts +3 -1
- package/dist/fields/layout.d.ts.map +1 -1
- package/dist/fields/layout.js +10 -1
- package/dist/fields/layout.js.map +1 -1
- package/dist/graph.d.ts +12 -1
- package/dist/graph.d.ts.map +1 -1
- package/dist/graph.js +39 -7
- package/dist/graph.js.map +1 -1
- package/dist/grid-values.d.ts +10 -0
- package/dist/grid-values.d.ts.map +1 -0
- package/dist/grid-values.js +53 -0
- package/dist/grid-values.js.map +1 -0
- package/dist/hit-test/drop.d.ts +20 -0
- package/dist/hit-test/drop.d.ts.map +1 -0
- package/dist/hit-test/drop.js +54 -0
- package/dist/hit-test/drop.js.map +1 -0
- package/dist/hit-test/geometry.js +86 -0
- package/dist/hit-test/geometry.js.map +1 -0
- package/dist/hit-test/masks.d.ts +14 -0
- package/dist/hit-test/masks.d.ts.map +1 -0
- package/dist/hit-test/masks.js +41 -0
- package/dist/hit-test/masks.js.map +1 -0
- package/dist/hit-test/paint.d.ts +6 -0
- package/dist/hit-test/paint.d.ts.map +1 -0
- package/dist/hit-test/paint.js +19 -0
- package/dist/hit-test/paint.js.map +1 -0
- package/dist/hit-test.d.ts +4 -15
- package/dist/hit-test.d.ts.map +1 -1
- package/dist/hit-test.js +5 -173
- package/dist/hit-test.js.map +1 -1
- package/dist/images.d.ts +3 -2
- package/dist/images.d.ts.map +1 -1
- package/dist/images.js +18 -2
- package/dist/images.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/instances/copies.js +2 -1
- package/dist/instances/copies.js.map +1 -1
- 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 +2 -1
- package/dist/instances/index.d.ts.map +1 -1
- package/dist/instances/index.js +13 -12
- package/dist/instances/index.js.map +1 -1
- package/dist/instances/overrides.d.ts +15 -1
- package/dist/instances/overrides.d.ts.map +1 -1
- package/dist/instances/overrides.js +37 -1
- package/dist/instances/overrides.js.map +1 -1
- package/dist/instances/source-walk.d.ts +12 -0
- package/dist/instances/source-walk.d.ts.map +1 -0
- package/dist/instances/swap-overrides.js +3 -3
- package/dist/instances/swap-overrides.js.map +1 -1
- package/dist/instances/sync.js +9 -7
- package/dist/instances/sync.js.map +1 -1
- package/dist/node-defaults.d.ts.map +1 -1
- package/dist/node-defaults.js +43 -38
- package/dist/node-defaults.js.map +1 -1
- package/dist/plugin-data/fields.d.ts +90 -0
- package/dist/plugin-data/fields.d.ts.map +1 -1
- package/dist/plugin-data/fields.js +33 -24
- package/dist/plugin-data/fields.js.map +1 -1
- package/dist/plugin-data/index.d.ts +2 -2
- package/dist/plugin-data/index.js +2 -2
- package/dist/plugin-data/schema.d.ts +6 -1
- package/dist/plugin-data/schema.d.ts.map +1 -1
- package/dist/plugin-data/schema.js +24 -1
- package/dist/plugin-data/schema.js.map +1 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +2 -0
- package/dist/preview.js.map +1 -1
- package/dist/scaling/node.d.ts.map +1 -1
- package/dist/scaling/node.js +5 -1
- package/dist/scaling/node.js.map +1 -1
- package/dist/shaders/animation.d.ts +21 -0
- package/dist/shaders/animation.d.ts.map +1 -0
- package/dist/shaders/animation.js +25 -0
- package/dist/shaders/animation.js.map +1 -0
- package/dist/shaders/index.d.ts +2 -1
- package/dist/shaders/index.js +2 -1
- package/dist/slots/content.d.ts.map +1 -1
- package/dist/slots/content.js +4 -4
- package/dist/slots/content.js.map +1 -1
- package/dist/statuses/index.d.ts +2 -2
- package/dist/statuses/index.js +2 -2
- package/dist/statuses/workflow.d.ts +3 -1
- package/dist/statuses/workflow.d.ts.map +1 -1
- package/dist/statuses/workflow.js +6 -2
- package/dist/statuses/workflow.js.map +1 -1
- package/dist/token-sources/document.d.ts +26 -0
- package/dist/token-sources/document.d.ts.map +1 -0
- package/dist/token-sources/document.js +80 -0
- package/dist/token-sources/document.js.map +1 -0
- package/dist/token-sources/index.d.ts +3 -0
- package/dist/token-sources/index.js +3 -0
- package/dist/token-sources/schema.d.ts +43 -0
- package/dist/token-sources/schema.d.ts.map +1 -0
- package/dist/token-sources/schema.js +50 -0
- package/dist/token-sources/schema.js.map +1 -0
- package/dist/transfer/variables.d.ts.map +1 -1
- package/dist/transfer/variables.js +1 -1
- package/dist/transfer/variables.js.map +1 -1
- package/dist/types.d.ts +135 -15
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/variables/bindings.d.ts +6 -5
- package/dist/variables/bindings.d.ts.map +1 -1
- package/dist/variables/bindings.js +24 -16
- package/dist/variables/bindings.js.map +1 -1
- package/dist/variables/formula/index.d.ts.map +1 -1
- package/dist/variables/formula/index.js +12 -6
- package/dist/variables/formula/index.js.map +1 -1
- package/dist/variables/index.d.ts +14 -1
- package/dist/variables/index.d.ts.map +1 -1
- package/dist/variables/index.js +38 -10
- package/dist/variables/index.js.map +1 -1
- package/dist/variables/paints.d.ts +9 -8
- package/dist/variables/paints.d.ts.map +1 -1
- package/dist/variables/paints.js +35 -26
- package/dist/variables/paints.js.map +1 -1
- package/dist/variables/resolver.d.ts +45 -0
- package/dist/variables/resolver.d.ts.map +1 -0
- package/dist/variables/resolver.js +106 -0
- package/dist/variables/resolver.js.map +1 -0
- package/dist/variations/copy.d.ts +16 -0
- package/dist/variations/copy.d.ts.map +1 -0
- package/dist/variations/copy.js +78 -0
- package/dist/variations/copy.js.map +1 -0
- package/dist/variations/index.d.ts +6 -0
- package/dist/variations/index.js +6 -0
- package/dist/variations/match.d.ts +23 -0
- package/dist/variations/match.d.ts.map +1 -0
- package/dist/variations/match.js +80 -0
- package/dist/variations/match.js.map +1 -0
- package/dist/variations/model.d.ts +39 -0
- package/dist/variations/model.d.ts.map +1 -0
- package/dist/variations/model.js +90 -0
- package/dist/variations/model.js.map +1 -0
- package/dist/variations/pick.d.ts +43 -0
- package/dist/variations/pick.d.ts.map +1 -0
- package/dist/variations/pick.js +211 -0
- package/dist/variations/pick.js.map +1 -0
- package/dist/variations/schema.d.ts +27 -0
- package/dist/variations/schema.d.ts.map +1 -0
- package/dist/variations/schema.js +27 -0
- package/dist/variations/schema.js.map +1 -0
- package/package.json +21 -1
- package/src/color/index.ts +43 -9
- package/src/color/management.ts +54 -1
- package/src/color/profile.ts +131 -0
- package/src/comments/merge.ts +3 -0
- package/src/comments/schema.ts +32 -2
- package/src/comments/threads.ts +5 -2
- package/src/components/properties.ts +13 -7
- package/src/components/variants.ts +39 -0
- package/src/constants.ts +75 -0
- package/src/copy.ts +20 -1
- package/src/css/index.ts +7 -1
- package/src/css/shadows.ts +8 -5
- package/src/css/text.ts +62 -0
- package/src/css/values.ts +12 -5
- package/src/design-memory/index.ts +2 -0
- package/src/design-memory/model.ts +143 -0
- package/src/design-memory/schema.ts +84 -0
- package/src/effects.ts +16 -7
- package/src/empty-values.ts +117 -0
- package/src/fields/components.ts +21 -0
- package/src/fields/index.ts +1 -0
- package/src/fields/layout.ts +10 -0
- package/src/graph.ts +50 -6
- package/src/grid-values.ts +47 -0
- package/src/hit-test/drop.ts +90 -0
- package/src/hit-test/geometry.ts +131 -0
- package/src/hit-test/masks.ts +57 -0
- package/src/hit-test/paint.ts +24 -0
- package/src/hit-test.ts +8 -277
- package/src/images.ts +27 -3
- package/src/instances/copies.ts +2 -1
- package/src/instances/fields.ts +2 -0
- package/src/instances/index.ts +20 -11
- package/src/instances/overrides.ts +49 -0
- package/src/instances/swap-overrides.ts +3 -2
- package/src/instances/sync.ts +11 -7
- package/src/node-defaults.ts +51 -41
- package/src/plugin-data/fields.ts +34 -32
- package/src/plugin-data/index.ts +1 -0
- package/src/plugin-data/schema.ts +30 -0
- package/src/preview.ts +2 -0
- package/src/scaling/node.ts +7 -2
- package/src/shaders/animation.ts +39 -0
- package/src/shaders/index.ts +1 -0
- package/src/slots/content.ts +8 -4
- package/src/statuses/workflow.ts +6 -4
- package/src/token-sources/document.ts +104 -0
- package/src/token-sources/index.ts +2 -0
- package/src/token-sources/schema.ts +59 -0
- package/src/transfer/variables.ts +2 -1
- package/src/types.ts +145 -14
- package/src/variables/bindings.ts +35 -18
- package/src/variables/formula/index.ts +19 -18
- package/src/variables/index.ts +49 -10
- package/src/variables/paints.ts +64 -29
- package/src/variables/resolver.ts +178 -0
- package/src/variations/copy.ts +89 -0
- package/src/variations/index.ts +5 -0
- package/src/variations/match.ts +100 -0
- package/src/variations/model.ts +132 -0
- package/src/variations/pick.ts +293 -0
- package/src/variations/schema.ts +29 -0
package/dist/color/index.d.ts
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
import { Color } from "../primitives/index.js";
|
|
2
|
+
import { DocumentColorSpace } from "../types.js";
|
|
2
3
|
import { compositeOver, contrastRatio, darkenForContrast, readableForeground } from "./contrast.js";
|
|
3
4
|
import { OkHCLColor, OkHCLColorSpace, OkHCLPayload, clearNodeFillOkHCL, clearNodeStrokeOkHCL, getFillOkHCL, getNodeOkHCLPayloads, getStrokeOkHCL, okhclToRGBA, parseOkHCLPayload, rgbaToOkHCL, serializeOkHCLPayload, setNodeFillOkHCL, setNodeStrokeOkHCL } from "./okhcl.js";
|
|
4
|
-
import { ColorIntentSpace, ColorPreviewOptions, RenderColorSpace, ResolvedRenderColor, colorToDisplayCSS, getDefaultRenderColorSpace, parseDisplayableColor, resolveNodeFillColor, resolveNodeStrokeColor, resolveOkHCLForPreview, resolveRGBAForPreview } from "./management.js";
|
|
5
|
+
import { ColorIntentSpace, ColorPreviewOptions, RenderColorSpace, ResolvedRenderColor, SRGB_SYNTAX_MODES, colorToDisplayCSS, fitIntoProfile, getDefaultRenderColorSpace, parseDisplayableColor, resolveNodeFillColor, resolveNodeStrokeColor, resolveOkHCLForPreview, resolveRGBAForPreview } from "./management.js";
|
|
6
|
+
import { GamutHandling, colorToProfileCSS, convertColor, fitsGamut, linearSRGBMatrix, toExtendedSRGB } from "./profile.js";
|
|
5
7
|
import { normalizeColor } from "./normalize.js";
|
|
6
8
|
import { colorSchema } from "./schema.js";
|
|
7
9
|
//#region src/color/index.d.ts
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
/**
|
|
11
|
+
* A CSS color as components in `profile`, or null when `input` is not one. Hex and the other sRGB
|
|
12
|
+
* syntaxes give their numbers as they are, as Figma takes typed or imported hex as the document's
|
|
13
|
+
* own numbers in either profile. A color that names its space, such as `color(display-p3 …)` or
|
|
14
|
+
* `oklch()`, is converted into `profile`, and brought inside it by reducing chroma when the profile
|
|
15
|
+
* cannot show it.
|
|
16
|
+
*/
|
|
17
|
+
declare function tryParseColor(input: string, profile?: DocumentColorSpace): Color | null;
|
|
18
|
+
/** A CSS color as components in `profile`, or black when `input` is not one. */
|
|
19
|
+
declare function parseColor(input: string, profile?: DocumentColorSpace): Color;
|
|
12
20
|
declare function colorToHex(color: Color): string;
|
|
13
21
|
declare function colorToHex8(color: Color, alpha?: number): string;
|
|
14
22
|
declare function colorToHexRaw(color: Color): string;
|
|
@@ -20,8 +28,21 @@ declare function colorToRgba255(color: Color): {
|
|
|
20
28
|
};
|
|
21
29
|
declare function colorToCSS(color: Color): string;
|
|
22
30
|
declare function colorToCSSCompact(color: Color): string;
|
|
31
|
+
/**
|
|
32
|
+
* A stored color as CSS in its document's profile: opaque ones as hex and translucent ones as
|
|
33
|
+
* `rgba()`, which Tailwind matches to its palette, or `color(display-p3 …)` in Display P3, where
|
|
34
|
+
* hex has no room for a component past 1. Colors already in the profile are written as stored.
|
|
35
|
+
*/
|
|
36
|
+
declare function colorToDocumentCSS(color: Color, profile?: DocumentColorSpace): string;
|
|
37
|
+
/**
|
|
38
|
+
* A stored color as CSS for a preview of it, such as a swatch or a slider thumb: exactly
|
|
39
|
+
* `colorToCSS` in an sRGB document, so nothing a document already showed changes, and in its own
|
|
40
|
+
* profile otherwise, where `rgb()` cannot show a component past 1.
|
|
41
|
+
*/
|
|
42
|
+
declare function colorToPreviewCSS(color: Color, profile?: DocumentColorSpace): string;
|
|
23
43
|
declare function rgba255ToColor(r: number, g: number, b: number, a?: number): Color;
|
|
24
|
-
|
|
44
|
+
/** A solid paint of `color`, a CSS color read into `profile` or components already in it. */
|
|
45
|
+
declare function colorToFill(color: string | Color, profile?: DocumentColorSpace): {
|
|
25
46
|
type: "SOLID";
|
|
26
47
|
color: {
|
|
27
48
|
r: number;
|
|
@@ -34,5 +55,5 @@ declare function colorToFill(color: string | Color): {
|
|
|
34
55
|
};
|
|
35
56
|
declare function colorDistance(c1: Color, c2: Color): number;
|
|
36
57
|
//#endregion
|
|
37
|
-
export { ColorIntentSpace, ColorPreviewOptions, OkHCLColor, OkHCLColorSpace, OkHCLPayload, RenderColorSpace, ResolvedRenderColor, clearNodeFillOkHCL, clearNodeStrokeOkHCL, colorDistance, colorSchema, colorToCSS, colorToCSSCompact, colorToDisplayCSS, colorToFill, colorToHex, colorToHex8, colorToHexRaw, colorToRgba255, compositeOver, contrastRatio, darkenForContrast, getDefaultRenderColorSpace, getFillOkHCL, getNodeOkHCLPayloads, getStrokeOkHCL, normalizeColor, okhclToRGBA, parseColor, parseDisplayableColor, parseOkHCLPayload, readableForeground, resolveNodeFillColor, resolveNodeStrokeColor, resolveOkHCLForPreview, resolveRGBAForPreview, rgba255ToColor, rgbaToOkHCL, serializeOkHCLPayload, setNodeFillOkHCL, setNodeStrokeOkHCL, tryParseColor };
|
|
58
|
+
export { ColorIntentSpace, ColorPreviewOptions, GamutHandling, OkHCLColor, OkHCLColorSpace, OkHCLPayload, RenderColorSpace, ResolvedRenderColor, SRGB_SYNTAX_MODES, clearNodeFillOkHCL, clearNodeStrokeOkHCL, colorDistance, colorSchema, colorToCSS, colorToCSSCompact, colorToDisplayCSS, colorToDocumentCSS, colorToFill, colorToHex, colorToHex8, colorToHexRaw, colorToPreviewCSS, colorToProfileCSS, colorToRgba255, compositeOver, contrastRatio, convertColor, darkenForContrast, fitIntoProfile, fitsGamut, getDefaultRenderColorSpace, getFillOkHCL, getNodeOkHCLPayloads, getStrokeOkHCL, linearSRGBMatrix, normalizeColor, okhclToRGBA, parseColor, parseDisplayableColor, parseOkHCLPayload, readableForeground, resolveNodeFillColor, resolveNodeStrokeColor, resolveOkHCLForPreview, resolveRGBAForPreview, rgba255ToColor, rgbaToOkHCL, serializeOkHCLPayload, setNodeFillOkHCL, setNodeStrokeOkHCL, toExtendedSRGB, tryParseColor };
|
|
38
59
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/color/index.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/color/index.ts"],"mappings":";;;;;;;;;;;;;;;;iBAyBgB,cAAc,eAAe,UAAS,qBAA8B;;iBAapE,WAAW,eAAe,UAAS,qBAA8B;iBAIjE,WAAW,OAAO;iBAIlB,YAAY,OAAO,OAAO;iBAM1B,cAAc,OAAO;iBAIrB,eAAe,OAAO;;;;;;iBAStB,WAAW,OAAO;iBAIlB,kBAAkB,OAAO;;;;;;iBAYzB,mBAAmB,OAAO,OAAO,UAAS;;;;;;iBAU1C,kBAAkB,OAAO,OAAO,UAAS;iBAIzC,eAAe,WAAW,WAAW,WAAW,aAAQ;;iBAKxD,YAAY,gBAAgB,OAAO,UAAS;;;;;;;;;;;iBAY5C,cAAc,IAAI,OAAO,IAAI"}
|
package/dist/color/index.js
CHANGED
|
@@ -3,25 +3,32 @@ import { colorSchema } from "./schema.js";
|
|
|
3
3
|
import { compositeOver, contrastRatio, darkenForContrast, readableForeground } from "./contrast.js";
|
|
4
4
|
import { normalizeColor } from "./normalize.js";
|
|
5
5
|
import { clearNodeFillOkHCL, clearNodeStrokeOkHCL, getFillOkHCL, getNodeOkHCLPayloads, getStrokeOkHCL, okhclToRGBA, parseOkHCLPayload, rgbaToOkHCL, serializeOkHCLPayload, setNodeFillOkHCL, setNodeStrokeOkHCL } from "./okhcl.js";
|
|
6
|
-
import { colorToDisplayCSS, getDefaultRenderColorSpace, parseDisplayableColor, resolveNodeFillColor, resolveNodeStrokeColor, resolveOkHCLForPreview, resolveRGBAForPreview } from "./management.js";
|
|
6
|
+
import { SRGB_SYNTAX_MODES, colorToDisplayCSS, fitIntoProfile, getDefaultRenderColorSpace, parseDisplayableColor, resolveNodeFillColor, resolveNodeStrokeColor, resolveOkHCLForPreview, resolveRGBAForPreview } from "./management.js";
|
|
7
|
+
import { colorToProfileCSS, convertColor, fitsGamut, linearSRGBMatrix, toExtendedSRGB } from "./profile.js";
|
|
7
8
|
import { converter, differenceEuclidean, formatHex, formatHex8, formatRgb, parse } from "culori";
|
|
8
9
|
//#region src/color/index.ts
|
|
9
10
|
const toRGB = converter("rgb");
|
|
10
|
-
/**
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* A CSS color as components in `profile`, or null when `input` is not one. Hex and the other sRGB
|
|
13
|
+
* syntaxes give their numbers as they are, as Figma takes typed or imported hex as the document's
|
|
14
|
+
* own numbers in either profile. A color that names its space, such as `color(display-p3 …)` or
|
|
15
|
+
* `oklch()`, is converted into `profile`, and brought inside it by reducing chroma when the profile
|
|
16
|
+
* cannot show it.
|
|
17
|
+
*/
|
|
18
|
+
function tryParseColor(input, profile = "srgb") {
|
|
12
19
|
const parsed = parse(input);
|
|
13
20
|
if (!parsed) return null;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
a: parsed.alpha
|
|
21
|
+
if (Object.values(parsed).some((value) => typeof value === "number" && !Number.isFinite(value))) return null;
|
|
22
|
+
const a = parsed.alpha ?? 1;
|
|
23
|
+
if (SRGB_SYNTAX_MODES.has(parsed.mode)) return normalizeColor({
|
|
24
|
+
...toRGB(parsed),
|
|
25
|
+
a
|
|
20
26
|
});
|
|
27
|
+
return fitIntoProfile(parsed, profile).color;
|
|
21
28
|
}
|
|
22
|
-
/** A CSS color
|
|
23
|
-
function parseColor(input) {
|
|
24
|
-
return tryParseColor(input) ?? { ...BLACK };
|
|
29
|
+
/** A CSS color as components in `profile`, or black when `input` is not one. */
|
|
30
|
+
function parseColor(input, profile = "srgb") {
|
|
31
|
+
return tryParseColor(input, profile) ?? { ...BLACK };
|
|
25
32
|
}
|
|
26
33
|
function colorToHex(color) {
|
|
27
34
|
return formatHex({
|
|
@@ -68,6 +75,23 @@ function colorToCSSCompact(color) {
|
|
|
68
75
|
if (a >= 1) return `rgb(${r},${g},${b})`;
|
|
69
76
|
return `rgba(${r},${g},${b},${a})`;
|
|
70
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* A stored color as CSS in its document's profile: opaque ones as hex and translucent ones as
|
|
80
|
+
* `rgba()`, which Tailwind matches to its palette, or `color(display-p3 …)` in Display P3, where
|
|
81
|
+
* hex has no room for a component past 1. Colors already in the profile are written as stored.
|
|
82
|
+
*/
|
|
83
|
+
function colorToDocumentCSS(color, profile = "srgb") {
|
|
84
|
+
if (profile === "srgb") return color.a >= 1 ? colorToHex(color) : colorToCSS(color);
|
|
85
|
+
return colorToProfileCSS(color, profile, "display-p3");
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* A stored color as CSS for a preview of it, such as a swatch or a slider thumb: exactly
|
|
89
|
+
* `colorToCSS` in an sRGB document, so nothing a document already showed changes, and in its own
|
|
90
|
+
* profile otherwise, where `rgb()` cannot show a component past 1.
|
|
91
|
+
*/
|
|
92
|
+
function colorToPreviewCSS(color, profile = "srgb") {
|
|
93
|
+
return profile === "srgb" ? colorToCSS(color) : colorToDocumentCSS(color, profile);
|
|
94
|
+
}
|
|
71
95
|
function rgba255ToColor(r, g, b, a = 1) {
|
|
72
96
|
return {
|
|
73
97
|
r: r / 255,
|
|
@@ -76,8 +100,9 @@ function rgba255ToColor(r, g, b, a = 1) {
|
|
|
76
100
|
a
|
|
77
101
|
};
|
|
78
102
|
}
|
|
79
|
-
|
|
80
|
-
|
|
103
|
+
/** A solid paint of `color`, a CSS color read into `profile` or components already in it. */
|
|
104
|
+
function colorToFill(color, profile = "srgb") {
|
|
105
|
+
const rgba = typeof color === "string" ? parseColor(color, profile) : color;
|
|
81
106
|
return {
|
|
82
107
|
type: "SOLID",
|
|
83
108
|
color: {
|
|
@@ -105,6 +130,6 @@ function colorDistance(c1, c2) {
|
|
|
105
130
|
}) * 255;
|
|
106
131
|
}
|
|
107
132
|
//#endregion
|
|
108
|
-
export { clearNodeFillOkHCL, clearNodeStrokeOkHCL, colorDistance, colorSchema, colorToCSS, colorToCSSCompact, colorToDisplayCSS, colorToFill, colorToHex, colorToHex8, colorToHexRaw, colorToRgba255, compositeOver, contrastRatio, darkenForContrast, getDefaultRenderColorSpace, getFillOkHCL, getNodeOkHCLPayloads, getStrokeOkHCL, normalizeColor, okhclToRGBA, parseColor, parseDisplayableColor, parseOkHCLPayload, readableForeground, resolveNodeFillColor, resolveNodeStrokeColor, resolveOkHCLForPreview, resolveRGBAForPreview, rgba255ToColor, rgbaToOkHCL, serializeOkHCLPayload, setNodeFillOkHCL, setNodeStrokeOkHCL, tryParseColor };
|
|
133
|
+
export { SRGB_SYNTAX_MODES, clearNodeFillOkHCL, clearNodeStrokeOkHCL, colorDistance, colorSchema, colorToCSS, colorToCSSCompact, colorToDisplayCSS, colorToDocumentCSS, colorToFill, colorToHex, colorToHex8, colorToHexRaw, colorToPreviewCSS, colorToProfileCSS, colorToRgba255, compositeOver, contrastRatio, convertColor, darkenForContrast, fitIntoProfile, fitsGamut, getDefaultRenderColorSpace, getFillOkHCL, getNodeOkHCLPayloads, getStrokeOkHCL, linearSRGBMatrix, normalizeColor, okhclToRGBA, parseColor, parseDisplayableColor, parseOkHCLPayload, readableForeground, resolveNodeFillColor, resolveNodeStrokeColor, resolveOkHCLForPreview, resolveRGBAForPreview, rgba255ToColor, rgbaToOkHCL, serializeOkHCLPayload, setNodeFillOkHCL, setNodeStrokeOkHCL, toExtendedSRGB, tryParseColor };
|
|
109
134
|
|
|
110
135
|
//# sourceMappingURL=index.js.map
|
package/dist/color/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/color/index.ts"],"sourcesContent":["export * from './contrast'\nexport * from './management'\nexport { normalizeColor } from './normalize'\nexport * from './okhcl'\nexport { colorSchema } from './schema'\n\nimport { parse, formatHex, formatHex8, formatRgb, converter, differenceEuclidean } from 'culori'\n\nimport { BLACK } from '../constants'\nimport type { Color } from '../primitives'\nimport { normalizeColor } from './normalize'\n\nconst toRGB = converter('rgb')\n\n
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/color/index.ts"],"sourcesContent":["export * from './contrast'\nexport * from './management'\nexport * from './profile'\nexport { normalizeColor } from './normalize'\nexport * from './okhcl'\nexport { colorSchema } from './schema'\n\nimport { parse, formatHex, formatHex8, formatRgb, converter, differenceEuclidean } from 'culori'\n\nimport { BLACK } from '../constants'\nimport type { Color } from '../primitives'\nimport type { DocumentColorSpace } from '../types'\nimport { fitIntoProfile, SRGB_SYNTAX_MODES } from './management'\nimport { normalizeColor } from './normalize'\nimport { colorToProfileCSS } from './profile'\n\nconst toRGB = converter('rgb')\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 profile\n * cannot show it.\n */\nexport function tryParseColor(input: string, profile: DocumentColorSpace = '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 fitIntoProfile(parsed, profile).color\n}\n\n/** A CSS color as components in `profile`, or black when `input` is not one. */\nexport function parseColor(input: string, profile: DocumentColorSpace = 'srgb'): Color {\n return tryParseColor(input, profile) ?? { ...BLACK }\n}\n\nexport function colorToHex(color: Color): string {\n return formatHex({ mode: 'rgb', r: color.r, g: color.g, b: color.b }).toUpperCase()\n}\n\nexport function colorToHex8(color: Color, alpha?: number): string {\n const a = alpha ?? color.a\n if (a >= 1) return colorToHex(color)\n return formatHex8({ mode: 'rgb', r: color.r, g: color.g, b: color.b, alpha: a }).toUpperCase()\n}\n\nexport function colorToHexRaw(color: Color): string {\n return colorToHex(color).slice(1)\n}\n\nexport function colorToRgba255(color: Color) {\n return {\n r: Math.round(color.r * 255),\n g: Math.round(color.g * 255),\n b: Math.round(color.b * 255),\n a: color.a\n }\n}\n\nexport function colorToCSS(color: Color): string {\n return formatRgb({ mode: 'rgb', r: color.r, g: color.g, b: color.b, alpha: color.a })\n}\n\nexport function colorToCSSCompact(color: Color): string {\n const { r, g, b } = colorToRgba255(color)\n const a = Number(color.a.toFixed(3))\n if (a >= 1) return `rgb(${r},${g},${b})`\n return `rgba(${r},${g},${b},${a})`\n}\n\n/**\n * A stored color as CSS in its document's profile: opaque ones as hex and translucent ones as\n * `rgba()`, which Tailwind matches to its palette, or `color(display-p3 …)` in Display P3, where\n * hex has no room for a component past 1. Colors already in the profile are written as stored.\n */\nexport function colorToDocumentCSS(color: Color, profile: DocumentColorSpace = 'srgb'): string {\n if (profile === 'srgb') return color.a >= 1 ? colorToHex(color) : colorToCSS(color)\n return colorToProfileCSS(color, profile, 'display-p3')\n}\n\n/**\n * A stored color as CSS for a preview of it, such as a swatch or a slider thumb: exactly\n * `colorToCSS` in an sRGB document, so nothing a document already showed changes, and in its own\n * profile otherwise, where `rgb()` cannot show a component past 1.\n */\nexport function colorToPreviewCSS(color: Color, profile: DocumentColorSpace = 'srgb'): string {\n return profile === 'srgb' ? colorToCSS(color) : colorToDocumentCSS(color, profile)\n}\n\nexport function rgba255ToColor(r: number, g: number, b: number, a = 1): Color {\n return { r: r / 255, g: g / 255, b: b / 255, a }\n}\n\n/** A solid paint of `color`, a CSS color read into `profile` or components already in it. */\nexport function colorToFill(color: string | Color, profile: DocumentColorSpace = 'srgb') {\n const rgba = typeof color === 'string' ? parseColor(color, profile) : color\n return {\n type: 'SOLID' as const,\n color: { r: rgba.r, g: rgba.g, b: rgba.b, a: rgba.a },\n opacity: rgba.a,\n visible: true\n }\n}\n\nconst euclideanRGB255 = differenceEuclidean('rgb')\n\nexport function colorDistance(c1: Color, c2: Color): number {\n return (\n euclideanRGB255(\n { mode: 'rgb', r: c1.r, g: c1.g, b: c1.b },\n { mode: 'rgb', r: c2.r, g: c2.g, b: c2.b }\n ) * 255\n )\n}\nexport * from './schema'\n"],"mappings":";;;;;;;;;AAgBA,MAAM,QAAQ,UAAU,KAAK;;;;;;;;AAS7B,SAAgB,cAAc,OAAe,UAA8B,QAAsB;CAC/F,MAAM,SA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1,6 +1,7 @@
|
|
|
1
|
-
import { Color } from "../primitives/index.js";
|
|
1
|
+
import { Color as Color$1 } from "../primitives/index.js";
|
|
2
2
|
import { DocumentColorSpace, Fill, SceneNode, Stroke } from "../types.js";
|
|
3
3
|
import { OkHCLColor } from "./okhcl.js";
|
|
4
|
+
import { Color } from "culori";
|
|
4
5
|
//#region src/color/management.d.ts
|
|
5
6
|
type RenderColorSpace = 'srgb' | 'display-p3';
|
|
6
7
|
type ColorIntentSpace = 'oklch' | 'srgb';
|
|
@@ -9,24 +10,36 @@ interface ColorPreviewOptions {
|
|
|
9
10
|
documentColorSpace?: DocumentColorSpace;
|
|
10
11
|
}
|
|
11
12
|
interface ResolvedRenderColor {
|
|
12
|
-
color: Color;
|
|
13
|
+
color: Color$1;
|
|
13
14
|
cssColor: string;
|
|
14
15
|
sourceSpace: ColorIntentSpace;
|
|
15
16
|
targetSpace: RenderColorSpace;
|
|
16
17
|
clipped: boolean;
|
|
17
18
|
}
|
|
18
19
|
declare function resolveOkHCLForPreview(color: OkHCLColor, options?: ColorPreviewOptions): ResolvedRenderColor;
|
|
20
|
+
/**
|
|
21
|
+
* `color`, any culori color, as components in `profile`, brought inside it the way CSS maps
|
|
22
|
+
* out-of-gamut colors — by reducing chroma in OKLCH so lightness and hue hold — and whether it had
|
|
23
|
+
* to be. A color inside already keeps its components, as mapping it would shift them by float
|
|
24
|
+
* noise. A missing (`none`) component reads as zero.
|
|
25
|
+
*/
|
|
26
|
+
declare function fitIntoProfile(color: Color, profile: DocumentColorSpace): {
|
|
27
|
+
color: Color$1;
|
|
28
|
+
clipped: boolean;
|
|
29
|
+
};
|
|
30
|
+
/** Syntaxes CSS reads as sRGB numbers: hex, `rgb()`, `hsl()`, `hwb()`, names, `color(srgb …)`. */
|
|
31
|
+
declare const SRGB_SYNTAX_MODES: Set<string>;
|
|
19
32
|
/**
|
|
20
33
|
* A CSS color in any space, mapped into sRGB the way CSS maps out-of-gamut colors: by reducing
|
|
21
34
|
* chroma in OKLCH, so a Display P3 red becomes the nearest sRGB red, not a clipped one. A missing
|
|
22
35
|
* (`none`) component reads as zero. Null when `input` is not a color.
|
|
23
36
|
*/
|
|
24
|
-
declare function parseDisplayableColor(input: string): Color | null;
|
|
25
|
-
declare function resolveRGBAForPreview(color: Color, options?: ColorPreviewOptions): ResolvedRenderColor;
|
|
37
|
+
declare function parseDisplayableColor(input: string): Color$1 | null;
|
|
38
|
+
declare function resolveRGBAForPreview(color: Color$1, options?: ColorPreviewOptions): ResolvedRenderColor;
|
|
26
39
|
declare function resolveNodeFillColor(fill: Fill, fillIndex: number, node: SceneNode, options?: ColorPreviewOptions): ResolvedRenderColor;
|
|
27
40
|
declare function resolveNodeStrokeColor(stroke: Stroke, strokeIndex: number, node: SceneNode, options?: ColorPreviewOptions): ResolvedRenderColor;
|
|
28
|
-
declare function colorToDisplayCSS(color: Color, options?: ColorPreviewOptions): string;
|
|
41
|
+
declare function colorToDisplayCSS(color: Color$1, options?: ColorPreviewOptions): string;
|
|
29
42
|
declare function getDefaultRenderColorSpace(): RenderColorSpace;
|
|
30
43
|
//#endregion
|
|
31
|
-
export { ColorIntentSpace, ColorPreviewOptions, RenderColorSpace, ResolvedRenderColor, colorToDisplayCSS, getDefaultRenderColorSpace, parseDisplayableColor, resolveNodeFillColor, resolveNodeStrokeColor, resolveOkHCLForPreview, resolveRGBAForPreview };
|
|
44
|
+
export { ColorIntentSpace, ColorPreviewOptions, RenderColorSpace, ResolvedRenderColor, SRGB_SYNTAX_MODES, colorToDisplayCSS, fitIntoProfile, getDefaultRenderColorSpace, parseDisplayableColor, resolveNodeFillColor, resolveNodeStrokeColor, resolveOkHCLForPreview, resolveRGBAForPreview };
|
|
32
45
|
//# sourceMappingURL=management.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"management.d.ts","names":[],"sources":["../../src/color/management.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"management.d.ts","names":[],"sources":["../../src/color/management.ts"],"mappings":";;;;;KAgBY;KACA;UAEK;EACf,aAAa;EACb,qBAAqB;;UAGN;EACf,OAAO;EACP;EACA,aAAa;EACb,aAAa;EACb;;iBA2Ec,uBACd,OAAO,YACP,UAAU,sBACT;;;;;;;iBA0Ea,eACd,OAAO,OACP,SAAS;EACN,OAAO;EAAO;;;cAmBN,mBAAiB;;;;;;iBAOd,sBAAsB,gBAAgB;iBActC,sBACd,OAAO,SACP,UAAU,sBACT;iBAaa,qBACd,MAAM,MACN,mBACA,MAAM,WACN,UAAU,sBACT;iBAMa,uBACd,QAAQ,QACR,qBACA,MAAM,WACN,UAAU,sBACT;iBAMa,kBAAkB,OAAO,SAAO,UAAU;iBAI1C,8BAA8B"}
|
package/dist/color/management.js
CHANGED
|
@@ -139,6 +139,61 @@ function resolveOkHCLForPreview(color, options) {
|
|
|
139
139
|
};
|
|
140
140
|
}
|
|
141
141
|
/**
|
|
142
|
+
* `color`, any culori color, as components in `profile`, past 0 and 1 if need be.
|
|
143
|
+
*/
|
|
144
|
+
function exactlyInProfile(color, profile) {
|
|
145
|
+
const converted = profile === "display-p3" ? toP3(color) : toRGB(color);
|
|
146
|
+
return {
|
|
147
|
+
r: converted.r,
|
|
148
|
+
g: converted.g,
|
|
149
|
+
b: converted.b,
|
|
150
|
+
a: converted.alpha ?? 1
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function isInsideProfile(color, profile) {
|
|
154
|
+
if (profile === "display-p3") return isDisplayableP3({
|
|
155
|
+
mode: "p3",
|
|
156
|
+
r: color.r,
|
|
157
|
+
g: color.g,
|
|
158
|
+
b: color.b,
|
|
159
|
+
alpha: color.a
|
|
160
|
+
});
|
|
161
|
+
return isDisplayableRGB({
|
|
162
|
+
mode: "rgb",
|
|
163
|
+
r: color.r,
|
|
164
|
+
g: color.g,
|
|
165
|
+
b: color.b,
|
|
166
|
+
alpha: color.a
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* `color`, any culori color, as components in `profile`, brought inside it the way CSS maps
|
|
171
|
+
* out-of-gamut colors — by reducing chroma in OKLCH so lightness and hue hold — and whether it had
|
|
172
|
+
* to be. A color inside already keeps its components, as mapping it would shift them by float
|
|
173
|
+
* noise. A missing (`none`) component reads as zero.
|
|
174
|
+
*/
|
|
175
|
+
function fitIntoProfile(color, profile) {
|
|
176
|
+
const exact = normalizeColor(exactlyInProfile(color, profile));
|
|
177
|
+
const clipped = !isInsideProfile(exact, profile);
|
|
178
|
+
const inside = clipped ? normalizeColor(exactlyInProfile(profile === "display-p3" ? toDisplayableP3(color) : toDisplayableRGB(color), profile)) : exact;
|
|
179
|
+
const clamp = (channel) => Math.min(1, Math.max(0, channel));
|
|
180
|
+
return {
|
|
181
|
+
color: {
|
|
182
|
+
r: clamp(inside.r),
|
|
183
|
+
g: clamp(inside.g),
|
|
184
|
+
b: clamp(inside.b),
|
|
185
|
+
a: color.alpha ?? 1
|
|
186
|
+
},
|
|
187
|
+
clipped
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
/** Syntaxes CSS reads as sRGB numbers: hex, `rgb()`, `hsl()`, `hwb()`, names, `color(srgb …)`. */
|
|
191
|
+
const SRGB_SYNTAX_MODES = /* @__PURE__ */ new Set([
|
|
192
|
+
"rgb",
|
|
193
|
+
"hsl",
|
|
194
|
+
"hwb"
|
|
195
|
+
]);
|
|
196
|
+
/**
|
|
142
197
|
* A CSS color in any space, mapped into sRGB the way CSS maps out-of-gamut colors: by reducing
|
|
143
198
|
* chroma in OKLCH, so a Display P3 red becomes the nearest sRGB red, not a clipped one. A missing
|
|
144
199
|
* (`none`) component reads as zero. Null when `input` is not a color.
|
|
@@ -184,6 +239,6 @@ function getDefaultRenderColorSpace() {
|
|
|
184
239
|
return DEFAULT_COLOR_SPACE;
|
|
185
240
|
}
|
|
186
241
|
//#endregion
|
|
187
|
-
export { colorToDisplayCSS, getDefaultRenderColorSpace, parseDisplayableColor, resolveNodeFillColor, resolveNodeStrokeColor, resolveOkHCLForPreview, resolveRGBAForPreview };
|
|
242
|
+
export { SRGB_SYNTAX_MODES, colorToDisplayCSS, fitIntoProfile, getDefaultRenderColorSpace, parseDisplayableColor, resolveNodeFillColor, resolveNodeStrokeColor, resolveOkHCLForPreview, resolveRGBAForPreview };
|
|
188
243
|
|
|
189
244
|
//# sourceMappingURL=management.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"management.js","names":[],"sources":["../../src/color/management.ts"],"sourcesContent":["import { clampGamut, 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 clampToRGB = clampGamut('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 // CSS clamps out-of-range sRGB channels and gamut-maps wider colors; clamping also removes the\n // float noise gamut mapping can leave just outside 0..1.\n const rgb = isDisplayableRGB(parsed)\n ? toRGB(parsed)\n : clampToRGB(parsed.mode === 'rgb' ? parsed : toDisplayableRGB(parsed))\n if (!rgb) return null\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,aAAa,WAAW,KAAK;AACnC,MAAM,kBAAkB,QAAQ,IAAI;AAEpC,SAAS,eAAe,OAAmB;CACzC,MAAM,MAAM,MAAM,IAAI;CACtB,OAAO;EACL,MAAM;EACN,GAAG,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,MAAM,CAAC,CAAC;EACnC,GAAG,KAAK,IAAI,GAAG,MAAM,CAAC;EACtB,GAAG,MAAM,IAAI,MAAM,MAAM;EACzB,OAAO,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,MAAM,KAAK,CAAC,CAAC;CAC9C;AACF;;;;;AAMA,SAAS,kBAAkB,OAAc,MAAwB,IAA6B;CAC5F,IAAI,SAAS,IAAI,OAAO;CACxB,MAAM,SACJ,SAAS,eACL;EAAE,MAAM;EAAe,GAAG,MAAM;EAAG,GAAG,MAAM;EAAG,G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AO;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 {\n clampGamut,\n converter,\n formatCss,\n formatRgb,\n inGamut,\n parse,\n toGamut,\n type Color as CuloriColor\n} 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 clampToRGB = clampGamut('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 * `color`, any culori color, as components in `profile`, past 0 and 1 if need be.\n */\nfunction exactlyInProfile(color: CuloriColor, profile: DocumentColorSpace): Color {\n const converted = profile === 'display-p3' ? toP3(color) : toRGB(color)\n return { r: converted.r, g: converted.g, b: converted.b, a: converted.alpha ?? 1 }\n}\n\nfunction isInsideProfile(color: Color, profile: DocumentColorSpace): boolean {\n if (profile === 'display-p3')\n return isDisplayableP3({ mode: 'p3', r: color.r, g: color.g, b: color.b, alpha: color.a })\n return isDisplayableRGB({ mode: 'rgb', r: color.r, g: color.g, b: color.b, alpha: color.a })\n}\n\n/**\n * `color`, any culori color, as components in `profile`, brought inside it the way CSS maps\n * out-of-gamut colors — by reducing chroma in OKLCH so lightness and hue hold — and whether it had\n * to be. A color inside already keeps its components, as mapping it would shift them by float\n * noise. A missing (`none`) component reads as zero.\n */\nexport function fitIntoProfile(\n color: CuloriColor,\n profile: DocumentColorSpace\n): { color: Color; clipped: boolean } {\n const exact = normalizeColor(exactlyInProfile(color, profile))\n const clipped = !isInsideProfile(exact, profile)\n const inside = clipped\n ? normalizeColor(\n exactlyInProfile(\n profile === 'display-p3' ? toDisplayableP3(color) : toDisplayableRGB(color),\n profile\n )\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\n/** Syntaxes CSS reads as sRGB numbers: hex, `rgb()`, `hsl()`, `hwb()`, names, `color(srgb …)`. */\nexport const SRGB_SYNTAX_MODES = new Set(['rgb', 'hsl', 'hwb'])\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 // CSS clamps out-of-range sRGB channels and gamut-maps wider colors; clamping also removes the\n // float noise gamut mapping can leave just outside 0..1.\n const rgb = isDisplayableRGB(parsed)\n ? toRGB(parsed)\n : clampToRGB(parsed.mode === 'rgb' ? parsed : toDisplayableRGB(parsed))\n if (!rgb) return null\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":";;;;AAgCA,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,aAAa,WAAW,KAAK;AACnC,MAAM,kBAAkB,QAAQ,IAAI;AAEpC,SAAS,eAAe,OAAmB;CACzC,MAAM,MAAM,MAAM,IAAI;CACtB,OAAO;EACL,MAAM;EACN,GAAG,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,MAAM,CAAC,CAAC;EACnC,GAAG,KAAK,IAAI,GAAG,MAAM,CAAC;EACtB,GAAG,MAAM,IAAI,MAAM,MAAM;EACzB,OAAO,KAAK,IAAI,GAAG,KAAK,IAAI,GAAG,MAAM,KAAK,CAAC,CAAC;C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B,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"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Color } from "../primitives/index.js";
|
|
2
|
+
import { ColorProfile } from "../types.js";
|
|
3
|
+
//#region src/color/profile.d.ts
|
|
4
|
+
/** Whether `color`'s components fit 0 to 1, give or take round-off. */
|
|
5
|
+
declare function fitsGamut(color: Pick<Color, 'r' | 'g' | 'b'>): boolean;
|
|
6
|
+
/**
|
|
7
|
+
* What to do with a color the target profile cannot show: `extend` keeps its components past 0
|
|
8
|
+
* and 1, for a renderer such as Skia that takes extended sRGB; `map` brings it inside by reducing
|
|
9
|
+
* chroma in OKLCH, as CSS maps out-of-gamut colors, keeping its lightness and hue.
|
|
10
|
+
*/
|
|
11
|
+
type GamutHandling = 'extend' | 'map';
|
|
12
|
+
/** `color`, whose components are in `from`, as the components in `to` that show the same color. */
|
|
13
|
+
declare function convertColor(color: Color, from: ColorProfile, to: ColorProfile, gamut?: GamutHandling): Color;
|
|
14
|
+
/**
|
|
15
|
+
* `color`, whose components are in `from`, as CSS showing it in `to`: `color(display-p3 …)` for
|
|
16
|
+
* Display P3, `rgb()` for sRGB, where a color outside sRGB clips per channel as a canvas clips it.
|
|
17
|
+
*/
|
|
18
|
+
declare function colorToProfileCSS(color: Color, from: ColorProfile, to: ColorProfile): string;
|
|
19
|
+
/**
|
|
20
|
+
* The 3×3 matrix, row by row, taking linear components in `profile` to linear sRGB ones: its
|
|
21
|
+
* columns are the profile's primaries in linear sRGB.
|
|
22
|
+
*/
|
|
23
|
+
declare function linearSRGBMatrix(profile: ColorProfile): number[];
|
|
24
|
+
/**
|
|
25
|
+
* `color`, whose components are in `profile`, as the sRGB components of the same color: past 0 and
|
|
26
|
+
* 1 where it lies outside sRGB. Skia takes colors this way and converts them into the space of
|
|
27
|
+
* whatever it draws on, so a wide-gamut color reaches it whole.
|
|
28
|
+
*/
|
|
29
|
+
declare function toExtendedSRGB(color: Color, profile: ColorProfile): Color;
|
|
30
|
+
//#endregion
|
|
31
|
+
export { GamutHandling, colorToProfileCSS, convertColor, fitsGamut, linearSRGBMatrix, toExtendedSRGB };
|
|
32
|
+
//# sourceMappingURL=profile.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"profile.d.ts","names":[],"sources":["../../src/color/profile.ts"],"mappings":";;;;iBAiCgB,UAAU,OAAO,KAAK;;;;;;KAgB1B;;iBAGI,aACd,OAAO,OACP,MAAM,cACN,IAAI,cACJ,QAAO,gBACN;;;;;iBAea,kBAAkB,OAAO,OAAO,MAAM,cAAc,IAAI;;;;;iBAYxD,iBAAiB,SAAS;;;;;;iBAkB1B,eAAe,OAAO,OAAO,SAAS,eAAe"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { normalizeColor } from "./normalize.js";
|
|
2
|
+
import { converter, formatCss, formatRgb, toGamut } from "culori";
|
|
3
|
+
//#region src/color/profile.ts
|
|
4
|
+
const toRGB = converter("rgb");
|
|
5
|
+
const toP3 = converter("p3");
|
|
6
|
+
const toLinearSRGB = converter("lrgb");
|
|
7
|
+
const toDisplayableRGB = toGamut("rgb", "oklch");
|
|
8
|
+
const toDisplayableP3 = toGamut("p3", "oklch");
|
|
9
|
+
function culoriColor(color, profile) {
|
|
10
|
+
return {
|
|
11
|
+
mode: profile === "display-p3" ? "p3" : "rgb",
|
|
12
|
+
r: color.r,
|
|
13
|
+
g: color.g,
|
|
14
|
+
b: color.b,
|
|
15
|
+
alpha: color.a
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function sceneColor(color, alpha) {
|
|
19
|
+
return {
|
|
20
|
+
r: color.r,
|
|
21
|
+
g: color.g,
|
|
22
|
+
b: color.b,
|
|
23
|
+
a: color.alpha ?? alpha
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/** Whether `color`'s components fit 0 to 1, give or take round-off. */
|
|
27
|
+
function fitsGamut(color) {
|
|
28
|
+
return [
|
|
29
|
+
color.r,
|
|
30
|
+
color.g,
|
|
31
|
+
color.b
|
|
32
|
+
].every((channel) => channel >= -1e-6 && channel <= 1.000001);
|
|
33
|
+
}
|
|
34
|
+
function clampToGamut(color) {
|
|
35
|
+
const clamp = (channel) => Math.min(1, Math.max(0, channel));
|
|
36
|
+
return {
|
|
37
|
+
r: clamp(color.r),
|
|
38
|
+
g: clamp(color.g),
|
|
39
|
+
b: clamp(color.b),
|
|
40
|
+
a: color.a
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** `color`, whose components are in `from`, as the components in `to` that show the same color. */
|
|
44
|
+
function convertColor(color, from, to, gamut = "extend") {
|
|
45
|
+
if (from === to) return color;
|
|
46
|
+
const source = culoriColor(color, from);
|
|
47
|
+
const converted = sceneColor(to === "display-p3" ? toP3(source) : toRGB(source), color.a);
|
|
48
|
+
if (gamut === "extend") return converted;
|
|
49
|
+
if (fitsGamut(converted)) return clampToGamut(converted);
|
|
50
|
+
const mapped = to === "display-p3" ? toP3(toDisplayableP3(source)) : toRGB(toDisplayableRGB(source));
|
|
51
|
+
return normalizeColor(sceneColor(mapped, color.a));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* `color`, whose components are in `from`, as CSS showing it in `to`: `color(display-p3 …)` for
|
|
55
|
+
* Display P3, `rgb()` for sRGB, where a color outside sRGB clips per channel as a canvas clips it.
|
|
56
|
+
*/
|
|
57
|
+
function colorToProfileCSS(color, from, to) {
|
|
58
|
+
const shown = convertColor(color, from, to);
|
|
59
|
+
if (to === "display-p3") return formatCss({
|
|
60
|
+
mode: "p3",
|
|
61
|
+
r: shown.r,
|
|
62
|
+
g: shown.g,
|
|
63
|
+
b: shown.b,
|
|
64
|
+
alpha: shown.a
|
|
65
|
+
});
|
|
66
|
+
return formatRgb({
|
|
67
|
+
mode: "rgb",
|
|
68
|
+
r: shown.r,
|
|
69
|
+
g: shown.g,
|
|
70
|
+
b: shown.b,
|
|
71
|
+
alpha: shown.a
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The 3×3 matrix, row by row, taking linear components in `profile` to linear sRGB ones: its
|
|
76
|
+
* columns are the profile's primaries in linear sRGB.
|
|
77
|
+
*/
|
|
78
|
+
function linearSRGBMatrix(profile) {
|
|
79
|
+
const mode = profile === "display-p3" ? "p3" : "rgb";
|
|
80
|
+
const columns = [
|
|
81
|
+
{
|
|
82
|
+
r: 1,
|
|
83
|
+
g: 0,
|
|
84
|
+
b: 0
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
r: 0,
|
|
88
|
+
g: 1,
|
|
89
|
+
b: 0
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
r: 0,
|
|
93
|
+
g: 0,
|
|
94
|
+
b: 1
|
|
95
|
+
}
|
|
96
|
+
].map((primary) => toLinearSRGB({
|
|
97
|
+
mode,
|
|
98
|
+
...primary
|
|
99
|
+
}));
|
|
100
|
+
return [
|
|
101
|
+
"r",
|
|
102
|
+
"g",
|
|
103
|
+
"b"
|
|
104
|
+
].flatMap((row) => columns.map((column) => column[row]));
|
|
105
|
+
}
|
|
106
|
+
/** Display P3's matrix, made once: renderers convert every paint they draw through it. */
|
|
107
|
+
const P3_TO_LINEAR_SRGB = linearSRGBMatrix("display-p3");
|
|
108
|
+
/**
|
|
109
|
+
* `color`, whose components are in `profile`, as the sRGB components of the same color: past 0 and
|
|
110
|
+
* 1 where it lies outside sRGB. Skia takes colors this way and converts them into the space of
|
|
111
|
+
* whatever it draws on, so a wide-gamut color reaches it whole.
|
|
112
|
+
*/
|
|
113
|
+
function toExtendedSRGB(color, profile) {
|
|
114
|
+
if (profile === "srgb") return color;
|
|
115
|
+
const m = P3_TO_LINEAR_SRGB;
|
|
116
|
+
const r = decodeSRGB(color.r);
|
|
117
|
+
const g = decodeSRGB(color.g);
|
|
118
|
+
const b = decodeSRGB(color.b);
|
|
119
|
+
return {
|
|
120
|
+
r: encodeSRGB(m[0] * r + m[1] * g + m[2] * b),
|
|
121
|
+
g: encodeSRGB(m[3] * r + m[4] * g + m[5] * b),
|
|
122
|
+
b: encodeSRGB(m[6] * r + m[7] * g + m[8] * b),
|
|
123
|
+
a: color.a
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/** The sRGB transfer curve, mirrored below zero as extended sRGB and culori's `lrgb` take it. */
|
|
127
|
+
function decodeSRGB(channel) {
|
|
128
|
+
const magnitude = Math.abs(channel);
|
|
129
|
+
if (magnitude <= .04045) return channel / 12.92;
|
|
130
|
+
return (Math.sign(channel) || 1) * ((magnitude + .055) / 1.055) ** 2.4;
|
|
131
|
+
}
|
|
132
|
+
function encodeSRGB(channel) {
|
|
133
|
+
const magnitude = Math.abs(channel);
|
|
134
|
+
if (magnitude <= .0031308) return channel * 12.92;
|
|
135
|
+
return (Math.sign(channel) || 1) * (1.055 * magnitude ** (1 / 2.4) - .055);
|
|
136
|
+
}
|
|
137
|
+
//#endregion
|
|
138
|
+
export { colorToProfileCSS, convertColor, fitsGamut, linearSRGBMatrix, toExtendedSRGB };
|
|
139
|
+
|
|
140
|
+
//# sourceMappingURL=profile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"profile.js","names":[],"sources":["../../src/color/profile.ts"],"sourcesContent":["import { converter, formatCss, formatRgb, toGamut } from 'culori'\n\nimport type { Color } from '../primitives'\nimport type { ColorProfile } from '../types'\nimport { normalizeColor } from './normalize'\n\nconst toRGB = converter('rgb')\nconst toP3 = converter('p3')\nconst toLinearSRGB = converter('lrgb')\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\ntype CuloriColor = { mode: 'rgb' | 'p3'; r: number; g: number; b: number; alpha?: number }\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\nfunction sceneColor(\n color: { r: number; g: number; b: number; alpha?: number },\n alpha: number\n): Color {\n return { r: color.r, g: color.g, b: color.b, a: color.alpha ?? alpha }\n}\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\nfunction clampToGamut(color: Color): Color {\n const clamp = (channel: number) => Math.min(1, Math.max(0, channel))\n return { r: clamp(color.r), g: clamp(color.g), b: clamp(color.b), a: 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 const converted = sceneColor(to === 'display-p3' ? toP3(source) : toRGB(source), color.a)\n if (gamut === 'extend') return converted\n if (fitsGamut(converted)) return clampToGamut(converted)\n const mapped =\n to === 'display-p3' ? toP3(toDisplayableP3(source)) : toRGB(toDisplayableRGB(source))\n return normalizeColor(sceneColor(mapped, color.a))\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 * `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"],"mappings":";;;AAMA,MAAM,QAAQ,UAAU,KAAK;AAC7B,MAAM,OAAO,UAAU,IAAI;AAC3B,MAAM,eAAe,UAAU,MAAM;AACrC,MAAM,mBAAmB,QAAQ,OAAO,OAAO;AAC/C,MAAM,kBAAkB,QAAQ,MAAM,OAAO;AAU7C,SAAS,YAAY,OAAc,SAAoC;CAErE,OAAO;EAAE,MADI,YAAY,eAAe,OAAO;EAChC,GAAG,MAAM;EAAG,GAAG,MAAM;EAAG,GAAG,MAAM;EAAG,OAAO,MAAM;CAAE;AACpE;AAEA,SAAS,WACP,OACA,OACO;CACP,OAAO;EAAE,GAAG,MAAM;EAAG,GAAG,MAAM;EAAG,GAAG,MAAM;EAAG,GAAG,MAAM,SAAS;CAAM;AACvE;;AAGA,SAAgB,UAAU,OAA8C;CACtE,OAAO;EAAC,MAAM;EAAG,MAAM;EAAG,MAAM;CAAC,CAAC,CAAC,OAChC,YAAY,WAAW,SAAoB,WAAW,QACzD;AACF;AAEA,SAAS,aA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KAAK,YAAY,aAAa;EAAE;EAAM,GAAG;CAAQ,CAAC,CAAC;CACrD,OAAQ;EAAC;EAAK;EAAK;CAAG,CAAC,CAAW,SAAS,QAAQ,QAAQ,KAAK,WAAW,OAAO,IAAI,CAAC;AACzF;;AAGA,MAAM,oBAAoB,iBAAiB,YAAY;;;;;;AAOvD,SAAgB,eAAe,OAAc,SAA8B;CACzE,IAAI,YAAY,QAAQ,OAAO;CAI/B,MAAM,IAAI;CACV,MAAM,IAAI,WAAW,MAAM,CAAC;CAC5B,MAAM,IAAI,WAAW,MAAM,CAAC;CAC5B,MAAM,IAAI,WAAW,MAAM,CAAC;CAC5B,OAAO;EACL,GAAG,WAAW,EAAE,KAAK,IAAI,EAAE,KAAK,IAAI,EAAE,KAAK,CAAC;EAC5C,GAAG,WAAW,EAAE,KAAK,IAAI,EAAE,KAAK,IAAI,EAAE,KAAK,CAAC;EAC5C,GAAG,WAAW,EAAE,KAAK,IAAI,EAAE,KAAK,IAAI,EAAE,KAAK,CAAC;EAC5C,GAAG,MAAM;CACX;AACF;;AAGA,SAAS,WAAW,SAAyB;CAC3C,MAAM,YAAY,KAAK,IAAI,OAAO;CAClC,IAAI,aAAa,QAAS,OAAO,UAAU;CAC3C,QAAQ,KAAK,KAAK,OAAO,KAAK,OAAO,YAAY,QAAS,UAAU;AACtE;AAEA,SAAS,WAAW,SAAyB;CAC3C,MAAM,YAAY,KAAK,IAAI,OAAO;CAClC,IAAI,aAAa,UAAW,OAAO,UAAU;CAC7C,QAAQ,KAAK,KAAK,OAAO,KAAK,MAAM,QAAQ,cAAc,IAAI,OAAO;AACvE"}
|
package/dist/comments/index.d.ts
CHANGED
|
@@ -2,5 +2,5 @@ import { readComments, withLiveComments, writeComments } from "./document.js";
|
|
|
2
2
|
import { COMMENT_SORTS, CommentListOptions, CommentSort, commentThreadNumbers, listCommentThreads } from "./list.js";
|
|
3
3
|
import { hasNewerComments, mergeCommentThreads } from "./merge.js";
|
|
4
4
|
import { CommentAuthor, byCreatedAt, commentAnchor, commentPosition, commentTimestamp, createCommentReply, createCommentThread, deleteCommentReply, deleteCommentThread, editCommentThread, findLiveCommentThread, liveReplies, replyToCommentThread, resolveCommentThread, takesPartInThread } from "./threads.js";
|
|
5
|
-
import { commentThreadsSchema } from "./schema.js";
|
|
6
|
-
export { COMMENT_SORTS, CommentAuthor, CommentListOptions, CommentSort, byCreatedAt, commentAnchor, commentPosition, commentThreadNumbers, commentThreadsSchema, commentTimestamp, createCommentReply, createCommentThread, deleteCommentReply, deleteCommentThread, editCommentThread, findLiveCommentThread, hasNewerComments, listCommentThreads, liveReplies, mergeCommentThreads, readComments, replyToCommentThread, resolveCommentThread, takesPartInThread, withLiveComments, writeComments };
|
|
5
|
+
import { MAX_COMMENT_REGION_POINTS, commentThreadsSchema } from "./schema.js";
|
|
6
|
+
export { COMMENT_SORTS, CommentAuthor, CommentListOptions, CommentSort, MAX_COMMENT_REGION_POINTS, byCreatedAt, commentAnchor, commentPosition, commentThreadNumbers, commentThreadsSchema, commentTimestamp, createCommentReply, createCommentThread, deleteCommentReply, deleteCommentThread, editCommentThread, findLiveCommentThread, hasNewerComments, listCommentThreads, liveReplies, mergeCommentThreads, readComments, replyToCommentThread, resolveCommentThread, takesPartInThread, withLiveComments, writeComments };
|
package/dist/comments/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { commentThreadsSchema } from "./schema.js";
|
|
1
|
+
import { MAX_COMMENT_REGION_POINTS, commentThreadsSchema } from "./schema.js";
|
|
2
2
|
import { readComments, withLiveComments, writeComments } from "./document.js";
|
|
3
3
|
import { byCreatedAt, commentAnchor, commentPosition, commentTimestamp, createCommentReply, createCommentThread, deleteCommentReply, deleteCommentThread, editCommentThread, findLiveCommentThread, liveReplies, replyToCommentThread, resolveCommentThread, takesPartInThread } from "./threads.js";
|
|
4
4
|
import { COMMENT_SORTS, commentThreadNumbers, listCommentThreads } from "./list.js";
|
|
5
5
|
import { hasNewerComments, mergeCommentThreads } from "./merge.js";
|
|
6
|
-
export { COMMENT_SORTS, byCreatedAt, commentAnchor, commentPosition, commentThreadNumbers, commentThreadsSchema, commentTimestamp, createCommentReply, createCommentThread, deleteCommentReply, deleteCommentThread, editCommentThread, findLiveCommentThread, hasNewerComments, listCommentThreads, liveReplies, mergeCommentThreads, readComments, replyToCommentThread, resolveCommentThread, takesPartInThread, withLiveComments, writeComments };
|
|
6
|
+
export { COMMENT_SORTS, MAX_COMMENT_REGION_POINTS, byCreatedAt, commentAnchor, commentPosition, commentThreadNumbers, commentThreadsSchema, commentTimestamp, createCommentReply, createCommentThread, deleteCommentReply, deleteCommentThread, editCommentThread, findLiveCommentThread, hasNewerComments, listCommentThreads, liveReplies, mergeCommentThreads, readComments, replyToCommentThread, resolveCommentThread, takesPartInThread, withLiveComments, writeComments };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merge.d.ts","names":[],"sources":["../../src/comments/merge.ts"],"mappings":";;;;;;;iBAqBgB,oBACd,gBAAgB,iBAChB,iBAAiB,kBAChB;;;;;
|
|
1
|
+
{"version":3,"file":"merge.d.ts","names":[],"sources":["../../src/comments/merge.ts"],"mappings":";;;;;;;iBAqBgB,oBACd,gBAAgB,iBAChB,iBAAiB,kBAChB;;;;;iBA0Ba,iBACd,gBAAgB,iBAChB,iBAAiB"}
|
package/dist/comments/merge.js
CHANGED
|
@@ -29,6 +29,7 @@ function mergeCommentThreads(local, remote) {
|
|
|
29
29
|
const newer = thread.updatedAt >= other.updatedAt ? thread : other;
|
|
30
30
|
byId.set(thread.id, {
|
|
31
31
|
...newer,
|
|
32
|
+
region: newer.region ?? thread.region ?? other.region,
|
|
32
33
|
deleted: thread.deleted || other.deleted,
|
|
33
34
|
replies: mergeReplies(other.replies, thread.replies)
|
|
34
35
|
});
|