@atlaskit/tokens 1.11.2 → 1.12.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/CHANGELOG.md +20 -0
- package/dist/cjs/artifacts/atlassian-dark-token-value-for-contrast-check.js +24 -0
- package/dist/cjs/artifacts/atlassian-light-token-value-for-contrast-check.js +24 -0
- package/dist/cjs/artifacts/themes/atlassian-dark-new-input-border.js +2 -2
- package/dist/cjs/artifacts/tokens-raw/atlassian-dark-new-input-border.js +3 -3
- package/dist/cjs/constants.js +3 -1
- package/dist/cjs/custom-theme.js +108 -0
- package/dist/cjs/get-token-value.js +1 -1
- package/dist/cjs/get-token.js +1 -1
- package/dist/cjs/set-global-theme.js +156 -59
- package/dist/cjs/tokens/atlassian-dark-new-input-border/color/border.js +1 -1
- package/dist/cjs/utils/color-utils.js +178 -0
- package/dist/cjs/utils/custom-theme-loading-utils.js +47 -0
- package/dist/cjs/utils/custom-theme-token-contrast-check.js +74 -0
- package/dist/cjs/utils/generate-custom-color-ramp.js +213 -0
- package/dist/cjs/utils/hash.js +17 -0
- package/dist/cjs/utils/hct-color-utils/color-utils.js +310 -0
- package/dist/cjs/utils/hct-color-utils/contrast.js +188 -0
- package/dist/cjs/utils/hct-color-utils/hct.js +1036 -0
- package/dist/cjs/utils/hct-color-utils/index.js +32 -0
- package/dist/cjs/utils/hct-color-utils/math-utils.js +159 -0
- package/dist/cjs/utils/theme-loading.js +1 -1
- package/dist/cjs/utils/theme-state-transformer.js +1 -1
- package/dist/cjs/version.json +1 -1
- package/dist/es2019/artifacts/atlassian-dark-token-value-for-contrast-check.js +17 -0
- package/dist/es2019/artifacts/atlassian-light-token-value-for-contrast-check.js +17 -0
- package/dist/es2019/artifacts/themes/atlassian-dark-new-input-border.js +2 -2
- package/dist/es2019/artifacts/tokens-raw/atlassian-dark-new-input-border.js +3 -3
- package/dist/es2019/constants.js +1 -0
- package/dist/es2019/custom-theme.js +77 -0
- package/dist/es2019/get-token-value.js +1 -1
- package/dist/es2019/get-token.js +1 -1
- package/dist/es2019/set-global-theme.js +67 -13
- package/dist/es2019/tokens/atlassian-dark-new-input-border/color/border.js +1 -1
- package/dist/es2019/utils/color-utils.js +154 -0
- package/dist/es2019/utils/custom-theme-loading-utils.js +31 -0
- package/dist/es2019/utils/custom-theme-token-contrast-check.js +68 -0
- package/dist/es2019/utils/generate-custom-color-ramp.js +187 -0
- package/dist/es2019/utils/hash.js +10 -0
- package/dist/es2019/utils/hct-color-utils/color-utils.js +286 -0
- package/dist/es2019/utils/hct-color-utils/contrast.js +161 -0
- package/dist/es2019/utils/hct-color-utils/hct.js +931 -0
- package/dist/es2019/utils/hct-color-utils/index.js +3 -0
- package/dist/es2019/utils/hct-color-utils/math-utils.js +145 -0
- package/dist/es2019/utils/theme-loading.js +2 -2
- package/dist/es2019/utils/theme-state-transformer.js +3 -1
- package/dist/es2019/version.json +1 -1
- package/dist/esm/artifacts/atlassian-dark-token-value-for-contrast-check.js +17 -0
- package/dist/esm/artifacts/atlassian-light-token-value-for-contrast-check.js +17 -0
- package/dist/esm/artifacts/themes/atlassian-dark-new-input-border.js +2 -2
- package/dist/esm/artifacts/tokens-raw/atlassian-dark-new-input-border.js +3 -3
- package/dist/esm/constants.js +1 -0
- package/dist/esm/custom-theme.js +98 -0
- package/dist/esm/get-token-value.js +1 -1
- package/dist/esm/get-token.js +1 -1
- package/dist/esm/set-global-theme.js +149 -60
- package/dist/esm/tokens/atlassian-dark-new-input-border/color/border.js +1 -1
- package/dist/esm/utils/color-utils.js +162 -0
- package/dist/esm/utils/custom-theme-loading-utils.js +38 -0
- package/dist/esm/utils/custom-theme-token-contrast-check.js +65 -0
- package/dist/esm/utils/generate-custom-color-ramp.js +202 -0
- package/dist/esm/utils/hash.js +10 -0
- package/dist/esm/utils/hct-color-utils/color-utils.js +285 -0
- package/dist/esm/utils/hct-color-utils/contrast.js +181 -0
- package/dist/esm/utils/hct-color-utils/hct.js +1029 -0
- package/dist/esm/utils/hct-color-utils/index.js +3 -0
- package/dist/esm/utils/hct-color-utils/math-utils.js +145 -0
- package/dist/esm/utils/theme-loading.js +2 -2
- package/dist/esm/utils/theme-state-transformer.js +1 -1
- package/dist/esm/version.json +1 -1
- package/dist/types/artifacts/atlassian-dark-token-value-for-contrast-check.d.ts +17 -0
- package/dist/types/artifacts/atlassian-light-token-value-for-contrast-check.d.ts +17 -0
- package/dist/types/artifacts/themes/atlassian-dark-new-input-border.d.ts +2 -2
- package/dist/types/artifacts/tokens-raw/atlassian-dark-new-input-border.d.ts +1 -1
- package/dist/types/constants.d.ts +1 -0
- package/dist/types/custom-theme.d.ts +30 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/set-global-theme.d.ts +9 -3
- package/dist/types/tokens/atlassian-dark/utility/utility.d.ts +1 -1
- package/dist/types/tokens/atlassian-light/utility/utility.d.ts +1 -1
- package/dist/types/tokens/default/utility/utility.d.ts +1 -1
- package/dist/types/utils/color-utils.d.ts +10 -0
- package/dist/types/utils/custom-theme-loading-utils.d.ts +11 -0
- package/dist/types/utils/custom-theme-token-contrast-check.d.ts +20 -0
- package/dist/types/utils/generate-custom-color-ramp.d.ts +19 -0
- package/dist/types/utils/hash.d.ts +1 -0
- package/dist/types/utils/hct-color-utils/color-utils.d.ts +131 -0
- package/dist/types/utils/hct-color-utils/contrast.d.ts +78 -0
- package/dist/types/utils/hct-color-utils/hct.d.ts +137 -0
- package/dist/types/utils/hct-color-utils/index.d.ts +3 -0
- package/dist/types/utils/hct-color-utils/math-utils.d.ts +86 -0
- package/dist/types-ts4.5/artifacts/atlassian-dark-token-value-for-contrast-check.d.ts +17 -0
- package/dist/types-ts4.5/artifacts/atlassian-light-token-value-for-contrast-check.d.ts +17 -0
- package/dist/types-ts4.5/artifacts/themes/atlassian-dark-new-input-border.d.ts +2 -2
- package/dist/types-ts4.5/artifacts/tokens-raw/atlassian-dark-new-input-border.d.ts +1 -1
- package/dist/types-ts4.5/constants.d.ts +1 -0
- package/dist/types-ts4.5/custom-theme.d.ts +30 -0
- package/dist/types-ts4.5/index.d.ts +1 -0
- package/dist/types-ts4.5/set-global-theme.d.ts +9 -3
- package/dist/types-ts4.5/tokens/atlassian-dark/utility/utility.d.ts +1 -1
- package/dist/types-ts4.5/tokens/atlassian-light/utility/utility.d.ts +1 -1
- package/dist/types-ts4.5/tokens/default/utility/utility.d.ts +1 -1
- package/dist/types-ts4.5/utils/color-utils.d.ts +27 -0
- package/dist/types-ts4.5/utils/custom-theme-loading-utils.d.ts +11 -0
- package/dist/types-ts4.5/utils/custom-theme-token-contrast-check.d.ts +20 -0
- package/dist/types-ts4.5/utils/generate-custom-color-ramp.d.ts +19 -0
- package/dist/types-ts4.5/utils/hash.d.ts +1 -0
- package/dist/types-ts4.5/utils/hct-color-utils/color-utils.d.ts +131 -0
- package/dist/types-ts4.5/utils/hct-color-utils/contrast.d.ts +78 -0
- package/dist/types-ts4.5/utils/hct-color-utils/hct.d.ts +137 -0
- package/dist/types-ts4.5/utils/hct-color-utils/index.d.ts +3 -0
- package/dist/types-ts4.5/utils/hct-color-utils/math-utils.d.ts +86 -0
- package/figma/atlassian-dark-new-input-border.json +1 -1
- package/package.json +35 -40
- package/report.api.md +24 -1
- package/tmp/api-report-tmp.d.ts +0 -1132
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Below lines are copied from @material/material-color-utilities.
|
|
3
|
+
* Do not modify it.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @license
|
|
8
|
+
* Copyright 2021 Google LLC
|
|
9
|
+
*
|
|
10
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
11
|
+
* you may not use this file except in compliance with the License.
|
|
12
|
+
* You may obtain a copy of the License at
|
|
13
|
+
*
|
|
14
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
15
|
+
*
|
|
16
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
17
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
18
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
19
|
+
* See the License for the specific language governing permissions and
|
|
20
|
+
* limitations under the License.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
// This file is automatically generated. Do not modify it.
|
|
24
|
+
|
|
25
|
+
import * as mathUtils from './math-utils';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Color science utilities.
|
|
29
|
+
*
|
|
30
|
+
* Utility methods for color science constants and color space
|
|
31
|
+
* conversions that aren't HCT or CAM16.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
const SRGB_TO_XYZ = [[0.41233895, 0.35762064, 0.18051042], [0.2126, 0.7152, 0.0722], [0.01932141, 0.11916382, 0.95034478]];
|
|
35
|
+
const XYZ_TO_SRGB = [[3.2413774792388685, -1.5376652402851851, -0.49885366846268053], [-0.9691452513005321, 1.8758853451067872, 0.04156585616912061], [0.05562093689691305, -0.20395524564742123, 1.0571799111220335]];
|
|
36
|
+
const WHITE_POINT_D65 = [95.047, 100.0, 108.883];
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Converts a color from RGB components to ARGB format.
|
|
40
|
+
*/
|
|
41
|
+
export function argbFromRgb(red, green, blue) {
|
|
42
|
+
return (255 << 24 | (red & 255) << 16 | (green & 255) << 8 | blue & 255) >>> 0;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Converts a color from linear RGB components to ARGB format.
|
|
47
|
+
*/
|
|
48
|
+
export function argbFromLinrgb(linrgb) {
|
|
49
|
+
const r = delinearized(linrgb[0]);
|
|
50
|
+
const g = delinearized(linrgb[1]);
|
|
51
|
+
const b = delinearized(linrgb[2]);
|
|
52
|
+
return argbFromRgb(r, g, b);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Returns the alpha component of a color in ARGB format.
|
|
57
|
+
*/
|
|
58
|
+
export function alphaFromArgb(argb) {
|
|
59
|
+
return argb >> 24 & 255;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Returns the red component of a color in ARGB format.
|
|
64
|
+
*/
|
|
65
|
+
export function redFromArgb(argb) {
|
|
66
|
+
return argb >> 16 & 255;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Returns the green component of a color in ARGB format.
|
|
71
|
+
*/
|
|
72
|
+
export function greenFromArgb(argb) {
|
|
73
|
+
return argb >> 8 & 255;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Returns the blue component of a color in ARGB format.
|
|
78
|
+
*/
|
|
79
|
+
export function blueFromArgb(argb) {
|
|
80
|
+
return argb & 255;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Returns whether a color in ARGB format is opaque.
|
|
85
|
+
*/
|
|
86
|
+
export function isOpaque(argb) {
|
|
87
|
+
return alphaFromArgb(argb) >= 255;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Converts a color from ARGB to XYZ.
|
|
92
|
+
*/
|
|
93
|
+
export function argbFromXyz(x, y, z) {
|
|
94
|
+
const matrix = XYZ_TO_SRGB;
|
|
95
|
+
const linearR = matrix[0][0] * x + matrix[0][1] * y + matrix[0][2] * z;
|
|
96
|
+
const linearG = matrix[1][0] * x + matrix[1][1] * y + matrix[1][2] * z;
|
|
97
|
+
const linearB = matrix[2][0] * x + matrix[2][1] * y + matrix[2][2] * z;
|
|
98
|
+
const r = delinearized(linearR);
|
|
99
|
+
const g = delinearized(linearG);
|
|
100
|
+
const b = delinearized(linearB);
|
|
101
|
+
return argbFromRgb(r, g, b);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Converts a color from XYZ to ARGB.
|
|
106
|
+
*/
|
|
107
|
+
export function xyzFromArgb(argb) {
|
|
108
|
+
const r = linearized(redFromArgb(argb));
|
|
109
|
+
const g = linearized(greenFromArgb(argb));
|
|
110
|
+
const b = linearized(blueFromArgb(argb));
|
|
111
|
+
return mathUtils.matrixMultiply([r, g, b], SRGB_TO_XYZ);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Converts an L* value to an ARGB representation.
|
|
116
|
+
*
|
|
117
|
+
* @param lstar L* in L*a*b*
|
|
118
|
+
* @return ARGB representation of grayscale color with lightness
|
|
119
|
+
* matching L*
|
|
120
|
+
*/
|
|
121
|
+
export function argbFromLstar(lstar) {
|
|
122
|
+
const y = yFromLstar(lstar);
|
|
123
|
+
const component = delinearized(y);
|
|
124
|
+
return argbFromRgb(component, component, component);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Computes the L* value of a color in ARGB representation.
|
|
129
|
+
*
|
|
130
|
+
* @param argb ARGB representation of a color
|
|
131
|
+
* @return L*, from L*a*b*, coordinate of the color
|
|
132
|
+
*/
|
|
133
|
+
export function lstarFromArgb(argb) {
|
|
134
|
+
const y = xyzFromArgb(argb)[1];
|
|
135
|
+
return 116.0 * labF(y / 100.0) - 16.0;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Converts an L* value to a Y value.
|
|
140
|
+
*
|
|
141
|
+
* L* in L*a*b* and Y in XYZ measure the same quantity, luminance.
|
|
142
|
+
*
|
|
143
|
+
* L* measures perceptual luminance, a linear scale. Y in XYZ
|
|
144
|
+
* measures relative luminance, a logarithmic scale.
|
|
145
|
+
*
|
|
146
|
+
* @param lstar L* in L*a*b*
|
|
147
|
+
* @return Y in XYZ
|
|
148
|
+
*/
|
|
149
|
+
export function yFromLstar(lstar) {
|
|
150
|
+
return 100.0 * labInvf((lstar + 16.0) / 116.0);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Converts a Y value to an L* value.
|
|
155
|
+
*
|
|
156
|
+
* L* in L*a*b* and Y in XYZ measure the same quantity, luminance.
|
|
157
|
+
*
|
|
158
|
+
* L* measures perceptual luminance, a linear scale. Y in XYZ
|
|
159
|
+
* measures relative luminance, a logarithmic scale.
|
|
160
|
+
*
|
|
161
|
+
* @param y Y in XYZ
|
|
162
|
+
* @return L* in L*a*b*
|
|
163
|
+
*/
|
|
164
|
+
export function lstarFromY(y) {
|
|
165
|
+
return labF(y / 100.0) * 116.0 - 16.0;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Linearizes an RGB component.
|
|
170
|
+
*
|
|
171
|
+
* @param rgbComponent 0 <= rgb_component <= 255, represents R/G/B
|
|
172
|
+
* channel
|
|
173
|
+
* @return 0.0 <= output <= 100.0, color channel converted to
|
|
174
|
+
* linear RGB space
|
|
175
|
+
*/
|
|
176
|
+
export function linearized(rgbComponent) {
|
|
177
|
+
const normalized = rgbComponent / 255.0;
|
|
178
|
+
if (normalized <= 0.040449936) {
|
|
179
|
+
return normalized / 12.92 * 100.0;
|
|
180
|
+
} else {
|
|
181
|
+
return Math.pow((normalized + 0.055) / 1.055, 2.4) * 100.0;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Delinearizes an RGB component.
|
|
187
|
+
*
|
|
188
|
+
* @param rgbComponent 0.0 <= rgb_component <= 100.0, represents
|
|
189
|
+
* linear R/G/B channel
|
|
190
|
+
* @return 0 <= output <= 255, color channel converted to regular
|
|
191
|
+
* RGB space
|
|
192
|
+
*/
|
|
193
|
+
export function delinearized(rgbComponent) {
|
|
194
|
+
const normalized = rgbComponent / 100.0;
|
|
195
|
+
let delinearized = 0.0;
|
|
196
|
+
if (normalized <= 0.0031308) {
|
|
197
|
+
delinearized = normalized * 12.92;
|
|
198
|
+
} else {
|
|
199
|
+
delinearized = 1.055 * Math.pow(normalized, 1.0 / 2.4) - 0.055;
|
|
200
|
+
}
|
|
201
|
+
return mathUtils.clampInt(0, 255, Math.round(delinearized * 255.0));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Returns the standard white point; white on a sunny day.
|
|
206
|
+
*
|
|
207
|
+
* @return The white point
|
|
208
|
+
*/
|
|
209
|
+
export function whitePointD65() {
|
|
210
|
+
return WHITE_POINT_D65;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* RGBA component
|
|
215
|
+
*
|
|
216
|
+
* @param r Red value should be between 0-255
|
|
217
|
+
* @param g Green value should be between 0-255
|
|
218
|
+
* @param b Blue value should be between 0-255
|
|
219
|
+
* @param a Alpha value should be between 0-255
|
|
220
|
+
*/
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Return RGBA from a given int32 color
|
|
224
|
+
*
|
|
225
|
+
* @param argb ARGB representation of a int32 color.
|
|
226
|
+
* @return RGBA representation of a int32 color.
|
|
227
|
+
*/
|
|
228
|
+
export function rgbaFromArgb(argb) {
|
|
229
|
+
const r = redFromArgb(argb);
|
|
230
|
+
const g = greenFromArgb(argb);
|
|
231
|
+
const b = blueFromArgb(argb);
|
|
232
|
+
const a = alphaFromArgb(argb);
|
|
233
|
+
return {
|
|
234
|
+
r,
|
|
235
|
+
g,
|
|
236
|
+
b,
|
|
237
|
+
a
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Return int32 color from a given RGBA component
|
|
243
|
+
*
|
|
244
|
+
* @param rgba RGBA representation of a int32 color.
|
|
245
|
+
* @returns ARGB representation of a int32 color.
|
|
246
|
+
*/
|
|
247
|
+
export function argbFromRgba({
|
|
248
|
+
r,
|
|
249
|
+
g,
|
|
250
|
+
b,
|
|
251
|
+
a
|
|
252
|
+
}) {
|
|
253
|
+
const rValue = clampComponent(r);
|
|
254
|
+
const gValue = clampComponent(g);
|
|
255
|
+
const bValue = clampComponent(b);
|
|
256
|
+
const aValue = clampComponent(a);
|
|
257
|
+
return aValue << 24 | rValue << 16 | gValue << 8 | bValue;
|
|
258
|
+
}
|
|
259
|
+
function clampComponent(value) {
|
|
260
|
+
if (value < 0) {
|
|
261
|
+
return 0;
|
|
262
|
+
}
|
|
263
|
+
if (value > 255) {
|
|
264
|
+
return 255;
|
|
265
|
+
}
|
|
266
|
+
return value;
|
|
267
|
+
}
|
|
268
|
+
function labF(t) {
|
|
269
|
+
const e = 216.0 / 24389.0;
|
|
270
|
+
const kappa = 24389.0 / 27.0;
|
|
271
|
+
if (t > e) {
|
|
272
|
+
return Math.pow(t, 1.0 / 3.0);
|
|
273
|
+
} else {
|
|
274
|
+
return (kappa * t + 16) / 116;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
function labInvf(ft) {
|
|
278
|
+
const e = 216.0 / 24389.0;
|
|
279
|
+
const kappa = 24389.0 / 27.0;
|
|
280
|
+
const ft3 = ft * ft * ft;
|
|
281
|
+
if (ft3 > e) {
|
|
282
|
+
return ft3;
|
|
283
|
+
} else {
|
|
284
|
+
return (116 * ft - 16) / kappa;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Below lines are copied from @material/material-color-utilities.
|
|
3
|
+
* Do not modify it.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @license
|
|
8
|
+
* Copyright 2022 Google LLC
|
|
9
|
+
*
|
|
10
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
11
|
+
* you may not use this file except in compliance with the License.
|
|
12
|
+
* You may obtain a copy of the License at
|
|
13
|
+
*
|
|
14
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
15
|
+
*
|
|
16
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
17
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
18
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
19
|
+
* See the License for the specific language governing permissions and
|
|
20
|
+
* limitations under the License.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
// material_color_utilities is designed to have a consistent API across
|
|
24
|
+
// platforms and modular components that can be moved around easily. Using a
|
|
25
|
+
// class as a namespace facilitates this.
|
|
26
|
+
//
|
|
27
|
+
// tslint:disable:class-as-namespace
|
|
28
|
+
|
|
29
|
+
import * as utils from './color-utils';
|
|
30
|
+
import * as math from './math-utils';
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Utility methods for calculating contrast given two colors, or calculating a
|
|
34
|
+
* color given one color and a contrast ratio.
|
|
35
|
+
*
|
|
36
|
+
* Contrast ratio is calculated using XYZ's Y. When linearized to match human
|
|
37
|
+
* perception, Y becomes HCT's tone and L*a*b*'s' L*. Informally, this is the
|
|
38
|
+
* lightness of a color.
|
|
39
|
+
*
|
|
40
|
+
* Methods refer to tone, T in the the HCT color space.
|
|
41
|
+
* Tone is equivalent to L* in the L*a*b* color space, or L in the LCH color
|
|
42
|
+
* space.
|
|
43
|
+
*/
|
|
44
|
+
export class Contrast {
|
|
45
|
+
/**
|
|
46
|
+
* Returns a contrast ratio, which ranges from 1 to 21.
|
|
47
|
+
*
|
|
48
|
+
* @param toneA Tone between 0 and 100. Values outside will be clamped.
|
|
49
|
+
* @param toneB Tone between 0 and 100. Values outside will be clamped.
|
|
50
|
+
*/
|
|
51
|
+
static ratioOfTones(toneA, toneB) {
|
|
52
|
+
toneA = math.clampDouble(0.0, 100.0, toneA);
|
|
53
|
+
toneB = math.clampDouble(0.0, 100.0, toneB);
|
|
54
|
+
return Contrast.ratioOfYs(utils.yFromLstar(toneA), utils.yFromLstar(toneB));
|
|
55
|
+
}
|
|
56
|
+
static ratioOfYs(y1, y2) {
|
|
57
|
+
const lighter = y1 > y2 ? y1 : y2;
|
|
58
|
+
const darker = lighter === y2 ? y1 : y2;
|
|
59
|
+
return (lighter + 5.0) / (darker + 5.0);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Returns a tone >= tone parameter that ensures ratio parameter.
|
|
64
|
+
* Return value is between 0 and 100.
|
|
65
|
+
* Returns -1 if ratio cannot be achieved with tone parameter.
|
|
66
|
+
*
|
|
67
|
+
* @param tone Tone return value must contrast with.
|
|
68
|
+
* Range is 0 to 100. Invalid values will result in -1 being returned.
|
|
69
|
+
* @param ratio Contrast ratio of return value and tone.
|
|
70
|
+
* Range is 1 to 21, invalid values have undefined behavior.
|
|
71
|
+
*/
|
|
72
|
+
static lighter(tone, ratio) {
|
|
73
|
+
if (tone < 0.0 || tone > 100.0) {
|
|
74
|
+
return -1.0;
|
|
75
|
+
}
|
|
76
|
+
const darkY = utils.yFromLstar(tone);
|
|
77
|
+
const lightY = ratio * (darkY + 5.0) - 5.0;
|
|
78
|
+
const realContrast = Contrast.ratioOfYs(lightY, darkY);
|
|
79
|
+
const delta = Math.abs(realContrast - ratio);
|
|
80
|
+
if (realContrast < ratio && delta > 0.04) {
|
|
81
|
+
return -1;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Ensure gamut mapping, which requires a 'range' on tone, will still result
|
|
85
|
+
// the correct ratio by darkening slightly.
|
|
86
|
+
const returnValue = utils.lstarFromY(lightY) + 0.4;
|
|
87
|
+
if (returnValue < 0 || returnValue > 100) {
|
|
88
|
+
return -1;
|
|
89
|
+
}
|
|
90
|
+
return returnValue;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Returns a tone <= tone parameter that ensures ratio parameter.
|
|
95
|
+
* Return value is between 0 and 100.
|
|
96
|
+
* Returns -1 if ratio cannot be achieved with tone parameter.
|
|
97
|
+
*
|
|
98
|
+
* @param tone Tone return value must contrast with.
|
|
99
|
+
* Range is 0 to 100. Invalid values will result in -1 being returned.
|
|
100
|
+
* @param ratio Contrast ratio of return value and tone.
|
|
101
|
+
* Range is 1 to 21, invalid values have undefined behavior.
|
|
102
|
+
*/
|
|
103
|
+
static darker(tone, ratio) {
|
|
104
|
+
if (tone < 0.0 || tone > 100.0) {
|
|
105
|
+
return -1.0;
|
|
106
|
+
}
|
|
107
|
+
const lightY = utils.yFromLstar(tone);
|
|
108
|
+
const darkY = (lightY + 5.0) / ratio - 5.0;
|
|
109
|
+
const realContrast = Contrast.ratioOfYs(lightY, darkY);
|
|
110
|
+
const delta = Math.abs(realContrast - ratio);
|
|
111
|
+
if (realContrast < ratio && delta > 0.04) {
|
|
112
|
+
return -1;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Ensure gamut mapping, which requires a 'range' on tone, will still result
|
|
116
|
+
// the correct ratio by darkening slightly.
|
|
117
|
+
const returnValue = utils.lstarFromY(darkY) - 0.4;
|
|
118
|
+
if (returnValue < 0 || returnValue > 100) {
|
|
119
|
+
return -1;
|
|
120
|
+
}
|
|
121
|
+
return returnValue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Returns a tone >= tone parameter that ensures ratio parameter.
|
|
126
|
+
* Return value is between 0 and 100.
|
|
127
|
+
* Returns 100 if ratio cannot be achieved with tone parameter.
|
|
128
|
+
*
|
|
129
|
+
* This method is unsafe because the returned value is guaranteed to be in
|
|
130
|
+
* bounds for tone, i.e. between 0 and 100. However, that value may not reach
|
|
131
|
+
* the ratio with tone. For example, there is no color lighter than T100.
|
|
132
|
+
*
|
|
133
|
+
* @param tone Tone return value must contrast with.
|
|
134
|
+
* Range is 0 to 100. Invalid values will result in 100 being returned.
|
|
135
|
+
* @param ratio Desired contrast ratio of return value and tone parameter.
|
|
136
|
+
* Range is 1 to 21, invalid values have undefined behavior.
|
|
137
|
+
*/
|
|
138
|
+
static lighterUnsafe(tone, ratio) {
|
|
139
|
+
const lighterSafe = Contrast.lighter(tone, ratio);
|
|
140
|
+
return lighterSafe < 0.0 ? 100.0 : lighterSafe;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Returns a tone >= tone parameter that ensures ratio parameter.
|
|
145
|
+
* Return value is between 0 and 100.
|
|
146
|
+
* Returns 100 if ratio cannot be achieved with tone parameter.
|
|
147
|
+
*
|
|
148
|
+
* This method is unsafe because the returned value is guaranteed to be in
|
|
149
|
+
* bounds for tone, i.e. between 0 and 100. However, that value may not reach
|
|
150
|
+
* the [ratio with [tone]. For example, there is no color darker than T0.
|
|
151
|
+
*
|
|
152
|
+
* @param tone Tone return value must contrast with.
|
|
153
|
+
* Range is 0 to 100. Invalid values will result in 0 being returned.
|
|
154
|
+
* @param ratio Desired contrast ratio of return value and tone parameter.
|
|
155
|
+
* Range is 1 to 21, invalid values have undefined behavior.
|
|
156
|
+
*/
|
|
157
|
+
static darkerUnsafe(tone, ratio) {
|
|
158
|
+
const darkerSafe = Contrast.darker(tone, ratio);
|
|
159
|
+
return darkerSafe < 0.0 ? 0.0 : darkerSafe;
|
|
160
|
+
}
|
|
161
|
+
}
|