html2canvas-pro 2.1.0 → 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/html2canvas-pro.esm.js +21 -7
- package/dist/html2canvas-pro.esm.js.map +1 -1
- package/dist/html2canvas-pro.js +21 -7
- package/dist/html2canvas-pro.js.map +1 -1
- package/dist/html2canvas-pro.min.js +3 -3
- package/dist/lib/core/cache-storage.js +2 -2
- package/dist/lib/core/features.js +2 -2
- package/dist/lib/render/canvas/background-renderer.js +6 -0
- package/dist/lib/render/canvas/canvas-renderer.js +5 -1
- package/dist/lib/render/canvas/foreignobject-renderer.js +5 -1
- package/package.json +3 -11
- package/dist/lib/invariant.js +0 -9
- package/dist/types/invariant.d.ts +0 -1
- package/src/__tests__/index.ts +0 -99
- package/src/config.ts +0 -107
- package/src/core/__mocks__/cache-storage.ts +0 -1
- package/src/core/__mocks__/context.ts +0 -19
- package/src/core/__mocks__/features.ts +0 -8
- package/src/core/__mocks__/logger.ts +0 -17
- package/src/core/__tests__/cache-storage.test.ts +0 -205
- package/src/core/__tests__/cache-storage.ts +0 -278
- package/src/core/__tests__/logger.ts +0 -29
- package/src/core/__tests__/validator.ts +0 -359
- package/src/core/bitwise.ts +0 -1
- package/src/core/cache-storage.ts +0 -315
- package/src/core/context.ts +0 -31
- package/src/core/debugger.ts +0 -32
- package/src/core/features.ts +0 -222
- package/src/core/logger.ts +0 -64
- package/src/core/origin-checker.ts +0 -57
- package/src/core/performance-monitor.ts +0 -241
- package/src/core/render-element.ts +0 -272
- package/src/core/util.ts +0 -1
- package/src/core/validator.ts +0 -593
- package/src/css/index.ts +0 -427
- package/src/css/layout/__mocks__/bounds.ts +0 -6
- package/src/css/layout/bounds.ts +0 -79
- package/src/css/layout/text.ts +0 -161
- package/src/css/property-descriptor.ts +0 -49
- package/src/css/property-descriptors/__tests__/background-tests.ts +0 -65
- package/src/css/property-descriptors/__tests__/clip-path.test.ts +0 -280
- package/src/css/property-descriptors/__tests__/font-family.ts +0 -25
- package/src/css/property-descriptors/__tests__/image-rendering-integration.test.ts +0 -153
- package/src/css/property-descriptors/__tests__/image-rendering-performance.test.ts +0 -175
- package/src/css/property-descriptors/__tests__/image-rendering.test.ts +0 -72
- package/src/css/property-descriptors/__tests__/paint-order.ts +0 -87
- package/src/css/property-descriptors/__tests__/text-shadow.ts +0 -94
- package/src/css/property-descriptors/__tests__/transform-tests.ts +0 -18
- package/src/css/property-descriptors/background-clip.ts +0 -30
- package/src/css/property-descriptors/background-color.ts +0 -9
- package/src/css/property-descriptors/background-image.ts +0 -27
- package/src/css/property-descriptors/background-origin.ts +0 -31
- package/src/css/property-descriptors/background-position.ts +0 -38
- package/src/css/property-descriptors/background-repeat.ts +0 -44
- package/src/css/property-descriptors/background-size.ts +0 -27
- package/src/css/property-descriptors/border-color.ts +0 -13
- package/src/css/property-descriptors/border-radius.ts +0 -19
- package/src/css/property-descriptors/border-style.ts +0 -34
- package/src/css/property-descriptors/border-width.ts +0 -20
- package/src/css/property-descriptors/box-shadow.ts +0 -60
- package/src/css/property-descriptors/clip-path.ts +0 -271
- package/src/css/property-descriptors/color.ts +0 -9
- package/src/css/property-descriptors/content.ts +0 -26
- package/src/css/property-descriptors/counter-increment.ts +0 -43
- package/src/css/property-descriptors/counter-reset.ts +0 -36
- package/src/css/property-descriptors/direction.ts +0 -23
- package/src/css/property-descriptors/display.ts +0 -117
- package/src/css/property-descriptors/duration.ts +0 -14
- package/src/css/property-descriptors/float.ts +0 -29
- package/src/css/property-descriptors/font-family.ts +0 -38
- package/src/css/property-descriptors/font-size.ts +0 -9
- package/src/css/property-descriptors/font-style.ts +0 -25
- package/src/css/property-descriptors/font-variant.ts +0 -12
- package/src/css/property-descriptors/font-weight.ts +0 -26
- package/src/css/property-descriptors/image-rendering.ts +0 -33
- package/src/css/property-descriptors/letter-spacing.ts +0 -25
- package/src/css/property-descriptors/line-break.ts +0 -22
- package/src/css/property-descriptors/line-height.ts +0 -22
- package/src/css/property-descriptors/list-style-image.ts +0 -19
- package/src/css/property-descriptors/list-style-position.ts +0 -22
- package/src/css/property-descriptors/list-style-type.ts +0 -179
- package/src/css/property-descriptors/margin.ts +0 -13
- package/src/css/property-descriptors/mix-blend-mode.ts +0 -35
- package/src/css/property-descriptors/object-fit.ts +0 -39
- package/src/css/property-descriptors/opacity.ts +0 -15
- package/src/css/property-descriptors/overflow-wrap.ts +0 -22
- package/src/css/property-descriptors/overflow.ts +0 -34
- package/src/css/property-descriptors/padding.ts +0 -14
- package/src/css/property-descriptors/paint-order.ts +0 -42
- package/src/css/property-descriptors/position.ts +0 -30
- package/src/css/property-descriptors/quotes.ts +0 -57
- package/src/css/property-descriptors/rotate.ts +0 -34
- package/src/css/property-descriptors/text-align.ts +0 -26
- package/src/css/property-descriptors/text-decoration-color.ts +0 -9
- package/src/css/property-descriptors/text-decoration-line.ts +0 -38
- package/src/css/property-descriptors/text-decoration-style.ts +0 -32
- package/src/css/property-descriptors/text-decoration-thickness.ts +0 -30
- package/src/css/property-descriptors/text-overflow.ts +0 -23
- package/src/css/property-descriptors/text-shadow.ts +0 -52
- package/src/css/property-descriptors/text-transform.ts +0 -27
- package/src/css/property-descriptors/text-underline-offset.ts +0 -27
- package/src/css/property-descriptors/transform-origin.ts +0 -29
- package/src/css/property-descriptors/transform.ts +0 -74
- package/src/css/property-descriptors/visibility.ts +0 -25
- package/src/css/property-descriptors/webkit-line-clamp.ts +0 -30
- package/src/css/property-descriptors/webkit-text-stroke-color.ts +0 -8
- package/src/css/property-descriptors/webkit-text-stroke-width.ts +0 -15
- package/src/css/property-descriptors/word-break.ts +0 -25
- package/src/css/property-descriptors/writing-mode.ts +0 -37
- package/src/css/property-descriptors/z-index.ts +0 -27
- package/src/css/syntax/__tests__/tokernizer-tests.ts +0 -29
- package/src/css/syntax/parser.ts +0 -188
- package/src/css/syntax/tokenizer.ts +0 -822
- package/src/css/type-descriptor.ts +0 -7
- package/src/css/types/__tests__/color-tests.ts +0 -147
- package/src/css/types/__tests__/image-tests.ts +0 -239
- package/src/css/types/angle.ts +0 -86
- package/src/css/types/color-math.ts +0 -22
- package/src/css/types/color-spaces/a98.ts +0 -86
- package/src/css/types/color-spaces/p3.ts +0 -92
- package/src/css/types/color-spaces/pro-photo.ts +0 -87
- package/src/css/types/color-spaces/rec2020.ts +0 -90
- package/src/css/types/color-spaces/srgb.ts +0 -87
- package/src/css/types/color-utilities.ts +0 -452
- package/src/css/types/color.ts +0 -485
- package/src/css/types/functions/-prefix-linear-gradient.ts +0 -35
- package/src/css/types/functions/-prefix-radial-gradient.ts +0 -106
- package/src/css/types/functions/-webkit-gradient.ts +0 -69
- package/src/css/types/functions/__tests__/radial-gradient.ts +0 -69
- package/src/css/types/functions/counter.ts +0 -511
- package/src/css/types/functions/gradient.ts +0 -206
- package/src/css/types/functions/linear-gradient.ts +0 -28
- package/src/css/types/functions/radial-gradient.ts +0 -101
- package/src/css/types/image.ts +0 -120
- package/src/css/types/index.ts +0 -1
- package/src/css/types/length-percentage.ts +0 -137
- package/src/css/types/length.ts +0 -7
- package/src/css/types/time.ts +0 -20
- package/src/dom/__mocks__/document-cloner.ts +0 -22
- package/src/dom/__tests__/dom-normalizer.test.ts +0 -133
- package/src/dom/__tests__/element-container.test.ts +0 -129
- package/src/dom/document-cloner.ts +0 -929
- package/src/dom/dom-normalizer.ts +0 -133
- package/src/dom/element-container.ts +0 -75
- package/src/dom/elements/li-element-container.ts +0 -10
- package/src/dom/elements/ol-element-container.ts +0 -12
- package/src/dom/elements/select-element-container.ts +0 -10
- package/src/dom/elements/textarea-element-container.ts +0 -9
- package/src/dom/node-parser.ts +0 -177
- package/src/dom/node-type-guards.ts +0 -70
- package/src/dom/replaced-elements/canvas-element-container.ts +0 -15
- package/src/dom/replaced-elements/iframe-element-container.ts +0 -55
- package/src/dom/replaced-elements/image-element-container.ts +0 -16
- package/src/dom/replaced-elements/index.ts +0 -5
- package/src/dom/replaced-elements/input-element-container.ts +0 -105
- package/src/dom/replaced-elements/pseudo-elements.ts +0 -0
- package/src/dom/replaced-elements/svg-element-container.ts +0 -23
- package/src/dom/text-container.ts +0 -42
- package/src/global.d.ts +0 -19
- package/src/index.ts +0 -82
- package/src/invariant.ts +0 -5
- package/src/options.ts +0 -55
- package/src/render/__tests__/object-fit.test.ts +0 -85
- package/src/render/background.ts +0 -298
- package/src/render/bezier-curve.ts +0 -47
- package/src/render/border.ts +0 -165
- package/src/render/bound-curves.ts +0 -388
- package/src/render/box-sizing.ts +0 -31
- package/src/render/canvas/__tests__/background-renderer.test.ts +0 -72
- package/src/render/canvas/__tests__/border-renderer.test.ts +0 -24
- package/src/render/canvas/__tests__/effects-renderer.test.ts +0 -32
- package/src/render/canvas/__tests__/text-renderer.test.ts +0 -471
- package/src/render/canvas/background-renderer.ts +0 -271
- package/src/render/canvas/border-renderer.ts +0 -224
- package/src/render/canvas/canvas-path.ts +0 -31
- package/src/render/canvas/canvas-renderer.ts +0 -641
- package/src/render/canvas/effects-renderer.ts +0 -130
- package/src/render/canvas/foreignobject-renderer.ts +0 -53
- package/src/render/canvas/text-renderer.ts +0 -700
- package/src/render/effects.ts +0 -75
- package/src/render/font-metrics.ts +0 -72
- package/src/render/object-fit.ts +0 -100
- package/src/render/path.ts +0 -37
- package/src/render/renderer-interface.ts +0 -28
- package/src/render/stacking-context.ts +0 -386
- package/src/render/vector.ts +0 -19
|
@@ -1,452 +0,0 @@
|
|
|
1
|
-
import { Context } from '../../core/context';
|
|
2
|
-
import { CSSValue, isDimensionToken, isNumberToken, nonFunctionArgSeparator } from '../syntax/parser';
|
|
3
|
-
import { rgb2rgbLinear, rgbLinear2xyz, srgbLinear2rgb, xyz2rgbLinear } from './color-spaces/srgb';
|
|
4
|
-
import { TokenType } from '../syntax/tokenizer';
|
|
5
|
-
import { angle, deg } from './angle';
|
|
6
|
-
import { getAbsoluteValue, isLengthPercentage } from './length-percentage';
|
|
7
|
-
import { clamp, multiplyMatrices } from './color-math';
|
|
8
|
-
|
|
9
|
-
type Color = number;
|
|
10
|
-
|
|
11
|
-
export const isTransparent = (color: Color): boolean => (0xff & color) === 0;
|
|
12
|
-
|
|
13
|
-
export const asString = (color: Color): string => {
|
|
14
|
-
const alpha = 0xff & color;
|
|
15
|
-
const blue = 0xff & (color >> 8);
|
|
16
|
-
const green = 0xff & (color >> 16);
|
|
17
|
-
const red = 0xff & (color >> 24);
|
|
18
|
-
return alpha < 255 ? `rgba(${red},${green},${blue},${alpha / 255})` : `rgb(${red},${green},${blue})`;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export const pack = (r: number, g: number, b: number, a: number): Color =>
|
|
22
|
-
((r << 24) | (g << 16) | (b << 8) | (Math.round(a * 255) << 0)) >>> 0;
|
|
23
|
-
|
|
24
|
-
export const getTokenColorValue = (token: CSSValue, i: number): number => {
|
|
25
|
-
if (token.type === TokenType.NUMBER_TOKEN) {
|
|
26
|
-
return token.number;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (token.type === TokenType.PERCENTAGE_TOKEN) {
|
|
30
|
-
const max = i === 3 ? 1 : 255;
|
|
31
|
-
return i === 3 ? (token.number / 100) * max : Math.round((token.number / 100) * max);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return 0;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export const isRelativeTransform = (tokens: CSSValue[]): boolean =>
|
|
38
|
-
(tokens[0].type === TokenType.IDENT_TOKEN ? tokens[0].value : 'unknown') === 'from';
|
|
39
|
-
|
|
40
|
-
// Re-export from color-math to maintain backward compatibility
|
|
41
|
-
export { clamp, multiplyMatrices };
|
|
42
|
-
|
|
43
|
-
export const packSrgb = (args: number[]): number => {
|
|
44
|
-
return pack(
|
|
45
|
-
clamp(Math.round(args[0] * 255), 0, 255),
|
|
46
|
-
clamp(Math.round(args[1] * 255), 0, 255),
|
|
47
|
-
clamp(Math.round(args[2] * 255), 0, 255),
|
|
48
|
-
clamp(args[3], 0, 1)
|
|
49
|
-
);
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
export const packSrgbLinear = ([r, g, b, a]: [number, number, number, number]): number => {
|
|
53
|
-
const rgb = srgbLinear2rgb([r, g, b]);
|
|
54
|
-
return pack(
|
|
55
|
-
clamp(Math.round(rgb[0] * 255), 0, 255),
|
|
56
|
-
clamp(Math.round(rgb[1] * 255), 0, 255),
|
|
57
|
-
clamp(Math.round(rgb[2] * 255), 0, 255),
|
|
58
|
-
a
|
|
59
|
-
);
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
export const packXYZ = (args: number[]): number => {
|
|
63
|
-
const srgb_linear = xyz2rgbLinear([args[0], args[1], args[2]]);
|
|
64
|
-
return packSrgbLinear([srgb_linear[0], srgb_linear[1], srgb_linear[2], args[3]]);
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export const packLab = (_context: Context, args: CSSValue[]): number => {
|
|
68
|
-
if (isRelativeTransform(args.filter(nonFunctionArgSeparator))) {
|
|
69
|
-
throw new Error('Relative color not supported for lab()');
|
|
70
|
-
}
|
|
71
|
-
const [l, a, b, alpha] = extractLabComponents(args),
|
|
72
|
-
rgb = srgbLinear2rgb(xyz2rgbLinear(lab2xyz([l, a, b])));
|
|
73
|
-
|
|
74
|
-
return pack(
|
|
75
|
-
clamp(Math.round(rgb[0] * 255), 0, 255),
|
|
76
|
-
clamp(Math.round(rgb[1] * 255), 0, 255),
|
|
77
|
-
clamp(Math.round(rgb[2] * 255), 0, 255),
|
|
78
|
-
alpha
|
|
79
|
-
);
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
export const packOkLab = (_context: Context, args: CSSValue[]): number => {
|
|
83
|
-
if (isRelativeTransform(args.filter(nonFunctionArgSeparator))) {
|
|
84
|
-
throw new Error('Relative color not supported for oklab()');
|
|
85
|
-
}
|
|
86
|
-
const [l, a, b, alpha] = extractLabComponents(args),
|
|
87
|
-
rgb = srgbLinear2rgb(xyz2rgbLinear(oklab2xyz([l, a, b])));
|
|
88
|
-
|
|
89
|
-
return pack(
|
|
90
|
-
clamp(Math.round(rgb[0] * 255), 0, 255),
|
|
91
|
-
clamp(Math.round(rgb[1] * 255), 0, 255),
|
|
92
|
-
clamp(Math.round(rgb[2] * 255), 0, 255),
|
|
93
|
-
alpha
|
|
94
|
-
);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
export const packOkLch = (_context: Context, args: CSSValue[]): number => {
|
|
98
|
-
if (isRelativeTransform(args.filter(nonFunctionArgSeparator))) {
|
|
99
|
-
throw new Error('Relative color not supported for oklch()');
|
|
100
|
-
}
|
|
101
|
-
const [l, c, h, alpha] = extractOkLchComponents(args),
|
|
102
|
-
rgb = srgbLinear2rgb(xyz2rgbLinear(oklab2xyz(lch2lab([l, c, h]))));
|
|
103
|
-
|
|
104
|
-
return pack(
|
|
105
|
-
clamp(Math.round(rgb[0] * 255), 0, 255),
|
|
106
|
-
clamp(Math.round(rgb[1] * 255), 0, 255),
|
|
107
|
-
clamp(Math.round(rgb[2] * 255), 0, 255),
|
|
108
|
-
alpha
|
|
109
|
-
);
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
export const packLch = (_context: Context, args: CSSValue[]): number => {
|
|
113
|
-
if (isRelativeTransform(args.filter(nonFunctionArgSeparator))) {
|
|
114
|
-
throw new Error('Relative color not supported for lch()');
|
|
115
|
-
}
|
|
116
|
-
const [l, c, h, a] = extractLchComponents(args),
|
|
117
|
-
rgb = srgbLinear2rgb(xyz2rgbLinear(lab2xyz(lch2lab([l, c, h]))));
|
|
118
|
-
|
|
119
|
-
return pack(
|
|
120
|
-
clamp(Math.round(rgb[0] * 255), 0, 255),
|
|
121
|
-
clamp(Math.round(rgb[1] * 255), 0, 255),
|
|
122
|
-
clamp(Math.round(rgb[2] * 255), 0, 255),
|
|
123
|
-
a
|
|
124
|
-
);
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
export const extractHslComponents = (context: Context, args: CSSValue[]): [number, number, number, number] => {
|
|
128
|
-
const tokens = args.filter(nonFunctionArgSeparator),
|
|
129
|
-
[hue, saturation, lightness, alpha] = tokens,
|
|
130
|
-
h = (hue.type === TokenType.NUMBER_TOKEN ? deg(hue.number) : angle.parse(context, hue)) / (Math.PI * 2),
|
|
131
|
-
s = isLengthPercentage(saturation) ? saturation.number / 100 : 0,
|
|
132
|
-
l = isLengthPercentage(lightness) ? lightness.number / 100 : 0,
|
|
133
|
-
a = typeof alpha !== 'undefined' && isLengthPercentage(alpha) ? getAbsoluteValue(alpha, 1) : 1;
|
|
134
|
-
return [h, s, l, a];
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
export const packHSL = (context: Context, args: CSSValue[]): number => {
|
|
138
|
-
if (isRelativeTransform(args)) {
|
|
139
|
-
throw new Error('Relative color not supported for hsl()');
|
|
140
|
-
}
|
|
141
|
-
const [h, s, l, a] = extractHslComponents(context, args),
|
|
142
|
-
rgb = hsl2rgb([h, s, l]);
|
|
143
|
-
return pack(rgb[0] * 255, rgb[1] * 255, rgb[2] * 255, s === 0 ? 1 : a);
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
export const extractLchComponents = (args: CSSValue[]): [number, number, number, number] => {
|
|
147
|
-
const tokens = args.filter(nonFunctionArgSeparator),
|
|
148
|
-
l = isLengthPercentage(tokens[0]) ? tokens[0].number : 0,
|
|
149
|
-
c = isLengthPercentage(tokens[1]) ? tokens[1].number : 0,
|
|
150
|
-
h = isNumberToken(tokens[2]) || isDimensionToken(tokens[2]) ? tokens[2].number : 0,
|
|
151
|
-
a = typeof tokens[4] !== 'undefined' && isLengthPercentage(tokens[4]) ? getAbsoluteValue(tokens[4], 1) : 1;
|
|
152
|
-
|
|
153
|
-
return [l, c, h, a];
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
export const extractLabComponents = (args: CSSValue[]): [number, number, number, number] => {
|
|
157
|
-
const tokens = args.filter(nonFunctionArgSeparator),
|
|
158
|
-
l =
|
|
159
|
-
tokens[0].type === TokenType.PERCENTAGE_TOKEN
|
|
160
|
-
? tokens[0].number / 100
|
|
161
|
-
: isNumberToken(tokens[0])
|
|
162
|
-
? tokens[0].number
|
|
163
|
-
: 0,
|
|
164
|
-
a =
|
|
165
|
-
tokens[1].type === TokenType.PERCENTAGE_TOKEN
|
|
166
|
-
? tokens[1].number / 100
|
|
167
|
-
: isNumberToken(tokens[1])
|
|
168
|
-
? tokens[1].number
|
|
169
|
-
: 0,
|
|
170
|
-
b = isNumberToken(tokens[2]) || isDimensionToken(tokens[2]) ? tokens[2].number : 0,
|
|
171
|
-
alpha = typeof tokens[4] !== 'undefined' && isLengthPercentage(tokens[4]) ? getAbsoluteValue(tokens[4], 1) : 1;
|
|
172
|
-
|
|
173
|
-
return [l, a, b, alpha];
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
export const extractOkLchComponents = (args: CSSValue[]): [number, number, number, number] => {
|
|
177
|
-
const tokens = args.filter(nonFunctionArgSeparator),
|
|
178
|
-
l =
|
|
179
|
-
tokens[0].type === TokenType.PERCENTAGE_TOKEN
|
|
180
|
-
? tokens[0].number / 100
|
|
181
|
-
: isNumberToken(tokens[0])
|
|
182
|
-
? tokens[0].number
|
|
183
|
-
: 0,
|
|
184
|
-
c =
|
|
185
|
-
tokens[1].type === TokenType.PERCENTAGE_TOKEN
|
|
186
|
-
? tokens[1].number / 100
|
|
187
|
-
: isNumberToken(tokens[1])
|
|
188
|
-
? tokens[1].number
|
|
189
|
-
: 0,
|
|
190
|
-
h = isNumberToken(tokens[2]) || isDimensionToken(tokens[2]) ? tokens[2].number : 0,
|
|
191
|
-
a = typeof tokens[4] !== 'undefined' && isLengthPercentage(tokens[4]) ? getAbsoluteValue(tokens[4], 1) : 1;
|
|
192
|
-
|
|
193
|
-
return [l, c, h, a];
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Convert D65 to D50
|
|
198
|
-
*
|
|
199
|
-
* @param xyz
|
|
200
|
-
*/
|
|
201
|
-
export const d65toD50 = (xyz: [number, number, number]): [number, number, number] => {
|
|
202
|
-
return multiplyMatrices(
|
|
203
|
-
[
|
|
204
|
-
1.0479297925449969, 0.022946870601609652, -0.05019226628920524, 0.02962780877005599, 0.9904344267538799,
|
|
205
|
-
-0.017073799063418826, -0.009243040646204504, 0.015055191490298152, 0.7518742814281371
|
|
206
|
-
],
|
|
207
|
-
xyz
|
|
208
|
-
);
|
|
209
|
-
};
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Convert D50 to D65
|
|
213
|
-
*
|
|
214
|
-
* @param xyz
|
|
215
|
-
*/
|
|
216
|
-
export const d50toD65 = (xyz: [number, number, number]): [number, number, number] => {
|
|
217
|
-
return multiplyMatrices(
|
|
218
|
-
[
|
|
219
|
-
0.955473421488075, -0.02309845494876471, 0.06325924320057072, -0.0283697093338637, 1.0099953980813041,
|
|
220
|
-
0.021041441191917323, 0.012314014864481998, -0.020507649298898964, 1.330365926242124
|
|
221
|
-
],
|
|
222
|
-
xyz
|
|
223
|
-
);
|
|
224
|
-
};
|
|
225
|
-
|
|
226
|
-
export const hue2rgb = (t1: number, t2: number, hue: number): number => {
|
|
227
|
-
if (hue < 0) {
|
|
228
|
-
hue += 1;
|
|
229
|
-
}
|
|
230
|
-
if (hue >= 1) {
|
|
231
|
-
hue -= 1;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
if (hue < 1 / 6) {
|
|
235
|
-
return (t2 - t1) * hue * 6 + t1;
|
|
236
|
-
} else if (hue < 1 / 2) {
|
|
237
|
-
return t2;
|
|
238
|
-
} else if (hue < 2 / 3) {
|
|
239
|
-
return (t2 - t1) * 6 * (2 / 3 - hue) + t1;
|
|
240
|
-
} else {
|
|
241
|
-
return t1;
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
const hsl2rgb = ([h, s, l]: [number, number, number]): [number, number, number] => {
|
|
246
|
-
if (s === 0) {
|
|
247
|
-
return [l * 255, l * 255, l * 255];
|
|
248
|
-
}
|
|
249
|
-
const t2 = l <= 0.5 ? l * (s + 1) : l + s - l * s,
|
|
250
|
-
t1 = l * 2 - t2,
|
|
251
|
-
r = hue2rgb(t1, t2, h + 1 / 3),
|
|
252
|
-
g = hue2rgb(t1, t2, h),
|
|
253
|
-
b = hue2rgb(t1, t2, h - 1 / 3);
|
|
254
|
-
|
|
255
|
-
return [r, g, b];
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* Convert lch to OKLab
|
|
260
|
-
*
|
|
261
|
-
* @param l
|
|
262
|
-
* @param c
|
|
263
|
-
* @param h
|
|
264
|
-
*/
|
|
265
|
-
const lch2lab = ([l, c, h]: [number, number, number]): [number, number, number] => {
|
|
266
|
-
if (c < 0) {
|
|
267
|
-
c = 0;
|
|
268
|
-
}
|
|
269
|
-
if (isNaN(h)) {
|
|
270
|
-
h = 0;
|
|
271
|
-
}
|
|
272
|
-
return [l, c * Math.cos((h * Math.PI) / 180), c * Math.sin((h * Math.PI) / 180)];
|
|
273
|
-
};
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Convert OKLab to XYZ relative to D65
|
|
277
|
-
*
|
|
278
|
-
* @param lab
|
|
279
|
-
*/
|
|
280
|
-
const oklab2xyz = (lab: [number, number, number]): [number, number, number] => {
|
|
281
|
-
const LMSg = multiplyMatrices(
|
|
282
|
-
[
|
|
283
|
-
1, 0.3963377773761749, 0.2158037573099136, 1, -0.1055613458156586, -0.0638541728258133, 1,
|
|
284
|
-
-0.0894841775298119, -1.2914855480194092
|
|
285
|
-
],
|
|
286
|
-
lab
|
|
287
|
-
),
|
|
288
|
-
LMS = LMSg.map((val: number) => val ** 3);
|
|
289
|
-
|
|
290
|
-
return multiplyMatrices(
|
|
291
|
-
[
|
|
292
|
-
1.2268798758459243, -0.5578149944602171, 0.2813910456659647, -0.0405757452148008, 1.112286803280317,
|
|
293
|
-
-0.0717110580655164, -0.0763729366746601, -0.4214933324022432, 1.5869240198367816
|
|
294
|
-
],
|
|
295
|
-
LMS
|
|
296
|
-
);
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
/**
|
|
300
|
-
* Convert Lab to D50-adapted XYZ
|
|
301
|
-
*
|
|
302
|
-
* @param lab
|
|
303
|
-
*/
|
|
304
|
-
const lab2xyz = (lab: [number, number, number]): [number, number, number] => {
|
|
305
|
-
const fy = (lab[0] + 16) / 116,
|
|
306
|
-
fx = lab[1] / 500 + fy,
|
|
307
|
-
fz = fy - lab[2] / 200,
|
|
308
|
-
k = 24389 / 27,
|
|
309
|
-
e = 24 / 116,
|
|
310
|
-
xyz = [
|
|
311
|
-
((fx > e ? fx ** 3 : (116 * fx - 16) / k) * 0.3457) / 0.3585,
|
|
312
|
-
lab[0] > 8 ? fy ** 3 : lab[0] / k,
|
|
313
|
-
((fz > e ? fz ** 3 : (116 * fz - 16) / k) * (1.0 - 0.3457 - 0.3585)) / 0.3585
|
|
314
|
-
];
|
|
315
|
-
|
|
316
|
-
return d50toD65([xyz[0], xyz[1], xyz[2]]);
|
|
317
|
-
};
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* Convert RGB to XYZ
|
|
321
|
-
*
|
|
322
|
-
* @param _context
|
|
323
|
-
* @param args
|
|
324
|
-
*/
|
|
325
|
-
export const rgbToXyz = (_context: Context, args: CSSValue[]): [number, number, number, number] => {
|
|
326
|
-
const tokens = args.filter(nonFunctionArgSeparator);
|
|
327
|
-
|
|
328
|
-
if (tokens.length === 3) {
|
|
329
|
-
const [r, g, b] = tokens.map(getTokenColorValue),
|
|
330
|
-
rgb_linear = rgb2rgbLinear([r / 255, g / 255, b / 255]),
|
|
331
|
-
[x, y, z] = rgbLinear2xyz([rgb_linear[0], rgb_linear[1], rgb_linear[2]]);
|
|
332
|
-
return [x, y, z, 1];
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
if (tokens.length === 4) {
|
|
336
|
-
const [r, g, b, a] = tokens.map(getTokenColorValue),
|
|
337
|
-
rgb_linear = rgb2rgbLinear([r / 255, g / 255, b / 255]),
|
|
338
|
-
[x, y, z] = rgbLinear2xyz([rgb_linear[0], rgb_linear[1], rgb_linear[2]]);
|
|
339
|
-
return [x, y, z, a];
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
return [0, 0, 0, 1];
|
|
343
|
-
};
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* HSL to XYZ
|
|
347
|
-
*
|
|
348
|
-
* @param context
|
|
349
|
-
* @param args
|
|
350
|
-
*/
|
|
351
|
-
export const hslToXyz = (context: Context, args: CSSValue[]): [number, number, number, number] => {
|
|
352
|
-
const [h, s, l, a] = extractHslComponents(context, args),
|
|
353
|
-
rgb_linear = rgb2rgbLinear(hsl2rgb([h, s, l])),
|
|
354
|
-
[x, y, z] = rgbLinear2xyz([rgb_linear[0], rgb_linear[1], rgb_linear[2]]);
|
|
355
|
-
|
|
356
|
-
return [x, y, z, a];
|
|
357
|
-
};
|
|
358
|
-
|
|
359
|
-
/**
|
|
360
|
-
* LAB to XYZ
|
|
361
|
-
*
|
|
362
|
-
* @param _context
|
|
363
|
-
* @param args
|
|
364
|
-
*/
|
|
365
|
-
export const labToXyz = (_context: Context, args: CSSValue[]): [number, number, number, number] => {
|
|
366
|
-
const [l, a, b, alpha] = extractLabComponents(args),
|
|
367
|
-
[x, y, z] = lab2xyz([l, a, b]);
|
|
368
|
-
return [x, y, z, alpha];
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* LCH to XYZ
|
|
373
|
-
*
|
|
374
|
-
* @param _context
|
|
375
|
-
* @param args
|
|
376
|
-
*/
|
|
377
|
-
export const lchToXyz = (_context: Context, args: CSSValue[]): [number, number, number, number] => {
|
|
378
|
-
const [l, c, h, alpha] = extractLchComponents(args),
|
|
379
|
-
[x, y, z] = lab2xyz(lch2lab([l, c, h]));
|
|
380
|
-
return [x, y, z, alpha];
|
|
381
|
-
};
|
|
382
|
-
|
|
383
|
-
/**
|
|
384
|
-
* OKLch to XYZ
|
|
385
|
-
*
|
|
386
|
-
* @param _context
|
|
387
|
-
* @param args
|
|
388
|
-
*/
|
|
389
|
-
export const oklchToXyz = (_context: Context, args: CSSValue[]): [number, number, number, number] => {
|
|
390
|
-
const [l, c, h, alpha] = extractOkLchComponents(args),
|
|
391
|
-
[x, y, z] = oklab2xyz(lch2lab([l, c, h]));
|
|
392
|
-
return [x, y, z, alpha];
|
|
393
|
-
};
|
|
394
|
-
|
|
395
|
-
/**
|
|
396
|
-
* OKLab to XYZ
|
|
397
|
-
*
|
|
398
|
-
* @param _context
|
|
399
|
-
* @param args
|
|
400
|
-
*/
|
|
401
|
-
export const oklabToXyz = (_context: Context, args: CSSValue[]): [number, number, number, number] => {
|
|
402
|
-
const [l, c, h, alpha] = extractLabComponents(args),
|
|
403
|
-
[x, y, z] = oklab2xyz([l, c, h]);
|
|
404
|
-
return [x, y, z, alpha];
|
|
405
|
-
};
|
|
406
|
-
|
|
407
|
-
/**
|
|
408
|
-
* XYZ-50 to XYZ
|
|
409
|
-
*
|
|
410
|
-
* @param args
|
|
411
|
-
*/
|
|
412
|
-
export const xyz50ToXYZ = (args: number[]): [number, number, number] => {
|
|
413
|
-
return d50toD65([args[0], args[1], args[2]]);
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
/**
|
|
417
|
-
* Does nothing, required for SUPPORTED_COLOR_SPACES_FROM_XYZ in the _color() function
|
|
418
|
-
*
|
|
419
|
-
* @param args
|
|
420
|
-
*/
|
|
421
|
-
export const xyzFromXYZ = (args: [number, number, number, number]): [number, number, number, number] => {
|
|
422
|
-
return args;
|
|
423
|
-
};
|
|
424
|
-
|
|
425
|
-
/**
|
|
426
|
-
* XYZ-65 to XYZ-50
|
|
427
|
-
*
|
|
428
|
-
* @param args
|
|
429
|
-
*/
|
|
430
|
-
export const xyz50FromXYZ = (args: [number, number, number, number]): [number, number, number, number] => {
|
|
431
|
-
const [x, y, z] = d65toD50([args[0], args[2], args[3]]);
|
|
432
|
-
return [x, y, z, args[3]];
|
|
433
|
-
};
|
|
434
|
-
|
|
435
|
-
/**
|
|
436
|
-
* Convert XYZ to SRGB and Pack
|
|
437
|
-
*
|
|
438
|
-
* @param args
|
|
439
|
-
*/
|
|
440
|
-
export const convertXyz = (args: number[]): number => {
|
|
441
|
-
return packXYZ([args[0], args[1], args[2], args[3]]);
|
|
442
|
-
};
|
|
443
|
-
|
|
444
|
-
/**
|
|
445
|
-
* Convert XYZ-50 to SRGB and Pack
|
|
446
|
-
*
|
|
447
|
-
* @param args
|
|
448
|
-
*/
|
|
449
|
-
export const convertXyz50 = (args: number[]): number => {
|
|
450
|
-
const xyz = xyz50ToXYZ([args[0], args[1], args[2]]);
|
|
451
|
-
return packXYZ([xyz[0], xyz[1], xyz[2], args[3]]);
|
|
452
|
-
};
|