@m3e/theme 1.0.0-rc.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/index.js ADDED
@@ -0,0 +1,3711 @@
1
+ /**
2
+ * @license MIT
3
+ * Copyright (c) 2025 matraic
4
+ * See LICENSE file in the project root for full license text.
5
+ */
6
+ import { LitElement, html, css } from 'lit';
7
+ import { Role, DesignToken } from '@m3e/core';
8
+
9
+ /******************************************************************************
10
+ Copyright (c) Microsoft Corporation.
11
+
12
+ Permission to use, copy, modify, and/or distribute this software for any
13
+ purpose with or without fee is hereby granted.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
16
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
17
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
18
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
19
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
20
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21
+ PERFORMANCE OF THIS SOFTWARE.
22
+ ***************************************************************************** */
23
+ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
24
+
25
+
26
+ function __decorate(decorators, target, key, desc) {
27
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
28
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
29
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
30
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
31
+ }
32
+
33
+ function __classPrivateFieldGet(receiver, state, kind, f) {
34
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
35
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
36
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
37
+ }
38
+
39
+ function __classPrivateFieldSet(receiver, state, value, kind, f) {
40
+ if (kind === "m") throw new TypeError("Private method is not writable");
41
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
42
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
43
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
44
+ }
45
+
46
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
47
+ var e = new Error(message);
48
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
49
+ };
50
+
51
+ /**
52
+ * @license
53
+ * Copyright 2017 Google LLC
54
+ * SPDX-License-Identifier: BSD-3-Clause
55
+ */
56
+ const t$1=t=>(e,o)=>{ void 0!==o?o.addInitializer((()=>{customElements.define(t,e);})):customElements.define(t,e);};
57
+
58
+ /**
59
+ * @license
60
+ * Copyright 2019 Google LLC
61
+ * SPDX-License-Identifier: BSD-3-Clause
62
+ */
63
+ const t=globalThis,e$1=t.ShadowRoot&&(void 0===t.ShadyCSS||t.ShadyCSS.nativeShadow)&&"adoptedStyleSheets"in Document.prototype&&"replace"in CSSStyleSheet.prototype,s=Symbol(),o$2=new WeakMap;let n$2 = class n{constructor(t,e,o){if(this._$cssResult$=true,o!==s)throw Error("CSSResult is not constructable. Use `unsafeCSS` or `css` instead.");this.cssText=t,this.t=e;}get styleSheet(){let t=this.o;const s=this.t;if(e$1&&void 0===t){const e=void 0!==s&&1===s.length;e&&(t=o$2.get(s)),void 0===t&&((this.o=t=new CSSStyleSheet).replaceSync(this.cssText),e&&o$2.set(s,t));}return t}toString(){return this.cssText}};const r$2=t=>new n$2("string"==typeof t?t:t+"",void 0,s),S=(s,o)=>{if(e$1)s.adoptedStyleSheets=o.map((t=>t instanceof CSSStyleSheet?t:t.styleSheet));else for(const e of o){const o=document.createElement("style"),n=t.litNonce;void 0!==n&&o.setAttribute("nonce",n),o.textContent=e.cssText,s.appendChild(o);}},c$1=e$1?t=>t:t=>t instanceof CSSStyleSheet?(t=>{let e="";for(const s of t.cssRules)e+=s.cssText;return r$2(e)})(t):t;
64
+
65
+ /**
66
+ * @license
67
+ * Copyright 2017 Google LLC
68
+ * SPDX-License-Identifier: BSD-3-Clause
69
+ */const{is:i,defineProperty:e,getOwnPropertyDescriptor:h,getOwnPropertyNames:r$1,getOwnPropertySymbols:o$1,getPrototypeOf:n$1}=Object,a=globalThis,c=a.trustedTypes,l=c?c.emptyScript:"",p=a.reactiveElementPolyfillSupport,d=(t,s)=>t,u={toAttribute(t,s){switch(s){case Boolean:t=t?l:null;break;case Object:case Array:t=null==t?t:JSON.stringify(t);}return t},fromAttribute(t,s){let i=t;switch(s){case Boolean:i=null!==t;break;case Number:i=null===t?null:Number(t);break;case Object:case Array:try{i=JSON.parse(t);}catch(t){i=null;}}return i}},f=(t,s)=>!i(t,s),b={attribute:true,type:String,converter:u,reflect:false,useDefault:false,hasChanged:f};Symbol.metadata??=Symbol("metadata"),a.litPropertyMetadata??=new WeakMap;class y extends HTMLElement{static addInitializer(t){this._$Ei(),(this.l??=[]).push(t);}static get observedAttributes(){return this.finalize(),this._$Eh&&[...this._$Eh.keys()]}static createProperty(t,s=b){if(s.state&&(s.attribute=false),this._$Ei(),this.prototype.hasOwnProperty(t)&&((s=Object.create(s)).wrapped=true),this.elementProperties.set(t,s),!s.noAccessor){const i=Symbol(),h=this.getPropertyDescriptor(t,i,s);void 0!==h&&e(this.prototype,t,h);}}static getPropertyDescriptor(t,s,i){const{get:e,set:r}=h(this.prototype,t)??{get(){return this[s]},set(t){this[s]=t;}};return {get:e,set(s){const h=e?.call(this);r?.call(this,s),this.requestUpdate(t,h,i);},configurable:true,enumerable:true}}static getPropertyOptions(t){return this.elementProperties.get(t)??b}static _$Ei(){if(this.hasOwnProperty(d("elementProperties")))return;const t=n$1(this);t.finalize(),void 0!==t.l&&(this.l=[...t.l]),this.elementProperties=new Map(t.elementProperties);}static finalize(){if(this.hasOwnProperty(d("finalized")))return;if(this.finalized=true,this._$Ei(),this.hasOwnProperty(d("properties"))){const t=this.properties,s=[...r$1(t),...o$1(t)];for(const i of s)this.createProperty(i,t[i]);}const t=this[Symbol.metadata];if(null!==t){const s=litPropertyMetadata.get(t);if(void 0!==s)for(const[t,i]of s)this.elementProperties.set(t,i);}this._$Eh=new Map;for(const[t,s]of this.elementProperties){const i=this._$Eu(t,s);void 0!==i&&this._$Eh.set(i,t);}this.elementStyles=this.finalizeStyles(this.styles);}static finalizeStyles(s){const i=[];if(Array.isArray(s)){const e=new Set(s.flat(1/0).reverse());for(const s of e)i.unshift(c$1(s));}else void 0!==s&&i.push(c$1(s));return i}static _$Eu(t,s){const i=s.attribute;return false===i?void 0:"string"==typeof i?i:"string"==typeof t?t.toLowerCase():void 0}constructor(){super(),this._$Ep=void 0,this.isUpdatePending=false,this.hasUpdated=false,this._$Em=null,this._$Ev();}_$Ev(){this._$ES=new Promise((t=>this.enableUpdating=t)),this._$AL=new Map,this._$E_(),this.requestUpdate(),this.constructor.l?.forEach((t=>t(this)));}addController(t){(this._$EO??=new Set).add(t),void 0!==this.renderRoot&&this.isConnected&&t.hostConnected?.();}removeController(t){this._$EO?.delete(t);}_$E_(){const t=new Map,s=this.constructor.elementProperties;for(const i of s.keys())this.hasOwnProperty(i)&&(t.set(i,this[i]),delete this[i]);t.size>0&&(this._$Ep=t);}createRenderRoot(){const t=this.shadowRoot??this.attachShadow(this.constructor.shadowRootOptions);return S(t,this.constructor.elementStyles),t}connectedCallback(){this.renderRoot??=this.createRenderRoot(),this.enableUpdating(true),this._$EO?.forEach((t=>t.hostConnected?.()));}enableUpdating(t){}disconnectedCallback(){this._$EO?.forEach((t=>t.hostDisconnected?.()));}attributeChangedCallback(t,s,i){this._$AK(t,i);}_$ET(t,s){const i=this.constructor.elementProperties.get(t),e=this.constructor._$Eu(t,i);if(void 0!==e&&true===i.reflect){const h=(void 0!==i.converter?.toAttribute?i.converter:u).toAttribute(s,i.type);this._$Em=t,null==h?this.removeAttribute(e):this.setAttribute(e,h),this._$Em=null;}}_$AK(t,s){const i=this.constructor,e=i._$Eh.get(t);if(void 0!==e&&this._$Em!==e){const t=i.getPropertyOptions(e),h="function"==typeof t.converter?{fromAttribute:t.converter}:void 0!==t.converter?.fromAttribute?t.converter:u;this._$Em=e;const r=h.fromAttribute(s,t.type);this[e]=r??this._$Ej?.get(e)??r,this._$Em=null;}}requestUpdate(t,s,i){if(void 0!==t){const e=this.constructor,h=this[t];if(i??=e.getPropertyOptions(t),!((i.hasChanged??f)(h,s)||i.useDefault&&i.reflect&&h===this._$Ej?.get(t)&&!this.hasAttribute(e._$Eu(t,i))))return;this.C(t,s,i);} false===this.isUpdatePending&&(this._$ES=this._$EP());}C(t,s,{useDefault:i,reflect:e,wrapped:h},r){i&&!(this._$Ej??=new Map).has(t)&&(this._$Ej.set(t,r??s??this[t]),true!==h||void 0!==r)||(this._$AL.has(t)||(this.hasUpdated||i||(s=void 0),this._$AL.set(t,s)),true===e&&this._$Em!==t&&(this._$Eq??=new Set).add(t));}async _$EP(){this.isUpdatePending=true;try{await this._$ES;}catch(t){Promise.reject(t);}const t=this.scheduleUpdate();return null!=t&&await t,!this.isUpdatePending}scheduleUpdate(){return this.performUpdate()}performUpdate(){if(!this.isUpdatePending)return;if(!this.hasUpdated){if(this.renderRoot??=this.createRenderRoot(),this._$Ep){for(const[t,s]of this._$Ep)this[t]=s;this._$Ep=void 0;}const t=this.constructor.elementProperties;if(t.size>0)for(const[s,i]of t){const{wrapped:t}=i,e=this[s];true!==t||this._$AL.has(s)||void 0===e||this.C(s,void 0,i,e);}}let t=false;const s=this._$AL;try{t=this.shouldUpdate(s),t?(this.willUpdate(s),this._$EO?.forEach((t=>t.hostUpdate?.())),this.update(s)):this._$EM();}catch(s){throw t=false,this._$EM(),s}t&&this._$AE(s);}willUpdate(t){}_$AE(t){this._$EO?.forEach((t=>t.hostUpdated?.())),this.hasUpdated||(this.hasUpdated=true,this.firstUpdated(t)),this.updated(t);}_$EM(){this._$AL=new Map,this.isUpdatePending=false;}get updateComplete(){return this.getUpdateComplete()}getUpdateComplete(){return this._$ES}shouldUpdate(t){return true}update(t){this._$Eq&&=this._$Eq.forEach((t=>this._$ET(t,this[t]))),this._$EM();}updated(t){}firstUpdated(t){}}y.elementStyles=[],y.shadowRootOptions={mode:"open"},y[d("elementProperties")]=new Map,y[d("finalized")]=new Map,p?.({ReactiveElement:y}),(a.reactiveElementVersions??=[]).push("2.1.1");
70
+
71
+ /**
72
+ * @license
73
+ * Copyright 2017 Google LLC
74
+ * SPDX-License-Identifier: BSD-3-Clause
75
+ */const o={attribute:true,type:String,converter:u,reflect:false,hasChanged:f},r=(t=o,e,r)=>{const{kind:n,metadata:i}=r;let s=globalThis.litPropertyMetadata.get(i);if(void 0===s&&globalThis.litPropertyMetadata.set(i,s=new Map),"setter"===n&&((t=Object.create(t)).wrapped=true),s.set(r.name,t),"accessor"===n){const{name:o}=r;return {set(r){const n=e.get.call(this);e.set.call(this,r),this.requestUpdate(o,n,t);},init(e){return void 0!==e&&this.C(o,void 0,t,e),e}}}if("setter"===n){const{name:o}=r;return function(r){const n=this[o];e.call(this,r),this.requestUpdate(o,n,t);}}throw Error("Unsupported decorator location: "+n)};function n(t){return (e,o)=>"object"==typeof o?r(t,e,o):((t,e,o)=>{const r=e.hasOwnProperty(o);return e.constructor.createProperty(o,t),r?Object.getOwnPropertyDescriptor(e,o):void 0})(t,e,o)}
76
+
77
+ /**
78
+ * @license
79
+ * Copyright 2021 Google LLC
80
+ *
81
+ * Licensed under the Apache License, Version 2.0 (the "License");
82
+ * you may not use this file except in compliance with the License.
83
+ * You may obtain a copy of the License at
84
+ *
85
+ * http://www.apache.org/licenses/LICENSE-2.0
86
+ *
87
+ * Unless required by applicable law or agreed to in writing, software
88
+ * distributed under the License is distributed on an "AS IS" BASIS,
89
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
90
+ * See the License for the specific language governing permissions and
91
+ * limitations under the License.
92
+ */
93
+ // This file is automatically generated. Do not modify it.
94
+ /**
95
+ * Utility methods for mathematical operations.
96
+ */
97
+ /**
98
+ * The signum function.
99
+ *
100
+ * @return 1 if num > 0, -1 if num < 0, and 0 if num = 0
101
+ */
102
+ function signum(num) {
103
+ if (num < 0) {
104
+ return -1;
105
+ }
106
+ else if (num === 0) {
107
+ return 0;
108
+ }
109
+ else {
110
+ return 1;
111
+ }
112
+ }
113
+ /**
114
+ * The linear interpolation function.
115
+ *
116
+ * @return start if amount = 0 and stop if amount = 1
117
+ */
118
+ function lerp(start, stop, amount) {
119
+ return (1.0 - amount) * start + amount * stop;
120
+ }
121
+ /**
122
+ * Clamps an integer between two integers.
123
+ *
124
+ * @return input when min <= input <= max, and either min or max
125
+ * otherwise.
126
+ */
127
+ function clampInt(min, max, input) {
128
+ if (input < min) {
129
+ return min;
130
+ }
131
+ else if (input > max) {
132
+ return max;
133
+ }
134
+ return input;
135
+ }
136
+ /**
137
+ * Clamps an integer between two floating-point numbers.
138
+ *
139
+ * @return input when min <= input <= max, and either min or max
140
+ * otherwise.
141
+ */
142
+ function clampDouble(min, max, input) {
143
+ if (input < min) {
144
+ return min;
145
+ }
146
+ else if (input > max) {
147
+ return max;
148
+ }
149
+ return input;
150
+ }
151
+ /**
152
+ * Sanitizes a degree measure as a floating-point number.
153
+ *
154
+ * @return a degree measure between 0.0 (inclusive) and 360.0
155
+ * (exclusive).
156
+ */
157
+ function sanitizeDegreesDouble(degrees) {
158
+ degrees = degrees % 360.0;
159
+ if (degrees < 0) {
160
+ degrees = degrees + 360.0;
161
+ }
162
+ return degrees;
163
+ }
164
+ /**
165
+ * Multiplies a 1x3 row vector with a 3x3 matrix.
166
+ */
167
+ function matrixMultiply(row, matrix) {
168
+ const a = row[0] * matrix[0][0] + row[1] * matrix[0][1] + row[2] * matrix[0][2];
169
+ const b = row[0] * matrix[1][0] + row[1] * matrix[1][1] + row[2] * matrix[1][2];
170
+ const c = row[0] * matrix[2][0] + row[1] * matrix[2][1] + row[2] * matrix[2][2];
171
+ return [a, b, c];
172
+ }
173
+
174
+ /**
175
+ * @license
176
+ * Copyright 2021 Google LLC
177
+ *
178
+ * Licensed under the Apache License, Version 2.0 (the "License");
179
+ * you may not use this file except in compliance with the License.
180
+ * You may obtain a copy of the License at
181
+ *
182
+ * http://www.apache.org/licenses/LICENSE-2.0
183
+ *
184
+ * Unless required by applicable law or agreed to in writing, software
185
+ * distributed under the License is distributed on an "AS IS" BASIS,
186
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
187
+ * See the License for the specific language governing permissions and
188
+ * limitations under the License.
189
+ */
190
+ // This file is automatically generated. Do not modify it.
191
+ /**
192
+ * Color science utilities.
193
+ *
194
+ * Utility methods for color science constants and color space
195
+ * conversions that aren't HCT or CAM16.
196
+ */
197
+ const SRGB_TO_XYZ = [
198
+ [0.41233895, 0.35762064, 0.18051042],
199
+ [0.2126, 0.7152, 0.0722],
200
+ [0.01932141, 0.11916382, 0.95034478],
201
+ ];
202
+ const XYZ_TO_SRGB = [
203
+ [
204
+ 3.2413774792388685,
205
+ -1.5376652402851851,
206
+ -0.49885366846268053,
207
+ ],
208
+ [
209
+ -0.9691452513005321,
210
+ 1.8758853451067872,
211
+ 0.04156585616912061,
212
+ ],
213
+ [
214
+ 0.05562093689691305,
215
+ -0.20395524564742123,
216
+ 1.0571799111220335,
217
+ ],
218
+ ];
219
+ const WHITE_POINT_D65 = [95.047, 100.0, 108.883];
220
+ /**
221
+ * Converts a color from RGB components to ARGB format.
222
+ */
223
+ function argbFromRgb(red, green, blue) {
224
+ return (255 << 24 | (red & 255) << 16 | (green & 255) << 8 | blue & 255) >>>
225
+ 0;
226
+ }
227
+ /**
228
+ * Converts a color from linear RGB components to ARGB format.
229
+ */
230
+ function argbFromLinrgb(linrgb) {
231
+ const r = delinearized(linrgb[0]);
232
+ const g = delinearized(linrgb[1]);
233
+ const b = delinearized(linrgb[2]);
234
+ return argbFromRgb(r, g, b);
235
+ }
236
+ /**
237
+ * Returns the red component of a color in ARGB format.
238
+ */
239
+ function redFromArgb(argb) {
240
+ return argb >> 16 & 255;
241
+ }
242
+ /**
243
+ * Returns the green component of a color in ARGB format.
244
+ */
245
+ function greenFromArgb(argb) {
246
+ return argb >> 8 & 255;
247
+ }
248
+ /**
249
+ * Returns the blue component of a color in ARGB format.
250
+ */
251
+ function blueFromArgb(argb) {
252
+ return argb & 255;
253
+ }
254
+ /**
255
+ * Converts a color from ARGB to XYZ.
256
+ */
257
+ function argbFromXyz(x, y, z) {
258
+ const matrix = XYZ_TO_SRGB;
259
+ const linearR = matrix[0][0] * x + matrix[0][1] * y + matrix[0][2] * z;
260
+ const linearG = matrix[1][0] * x + matrix[1][1] * y + matrix[1][2] * z;
261
+ const linearB = matrix[2][0] * x + matrix[2][1] * y + matrix[2][2] * z;
262
+ const r = delinearized(linearR);
263
+ const g = delinearized(linearG);
264
+ const b = delinearized(linearB);
265
+ return argbFromRgb(r, g, b);
266
+ }
267
+ /**
268
+ * Converts a color from XYZ to ARGB.
269
+ */
270
+ function xyzFromArgb(argb) {
271
+ const r = linearized(redFromArgb(argb));
272
+ const g = linearized(greenFromArgb(argb));
273
+ const b = linearized(blueFromArgb(argb));
274
+ return matrixMultiply([r, g, b], SRGB_TO_XYZ);
275
+ }
276
+ /**
277
+ * Converts an L* value to an ARGB representation.
278
+ *
279
+ * @param lstar L* in L*a*b*
280
+ * @return ARGB representation of grayscale color with lightness
281
+ * matching L*
282
+ */
283
+ function argbFromLstar(lstar) {
284
+ const y = yFromLstar(lstar);
285
+ const component = delinearized(y);
286
+ return argbFromRgb(component, component, component);
287
+ }
288
+ /**
289
+ * Computes the L* value of a color in ARGB representation.
290
+ *
291
+ * @param argb ARGB representation of a color
292
+ * @return L*, from L*a*b*, coordinate of the color
293
+ */
294
+ function lstarFromArgb(argb) {
295
+ const y = xyzFromArgb(argb)[1];
296
+ return 116.0 * labF(y / 100.0) - 16.0;
297
+ }
298
+ /**
299
+ * Converts an L* value to a Y value.
300
+ *
301
+ * L* in L*a*b* and Y in XYZ measure the same quantity, luminance.
302
+ *
303
+ * L* measures perceptual luminance, a linear scale. Y in XYZ
304
+ * measures relative luminance, a logarithmic scale.
305
+ *
306
+ * @param lstar L* in L*a*b*
307
+ * @return Y in XYZ
308
+ */
309
+ function yFromLstar(lstar) {
310
+ return 100.0 * labInvf((lstar + 16.0) / 116.0);
311
+ }
312
+ /**
313
+ * Converts a Y value to an L* value.
314
+ *
315
+ * L* in L*a*b* and Y in XYZ measure the same quantity, luminance.
316
+ *
317
+ * L* measures perceptual luminance, a linear scale. Y in XYZ
318
+ * measures relative luminance, a logarithmic scale.
319
+ *
320
+ * @param y Y in XYZ
321
+ * @return L* in L*a*b*
322
+ */
323
+ function lstarFromY(y) {
324
+ return labF(y / 100.0) * 116.0 - 16.0;
325
+ }
326
+ /**
327
+ * Linearizes an RGB component.
328
+ *
329
+ * @param rgbComponent 0 <= rgb_component <= 255, represents R/G/B
330
+ * channel
331
+ * @return 0.0 <= output <= 100.0, color channel converted to
332
+ * linear RGB space
333
+ */
334
+ function linearized(rgbComponent) {
335
+ const normalized = rgbComponent / 255.0;
336
+ if (normalized <= 0.040449936) {
337
+ return normalized / 12.92 * 100.0;
338
+ }
339
+ else {
340
+ return Math.pow((normalized + 0.055) / 1.055, 2.4) * 100.0;
341
+ }
342
+ }
343
+ /**
344
+ * Delinearizes an RGB component.
345
+ *
346
+ * @param rgbComponent 0.0 <= rgb_component <= 100.0, represents
347
+ * linear R/G/B channel
348
+ * @return 0 <= output <= 255, color channel converted to regular
349
+ * RGB space
350
+ */
351
+ function delinearized(rgbComponent) {
352
+ const normalized = rgbComponent / 100.0;
353
+ let delinearized = 0.0;
354
+ if (normalized <= 0.0031308) {
355
+ delinearized = normalized * 12.92;
356
+ }
357
+ else {
358
+ delinearized = 1.055 * Math.pow(normalized, 1.0 / 2.4) - 0.055;
359
+ }
360
+ return clampInt(0, 255, Math.round(delinearized * 255.0));
361
+ }
362
+ /**
363
+ * Returns the standard white point; white on a sunny day.
364
+ *
365
+ * @return The white point
366
+ */
367
+ function whitePointD65() {
368
+ return WHITE_POINT_D65;
369
+ }
370
+ function labF(t) {
371
+ const e = 216.0 / 24389.0;
372
+ const kappa = 24389.0 / 27.0;
373
+ if (t > e) {
374
+ return Math.pow(t, 1.0 / 3.0);
375
+ }
376
+ else {
377
+ return (kappa * t + 16) / 116;
378
+ }
379
+ }
380
+ function labInvf(ft) {
381
+ const e = 216.0 / 24389.0;
382
+ const kappa = 24389.0 / 27.0;
383
+ const ft3 = ft * ft * ft;
384
+ if (ft3 > e) {
385
+ return ft3;
386
+ }
387
+ else {
388
+ return (116 * ft - 16) / kappa;
389
+ }
390
+ }
391
+
392
+ /**
393
+ * @license
394
+ * Copyright 2021 Google LLC
395
+ *
396
+ * Licensed under the Apache License, Version 2.0 (the "License");
397
+ * you may not use this file except in compliance with the License.
398
+ * You may obtain a copy of the License at
399
+ *
400
+ * http://www.apache.org/licenses/LICENSE-2.0
401
+ *
402
+ * Unless required by applicable law or agreed to in writing, software
403
+ * distributed under the License is distributed on an "AS IS" BASIS,
404
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
405
+ * See the License for the specific language governing permissions and
406
+ * limitations under the License.
407
+ */
408
+ /**
409
+ * In traditional color spaces, a color can be identified solely by the
410
+ * observer's measurement of the color. Color appearance models such as CAM16
411
+ * also use information about the environment where the color was
412
+ * observed, known as the viewing conditions.
413
+ *
414
+ * For example, white under the traditional assumption of a midday sun white
415
+ * point is accurately measured as a slightly chromatic blue by CAM16. (roughly,
416
+ * hue 203, chroma 3, lightness 100)
417
+ *
418
+ * This class caches intermediate values of the CAM16 conversion process that
419
+ * depend only on viewing conditions, enabling speed ups.
420
+ */
421
+ class ViewingConditions {
422
+ /**
423
+ * Create ViewingConditions from a simple, physically relevant, set of
424
+ * parameters.
425
+ *
426
+ * @param whitePoint White point, measured in the XYZ color space.
427
+ * default = D65, or sunny day afternoon
428
+ * @param adaptingLuminance The luminance of the adapting field. Informally,
429
+ * how bright it is in the room where the color is viewed. Can be
430
+ * calculated from lux by multiplying lux by 0.0586. default = 11.72,
431
+ * or 200 lux.
432
+ * @param backgroundLstar The lightness of the area surrounding the color.
433
+ * measured by L* in L*a*b*. default = 50.0
434
+ * @param surround A general description of the lighting surrounding the
435
+ * color. 0 is pitch dark, like watching a movie in a theater. 1.0 is a
436
+ * dimly light room, like watching TV at home at night. 2.0 means there
437
+ * is no difference between the lighting on the color and around it.
438
+ * default = 2.0
439
+ * @param discountingIlluminant Whether the eye accounts for the tint of the
440
+ * ambient lighting, such as knowing an apple is still red in green light.
441
+ * default = false, the eye does not perform this process on
442
+ * self-luminous objects like displays.
443
+ */
444
+ static make(whitePoint = whitePointD65(), adaptingLuminance = (200.0 / Math.PI) * yFromLstar(50.0) / 100.0, backgroundLstar = 50.0, surround = 2.0, discountingIlluminant = false) {
445
+ const xyz = whitePoint;
446
+ const rW = xyz[0] * 0.401288 + xyz[1] * 0.650173 + xyz[2] * -0.051461;
447
+ const gW = xyz[0] * -0.250268 + xyz[1] * 1.204414 + xyz[2] * 0.045854;
448
+ const bW = xyz[0] * -2079e-6 + xyz[1] * 0.048952 + xyz[2] * 0.953127;
449
+ const f = 0.8 + surround / 10.0;
450
+ const c = f >= 0.9 ? lerp(0.59, 0.69, (f - 0.9) * 10.0) :
451
+ lerp(0.525, 0.59, (f - 0.8) * 10.0);
452
+ let d = discountingIlluminant ?
453
+ 1.0 :
454
+ f * (1.0 - (1.0 / 3.6) * Math.exp((-adaptingLuminance - 42.0) / 92.0));
455
+ d = d > 1.0 ? 1.0 : d < 0.0 ? 0.0 : d;
456
+ const nc = f;
457
+ const rgbD = [
458
+ d * (100.0 / rW) + 1.0 - d,
459
+ d * (100.0 / gW) + 1.0 - d,
460
+ d * (100.0 / bW) + 1.0 - d,
461
+ ];
462
+ const k = 1.0 / (5.0 * adaptingLuminance + 1.0);
463
+ const k4 = k * k * k * k;
464
+ const k4F = 1.0 - k4;
465
+ const fl = k4 * adaptingLuminance +
466
+ 0.1 * k4F * k4F * Math.cbrt(5.0 * adaptingLuminance);
467
+ const n = yFromLstar(backgroundLstar) / whitePoint[1];
468
+ const z = 1.48 + Math.sqrt(n);
469
+ const nbb = 0.725 / Math.pow(n, 0.2);
470
+ const ncb = nbb;
471
+ const rgbAFactors = [
472
+ Math.pow((fl * rgbD[0] * rW) / 100.0, 0.42),
473
+ Math.pow((fl * rgbD[1] * gW) / 100.0, 0.42),
474
+ Math.pow((fl * rgbD[2] * bW) / 100.0, 0.42),
475
+ ];
476
+ const rgbA = [
477
+ (400.0 * rgbAFactors[0]) / (rgbAFactors[0] + 27.13),
478
+ (400.0 * rgbAFactors[1]) / (rgbAFactors[1] + 27.13),
479
+ (400.0 * rgbAFactors[2]) / (rgbAFactors[2] + 27.13),
480
+ ];
481
+ const aw = (2.0 * rgbA[0] + rgbA[1] + 0.05 * rgbA[2]) * nbb;
482
+ return new ViewingConditions(n, aw, nbb, ncb, c, nc, rgbD, fl, Math.pow(fl, 0.25), z);
483
+ }
484
+ /**
485
+ * Parameters are intermediate values of the CAM16 conversion process. Their
486
+ * names are shorthand for technical color science terminology, this class
487
+ * would not benefit from documenting them individually. A brief overview
488
+ * is available in the CAM16 specification, and a complete overview requires
489
+ * a color science textbook, such as Fairchild's Color Appearance Models.
490
+ */
491
+ constructor(n, aw, nbb, ncb, c, nc, rgbD, fl, fLRoot, z) {
492
+ this.n = n;
493
+ this.aw = aw;
494
+ this.nbb = nbb;
495
+ this.ncb = ncb;
496
+ this.c = c;
497
+ this.nc = nc;
498
+ this.rgbD = rgbD;
499
+ this.fl = fl;
500
+ this.fLRoot = fLRoot;
501
+ this.z = z;
502
+ }
503
+ }
504
+ /** sRGB-like viewing conditions. */
505
+ ViewingConditions.DEFAULT = ViewingConditions.make();
506
+
507
+ /**
508
+ * @license
509
+ * Copyright 2021 Google LLC
510
+ *
511
+ * Licensed under the Apache License, Version 2.0 (the "License");
512
+ * you may not use this file except in compliance with the License.
513
+ * You may obtain a copy of the License at
514
+ *
515
+ * http://www.apache.org/licenses/LICENSE-2.0
516
+ *
517
+ * Unless required by applicable law or agreed to in writing, software
518
+ * distributed under the License is distributed on an "AS IS" BASIS,
519
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
520
+ * See the License for the specific language governing permissions and
521
+ * limitations under the License.
522
+ */
523
+ /**
524
+ * CAM16, a color appearance model. Colors are not just defined by their hex
525
+ * code, but rather, a hex code and viewing conditions.
526
+ *
527
+ * CAM16 instances also have coordinates in the CAM16-UCS space, called J*, a*,
528
+ * b*, or jstar, astar, bstar in code. CAM16-UCS is included in the CAM16
529
+ * specification, and should be used when measuring distances between colors.
530
+ *
531
+ * In traditional color spaces, a color can be identified solely by the
532
+ * observer's measurement of the color. Color appearance models such as CAM16
533
+ * also use information about the environment where the color was
534
+ * observed, known as the viewing conditions.
535
+ *
536
+ * For example, white under the traditional assumption of a midday sun white
537
+ * point is accurately measured as a slightly chromatic blue by CAM16. (roughly,
538
+ * hue 203, chroma 3, lightness 100)
539
+ */
540
+ class Cam16 {
541
+ /**
542
+ * All of the CAM16 dimensions can be calculated from 3 of the dimensions, in
543
+ * the following combinations:
544
+ * - {j or q} and {c, m, or s} and hue
545
+ * - jstar, astar, bstar
546
+ * Prefer using a static method that constructs from 3 of those dimensions.
547
+ * This constructor is intended for those methods to use to return all
548
+ * possible dimensions.
549
+ *
550
+ * @param hue
551
+ * @param chroma informally, colorfulness / color intensity. like saturation
552
+ * in HSL, except perceptually accurate.
553
+ * @param j lightness
554
+ * @param q brightness; ratio of lightness to white point's lightness
555
+ * @param m colorfulness
556
+ * @param s saturation; ratio of chroma to white point's chroma
557
+ * @param jstar CAM16-UCS J coordinate
558
+ * @param astar CAM16-UCS a coordinate
559
+ * @param bstar CAM16-UCS b coordinate
560
+ */
561
+ constructor(hue, chroma, j, q, m, s, jstar, astar, bstar) {
562
+ this.hue = hue;
563
+ this.chroma = chroma;
564
+ this.j = j;
565
+ this.q = q;
566
+ this.m = m;
567
+ this.s = s;
568
+ this.jstar = jstar;
569
+ this.astar = astar;
570
+ this.bstar = bstar;
571
+ }
572
+ /**
573
+ * CAM16 instances also have coordinates in the CAM16-UCS space, called J*,
574
+ * a*, b*, or jstar, astar, bstar in code. CAM16-UCS is included in the CAM16
575
+ * specification, and is used to measure distances between colors.
576
+ */
577
+ distance(other) {
578
+ const dJ = this.jstar - other.jstar;
579
+ const dA = this.astar - other.astar;
580
+ const dB = this.bstar - other.bstar;
581
+ const dEPrime = Math.sqrt(dJ * dJ + dA * dA + dB * dB);
582
+ const dE = 1.41 * Math.pow(dEPrime, 0.63);
583
+ return dE;
584
+ }
585
+ /**
586
+ * @param argb ARGB representation of a color.
587
+ * @return CAM16 color, assuming the color was viewed in default viewing
588
+ * conditions.
589
+ */
590
+ static fromInt(argb) {
591
+ return Cam16.fromIntInViewingConditions(argb, ViewingConditions.DEFAULT);
592
+ }
593
+ /**
594
+ * @param argb ARGB representation of a color.
595
+ * @param viewingConditions Information about the environment where the color
596
+ * was observed.
597
+ * @return CAM16 color.
598
+ */
599
+ static fromIntInViewingConditions(argb, viewingConditions) {
600
+ const red = (argb & 0x00ff0000) >> 16;
601
+ const green = (argb & 0x0000ff00) >> 8;
602
+ const blue = (argb & 0x000000ff);
603
+ const redL = linearized(red);
604
+ const greenL = linearized(green);
605
+ const blueL = linearized(blue);
606
+ const x = 0.41233895 * redL + 0.35762064 * greenL + 0.18051042 * blueL;
607
+ const y = 0.2126 * redL + 0.7152 * greenL + 0.0722 * blueL;
608
+ const z = 0.01932141 * redL + 0.11916382 * greenL + 0.95034478 * blueL;
609
+ const rC = 0.401288 * x + 0.650173 * y - 0.051461 * z;
610
+ const gC = -0.250268 * x + 1.204414 * y + 0.045854 * z;
611
+ const bC = -2079e-6 * x + 0.048952 * y + 0.953127 * z;
612
+ const rD = viewingConditions.rgbD[0] * rC;
613
+ const gD = viewingConditions.rgbD[1] * gC;
614
+ const bD = viewingConditions.rgbD[2] * bC;
615
+ const rAF = Math.pow((viewingConditions.fl * Math.abs(rD)) / 100.0, 0.42);
616
+ const gAF = Math.pow((viewingConditions.fl * Math.abs(gD)) / 100.0, 0.42);
617
+ const bAF = Math.pow((viewingConditions.fl * Math.abs(bD)) / 100.0, 0.42);
618
+ const rA = (signum(rD) * 400.0 * rAF) / (rAF + 27.13);
619
+ const gA = (signum(gD) * 400.0 * gAF) / (gAF + 27.13);
620
+ const bA = (signum(bD) * 400.0 * bAF) / (bAF + 27.13);
621
+ const a = (11.0 * rA + -12 * gA + bA) / 11.0;
622
+ const b = (rA + gA - 2.0 * bA) / 9.0;
623
+ const u = (20.0 * rA + 20.0 * gA + 21.0 * bA) / 20.0;
624
+ const p2 = (40.0 * rA + 20.0 * gA + bA) / 20.0;
625
+ const atan2 = Math.atan2(b, a);
626
+ const atanDegrees = (atan2 * 180.0) / Math.PI;
627
+ const hue = atanDegrees < 0 ? atanDegrees + 360.0 :
628
+ atanDegrees >= 360 ? atanDegrees - 360.0 :
629
+ atanDegrees;
630
+ const hueRadians = (hue * Math.PI) / 180.0;
631
+ const ac = p2 * viewingConditions.nbb;
632
+ const j = 100.0 *
633
+ Math.pow(ac / viewingConditions.aw, viewingConditions.c * viewingConditions.z);
634
+ const q = (4.0 / viewingConditions.c) * Math.sqrt(j / 100.0) *
635
+ (viewingConditions.aw + 4.0) * viewingConditions.fLRoot;
636
+ const huePrime = hue < 20.14 ? hue + 360 : hue;
637
+ const eHue = 0.25 * (Math.cos((huePrime * Math.PI) / 180.0 + 2.0) + 3.8);
638
+ const p1 = (50000.0 / 13.0) * eHue * viewingConditions.nc * viewingConditions.ncb;
639
+ const t = (p1 * Math.sqrt(a * a + b * b)) / (u + 0.305);
640
+ const alpha = Math.pow(t, 0.9) *
641
+ Math.pow(1.64 - Math.pow(0.29, viewingConditions.n), 0.73);
642
+ const c = alpha * Math.sqrt(j / 100.0);
643
+ const m = c * viewingConditions.fLRoot;
644
+ const s = 50.0 *
645
+ Math.sqrt((alpha * viewingConditions.c) / (viewingConditions.aw + 4.0));
646
+ const jstar = ((1.0 + 100.0 * 0.007) * j) / (1.0 + 0.007 * j);
647
+ const mstar = (1.0 / 0.0228) * Math.log(1.0 + 0.0228 * m);
648
+ const astar = mstar * Math.cos(hueRadians);
649
+ const bstar = mstar * Math.sin(hueRadians);
650
+ return new Cam16(hue, c, j, q, m, s, jstar, astar, bstar);
651
+ }
652
+ /**
653
+ * @param j CAM16 lightness
654
+ * @param c CAM16 chroma
655
+ * @param h CAM16 hue
656
+ */
657
+ static fromJch(j, c, h) {
658
+ return Cam16.fromJchInViewingConditions(j, c, h, ViewingConditions.DEFAULT);
659
+ }
660
+ /**
661
+ * @param j CAM16 lightness
662
+ * @param c CAM16 chroma
663
+ * @param h CAM16 hue
664
+ * @param viewingConditions Information about the environment where the color
665
+ * was observed.
666
+ */
667
+ static fromJchInViewingConditions(j, c, h, viewingConditions) {
668
+ const q = (4.0 / viewingConditions.c) * Math.sqrt(j / 100.0) *
669
+ (viewingConditions.aw + 4.0) * viewingConditions.fLRoot;
670
+ const m = c * viewingConditions.fLRoot;
671
+ const alpha = c / Math.sqrt(j / 100.0);
672
+ const s = 50.0 *
673
+ Math.sqrt((alpha * viewingConditions.c) / (viewingConditions.aw + 4.0));
674
+ const hueRadians = (h * Math.PI) / 180.0;
675
+ const jstar = ((1.0 + 100.0 * 0.007) * j) / (1.0 + 0.007 * j);
676
+ const mstar = (1.0 / 0.0228) * Math.log(1.0 + 0.0228 * m);
677
+ const astar = mstar * Math.cos(hueRadians);
678
+ const bstar = mstar * Math.sin(hueRadians);
679
+ return new Cam16(h, c, j, q, m, s, jstar, astar, bstar);
680
+ }
681
+ /**
682
+ * @param jstar CAM16-UCS lightness.
683
+ * @param astar CAM16-UCS a dimension. Like a* in L*a*b*, it is a Cartesian
684
+ * coordinate on the Y axis.
685
+ * @param bstar CAM16-UCS b dimension. Like a* in L*a*b*, it is a Cartesian
686
+ * coordinate on the X axis.
687
+ */
688
+ static fromUcs(jstar, astar, bstar) {
689
+ return Cam16.fromUcsInViewingConditions(jstar, astar, bstar, ViewingConditions.DEFAULT);
690
+ }
691
+ /**
692
+ * @param jstar CAM16-UCS lightness.
693
+ * @param astar CAM16-UCS a dimension. Like a* in L*a*b*, it is a Cartesian
694
+ * coordinate on the Y axis.
695
+ * @param bstar CAM16-UCS b dimension. Like a* in L*a*b*, it is a Cartesian
696
+ * coordinate on the X axis.
697
+ * @param viewingConditions Information about the environment where the color
698
+ * was observed.
699
+ */
700
+ static fromUcsInViewingConditions(jstar, astar, bstar, viewingConditions) {
701
+ const a = astar;
702
+ const b = bstar;
703
+ const m = Math.sqrt(a * a + b * b);
704
+ const M = (Math.exp(m * 0.0228) - 1.0) / 0.0228;
705
+ const c = M / viewingConditions.fLRoot;
706
+ let h = Math.atan2(b, a) * (180.0 / Math.PI);
707
+ if (h < 0.0) {
708
+ h += 360.0;
709
+ }
710
+ const j = jstar / (1 - (jstar - 100) * 0.007);
711
+ return Cam16.fromJchInViewingConditions(j, c, h, viewingConditions);
712
+ }
713
+ /**
714
+ * @return ARGB representation of color, assuming the color was viewed in
715
+ * default viewing conditions, which are near-identical to the default
716
+ * viewing conditions for sRGB.
717
+ */
718
+ toInt() {
719
+ return this.viewed(ViewingConditions.DEFAULT);
720
+ }
721
+ /**
722
+ * @param viewingConditions Information about the environment where the color
723
+ * will be viewed.
724
+ * @return ARGB representation of color
725
+ */
726
+ viewed(viewingConditions) {
727
+ const alpha = this.chroma === 0.0 || this.j === 0.0 ?
728
+ 0.0 :
729
+ this.chroma / Math.sqrt(this.j / 100.0);
730
+ const t = Math.pow(alpha / Math.pow(1.64 - Math.pow(0.29, viewingConditions.n), 0.73), 1.0 / 0.9);
731
+ const hRad = (this.hue * Math.PI) / 180.0;
732
+ const eHue = 0.25 * (Math.cos(hRad + 2.0) + 3.8);
733
+ const ac = viewingConditions.aw *
734
+ Math.pow(this.j / 100.0, 1.0 / viewingConditions.c / viewingConditions.z);
735
+ const p1 = eHue * (50000.0 / 13.0) * viewingConditions.nc * viewingConditions.ncb;
736
+ const p2 = ac / viewingConditions.nbb;
737
+ const hSin = Math.sin(hRad);
738
+ const hCos = Math.cos(hRad);
739
+ const gamma = (23.0 * (p2 + 0.305) * t) /
740
+ (23.0 * p1 + 11.0 * t * hCos + 108.0 * t * hSin);
741
+ const a = gamma * hCos;
742
+ const b = gamma * hSin;
743
+ const rA = (460.0 * p2 + 451.0 * a + 288.0 * b) / 1403.0;
744
+ const gA = (460.0 * p2 - 891.0 * a - 261.0 * b) / 1403.0;
745
+ const bA = (460.0 * p2 - 220.0 * a - 6300.0 * b) / 1403.0;
746
+ const rCBase = Math.max(0, (27.13 * Math.abs(rA)) / (400.0 - Math.abs(rA)));
747
+ const rC = signum(rA) * (100.0 / viewingConditions.fl) *
748
+ Math.pow(rCBase, 1.0 / 0.42);
749
+ const gCBase = Math.max(0, (27.13 * Math.abs(gA)) / (400.0 - Math.abs(gA)));
750
+ const gC = signum(gA) * (100.0 / viewingConditions.fl) *
751
+ Math.pow(gCBase, 1.0 / 0.42);
752
+ const bCBase = Math.max(0, (27.13 * Math.abs(bA)) / (400.0 - Math.abs(bA)));
753
+ const bC = signum(bA) * (100.0 / viewingConditions.fl) *
754
+ Math.pow(bCBase, 1.0 / 0.42);
755
+ const rF = rC / viewingConditions.rgbD[0];
756
+ const gF = gC / viewingConditions.rgbD[1];
757
+ const bF = bC / viewingConditions.rgbD[2];
758
+ const x = 1.86206786 * rF - 1.01125463 * gF + 0.14918677 * bF;
759
+ const y = 0.38752654 * rF + 0.62144744 * gF - 0.00897398 * bF;
760
+ const z = -0.0158415 * rF - 0.03412294 * gF + 1.04996444 * bF;
761
+ const argb = argbFromXyz(x, y, z);
762
+ return argb;
763
+ }
764
+ /// Given color expressed in XYZ and viewed in [viewingConditions], convert to
765
+ /// CAM16.
766
+ static fromXyzInViewingConditions(x, y, z, viewingConditions) {
767
+ // Transform XYZ to 'cone'/'rgb' responses
768
+ const rC = 0.401288 * x + 0.650173 * y - 0.051461 * z;
769
+ const gC = -0.250268 * x + 1.204414 * y + 0.045854 * z;
770
+ const bC = -2079e-6 * x + 0.048952 * y + 0.953127 * z;
771
+ // Discount illuminant
772
+ const rD = viewingConditions.rgbD[0] * rC;
773
+ const gD = viewingConditions.rgbD[1] * gC;
774
+ const bD = viewingConditions.rgbD[2] * bC;
775
+ // chromatic adaptation
776
+ const rAF = Math.pow(viewingConditions.fl * Math.abs(rD) / 100.0, 0.42);
777
+ const gAF = Math.pow(viewingConditions.fl * Math.abs(gD) / 100.0, 0.42);
778
+ const bAF = Math.pow(viewingConditions.fl * Math.abs(bD) / 100.0, 0.42);
779
+ const rA = signum(rD) * 400.0 * rAF / (rAF + 27.13);
780
+ const gA = signum(gD) * 400.0 * gAF / (gAF + 27.13);
781
+ const bA = signum(bD) * 400.0 * bAF / (bAF + 27.13);
782
+ // redness-greenness
783
+ const a = (11.0 * rA + -12 * gA + bA) / 11.0;
784
+ // yellowness-blueness
785
+ const b = (rA + gA - 2.0 * bA) / 9.0;
786
+ // auxiliary components
787
+ const u = (20.0 * rA + 20.0 * gA + 21.0 * bA) / 20.0;
788
+ const p2 = (40.0 * rA + 20.0 * gA + bA) / 20.0;
789
+ // hue
790
+ const atan2 = Math.atan2(b, a);
791
+ const atanDegrees = atan2 * 180.0 / Math.PI;
792
+ const hue = atanDegrees < 0 ? atanDegrees + 360.0 :
793
+ atanDegrees >= 360 ? atanDegrees - 360 :
794
+ atanDegrees;
795
+ const hueRadians = hue * Math.PI / 180.0;
796
+ // achromatic response to color
797
+ const ac = p2 * viewingConditions.nbb;
798
+ // CAM16 lightness and brightness
799
+ const J = 100.0 *
800
+ Math.pow(ac / viewingConditions.aw, viewingConditions.c * viewingConditions.z);
801
+ const Q = (4.0 / viewingConditions.c) * Math.sqrt(J / 100.0) *
802
+ (viewingConditions.aw + 4.0) * (viewingConditions.fLRoot);
803
+ const huePrime = (hue < 20.14) ? hue + 360 : hue;
804
+ const eHue = (1.0 / 4.0) * (Math.cos(huePrime * Math.PI / 180.0 + 2.0) + 3.8);
805
+ const p1 = 50000.0 / 13.0 * eHue * viewingConditions.nc * viewingConditions.ncb;
806
+ const t = p1 * Math.sqrt(a * a + b * b) / (u + 0.305);
807
+ const alpha = Math.pow(t, 0.9) *
808
+ Math.pow(1.64 - Math.pow(0.29, viewingConditions.n), 0.73);
809
+ // CAM16 chroma, colorfulness, chroma
810
+ const C = alpha * Math.sqrt(J / 100.0);
811
+ const M = C * viewingConditions.fLRoot;
812
+ const s = 50.0 *
813
+ Math.sqrt((alpha * viewingConditions.c) / (viewingConditions.aw + 4.0));
814
+ // CAM16-UCS components
815
+ const jstar = (1.0 + 100.0 * 0.007) * J / (1.0 + 0.007 * J);
816
+ const mstar = Math.log(1.0 + 0.0228 * M) / 0.0228;
817
+ const astar = mstar * Math.cos(hueRadians);
818
+ const bstar = mstar * Math.sin(hueRadians);
819
+ return new Cam16(hue, C, J, Q, M, s, jstar, astar, bstar);
820
+ }
821
+ /// XYZ representation of CAM16 seen in [viewingConditions].
822
+ xyzInViewingConditions(viewingConditions) {
823
+ const alpha = (this.chroma === 0.0 || this.j === 0.0) ?
824
+ 0.0 :
825
+ this.chroma / Math.sqrt(this.j / 100.0);
826
+ const t = Math.pow(alpha / Math.pow(1.64 - Math.pow(0.29, viewingConditions.n), 0.73), 1.0 / 0.9);
827
+ const hRad = this.hue * Math.PI / 180.0;
828
+ const eHue = 0.25 * (Math.cos(hRad + 2.0) + 3.8);
829
+ const ac = viewingConditions.aw *
830
+ Math.pow(this.j / 100.0, 1.0 / viewingConditions.c / viewingConditions.z);
831
+ const p1 = eHue * (50000.0 / 13.0) * viewingConditions.nc * viewingConditions.ncb;
832
+ const p2 = (ac / viewingConditions.nbb);
833
+ const hSin = Math.sin(hRad);
834
+ const hCos = Math.cos(hRad);
835
+ const gamma = 23.0 * (p2 + 0.305) * t /
836
+ (23.0 * p1 + 11 * t * hCos + 108.0 * t * hSin);
837
+ const a = gamma * hCos;
838
+ const b = gamma * hSin;
839
+ const rA = (460.0 * p2 + 451.0 * a + 288.0 * b) / 1403.0;
840
+ const gA = (460.0 * p2 - 891.0 * a - 261.0 * b) / 1403.0;
841
+ const bA = (460.0 * p2 - 220.0 * a - 6300.0 * b) / 1403.0;
842
+ const rCBase = Math.max(0, (27.13 * Math.abs(rA)) / (400.0 - Math.abs(rA)));
843
+ const rC = signum(rA) * (100.0 / viewingConditions.fl) *
844
+ Math.pow(rCBase, 1.0 / 0.42);
845
+ const gCBase = Math.max(0, (27.13 * Math.abs(gA)) / (400.0 - Math.abs(gA)));
846
+ const gC = signum(gA) * (100.0 / viewingConditions.fl) *
847
+ Math.pow(gCBase, 1.0 / 0.42);
848
+ const bCBase = Math.max(0, (27.13 * Math.abs(bA)) / (400.0 - Math.abs(bA)));
849
+ const bC = signum(bA) * (100.0 / viewingConditions.fl) *
850
+ Math.pow(bCBase, 1.0 / 0.42);
851
+ const rF = rC / viewingConditions.rgbD[0];
852
+ const gF = gC / viewingConditions.rgbD[1];
853
+ const bF = bC / viewingConditions.rgbD[2];
854
+ const x = 1.86206786 * rF - 1.01125463 * gF + 0.14918677 * bF;
855
+ const y = 0.38752654 * rF + 0.62144744 * gF - 0.00897398 * bF;
856
+ const z = -0.0158415 * rF - 0.03412294 * gF + 1.04996444 * bF;
857
+ return [x, y, z];
858
+ }
859
+ }
860
+
861
+ /**
862
+ * @license
863
+ * Copyright 2021 Google LLC
864
+ *
865
+ * Licensed under the Apache License, Version 2.0 (the "License");
866
+ * you may not use this file except in compliance with the License.
867
+ * You may obtain a copy of the License at
868
+ *
869
+ * http://www.apache.org/licenses/LICENSE-2.0
870
+ *
871
+ * Unless required by applicable law or agreed to in writing, software
872
+ * distributed under the License is distributed on an "AS IS" BASIS,
873
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
874
+ * See the License for the specific language governing permissions and
875
+ * limitations under the License.
876
+ */
877
+ // This file is automatically generated. Do not modify it.
878
+ // material_color_utilities is designed to have a consistent API across
879
+ // platforms and modular components that can be moved around easily. Using a
880
+ // class as a namespace facilitates this.
881
+ //
882
+ // tslint:disable:class-as-namespace
883
+ /**
884
+ * A class that solves the HCT equation.
885
+ */
886
+ class HctSolver {
887
+ /**
888
+ * Sanitizes a small enough angle in radians.
889
+ *
890
+ * @param angle An angle in radians; must not deviate too much
891
+ * from 0.
892
+ * @return A coterminal angle between 0 and 2pi.
893
+ */
894
+ static sanitizeRadians(angle) {
895
+ return (angle + Math.PI * 8) % (Math.PI * 2);
896
+ }
897
+ /**
898
+ * Delinearizes an RGB component, returning a floating-point
899
+ * number.
900
+ *
901
+ * @param rgbComponent 0.0 <= rgb_component <= 100.0, represents
902
+ * linear R/G/B channel
903
+ * @return 0.0 <= output <= 255.0, color channel converted to
904
+ * regular RGB space
905
+ */
906
+ static trueDelinearized(rgbComponent) {
907
+ const normalized = rgbComponent / 100.0;
908
+ let delinearized = 0.0;
909
+ if (normalized <= 0.0031308) {
910
+ delinearized = normalized * 12.92;
911
+ }
912
+ else {
913
+ delinearized = 1.055 * Math.pow(normalized, 1.0 / 2.4) - 0.055;
914
+ }
915
+ return delinearized * 255.0;
916
+ }
917
+ static chromaticAdaptation(component) {
918
+ const af = Math.pow(Math.abs(component), 0.42);
919
+ return signum(component) * 400.0 * af / (af + 27.13);
920
+ }
921
+ /**
922
+ * Returns the hue of a linear RGB color in CAM16.
923
+ *
924
+ * @param linrgb The linear RGB coordinates of a color.
925
+ * @return The hue of the color in CAM16, in radians.
926
+ */
927
+ static hueOf(linrgb) {
928
+ const scaledDiscount = matrixMultiply(linrgb, HctSolver.SCALED_DISCOUNT_FROM_LINRGB);
929
+ const rA = HctSolver.chromaticAdaptation(scaledDiscount[0]);
930
+ const gA = HctSolver.chromaticAdaptation(scaledDiscount[1]);
931
+ const bA = HctSolver.chromaticAdaptation(scaledDiscount[2]);
932
+ // redness-greenness
933
+ const a = (11.0 * rA + -12 * gA + bA) / 11.0;
934
+ // yellowness-blueness
935
+ const b = (rA + gA - 2.0 * bA) / 9.0;
936
+ return Math.atan2(b, a);
937
+ }
938
+ static areInCyclicOrder(a, b, c) {
939
+ const deltaAB = HctSolver.sanitizeRadians(b - a);
940
+ const deltaAC = HctSolver.sanitizeRadians(c - a);
941
+ return deltaAB < deltaAC;
942
+ }
943
+ /**
944
+ * Solves the lerp equation.
945
+ *
946
+ * @param source The starting number.
947
+ * @param mid The number in the middle.
948
+ * @param target The ending number.
949
+ * @return A number t such that lerp(source, target, t) = mid.
950
+ */
951
+ static intercept(source, mid, target) {
952
+ return (mid - source) / (target - source);
953
+ }
954
+ static lerpPoint(source, t, target) {
955
+ return [
956
+ source[0] + (target[0] - source[0]) * t,
957
+ source[1] + (target[1] - source[1]) * t,
958
+ source[2] + (target[2] - source[2]) * t,
959
+ ];
960
+ }
961
+ /**
962
+ * Intersects a segment with a plane.
963
+ *
964
+ * @param source The coordinates of point A.
965
+ * @param coordinate The R-, G-, or B-coordinate of the plane.
966
+ * @param target The coordinates of point B.
967
+ * @param axis The axis the plane is perpendicular with. (0: R, 1:
968
+ * G, 2: B)
969
+ * @return The intersection point of the segment AB with the plane
970
+ * R=coordinate, G=coordinate, or B=coordinate
971
+ */
972
+ static setCoordinate(source, coordinate, target, axis) {
973
+ const t = HctSolver.intercept(source[axis], coordinate, target[axis]);
974
+ return HctSolver.lerpPoint(source, t, target);
975
+ }
976
+ static isBounded(x) {
977
+ return 0.0 <= x && x <= 100.0;
978
+ }
979
+ /**
980
+ * Returns the nth possible vertex of the polygonal intersection.
981
+ *
982
+ * @param y The Y value of the plane.
983
+ * @param n The zero-based index of the point. 0 <= n <= 11.
984
+ * @return The nth possible vertex of the polygonal intersection
985
+ * of the y plane and the RGB cube, in linear RGB coordinates, if
986
+ * it exists. If this possible vertex lies outside of the cube,
987
+ * [-1.0, -1.0, -1.0] is returned.
988
+ */
989
+ static nthVertex(y, n) {
990
+ const kR = HctSolver.Y_FROM_LINRGB[0];
991
+ const kG = HctSolver.Y_FROM_LINRGB[1];
992
+ const kB = HctSolver.Y_FROM_LINRGB[2];
993
+ const coordA = n % 4 <= 1 ? 0.0 : 100.0;
994
+ const coordB = n % 2 === 0 ? 0.0 : 100.0;
995
+ if (n < 4) {
996
+ const g = coordA;
997
+ const b = coordB;
998
+ const r = (y - g * kG - b * kB) / kR;
999
+ if (HctSolver.isBounded(r)) {
1000
+ return [r, g, b];
1001
+ }
1002
+ else {
1003
+ return [-1, -1, -1];
1004
+ }
1005
+ }
1006
+ else if (n < 8) {
1007
+ const b = coordA;
1008
+ const r = coordB;
1009
+ const g = (y - r * kR - b * kB) / kG;
1010
+ if (HctSolver.isBounded(g)) {
1011
+ return [r, g, b];
1012
+ }
1013
+ else {
1014
+ return [-1, -1, -1];
1015
+ }
1016
+ }
1017
+ else {
1018
+ const r = coordA;
1019
+ const g = coordB;
1020
+ const b = (y - r * kR - g * kG) / kB;
1021
+ if (HctSolver.isBounded(b)) {
1022
+ return [r, g, b];
1023
+ }
1024
+ else {
1025
+ return [-1, -1, -1];
1026
+ }
1027
+ }
1028
+ }
1029
+ /**
1030
+ * Finds the segment containing the desired color.
1031
+ *
1032
+ * @param y The Y value of the color.
1033
+ * @param targetHue The hue of the color.
1034
+ * @return A list of two sets of linear RGB coordinates, each
1035
+ * corresponding to an endpoint of the segment containing the
1036
+ * desired color.
1037
+ */
1038
+ static bisectToSegment(y, targetHue) {
1039
+ let left = [-1, -1, -1];
1040
+ let right = left;
1041
+ let leftHue = 0.0;
1042
+ let rightHue = 0.0;
1043
+ let initialized = false;
1044
+ let uncut = true;
1045
+ for (let n = 0; n < 12; n++) {
1046
+ const mid = HctSolver.nthVertex(y, n);
1047
+ if (mid[0] < 0) {
1048
+ continue;
1049
+ }
1050
+ const midHue = HctSolver.hueOf(mid);
1051
+ if (!initialized) {
1052
+ left = mid;
1053
+ right = mid;
1054
+ leftHue = midHue;
1055
+ rightHue = midHue;
1056
+ initialized = true;
1057
+ continue;
1058
+ }
1059
+ if (uncut || HctSolver.areInCyclicOrder(leftHue, midHue, rightHue)) {
1060
+ uncut = false;
1061
+ if (HctSolver.areInCyclicOrder(leftHue, targetHue, midHue)) {
1062
+ right = mid;
1063
+ rightHue = midHue;
1064
+ }
1065
+ else {
1066
+ left = mid;
1067
+ leftHue = midHue;
1068
+ }
1069
+ }
1070
+ }
1071
+ return [left, right];
1072
+ }
1073
+ static midpoint(a, b) {
1074
+ return [
1075
+ (a[0] + b[0]) / 2,
1076
+ (a[1] + b[1]) / 2,
1077
+ (a[2] + b[2]) / 2,
1078
+ ];
1079
+ }
1080
+ static criticalPlaneBelow(x) {
1081
+ return Math.floor(x - 0.5);
1082
+ }
1083
+ static criticalPlaneAbove(x) {
1084
+ return Math.ceil(x - 0.5);
1085
+ }
1086
+ /**
1087
+ * Finds a color with the given Y and hue on the boundary of the
1088
+ * cube.
1089
+ *
1090
+ * @param y The Y value of the color.
1091
+ * @param targetHue The hue of the color.
1092
+ * @return The desired color, in linear RGB coordinates.
1093
+ */
1094
+ static bisectToLimit(y, targetHue) {
1095
+ const segment = HctSolver.bisectToSegment(y, targetHue);
1096
+ let left = segment[0];
1097
+ let leftHue = HctSolver.hueOf(left);
1098
+ let right = segment[1];
1099
+ for (let axis = 0; axis < 3; axis++) {
1100
+ if (left[axis] !== right[axis]) {
1101
+ let lPlane = -1;
1102
+ let rPlane = 255;
1103
+ if (left[axis] < right[axis]) {
1104
+ lPlane = HctSolver.criticalPlaneBelow(HctSolver.trueDelinearized(left[axis]));
1105
+ rPlane = HctSolver.criticalPlaneAbove(HctSolver.trueDelinearized(right[axis]));
1106
+ }
1107
+ else {
1108
+ lPlane = HctSolver.criticalPlaneAbove(HctSolver.trueDelinearized(left[axis]));
1109
+ rPlane = HctSolver.criticalPlaneBelow(HctSolver.trueDelinearized(right[axis]));
1110
+ }
1111
+ for (let i = 0; i < 8; i++) {
1112
+ if (Math.abs(rPlane - lPlane) <= 1) {
1113
+ break;
1114
+ }
1115
+ else {
1116
+ const mPlane = Math.floor((lPlane + rPlane) / 2.0);
1117
+ const midPlaneCoordinate = HctSolver.CRITICAL_PLANES[mPlane];
1118
+ const mid = HctSolver.setCoordinate(left, midPlaneCoordinate, right, axis);
1119
+ const midHue = HctSolver.hueOf(mid);
1120
+ if (HctSolver.areInCyclicOrder(leftHue, targetHue, midHue)) {
1121
+ right = mid;
1122
+ rPlane = mPlane;
1123
+ }
1124
+ else {
1125
+ left = mid;
1126
+ leftHue = midHue;
1127
+ lPlane = mPlane;
1128
+ }
1129
+ }
1130
+ }
1131
+ }
1132
+ }
1133
+ return HctSolver.midpoint(left, right);
1134
+ }
1135
+ static inverseChromaticAdaptation(adapted) {
1136
+ const adaptedAbs = Math.abs(adapted);
1137
+ const base = Math.max(0, 27.13 * adaptedAbs / (400.0 - adaptedAbs));
1138
+ return signum(adapted) * Math.pow(base, 1.0 / 0.42);
1139
+ }
1140
+ /**
1141
+ * Finds a color with the given hue, chroma, and Y.
1142
+ *
1143
+ * @param hueRadians The desired hue in radians.
1144
+ * @param chroma The desired chroma.
1145
+ * @param y The desired Y.
1146
+ * @return The desired color as a hexadecimal integer, if found; 0
1147
+ * otherwise.
1148
+ */
1149
+ static findResultByJ(hueRadians, chroma, y) {
1150
+ // Initial estimate of j.
1151
+ let j = Math.sqrt(y) * 11.0;
1152
+ // ===========================================================
1153
+ // Operations inlined from Cam16 to avoid repeated calculation
1154
+ // ===========================================================
1155
+ const viewingConditions = ViewingConditions.DEFAULT;
1156
+ const tInnerCoeff = 1 / Math.pow(1.64 - Math.pow(0.29, viewingConditions.n), 0.73);
1157
+ const eHue = 0.25 * (Math.cos(hueRadians + 2.0) + 3.8);
1158
+ const p1 = eHue * (50000.0 / 13.0) * viewingConditions.nc * viewingConditions.ncb;
1159
+ const hSin = Math.sin(hueRadians);
1160
+ const hCos = Math.cos(hueRadians);
1161
+ for (let iterationRound = 0; iterationRound < 5; iterationRound++) {
1162
+ // ===========================================================
1163
+ // Operations inlined from Cam16 to avoid repeated calculation
1164
+ // ===========================================================
1165
+ const jNormalized = j / 100.0;
1166
+ const alpha = chroma === 0.0 || j === 0.0 ? 0.0 : chroma / Math.sqrt(jNormalized);
1167
+ const t = Math.pow(alpha * tInnerCoeff, 1.0 / 0.9);
1168
+ const ac = viewingConditions.aw *
1169
+ Math.pow(jNormalized, 1.0 / viewingConditions.c / viewingConditions.z);
1170
+ const p2 = ac / viewingConditions.nbb;
1171
+ const gamma = 23.0 * (p2 + 0.305) * t /
1172
+ (23.0 * p1 + 11 * t * hCos + 108.0 * t * hSin);
1173
+ const a = gamma * hCos;
1174
+ const b = gamma * hSin;
1175
+ const rA = (460.0 * p2 + 451.0 * a + 288.0 * b) / 1403.0;
1176
+ const gA = (460.0 * p2 - 891.0 * a - 261.0 * b) / 1403.0;
1177
+ const bA = (460.0 * p2 - 220.0 * a - 6300.0 * b) / 1403.0;
1178
+ const rCScaled = HctSolver.inverseChromaticAdaptation(rA);
1179
+ const gCScaled = HctSolver.inverseChromaticAdaptation(gA);
1180
+ const bCScaled = HctSolver.inverseChromaticAdaptation(bA);
1181
+ const linrgb = matrixMultiply([rCScaled, gCScaled, bCScaled], HctSolver.LINRGB_FROM_SCALED_DISCOUNT);
1182
+ // ===========================================================
1183
+ // Operations inlined from Cam16 to avoid repeated calculation
1184
+ // ===========================================================
1185
+ if (linrgb[0] < 0 || linrgb[1] < 0 || linrgb[2] < 0) {
1186
+ return 0;
1187
+ }
1188
+ const kR = HctSolver.Y_FROM_LINRGB[0];
1189
+ const kG = HctSolver.Y_FROM_LINRGB[1];
1190
+ const kB = HctSolver.Y_FROM_LINRGB[2];
1191
+ const fnj = kR * linrgb[0] + kG * linrgb[1] + kB * linrgb[2];
1192
+ if (fnj <= 0) {
1193
+ return 0;
1194
+ }
1195
+ if (iterationRound === 4 || Math.abs(fnj - y) < 0.002) {
1196
+ if (linrgb[0] > 100.01 || linrgb[1] > 100.01 || linrgb[2] > 100.01) {
1197
+ return 0;
1198
+ }
1199
+ return argbFromLinrgb(linrgb);
1200
+ }
1201
+ // Iterates with Newton method,
1202
+ // Using 2 * fn(j) / j as the approximation of fn'(j)
1203
+ j = j - (fnj - y) * j / (2 * fnj);
1204
+ }
1205
+ return 0;
1206
+ }
1207
+ /**
1208
+ * Finds an sRGB color with the given hue, chroma, and L*, if
1209
+ * possible.
1210
+ *
1211
+ * @param hueDegrees The desired hue, in degrees.
1212
+ * @param chroma The desired chroma.
1213
+ * @param lstar The desired L*.
1214
+ * @return A hexadecimal representing the sRGB color. The color
1215
+ * has sufficiently close hue, chroma, and L* to the desired
1216
+ * values, if possible; otherwise, the hue and L* will be
1217
+ * sufficiently close, and chroma will be maximized.
1218
+ */
1219
+ static solveToInt(hueDegrees, chroma, lstar) {
1220
+ if (chroma < 0.0001 || lstar < 0.0001 || lstar > 99.9999) {
1221
+ return argbFromLstar(lstar);
1222
+ }
1223
+ hueDegrees = sanitizeDegreesDouble(hueDegrees);
1224
+ const hueRadians = hueDegrees / 180 * Math.PI;
1225
+ const y = yFromLstar(lstar);
1226
+ const exactAnswer = HctSolver.findResultByJ(hueRadians, chroma, y);
1227
+ if (exactAnswer !== 0) {
1228
+ return exactAnswer;
1229
+ }
1230
+ const linrgb = HctSolver.bisectToLimit(y, hueRadians);
1231
+ return argbFromLinrgb(linrgb);
1232
+ }
1233
+ /**
1234
+ * Finds an sRGB color with the given hue, chroma, and L*, if
1235
+ * possible.
1236
+ *
1237
+ * @param hueDegrees The desired hue, in degrees.
1238
+ * @param chroma The desired chroma.
1239
+ * @param lstar The desired L*.
1240
+ * @return An CAM16 object representing the sRGB color. The color
1241
+ * has sufficiently close hue, chroma, and L* to the desired
1242
+ * values, if possible; otherwise, the hue and L* will be
1243
+ * sufficiently close, and chroma will be maximized.
1244
+ */
1245
+ static solveToCam(hueDegrees, chroma, lstar) {
1246
+ return Cam16.fromInt(HctSolver.solveToInt(hueDegrees, chroma, lstar));
1247
+ }
1248
+ }
1249
+ HctSolver.SCALED_DISCOUNT_FROM_LINRGB = [
1250
+ [
1251
+ 0.001200833568784504,
1252
+ 0.002389694492170889,
1253
+ 0.0002795742885861124,
1254
+ ],
1255
+ [
1256
+ 0.0005891086651375999,
1257
+ 0.0029785502573438758,
1258
+ 0.0003270666104008398,
1259
+ ],
1260
+ [
1261
+ 0.00010146692491640572,
1262
+ 0.0005364214359186694,
1263
+ 0.0032979401770712076,
1264
+ ],
1265
+ ];
1266
+ HctSolver.LINRGB_FROM_SCALED_DISCOUNT = [
1267
+ [
1268
+ 1373.2198709594231,
1269
+ -1100.4251190754821,
1270
+ -7.278681089101213,
1271
+ ],
1272
+ [
1273
+ -271.815969077903,
1274
+ 559.6580465940733,
1275
+ -32.46047482791194,
1276
+ ],
1277
+ [
1278
+ 1.9622899599665666,
1279
+ -57.173814538844006,
1280
+ 308.7233197812385,
1281
+ ],
1282
+ ];
1283
+ HctSolver.Y_FROM_LINRGB = [0.2126, 0.7152, 0.0722];
1284
+ HctSolver.CRITICAL_PLANES = [
1285
+ 0.015176349177441876, 0.045529047532325624, 0.07588174588720938,
1286
+ 0.10623444424209313, 0.13658714259697685, 0.16693984095186062,
1287
+ 0.19729253930674434, 0.2276452376616281, 0.2579979360165119,
1288
+ 0.28835063437139563, 0.3188300904430532, 0.350925934958123,
1289
+ 0.3848314933096426, 0.42057480301049466, 0.458183274052838,
1290
+ 0.4976837250274023, 0.5391024159806381, 0.5824650784040898,
1291
+ 0.6277969426914107, 0.6751227633498623, 0.7244668422128921,
1292
+ 0.775853049866786, 0.829304845476233, 0.8848452951698498,
1293
+ 0.942497089126609, 1.0022825574869039, 1.0642236851973577,
1294
+ 1.1283421258858297, 1.1946592148522128, 1.2631959812511864,
1295
+ 1.3339731595349034, 1.407011200216447, 1.4823302800086415,
1296
+ 1.5599503113873272, 1.6398909516233677, 1.7221716113234105,
1297
+ 1.8068114625156377, 1.8938294463134073, 1.9832442801866852,
1298
+ 2.075074464868551, 2.1693382909216234, 2.2660538449872063,
1299
+ 2.36523901573795, 2.4669114995532007, 2.5710888059345764,
1300
+ 2.6777882626779785, 2.7870270208169257, 2.898822059350997,
1301
+ 3.0131901897720907, 3.1301480604002863, 3.2497121605402226,
1302
+ 3.3718988244681087, 3.4967242352587946, 3.624204428461639,
1303
+ 3.754355295633311, 3.887192587735158, 4.022731918402185,
1304
+ 4.160988767090289, 4.301978482107941, 4.445716283538092,
1305
+ 4.592217266055746, 4.741496401646282, 4.893568542229298,
1306
+ 5.048448422192488, 5.20615066083972, 5.3666897647573375,
1307
+ 5.5300801301023865, 5.696336044816294, 5.865471690767354,
1308
+ 6.037501145825082, 6.212438385869475, 6.390297286737924,
1309
+ 6.571091626112461, 6.7548350853498045, 6.941541251256611,
1310
+ 7.131223617812143, 7.323895587840543, 7.5195704746346665,
1311
+ 7.7182615035334345, 7.919981813454504, 8.124744458384042,
1312
+ 8.332562408825165, 8.543448553206703, 8.757415699253682,
1313
+ 8.974476575321063, 9.194643831691977, 9.417930041841839,
1314
+ 9.644347703669503, 9.873909240696694, 10.106627003236781,
1315
+ 10.342513269534024, 10.58158024687427, 10.8238400726681,
1316
+ 11.069304815507364, 11.317986476196008, 11.569896988756009,
1317
+ 11.825048221409341, 12.083451977536606, 12.345119996613247,
1318
+ 12.610063955123938, 12.878295467455942, 13.149826086772048,
1319
+ 13.42466730586372, 13.702830557985108, 13.984327217668513,
1320
+ 14.269168601521828, 14.55736596900856, 14.848930523210871,
1321
+ 15.143873411576273, 15.44220572664832, 15.743938506781891,
1322
+ 16.04908273684337, 16.35764934889634, 16.66964922287304,
1323
+ 16.985093187232053, 17.30399201960269, 17.62635644741625,
1324
+ 17.95219714852476, 18.281524751807332, 18.614349837764564,
1325
+ 18.95068293910138, 19.290534541298456, 19.633915083172692,
1326
+ 19.98083495742689, 20.331304511189067, 20.685334046541502,
1327
+ 21.042933821039977, 21.404114048223256, 21.76888489811322,
1328
+ 22.137256497705877, 22.50923893145328, 22.884842241736916,
1329
+ 23.264076429332462, 23.6469514538663, 24.033477234264016,
1330
+ 24.42366364919083, 24.817520537484558, 25.21505769858089,
1331
+ 25.61628489293138, 26.021211842414342, 26.429848230738664,
1332
+ 26.842203703840827, 27.258287870275353, 27.678110301598522,
1333
+ 28.10168053274597, 28.529008062403893, 28.96010235337422,
1334
+ 29.39497283293396, 29.83362889318845, 30.276079891419332,
1335
+ 30.722335150426627, 31.172403958865512, 31.62629557157785,
1336
+ 32.08401920991837, 32.54558406207592, 33.010999283389665,
1337
+ 33.4802739966603, 33.953417292456834, 34.430438229418264,
1338
+ 34.911345834551085, 35.39614910352207, 35.88485700094671,
1339
+ 36.37747846067349, 36.87402238606382, 37.37449765026789,
1340
+ 37.87891309649659, 38.38727753828926, 38.89959975977785,
1341
+ 39.41588851594697, 39.93615253289054, 40.460400508064545,
1342
+ 40.98864111053629, 41.520882981230194, 42.05713473317016,
1343
+ 42.597404951718396, 43.141702194811224, 43.6900349931913,
1344
+ 44.24241185063697, 44.798841244188324, 45.35933162437017,
1345
+ 45.92389141541209, 46.49252901546552, 47.065252796817916,
1346
+ 47.64207110610409, 48.22299226451468, 48.808024568002054,
1347
+ 49.3971762874833, 49.9904556690408, 50.587870934119984,
1348
+ 51.189430279724725, 51.79514187861014, 52.40501387947288,
1349
+ 53.0190544071392, 53.637271562750364, 54.259673423945976,
1350
+ 54.88626804504493, 55.517063457223934, 56.15206766869424,
1351
+ 56.79128866487574, 57.43473440856916, 58.08241284012621,
1352
+ 58.734331877617365, 59.39049941699807, 60.05092333227251,
1353
+ 60.715611475655585, 61.38457167773311, 62.057811747619894,
1354
+ 62.7353394731159, 63.417162620860914, 64.10328893648692,
1355
+ 64.79372614476921, 65.48848194977529, 66.18756403501224,
1356
+ 66.89098006357258, 67.59873767827808, 68.31084450182222,
1357
+ 69.02730813691093, 69.74813616640164, 70.47333615344107,
1358
+ 71.20291564160104, 71.93688215501312, 72.67524319850172,
1359
+ 73.41800625771542, 74.16517879925733, 74.9167682708136,
1360
+ 75.67278210128072, 76.43322770089146, 77.1981124613393,
1361
+ 77.96744375590167, 78.74122893956174, 79.51947534912904,
1362
+ 80.30219030335869, 81.08938110306934, 81.88105503125999,
1363
+ 82.67721935322541, 83.4778813166706, 84.28304815182372,
1364
+ 85.09272707154808, 85.90692527145302, 86.72564993000343,
1365
+ 87.54890820862819, 88.3767072518277, 89.2090541872801,
1366
+ 90.04595612594655, 90.88742016217518, 91.73345337380438,
1367
+ 92.58406282226491, 93.43925555268066, 94.29903859396902,
1368
+ 95.16341895893969, 96.03240364439274, 96.9059996312159,
1369
+ 97.78421388448044, 98.6670533535366, 99.55452497210776,
1370
+ ];
1371
+
1372
+ /**
1373
+ * @license
1374
+ * Copyright 2021 Google LLC
1375
+ *
1376
+ * Licensed under the Apache License, Version 2.0 (the "License");
1377
+ * you may not use this file except in compliance with the License.
1378
+ * You may obtain a copy of the License at
1379
+ *
1380
+ * http://www.apache.org/licenses/LICENSE-2.0
1381
+ *
1382
+ * Unless required by applicable law or agreed to in writing, software
1383
+ * distributed under the License is distributed on an "AS IS" BASIS,
1384
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1385
+ * See the License for the specific language governing permissions and
1386
+ * limitations under the License.
1387
+ */
1388
+ /**
1389
+ * A color system built using CAM16 hue and chroma, and L* from
1390
+ * L*a*b*.
1391
+ *
1392
+ * Using L* creates a link between the color system, contrast, and thus
1393
+ * accessibility. Contrast ratio depends on relative luminance, or Y in the XYZ
1394
+ * color space. L*, or perceptual luminance can be calculated from Y.
1395
+ *
1396
+ * Unlike Y, L* is linear to human perception, allowing trivial creation of
1397
+ * accurate color tones.
1398
+ *
1399
+ * Unlike contrast ratio, measuring contrast in L* is linear, and simple to
1400
+ * calculate. A difference of 40 in HCT tone guarantees a contrast ratio >= 3.0,
1401
+ * and a difference of 50 guarantees a contrast ratio >= 4.5.
1402
+ */
1403
+ /**
1404
+ * HCT, hue, chroma, and tone. A color system that provides a perceptually
1405
+ * accurate color measurement system that can also accurately render what colors
1406
+ * will appear as in different lighting environments.
1407
+ */
1408
+ class Hct {
1409
+ static from(hue, chroma, tone) {
1410
+ return new Hct(HctSolver.solveToInt(hue, chroma, tone));
1411
+ }
1412
+ /**
1413
+ * @param argb ARGB representation of a color.
1414
+ * @return HCT representation of a color in default viewing conditions
1415
+ */
1416
+ static fromInt(argb) {
1417
+ return new Hct(argb);
1418
+ }
1419
+ toInt() {
1420
+ return this.argb;
1421
+ }
1422
+ /**
1423
+ * A number, in degrees, representing ex. red, orange, yellow, etc.
1424
+ * Ranges from 0 <= hue < 360.
1425
+ */
1426
+ get hue() {
1427
+ return this.internalHue;
1428
+ }
1429
+ /**
1430
+ * @param newHue 0 <= newHue < 360; invalid values are corrected.
1431
+ * Chroma may decrease because chroma has a different maximum for any given
1432
+ * hue and tone.
1433
+ */
1434
+ set hue(newHue) {
1435
+ this.setInternalState(HctSolver.solveToInt(newHue, this.internalChroma, this.internalTone));
1436
+ }
1437
+ get chroma() {
1438
+ return this.internalChroma;
1439
+ }
1440
+ /**
1441
+ * @param newChroma 0 <= newChroma < ?
1442
+ * Chroma may decrease because chroma has a different maximum for any given
1443
+ * hue and tone.
1444
+ */
1445
+ set chroma(newChroma) {
1446
+ this.setInternalState(HctSolver.solveToInt(this.internalHue, newChroma, this.internalTone));
1447
+ }
1448
+ /** Lightness. Ranges from 0 to 100. */
1449
+ get tone() {
1450
+ return this.internalTone;
1451
+ }
1452
+ /**
1453
+ * @param newTone 0 <= newTone <= 100; invalid valids are corrected.
1454
+ * Chroma may decrease because chroma has a different maximum for any given
1455
+ * hue and tone.
1456
+ */
1457
+ set tone(newTone) {
1458
+ this.setInternalState(HctSolver.solveToInt(this.internalHue, this.internalChroma, newTone));
1459
+ }
1460
+ constructor(argb) {
1461
+ this.argb = argb;
1462
+ const cam = Cam16.fromInt(argb);
1463
+ this.internalHue = cam.hue;
1464
+ this.internalChroma = cam.chroma;
1465
+ this.internalTone = lstarFromArgb(argb);
1466
+ this.argb = argb;
1467
+ }
1468
+ setInternalState(argb) {
1469
+ const cam = Cam16.fromInt(argb);
1470
+ this.internalHue = cam.hue;
1471
+ this.internalChroma = cam.chroma;
1472
+ this.internalTone = lstarFromArgb(argb);
1473
+ this.argb = argb;
1474
+ }
1475
+ /**
1476
+ * Translates a color into different [ViewingConditions].
1477
+ *
1478
+ * Colors change appearance. They look different with lights on versus off,
1479
+ * the same color, as in hex code, on white looks different when on black.
1480
+ * This is called color relativity, most famously explicated by Josef Albers
1481
+ * in Interaction of Color.
1482
+ *
1483
+ * In color science, color appearance models can account for this and
1484
+ * calculate the appearance of a color in different settings. HCT is based on
1485
+ * CAM16, a color appearance model, and uses it to make these calculations.
1486
+ *
1487
+ * See [ViewingConditions.make] for parameters affecting color appearance.
1488
+ */
1489
+ inViewingConditions(vc) {
1490
+ // 1. Use CAM16 to find XYZ coordinates of color in specified VC.
1491
+ const cam = Cam16.fromInt(this.toInt());
1492
+ const viewedInVc = cam.xyzInViewingConditions(vc);
1493
+ // 2. Create CAM16 of those XYZ coordinates in default VC.
1494
+ const recastInVc = Cam16.fromXyzInViewingConditions(viewedInVc[0], viewedInVc[1], viewedInVc[2], ViewingConditions.make());
1495
+ // 3. Create HCT from:
1496
+ // - CAM16 using default VC with XYZ coordinates in specified VC.
1497
+ // - L* converted from Y in XYZ coordinates in specified VC.
1498
+ const recastHct = Hct.from(recastInVc.hue, recastInVc.chroma, lstarFromY(viewedInVc[1]));
1499
+ return recastHct;
1500
+ }
1501
+ }
1502
+
1503
+ /**
1504
+ * @license
1505
+ * Copyright 2022 Google LLC
1506
+ *
1507
+ * Licensed under the Apache License, Version 2.0 (the "License");
1508
+ * you may not use this file except in compliance with the License.
1509
+ * You may obtain a copy of the License at
1510
+ *
1511
+ * http://www.apache.org/licenses/LICENSE-2.0
1512
+ *
1513
+ * Unless required by applicable law or agreed to in writing, software
1514
+ * distributed under the License is distributed on an "AS IS" BASIS,
1515
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1516
+ * See the License for the specific language governing permissions and
1517
+ * limitations under the License.
1518
+ */
1519
+ // material_color_utilities is designed to have a consistent API across
1520
+ // platforms and modular components that can be moved around easily. Using a
1521
+ // class as a namespace facilitates this.
1522
+ //
1523
+ // tslint:disable:class-as-namespace
1524
+ /**
1525
+ * Utility methods for calculating contrast given two colors, or calculating a
1526
+ * color given one color and a contrast ratio.
1527
+ *
1528
+ * Contrast ratio is calculated using XYZ's Y. When linearized to match human
1529
+ * perception, Y becomes HCT's tone and L*a*b*'s' L*. Informally, this is the
1530
+ * lightness of a color.
1531
+ *
1532
+ * Methods refer to tone, T in the the HCT color space.
1533
+ * Tone is equivalent to L* in the L*a*b* color space, or L in the LCH color
1534
+ * space.
1535
+ */
1536
+ class Contrast {
1537
+ /**
1538
+ * Returns a contrast ratio, which ranges from 1 to 21.
1539
+ *
1540
+ * @param toneA Tone between 0 and 100. Values outside will be clamped.
1541
+ * @param toneB Tone between 0 and 100. Values outside will be clamped.
1542
+ */
1543
+ static ratioOfTones(toneA, toneB) {
1544
+ toneA = clampDouble(0.0, 100.0, toneA);
1545
+ toneB = clampDouble(0.0, 100.0, toneB);
1546
+ return Contrast.ratioOfYs(yFromLstar(toneA), yFromLstar(toneB));
1547
+ }
1548
+ static ratioOfYs(y1, y2) {
1549
+ const lighter = y1 > y2 ? y1 : y2;
1550
+ const darker = (lighter === y2) ? y1 : y2;
1551
+ return (lighter + 5.0) / (darker + 5.0);
1552
+ }
1553
+ /**
1554
+ * Returns a tone >= tone parameter that ensures ratio parameter.
1555
+ * Return value is between 0 and 100.
1556
+ * Returns -1 if ratio cannot be achieved with tone parameter.
1557
+ *
1558
+ * @param tone Tone return value must contrast with.
1559
+ * Range is 0 to 100. Invalid values will result in -1 being returned.
1560
+ * @param ratio Contrast ratio of return value and tone.
1561
+ * Range is 1 to 21, invalid values have undefined behavior.
1562
+ */
1563
+ static lighter(tone, ratio) {
1564
+ if (tone < 0.0 || tone > 100.0) {
1565
+ return -1;
1566
+ }
1567
+ const darkY = yFromLstar(tone);
1568
+ const lightY = ratio * (darkY + 5.0) - 5.0;
1569
+ const realContrast = Contrast.ratioOfYs(lightY, darkY);
1570
+ const delta = Math.abs(realContrast - ratio);
1571
+ if (realContrast < ratio && delta > 0.04) {
1572
+ return -1;
1573
+ }
1574
+ // Ensure gamut mapping, which requires a 'range' on tone, will still result
1575
+ // the correct ratio by darkening slightly.
1576
+ const returnValue = lstarFromY(lightY) + 0.4;
1577
+ if (returnValue < 0 || returnValue > 100) {
1578
+ return -1;
1579
+ }
1580
+ return returnValue;
1581
+ }
1582
+ /**
1583
+ * Returns a tone <= tone parameter that ensures ratio parameter.
1584
+ * Return value is between 0 and 100.
1585
+ * Returns -1 if ratio cannot be achieved with tone parameter.
1586
+ *
1587
+ * @param tone Tone return value must contrast with.
1588
+ * Range is 0 to 100. Invalid values will result in -1 being returned.
1589
+ * @param ratio Contrast ratio of return value and tone.
1590
+ * Range is 1 to 21, invalid values have undefined behavior.
1591
+ */
1592
+ static darker(tone, ratio) {
1593
+ if (tone < 0.0 || tone > 100.0) {
1594
+ return -1;
1595
+ }
1596
+ const lightY = yFromLstar(tone);
1597
+ const darkY = ((lightY + 5.0) / ratio) - 5.0;
1598
+ const realContrast = Contrast.ratioOfYs(lightY, darkY);
1599
+ const delta = Math.abs(realContrast - ratio);
1600
+ if (realContrast < ratio && delta > 0.04) {
1601
+ return -1;
1602
+ }
1603
+ // Ensure gamut mapping, which requires a 'range' on tone, will still result
1604
+ // the correct ratio by darkening slightly.
1605
+ const returnValue = lstarFromY(darkY) - 0.4;
1606
+ if (returnValue < 0 || returnValue > 100) {
1607
+ return -1;
1608
+ }
1609
+ return returnValue;
1610
+ }
1611
+ /**
1612
+ * Returns a tone >= tone parameter that ensures ratio parameter.
1613
+ * Return value is between 0 and 100.
1614
+ * Returns 100 if ratio cannot be achieved with tone parameter.
1615
+ *
1616
+ * This method is unsafe because the returned value is guaranteed to be in
1617
+ * bounds for tone, i.e. between 0 and 100. However, that value may not reach
1618
+ * the ratio with tone. For example, there is no color lighter than T100.
1619
+ *
1620
+ * @param tone Tone return value must contrast with.
1621
+ * Range is 0 to 100. Invalid values will result in 100 being returned.
1622
+ * @param ratio Desired contrast ratio of return value and tone parameter.
1623
+ * Range is 1 to 21, invalid values have undefined behavior.
1624
+ */
1625
+ static lighterUnsafe(tone, ratio) {
1626
+ const lighterSafe = Contrast.lighter(tone, ratio);
1627
+ return (lighterSafe < 0.0) ? 100.0 : lighterSafe;
1628
+ }
1629
+ /**
1630
+ * Returns a tone >= tone parameter that ensures ratio parameter.
1631
+ * Return value is between 0 and 100.
1632
+ * Returns 100 if ratio cannot be achieved with tone parameter.
1633
+ *
1634
+ * This method is unsafe because the returned value is guaranteed to be in
1635
+ * bounds for tone, i.e. between 0 and 100. However, that value may not reach
1636
+ * the [ratio with [tone]. For example, there is no color darker than T0.
1637
+ *
1638
+ * @param tone Tone return value must contrast with.
1639
+ * Range is 0 to 100. Invalid values will result in 0 being returned.
1640
+ * @param ratio Desired contrast ratio of return value and tone parameter.
1641
+ * Range is 1 to 21, invalid values have undefined behavior.
1642
+ */
1643
+ static darkerUnsafe(tone, ratio) {
1644
+ const darkerSafe = Contrast.darker(tone, ratio);
1645
+ return (darkerSafe < 0.0) ? 0.0 : darkerSafe;
1646
+ }
1647
+ }
1648
+
1649
+ /**
1650
+ * @license
1651
+ * Copyright 2023 Google LLC
1652
+ *
1653
+ * Licensed under the Apache License, Version 2.0 (the "License");
1654
+ * you may not use this file except in compliance with the License.
1655
+ * You may obtain a copy of the License at
1656
+ *
1657
+ * http://www.apache.org/licenses/LICENSE-2.0
1658
+ *
1659
+ * Unless required by applicable law or agreed to in writing, software
1660
+ * distributed under the License is distributed on an "AS IS" BASIS,
1661
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1662
+ * See the License for the specific language governing permissions and
1663
+ * limitations under the License.
1664
+ */
1665
+ // material_color_utilities is designed to have a consistent API across
1666
+ // platforms and modular components that can be moved around easily. Using a
1667
+ // class as a namespace facilitates this.
1668
+ //
1669
+ // tslint:disable:class-as-namespace
1670
+ /**
1671
+ * Check and/or fix universally disliked colors.
1672
+ * Color science studies of color preference indicate universal distaste for
1673
+ * dark yellow-greens, and also show this is correlated to distate for
1674
+ * biological waste and rotting food.
1675
+ *
1676
+ * See Palmer and Schloss, 2010 or Schloss and Palmer's Chapter 21 in Handbook
1677
+ * of Color Psychology (2015).
1678
+ */
1679
+ class DislikeAnalyzer {
1680
+ /**
1681
+ * Returns true if a color is disliked.
1682
+ *
1683
+ * @param hct A color to be judged.
1684
+ * @return Whether the color is disliked.
1685
+ *
1686
+ * Disliked is defined as a dark yellow-green that is not neutral.
1687
+ */
1688
+ static isDisliked(hct) {
1689
+ const huePasses = Math.round(hct.hue) >= 90.0 && Math.round(hct.hue) <= 111.0;
1690
+ const chromaPasses = Math.round(hct.chroma) > 16.0;
1691
+ const tonePasses = Math.round(hct.tone) < 65.0;
1692
+ return huePasses && chromaPasses && tonePasses;
1693
+ }
1694
+ /**
1695
+ * If a color is disliked, lighten it to make it likable.
1696
+ *
1697
+ * @param hct A color to be judged.
1698
+ * @return A new color if the original color is disliked, or the original
1699
+ * color if it is acceptable.
1700
+ */
1701
+ static fixIfDisliked(hct) {
1702
+ if (DislikeAnalyzer.isDisliked(hct)) {
1703
+ return Hct.from(hct.hue, hct.chroma, 70.0);
1704
+ }
1705
+ return hct;
1706
+ }
1707
+ }
1708
+
1709
+ /**
1710
+ * @license
1711
+ * Copyright 2022 Google LLC
1712
+ *
1713
+ * Licensed under the Apache License, Version 2.0 (the "License");
1714
+ * you may not use this file except in compliance with the License.
1715
+ * You may obtain a copy of the License at
1716
+ *
1717
+ * http://www.apache.org/licenses/LICENSE-2.0
1718
+ *
1719
+ * Unless required by applicable law or agreed to in writing, software
1720
+ * distributed under the License is distributed on an "AS IS" BASIS,
1721
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1722
+ * See the License for the specific language governing permissions and
1723
+ * limitations under the License.
1724
+ */
1725
+ /**
1726
+ * A color that adjusts itself based on UI state provided by DynamicScheme.
1727
+ *
1728
+ * Colors without backgrounds do not change tone when contrast changes. Colors
1729
+ * with backgrounds become closer to their background as contrast lowers, and
1730
+ * further when contrast increases.
1731
+ *
1732
+ * Prefer static constructors. They require either a hexcode, a palette and
1733
+ * tone, or a hue and chroma. Optionally, they can provide a background
1734
+ * DynamicColor.
1735
+ */
1736
+ class DynamicColor {
1737
+ /**
1738
+ * Create a DynamicColor defined by a TonalPalette and HCT tone.
1739
+ *
1740
+ * @param args Functions with DynamicScheme as input. Must provide a palette
1741
+ * and tone. May provide a background DynamicColor and ToneDeltaConstraint.
1742
+ */
1743
+ static fromPalette(args) {
1744
+ return new DynamicColor(args.name ?? '', args.palette, args.tone, args.isBackground ?? false, args.background, args.secondBackground, args.contrastCurve, args.toneDeltaPair);
1745
+ }
1746
+ /**
1747
+ * The base constructor for DynamicColor.
1748
+ *
1749
+ * _Strongly_ prefer using one of the convenience constructors. This class is
1750
+ * arguably too flexible to ensure it can support any scenario. Functional
1751
+ * arguments allow overriding without risks that come with subclasses.
1752
+ *
1753
+ * For example, the default behavior of adjust tone at max contrast
1754
+ * to be at a 7.0 ratio with its background is principled and
1755
+ * matches accessibility guidance. That does not mean it's the desired
1756
+ * approach for _every_ design system, and every color pairing,
1757
+ * always, in every case.
1758
+ *
1759
+ * @param name The name of the dynamic color. Defaults to empty.
1760
+ * @param palette Function that provides a TonalPalette given
1761
+ * DynamicScheme. A TonalPalette is defined by a hue and chroma, so this
1762
+ * replaces the need to specify hue/chroma. By providing a tonal palette, when
1763
+ * contrast adjustments are made, intended chroma can be preserved.
1764
+ * @param tone Function that provides a tone, given a DynamicScheme.
1765
+ * @param isBackground Whether this dynamic color is a background, with
1766
+ * some other color as the foreground. Defaults to false.
1767
+ * @param background The background of the dynamic color (as a function of a
1768
+ * `DynamicScheme`), if it exists.
1769
+ * @param secondBackground A second background of the dynamic color (as a
1770
+ * function of a `DynamicScheme`), if it
1771
+ * exists.
1772
+ * @param contrastCurve A `ContrastCurve` object specifying how its contrast
1773
+ * against its background should behave in various contrast levels options.
1774
+ * @param toneDeltaPair A `ToneDeltaPair` object specifying a tone delta
1775
+ * constraint between two colors. One of them must be the color being
1776
+ * constructed.
1777
+ */
1778
+ constructor(name, palette, tone, isBackground, background, secondBackground, contrastCurve, toneDeltaPair) {
1779
+ this.name = name;
1780
+ this.palette = palette;
1781
+ this.tone = tone;
1782
+ this.isBackground = isBackground;
1783
+ this.background = background;
1784
+ this.secondBackground = secondBackground;
1785
+ this.contrastCurve = contrastCurve;
1786
+ this.toneDeltaPair = toneDeltaPair;
1787
+ this.hctCache = new Map();
1788
+ if ((!background) && secondBackground) {
1789
+ throw new Error(`Color ${name} has secondBackground` +
1790
+ `defined, but background is not defined.`);
1791
+ }
1792
+ if ((!background) && contrastCurve) {
1793
+ throw new Error(`Color ${name} has contrastCurve` +
1794
+ `defined, but background is not defined.`);
1795
+ }
1796
+ if (background && !contrastCurve) {
1797
+ throw new Error(`Color ${name} has background` +
1798
+ `defined, but contrastCurve is not defined.`);
1799
+ }
1800
+ }
1801
+ /**
1802
+ * Return a ARGB integer (i.e. a hex code).
1803
+ *
1804
+ * @param scheme Defines the conditions of the user interface, for example,
1805
+ * whether or not it is dark mode or light mode, and what the desired
1806
+ * contrast level is.
1807
+ */
1808
+ getArgb(scheme) {
1809
+ return this.getHct(scheme).toInt();
1810
+ }
1811
+ /**
1812
+ * Return a color, expressed in the HCT color space, that this
1813
+ * DynamicColor is under the conditions in scheme.
1814
+ *
1815
+ * @param scheme Defines the conditions of the user interface, for example,
1816
+ * whether or not it is dark mode or light mode, and what the desired
1817
+ * contrast level is.
1818
+ */
1819
+ getHct(scheme) {
1820
+ const cachedAnswer = this.hctCache.get(scheme);
1821
+ if (cachedAnswer != null) {
1822
+ return cachedAnswer;
1823
+ }
1824
+ const tone = this.getTone(scheme);
1825
+ const answer = this.palette(scheme).getHct(tone);
1826
+ if (this.hctCache.size > 4) {
1827
+ this.hctCache.clear();
1828
+ }
1829
+ this.hctCache.set(scheme, answer);
1830
+ return answer;
1831
+ }
1832
+ /**
1833
+ * Return a tone, T in the HCT color space, that this DynamicColor is under
1834
+ * the conditions in scheme.
1835
+ *
1836
+ * @param scheme Defines the conditions of the user interface, for example,
1837
+ * whether or not it is dark mode or light mode, and what the desired
1838
+ * contrast level is.
1839
+ */
1840
+ getTone(scheme) {
1841
+ const decreasingContrast = scheme.contrastLevel < 0;
1842
+ // Case 1: dual foreground, pair of colors with delta constraint.
1843
+ if (this.toneDeltaPair) {
1844
+ const toneDeltaPair = this.toneDeltaPair(scheme);
1845
+ const roleA = toneDeltaPair.roleA;
1846
+ const roleB = toneDeltaPair.roleB;
1847
+ const delta = toneDeltaPair.delta;
1848
+ const polarity = toneDeltaPair.polarity;
1849
+ const stayTogether = toneDeltaPair.stayTogether;
1850
+ const bg = this.background(scheme);
1851
+ const bgTone = bg.getTone(scheme);
1852
+ const aIsNearer = (polarity === 'nearer' ||
1853
+ (polarity === 'lighter' && !scheme.isDark) ||
1854
+ (polarity === 'darker' && scheme.isDark));
1855
+ const nearer = aIsNearer ? roleA : roleB;
1856
+ const farther = aIsNearer ? roleB : roleA;
1857
+ const amNearer = this.name === nearer.name;
1858
+ const expansionDir = scheme.isDark ? 1 : -1;
1859
+ // 1st round: solve to min, each
1860
+ const nContrast = nearer.contrastCurve.get(scheme.contrastLevel);
1861
+ const fContrast = farther.contrastCurve.get(scheme.contrastLevel);
1862
+ // If a color is good enough, it is not adjusted.
1863
+ // Initial and adjusted tones for `nearer`
1864
+ const nInitialTone = nearer.tone(scheme);
1865
+ let nTone = Contrast.ratioOfTones(bgTone, nInitialTone) >= nContrast ?
1866
+ nInitialTone :
1867
+ DynamicColor.foregroundTone(bgTone, nContrast);
1868
+ // Initial and adjusted tones for `farther`
1869
+ const fInitialTone = farther.tone(scheme);
1870
+ let fTone = Contrast.ratioOfTones(bgTone, fInitialTone) >= fContrast ?
1871
+ fInitialTone :
1872
+ DynamicColor.foregroundTone(bgTone, fContrast);
1873
+ if (decreasingContrast) {
1874
+ // If decreasing contrast, adjust color to the "bare minimum"
1875
+ // that satisfies contrast.
1876
+ nTone = DynamicColor.foregroundTone(bgTone, nContrast);
1877
+ fTone = DynamicColor.foregroundTone(bgTone, fContrast);
1878
+ }
1879
+ if ((fTone - nTone) * expansionDir >= delta) ;
1880
+ else {
1881
+ // 2nd round: expand farther to match delta.
1882
+ fTone = clampDouble(0, 100, nTone + delta * expansionDir);
1883
+ if ((fTone - nTone) * expansionDir >= delta) ;
1884
+ else {
1885
+ // 3rd round: contract nearer to match delta.
1886
+ nTone = clampDouble(0, 100, fTone - delta * expansionDir);
1887
+ }
1888
+ }
1889
+ // Avoids the 50-59 awkward zone.
1890
+ if (50 <= nTone && nTone < 60) {
1891
+ // If `nearer` is in the awkward zone, move it away, together with
1892
+ // `farther`.
1893
+ if (expansionDir > 0) {
1894
+ nTone = 60;
1895
+ fTone = Math.max(fTone, nTone + delta * expansionDir);
1896
+ }
1897
+ else {
1898
+ nTone = 49;
1899
+ fTone = Math.min(fTone, nTone + delta * expansionDir);
1900
+ }
1901
+ }
1902
+ else if (50 <= fTone && fTone < 60) {
1903
+ if (stayTogether) {
1904
+ // Fixes both, to avoid two colors on opposite sides of the "awkward
1905
+ // zone".
1906
+ if (expansionDir > 0) {
1907
+ nTone = 60;
1908
+ fTone = Math.max(fTone, nTone + delta * expansionDir);
1909
+ }
1910
+ else {
1911
+ nTone = 49;
1912
+ fTone = Math.min(fTone, nTone + delta * expansionDir);
1913
+ }
1914
+ }
1915
+ else {
1916
+ // Not required to stay together; fixes just one.
1917
+ if (expansionDir > 0) {
1918
+ fTone = 60;
1919
+ }
1920
+ else {
1921
+ fTone = 49;
1922
+ }
1923
+ }
1924
+ }
1925
+ // Returns `nTone` if this color is `nearer`, otherwise `fTone`.
1926
+ return amNearer ? nTone : fTone;
1927
+ }
1928
+ else {
1929
+ // Case 2: No contrast pair; just solve for itself.
1930
+ let answer = this.tone(scheme);
1931
+ if (this.background == null) {
1932
+ return answer; // No adjustment for colors with no background.
1933
+ }
1934
+ const bgTone = this.background(scheme).getTone(scheme);
1935
+ const desiredRatio = this.contrastCurve.get(scheme.contrastLevel);
1936
+ if (Contrast.ratioOfTones(bgTone, answer) >= desiredRatio) ;
1937
+ else {
1938
+ // Rough improvement.
1939
+ answer = DynamicColor.foregroundTone(bgTone, desiredRatio);
1940
+ }
1941
+ if (decreasingContrast) {
1942
+ answer = DynamicColor.foregroundTone(bgTone, desiredRatio);
1943
+ }
1944
+ if (this.isBackground && 50 <= answer && answer < 60) {
1945
+ // Must adjust
1946
+ if (Contrast.ratioOfTones(49, bgTone) >= desiredRatio) {
1947
+ answer = 49;
1948
+ }
1949
+ else {
1950
+ answer = 60;
1951
+ }
1952
+ }
1953
+ if (this.secondBackground) {
1954
+ // Case 3: Adjust for dual backgrounds.
1955
+ const [bg1, bg2] = [this.background, this.secondBackground];
1956
+ const [bgTone1, bgTone2] = [bg1(scheme).getTone(scheme), bg2(scheme).getTone(scheme)];
1957
+ const [upper, lower] = [Math.max(bgTone1, bgTone2), Math.min(bgTone1, bgTone2)];
1958
+ if (Contrast.ratioOfTones(upper, answer) >= desiredRatio &&
1959
+ Contrast.ratioOfTones(lower, answer) >= desiredRatio) {
1960
+ return answer;
1961
+ }
1962
+ // The darkest light tone that satisfies the desired ratio,
1963
+ // or -1 if such ratio cannot be reached.
1964
+ const lightOption = Contrast.lighter(upper, desiredRatio);
1965
+ // The lightest dark tone that satisfies the desired ratio,
1966
+ // or -1 if such ratio cannot be reached.
1967
+ const darkOption = Contrast.darker(lower, desiredRatio);
1968
+ // Tones suitable for the foreground.
1969
+ const availables = [];
1970
+ if (lightOption !== -1)
1971
+ availables.push(lightOption);
1972
+ if (darkOption !== -1)
1973
+ availables.push(darkOption);
1974
+ const prefersLight = DynamicColor.tonePrefersLightForeground(bgTone1) ||
1975
+ DynamicColor.tonePrefersLightForeground(bgTone2);
1976
+ if (prefersLight) {
1977
+ return (lightOption < 0) ? 100 : lightOption;
1978
+ }
1979
+ if (availables.length === 1) {
1980
+ return availables[0];
1981
+ }
1982
+ return (darkOption < 0) ? 0 : darkOption;
1983
+ }
1984
+ return answer;
1985
+ }
1986
+ }
1987
+ /**
1988
+ * Given a background tone, find a foreground tone, while ensuring they reach
1989
+ * a contrast ratio that is as close to [ratio] as possible.
1990
+ *
1991
+ * @param bgTone Tone in HCT. Range is 0 to 100, undefined behavior when it
1992
+ * falls outside that range.
1993
+ * @param ratio The contrast ratio desired between bgTone and the return
1994
+ * value.
1995
+ */
1996
+ static foregroundTone(bgTone, ratio) {
1997
+ const lighterTone = Contrast.lighterUnsafe(bgTone, ratio);
1998
+ const darkerTone = Contrast.darkerUnsafe(bgTone, ratio);
1999
+ const lighterRatio = Contrast.ratioOfTones(lighterTone, bgTone);
2000
+ const darkerRatio = Contrast.ratioOfTones(darkerTone, bgTone);
2001
+ const preferLighter = DynamicColor.tonePrefersLightForeground(bgTone);
2002
+ if (preferLighter) {
2003
+ // This handles an edge case where the initial contrast ratio is high
2004
+ // (ex. 13.0), and the ratio passed to the function is that high
2005
+ // ratio, and both the lighter and darker ratio fails to pass that
2006
+ // ratio.
2007
+ //
2008
+ // This was observed with Tonal Spot's On Primary Container turning
2009
+ // black momentarily between high and max contrast in light mode. PC's
2010
+ // standard tone was T90, OPC's was T10, it was light mode, and the
2011
+ // contrast value was 0.6568521221032331.
2012
+ const negligibleDifference = Math.abs(lighterRatio - darkerRatio) < 0.1 &&
2013
+ lighterRatio < ratio && darkerRatio < ratio;
2014
+ return lighterRatio >= ratio || lighterRatio >= darkerRatio ||
2015
+ negligibleDifference ?
2016
+ lighterTone :
2017
+ darkerTone;
2018
+ }
2019
+ else {
2020
+ return darkerRatio >= ratio || darkerRatio >= lighterRatio ? darkerTone :
2021
+ lighterTone;
2022
+ }
2023
+ }
2024
+ /**
2025
+ * Returns whether [tone] prefers a light foreground.
2026
+ *
2027
+ * People prefer white foregrounds on ~T60-70. Observed over time, and also
2028
+ * by Andrew Somers during research for APCA.
2029
+ *
2030
+ * T60 used as to create the smallest discontinuity possible when skipping
2031
+ * down to T49 in order to ensure light foregrounds.
2032
+ * Since `tertiaryContainer` in dark monochrome scheme requires a tone of
2033
+ * 60, it should not be adjusted. Therefore, 60 is excluded here.
2034
+ */
2035
+ static tonePrefersLightForeground(tone) {
2036
+ return Math.round(tone) < 60.0;
2037
+ }
2038
+ /**
2039
+ * Returns whether [tone] can reach a contrast ratio of 4.5 with a lighter
2040
+ * color.
2041
+ */
2042
+ static toneAllowsLightForeground(tone) {
2043
+ return Math.round(tone) <= 49.0;
2044
+ }
2045
+ /**
2046
+ * Adjust a tone such that white has 4.5 contrast, if the tone is
2047
+ * reasonably close to supporting it.
2048
+ */
2049
+ static enableLightForeground(tone) {
2050
+ if (DynamicColor.tonePrefersLightForeground(tone) &&
2051
+ !DynamicColor.toneAllowsLightForeground(tone)) {
2052
+ return 49.0;
2053
+ }
2054
+ return tone;
2055
+ }
2056
+ }
2057
+
2058
+ /**
2059
+ * @license
2060
+ * Copyright 2021 Google LLC
2061
+ *
2062
+ * Licensed under the Apache License, Version 2.0 (the "License");
2063
+ * you may not use this file except in compliance with the License.
2064
+ * You may obtain a copy of the License at
2065
+ *
2066
+ * http://www.apache.org/licenses/LICENSE-2.0
2067
+ *
2068
+ * Unless required by applicable law or agreed to in writing, software
2069
+ * distributed under the License is distributed on an "AS IS" BASIS,
2070
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2071
+ * See the License for the specific language governing permissions and
2072
+ * limitations under the License.
2073
+ */
2074
+ /**
2075
+ * A convenience class for retrieving colors that are constant in hue and
2076
+ * chroma, but vary in tone.
2077
+ */
2078
+ class TonalPalette {
2079
+ /**
2080
+ * @param argb ARGB representation of a color
2081
+ * @return Tones matching that color's hue and chroma.
2082
+ */
2083
+ static fromInt(argb) {
2084
+ const hct = Hct.fromInt(argb);
2085
+ return TonalPalette.fromHct(hct);
2086
+ }
2087
+ /**
2088
+ * @param hct Hct
2089
+ * @return Tones matching that color's hue and chroma.
2090
+ */
2091
+ static fromHct(hct) {
2092
+ return new TonalPalette(hct.hue, hct.chroma, hct);
2093
+ }
2094
+ /**
2095
+ * @param hue HCT hue
2096
+ * @param chroma HCT chroma
2097
+ * @return Tones matching hue and chroma.
2098
+ */
2099
+ static fromHueAndChroma(hue, chroma) {
2100
+ const keyColor = new KeyColor(hue, chroma).create();
2101
+ return new TonalPalette(hue, chroma, keyColor);
2102
+ }
2103
+ constructor(hue, chroma, keyColor) {
2104
+ this.hue = hue;
2105
+ this.chroma = chroma;
2106
+ this.keyColor = keyColor;
2107
+ this.cache = new Map();
2108
+ }
2109
+ /**
2110
+ * @param tone HCT tone, measured from 0 to 100.
2111
+ * @return ARGB representation of a color with that tone.
2112
+ */
2113
+ tone(tone) {
2114
+ let argb = this.cache.get(tone);
2115
+ if (argb === undefined) {
2116
+ argb = Hct.from(this.hue, this.chroma, tone).toInt();
2117
+ this.cache.set(tone, argb);
2118
+ }
2119
+ return argb;
2120
+ }
2121
+ /**
2122
+ * @param tone HCT tone.
2123
+ * @return HCT representation of a color with that tone.
2124
+ */
2125
+ getHct(tone) {
2126
+ return Hct.fromInt(this.tone(tone));
2127
+ }
2128
+ }
2129
+ /**
2130
+ * Key color is a color that represents the hue and chroma of a tonal palette
2131
+ */
2132
+ class KeyColor {
2133
+ constructor(hue, requestedChroma) {
2134
+ this.hue = hue;
2135
+ this.requestedChroma = requestedChroma;
2136
+ // Cache that maps tone to max chroma to avoid duplicated HCT calculation.
2137
+ this.chromaCache = new Map();
2138
+ this.maxChromaValue = 200.0;
2139
+ }
2140
+ /**
2141
+ * Creates a key color from a [hue] and a [chroma].
2142
+ * The key color is the first tone, starting from T50, matching the given hue
2143
+ * and chroma.
2144
+ *
2145
+ * @return Key color [Hct]
2146
+ */
2147
+ create() {
2148
+ // Pivot around T50 because T50 has the most chroma available, on
2149
+ // average. Thus it is most likely to have a direct answer.
2150
+ const pivotTone = 50;
2151
+ const toneStepSize = 1;
2152
+ // Epsilon to accept values slightly higher than the requested chroma.
2153
+ const epsilon = 0.01;
2154
+ // Binary search to find the tone that can provide a chroma that is closest
2155
+ // to the requested chroma.
2156
+ let lowerTone = 0;
2157
+ let upperTone = 100;
2158
+ while (lowerTone < upperTone) {
2159
+ const midTone = Math.floor((lowerTone + upperTone) / 2);
2160
+ const isAscending = this.maxChroma(midTone) < this.maxChroma(midTone + toneStepSize);
2161
+ const sufficientChroma = this.maxChroma(midTone) >= this.requestedChroma - epsilon;
2162
+ if (sufficientChroma) {
2163
+ // Either range [lowerTone, midTone] or [midTone, upperTone] has
2164
+ // the answer, so search in the range that is closer the pivot tone.
2165
+ if (Math.abs(lowerTone - pivotTone) < Math.abs(upperTone - pivotTone)) {
2166
+ upperTone = midTone;
2167
+ }
2168
+ else {
2169
+ if (lowerTone === midTone) {
2170
+ return Hct.from(this.hue, this.requestedChroma, lowerTone);
2171
+ }
2172
+ lowerTone = midTone;
2173
+ }
2174
+ }
2175
+ else {
2176
+ // As there is no sufficient chroma in the midTone, follow the direction
2177
+ // to the chroma peak.
2178
+ if (isAscending) {
2179
+ lowerTone = midTone + toneStepSize;
2180
+ }
2181
+ else {
2182
+ // Keep midTone for potential chroma peak.
2183
+ upperTone = midTone;
2184
+ }
2185
+ }
2186
+ }
2187
+ return Hct.from(this.hue, this.requestedChroma, lowerTone);
2188
+ }
2189
+ // Find the maximum chroma for a given tone
2190
+ maxChroma(tone) {
2191
+ if (this.chromaCache.has(tone)) {
2192
+ return this.chromaCache.get(tone);
2193
+ }
2194
+ const chroma = Hct.from(this.hue, this.maxChromaValue, tone).chroma;
2195
+ this.chromaCache.set(tone, chroma);
2196
+ return chroma;
2197
+ }
2198
+ }
2199
+
2200
+ /**
2201
+ * @license
2202
+ * Copyright 2023 Google LLC
2203
+ *
2204
+ * Licensed under the Apache License, Version 2.0 (the "License");
2205
+ * you may not use this file except in compliance with the License.
2206
+ * You may obtain a copy of the License at
2207
+ *
2208
+ * http://www.apache.org/licenses/LICENSE-2.0
2209
+ *
2210
+ * Unless required by applicable law or agreed to in writing, software
2211
+ * distributed under the License is distributed on an "AS IS" BASIS,
2212
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2213
+ * See the License for the specific language governing permissions and
2214
+ * limitations under the License.
2215
+ */
2216
+ /**
2217
+ * A class containing a value that changes with the contrast level.
2218
+ *
2219
+ * Usually represents the contrast requirements for a dynamic color on its
2220
+ * background. The four values correspond to values for contrast levels -1.0,
2221
+ * 0.0, 0.5, and 1.0, respectively.
2222
+ */
2223
+ class ContrastCurve {
2224
+ /**
2225
+ * Creates a `ContrastCurve` object.
2226
+ *
2227
+ * @param low Value for contrast level -1.0
2228
+ * @param normal Value for contrast level 0.0
2229
+ * @param medium Value for contrast level 0.5
2230
+ * @param high Value for contrast level 1.0
2231
+ */
2232
+ constructor(low, normal, medium, high) {
2233
+ this.low = low;
2234
+ this.normal = normal;
2235
+ this.medium = medium;
2236
+ this.high = high;
2237
+ }
2238
+ /**
2239
+ * Returns the value at a given contrast level.
2240
+ *
2241
+ * @param contrastLevel The contrast level. 0.0 is the default (normal); -1.0
2242
+ * is the lowest; 1.0 is the highest.
2243
+ * @return The value. For contrast ratios, a number between 1.0 and 21.0.
2244
+ */
2245
+ get(contrastLevel) {
2246
+ if (contrastLevel <= -1) {
2247
+ return this.low;
2248
+ }
2249
+ else if (contrastLevel < 0.0) {
2250
+ return lerp(this.low, this.normal, (contrastLevel - (-1)) / 1);
2251
+ }
2252
+ else if (contrastLevel < 0.5) {
2253
+ return lerp(this.normal, this.medium, (contrastLevel - 0) / 0.5);
2254
+ }
2255
+ else if (contrastLevel < 1.0) {
2256
+ return lerp(this.medium, this.high, (contrastLevel - 0.5) / 0.5);
2257
+ }
2258
+ else {
2259
+ return this.high;
2260
+ }
2261
+ }
2262
+ }
2263
+
2264
+ /**
2265
+ * @license
2266
+ * Copyright 2023 Google LLC
2267
+ *
2268
+ * Licensed under the Apache License, Version 2.0 (the "License");
2269
+ * you may not use this file except in compliance with the License.
2270
+ * You may obtain a copy of the License at
2271
+ *
2272
+ * http://www.apache.org/licenses/LICENSE-2.0
2273
+ *
2274
+ * Unless required by applicable law or agreed to in writing, software
2275
+ * distributed under the License is distributed on an "AS IS" BASIS,
2276
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2277
+ * See the License for the specific language governing permissions and
2278
+ * limitations under the License.
2279
+ */
2280
+ /**
2281
+ * Documents a constraint between two DynamicColors, in which their tones must
2282
+ * have a certain distance from each other.
2283
+ *
2284
+ * Prefer a DynamicColor with a background, this is for special cases when
2285
+ * designers want tonal distance, literally contrast, between two colors that
2286
+ * don't have a background / foreground relationship or a contrast guarantee.
2287
+ */
2288
+ class ToneDeltaPair {
2289
+ /**
2290
+ * Documents a constraint in tone distance between two DynamicColors.
2291
+ *
2292
+ * The polarity is an adjective that describes "A", compared to "B".
2293
+ *
2294
+ * For instance, ToneDeltaPair(A, B, 15, 'darker', stayTogether) states that
2295
+ * A's tone should be at least 15 darker than B's.
2296
+ *
2297
+ * 'nearer' and 'farther' describes closeness to the surface roles. For
2298
+ * instance, ToneDeltaPair(A, B, 10, 'nearer', stayTogether) states that A
2299
+ * should be 10 lighter than B in light mode, and 10 darker than B in dark
2300
+ * mode.
2301
+ *
2302
+ * @param roleA The first role in a pair.
2303
+ * @param roleB The second role in a pair.
2304
+ * @param delta Required difference between tones. Absolute value, negative
2305
+ * values have undefined behavior.
2306
+ * @param polarity The relative relation between tones of roleA and roleB,
2307
+ * as described above.
2308
+ * @param stayTogether Whether these two roles should stay on the same side of
2309
+ * the "awkward zone" (T50-59). This is necessary for certain cases where
2310
+ * one role has two backgrounds.
2311
+ */
2312
+ constructor(roleA, roleB, delta, polarity, stayTogether) {
2313
+ this.roleA = roleA;
2314
+ this.roleB = roleB;
2315
+ this.delta = delta;
2316
+ this.polarity = polarity;
2317
+ this.stayTogether = stayTogether;
2318
+ }
2319
+ }
2320
+
2321
+ /**
2322
+ * @license
2323
+ * Copyright 2022 Google LLC
2324
+ *
2325
+ * Licensed under the Apache License, Version 2.0 (the "License");
2326
+ * you may not use this file except in compliance with the License.
2327
+ * You may obtain a copy of the License at
2328
+ *
2329
+ * http://www.apache.org/licenses/LICENSE-2.0
2330
+ *
2331
+ * Unless required by applicable law or agreed to in writing, software
2332
+ * distributed under the License is distributed on an "AS IS" BASIS,
2333
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2334
+ * See the License for the specific language governing permissions and
2335
+ * limitations under the License.
2336
+ */
2337
+ /**
2338
+ * Set of themes supported by Dynamic Color.
2339
+ * Instantiate the corresponding subclass, ex. SchemeTonalSpot, to create
2340
+ * colors corresponding to the theme.
2341
+ */
2342
+ var Variant;
2343
+ (function (Variant) {
2344
+ Variant[Variant["MONOCHROME"] = 0] = "MONOCHROME";
2345
+ Variant[Variant["NEUTRAL"] = 1] = "NEUTRAL";
2346
+ Variant[Variant["TONAL_SPOT"] = 2] = "TONAL_SPOT";
2347
+ Variant[Variant["VIBRANT"] = 3] = "VIBRANT";
2348
+ Variant[Variant["EXPRESSIVE"] = 4] = "EXPRESSIVE";
2349
+ Variant[Variant["FIDELITY"] = 5] = "FIDELITY";
2350
+ Variant[Variant["CONTENT"] = 6] = "CONTENT";
2351
+ Variant[Variant["RAINBOW"] = 7] = "RAINBOW";
2352
+ Variant[Variant["FRUIT_SALAD"] = 8] = "FRUIT_SALAD";
2353
+ })(Variant || (Variant = {}));
2354
+
2355
+ /**
2356
+ * @license
2357
+ * Copyright 2022 Google LLC
2358
+ *
2359
+ * Licensed under the Apache License, Version 2.0 (the "License");
2360
+ * you may not use this file except in compliance with the License.
2361
+ * You may obtain a copy of the License at
2362
+ *
2363
+ * http://www.apache.org/licenses/LICENSE-2.0
2364
+ *
2365
+ * Unless required by applicable law or agreed to in writing, software
2366
+ * distributed under the License is distributed on an "AS IS" BASIS,
2367
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2368
+ * See the License for the specific language governing permissions and
2369
+ * limitations under the License.
2370
+ */
2371
+ function isFidelity(scheme) {
2372
+ return scheme.variant === Variant.FIDELITY ||
2373
+ scheme.variant === Variant.CONTENT;
2374
+ }
2375
+ function isMonochrome(scheme) {
2376
+ return scheme.variant === Variant.MONOCHROME;
2377
+ }
2378
+ function findDesiredChromaByTone(hue, chroma, tone, byDecreasingTone) {
2379
+ let answer = tone;
2380
+ let closestToChroma = Hct.from(hue, chroma, tone);
2381
+ if (closestToChroma.chroma < chroma) {
2382
+ let chromaPeak = closestToChroma.chroma;
2383
+ while (closestToChroma.chroma < chroma) {
2384
+ answer += byDecreasingTone ? -1 : 1.0;
2385
+ const potentialSolution = Hct.from(hue, chroma, answer);
2386
+ if (chromaPeak > potentialSolution.chroma) {
2387
+ break;
2388
+ }
2389
+ if (Math.abs(potentialSolution.chroma - chroma) < 0.4) {
2390
+ break;
2391
+ }
2392
+ const potentialDelta = Math.abs(potentialSolution.chroma - chroma);
2393
+ const currentDelta = Math.abs(closestToChroma.chroma - chroma);
2394
+ if (potentialDelta < currentDelta) {
2395
+ closestToChroma = potentialSolution;
2396
+ }
2397
+ chromaPeak = Math.max(chromaPeak, potentialSolution.chroma);
2398
+ }
2399
+ }
2400
+ return answer;
2401
+ }
2402
+ /**
2403
+ * DynamicColors for the colors in the Material Design system.
2404
+ */
2405
+ // Material Color Utilities namespaces the various utilities it provides.
2406
+ // tslint:disable-next-line:class-as-namespace
2407
+ class MaterialDynamicColors {
2408
+ static highestSurface(s) {
2409
+ return s.isDark ? MaterialDynamicColors.surfaceBright :
2410
+ MaterialDynamicColors.surfaceDim;
2411
+ }
2412
+ }
2413
+ MaterialDynamicColors.contentAccentToneDelta = 15.0;
2414
+ MaterialDynamicColors.primaryPaletteKeyColor = DynamicColor.fromPalette({
2415
+ name: 'primary_palette_key_color',
2416
+ palette: (s) => s.primaryPalette,
2417
+ tone: (s) => s.primaryPalette.keyColor.tone,
2418
+ });
2419
+ MaterialDynamicColors.secondaryPaletteKeyColor = DynamicColor.fromPalette({
2420
+ name: 'secondary_palette_key_color',
2421
+ palette: (s) => s.secondaryPalette,
2422
+ tone: (s) => s.secondaryPalette.keyColor.tone,
2423
+ });
2424
+ MaterialDynamicColors.tertiaryPaletteKeyColor = DynamicColor.fromPalette({
2425
+ name: 'tertiary_palette_key_color',
2426
+ palette: (s) => s.tertiaryPalette,
2427
+ tone: (s) => s.tertiaryPalette.keyColor.tone,
2428
+ });
2429
+ MaterialDynamicColors.neutralPaletteKeyColor = DynamicColor.fromPalette({
2430
+ name: 'neutral_palette_key_color',
2431
+ palette: (s) => s.neutralPalette,
2432
+ tone: (s) => s.neutralPalette.keyColor.tone,
2433
+ });
2434
+ MaterialDynamicColors.neutralVariantPaletteKeyColor = DynamicColor.fromPalette({
2435
+ name: 'neutral_variant_palette_key_color',
2436
+ palette: (s) => s.neutralVariantPalette,
2437
+ tone: (s) => s.neutralVariantPalette.keyColor.tone,
2438
+ });
2439
+ MaterialDynamicColors.background = DynamicColor.fromPalette({
2440
+ name: 'background',
2441
+ palette: (s) => s.neutralPalette,
2442
+ tone: (s) => s.isDark ? 6 : 98,
2443
+ isBackground: true,
2444
+ });
2445
+ MaterialDynamicColors.onBackground = DynamicColor.fromPalette({
2446
+ name: 'on_background',
2447
+ palette: (s) => s.neutralPalette,
2448
+ tone: (s) => s.isDark ? 90 : 10,
2449
+ background: (s) => MaterialDynamicColors.background,
2450
+ contrastCurve: new ContrastCurve(3, 3, 4.5, 7),
2451
+ });
2452
+ MaterialDynamicColors.surface = DynamicColor.fromPalette({
2453
+ name: 'surface',
2454
+ palette: (s) => s.neutralPalette,
2455
+ tone: (s) => s.isDark ? 6 : 98,
2456
+ isBackground: true,
2457
+ });
2458
+ MaterialDynamicColors.surfaceDim = DynamicColor.fromPalette({
2459
+ name: 'surface_dim',
2460
+ palette: (s) => s.neutralPalette,
2461
+ tone: (s) => s.isDark ? 6 : new ContrastCurve(87, 87, 80, 75).get(s.contrastLevel),
2462
+ isBackground: true,
2463
+ });
2464
+ MaterialDynamicColors.surfaceBright = DynamicColor.fromPalette({
2465
+ name: 'surface_bright',
2466
+ palette: (s) => s.neutralPalette,
2467
+ tone: (s) => s.isDark ? new ContrastCurve(24, 24, 29, 34).get(s.contrastLevel) : 98,
2468
+ isBackground: true,
2469
+ });
2470
+ MaterialDynamicColors.surfaceContainerLowest = DynamicColor.fromPalette({
2471
+ name: 'surface_container_lowest',
2472
+ palette: (s) => s.neutralPalette,
2473
+ tone: (s) => s.isDark ? new ContrastCurve(4, 4, 2, 0).get(s.contrastLevel) : 100,
2474
+ isBackground: true,
2475
+ });
2476
+ MaterialDynamicColors.surfaceContainerLow = DynamicColor.fromPalette({
2477
+ name: 'surface_container_low',
2478
+ palette: (s) => s.neutralPalette,
2479
+ tone: (s) => s.isDark ?
2480
+ new ContrastCurve(10, 10, 11, 12).get(s.contrastLevel) :
2481
+ new ContrastCurve(96, 96, 96, 95).get(s.contrastLevel),
2482
+ isBackground: true,
2483
+ });
2484
+ MaterialDynamicColors.surfaceContainer = DynamicColor.fromPalette({
2485
+ name: 'surface_container',
2486
+ palette: (s) => s.neutralPalette,
2487
+ tone: (s) => s.isDark ?
2488
+ new ContrastCurve(12, 12, 16, 20).get(s.contrastLevel) :
2489
+ new ContrastCurve(94, 94, 92, 90).get(s.contrastLevel),
2490
+ isBackground: true,
2491
+ });
2492
+ MaterialDynamicColors.surfaceContainerHigh = DynamicColor.fromPalette({
2493
+ name: 'surface_container_high',
2494
+ palette: (s) => s.neutralPalette,
2495
+ tone: (s) => s.isDark ?
2496
+ new ContrastCurve(17, 17, 21, 25).get(s.contrastLevel) :
2497
+ new ContrastCurve(92, 92, 88, 85).get(s.contrastLevel),
2498
+ isBackground: true,
2499
+ });
2500
+ MaterialDynamicColors.surfaceContainerHighest = DynamicColor.fromPalette({
2501
+ name: 'surface_container_highest',
2502
+ palette: (s) => s.neutralPalette,
2503
+ tone: (s) => s.isDark ?
2504
+ new ContrastCurve(22, 22, 26, 30).get(s.contrastLevel) :
2505
+ new ContrastCurve(90, 90, 84, 80).get(s.contrastLevel),
2506
+ isBackground: true,
2507
+ });
2508
+ MaterialDynamicColors.onSurface = DynamicColor.fromPalette({
2509
+ name: 'on_surface',
2510
+ palette: (s) => s.neutralPalette,
2511
+ tone: (s) => s.isDark ? 90 : 10,
2512
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2513
+ contrastCurve: new ContrastCurve(4.5, 7, 11, 21),
2514
+ });
2515
+ MaterialDynamicColors.surfaceVariant = DynamicColor.fromPalette({
2516
+ name: 'surface_variant',
2517
+ palette: (s) => s.neutralVariantPalette,
2518
+ tone: (s) => s.isDark ? 30 : 90,
2519
+ isBackground: true,
2520
+ });
2521
+ MaterialDynamicColors.onSurfaceVariant = DynamicColor.fromPalette({
2522
+ name: 'on_surface_variant',
2523
+ palette: (s) => s.neutralVariantPalette,
2524
+ tone: (s) => s.isDark ? 80 : 30,
2525
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2526
+ contrastCurve: new ContrastCurve(3, 4.5, 7, 11),
2527
+ });
2528
+ MaterialDynamicColors.inverseSurface = DynamicColor.fromPalette({
2529
+ name: 'inverse_surface',
2530
+ palette: (s) => s.neutralPalette,
2531
+ tone: (s) => s.isDark ? 90 : 20,
2532
+ });
2533
+ MaterialDynamicColors.inverseOnSurface = DynamicColor.fromPalette({
2534
+ name: 'inverse_on_surface',
2535
+ palette: (s) => s.neutralPalette,
2536
+ tone: (s) => s.isDark ? 20 : 95,
2537
+ background: (s) => MaterialDynamicColors.inverseSurface,
2538
+ contrastCurve: new ContrastCurve(4.5, 7, 11, 21),
2539
+ });
2540
+ MaterialDynamicColors.outline = DynamicColor.fromPalette({
2541
+ name: 'outline',
2542
+ palette: (s) => s.neutralVariantPalette,
2543
+ tone: (s) => s.isDark ? 60 : 50,
2544
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2545
+ contrastCurve: new ContrastCurve(1.5, 3, 4.5, 7),
2546
+ });
2547
+ MaterialDynamicColors.outlineVariant = DynamicColor.fromPalette({
2548
+ name: 'outline_variant',
2549
+ palette: (s) => s.neutralVariantPalette,
2550
+ tone: (s) => s.isDark ? 30 : 80,
2551
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2552
+ contrastCurve: new ContrastCurve(1, 1, 3, 4.5),
2553
+ });
2554
+ MaterialDynamicColors.shadow = DynamicColor.fromPalette({
2555
+ name: 'shadow',
2556
+ palette: (s) => s.neutralPalette,
2557
+ tone: (s) => 0,
2558
+ });
2559
+ MaterialDynamicColors.scrim = DynamicColor.fromPalette({
2560
+ name: 'scrim',
2561
+ palette: (s) => s.neutralPalette,
2562
+ tone: (s) => 0,
2563
+ });
2564
+ MaterialDynamicColors.surfaceTint = DynamicColor.fromPalette({
2565
+ name: 'surface_tint',
2566
+ palette: (s) => s.primaryPalette,
2567
+ tone: (s) => s.isDark ? 80 : 40,
2568
+ isBackground: true,
2569
+ });
2570
+ MaterialDynamicColors.primary = DynamicColor.fromPalette({
2571
+ name: 'primary',
2572
+ palette: (s) => s.primaryPalette,
2573
+ tone: (s) => {
2574
+ if (isMonochrome(s)) {
2575
+ return s.isDark ? 100 : 0;
2576
+ }
2577
+ return s.isDark ? 80 : 40;
2578
+ },
2579
+ isBackground: true,
2580
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2581
+ contrastCurve: new ContrastCurve(3, 4.5, 7, 7),
2582
+ toneDeltaPair: (s) => new ToneDeltaPair(MaterialDynamicColors.primaryContainer, MaterialDynamicColors.primary, 10, 'nearer', false),
2583
+ });
2584
+ MaterialDynamicColors.onPrimary = DynamicColor.fromPalette({
2585
+ name: 'on_primary',
2586
+ palette: (s) => s.primaryPalette,
2587
+ tone: (s) => {
2588
+ if (isMonochrome(s)) {
2589
+ return s.isDark ? 10 : 90;
2590
+ }
2591
+ return s.isDark ? 20 : 100;
2592
+ },
2593
+ background: (s) => MaterialDynamicColors.primary,
2594
+ contrastCurve: new ContrastCurve(4.5, 7, 11, 21),
2595
+ });
2596
+ MaterialDynamicColors.primaryContainer = DynamicColor.fromPalette({
2597
+ name: 'primary_container',
2598
+ palette: (s) => s.primaryPalette,
2599
+ tone: (s) => {
2600
+ if (isFidelity(s)) {
2601
+ return s.sourceColorHct.tone;
2602
+ }
2603
+ if (isMonochrome(s)) {
2604
+ return s.isDark ? 85 : 25;
2605
+ }
2606
+ return s.isDark ? 30 : 90;
2607
+ },
2608
+ isBackground: true,
2609
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2610
+ contrastCurve: new ContrastCurve(1, 1, 3, 4.5),
2611
+ toneDeltaPair: (s) => new ToneDeltaPair(MaterialDynamicColors.primaryContainer, MaterialDynamicColors.primary, 10, 'nearer', false),
2612
+ });
2613
+ MaterialDynamicColors.onPrimaryContainer = DynamicColor.fromPalette({
2614
+ name: 'on_primary_container',
2615
+ palette: (s) => s.primaryPalette,
2616
+ tone: (s) => {
2617
+ if (isFidelity(s)) {
2618
+ return DynamicColor.foregroundTone(MaterialDynamicColors.primaryContainer.tone(s), 4.5);
2619
+ }
2620
+ if (isMonochrome(s)) {
2621
+ return s.isDark ? 0 : 100;
2622
+ }
2623
+ return s.isDark ? 90 : 30;
2624
+ },
2625
+ background: (s) => MaterialDynamicColors.primaryContainer,
2626
+ contrastCurve: new ContrastCurve(3, 4.5, 7, 11),
2627
+ });
2628
+ MaterialDynamicColors.inversePrimary = DynamicColor.fromPalette({
2629
+ name: 'inverse_primary',
2630
+ palette: (s) => s.primaryPalette,
2631
+ tone: (s) => s.isDark ? 40 : 80,
2632
+ background: (s) => MaterialDynamicColors.inverseSurface,
2633
+ contrastCurve: new ContrastCurve(3, 4.5, 7, 7),
2634
+ });
2635
+ MaterialDynamicColors.secondary = DynamicColor.fromPalette({
2636
+ name: 'secondary',
2637
+ palette: (s) => s.secondaryPalette,
2638
+ tone: (s) => s.isDark ? 80 : 40,
2639
+ isBackground: true,
2640
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2641
+ contrastCurve: new ContrastCurve(3, 4.5, 7, 7),
2642
+ toneDeltaPair: (s) => new ToneDeltaPair(MaterialDynamicColors.secondaryContainer, MaterialDynamicColors.secondary, 10, 'nearer', false),
2643
+ });
2644
+ MaterialDynamicColors.onSecondary = DynamicColor.fromPalette({
2645
+ name: 'on_secondary',
2646
+ palette: (s) => s.secondaryPalette,
2647
+ tone: (s) => {
2648
+ if (isMonochrome(s)) {
2649
+ return s.isDark ? 10 : 100;
2650
+ }
2651
+ else {
2652
+ return s.isDark ? 20 : 100;
2653
+ }
2654
+ },
2655
+ background: (s) => MaterialDynamicColors.secondary,
2656
+ contrastCurve: new ContrastCurve(4.5, 7, 11, 21),
2657
+ });
2658
+ MaterialDynamicColors.secondaryContainer = DynamicColor.fromPalette({
2659
+ name: 'secondary_container',
2660
+ palette: (s) => s.secondaryPalette,
2661
+ tone: (s) => {
2662
+ const initialTone = s.isDark ? 30 : 90;
2663
+ if (isMonochrome(s)) {
2664
+ return s.isDark ? 30 : 85;
2665
+ }
2666
+ if (!isFidelity(s)) {
2667
+ return initialTone;
2668
+ }
2669
+ return findDesiredChromaByTone(s.secondaryPalette.hue, s.secondaryPalette.chroma, initialTone, s.isDark ? false : true);
2670
+ },
2671
+ isBackground: true,
2672
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2673
+ contrastCurve: new ContrastCurve(1, 1, 3, 4.5),
2674
+ toneDeltaPair: (s) => new ToneDeltaPair(MaterialDynamicColors.secondaryContainer, MaterialDynamicColors.secondary, 10, 'nearer', false),
2675
+ });
2676
+ MaterialDynamicColors.onSecondaryContainer = DynamicColor.fromPalette({
2677
+ name: 'on_secondary_container',
2678
+ palette: (s) => s.secondaryPalette,
2679
+ tone: (s) => {
2680
+ if (isMonochrome(s)) {
2681
+ return s.isDark ? 90 : 10;
2682
+ }
2683
+ if (!isFidelity(s)) {
2684
+ return s.isDark ? 90 : 30;
2685
+ }
2686
+ return DynamicColor.foregroundTone(MaterialDynamicColors.secondaryContainer.tone(s), 4.5);
2687
+ },
2688
+ background: (s) => MaterialDynamicColors.secondaryContainer,
2689
+ contrastCurve: new ContrastCurve(3, 4.5, 7, 11),
2690
+ });
2691
+ MaterialDynamicColors.tertiary = DynamicColor.fromPalette({
2692
+ name: 'tertiary',
2693
+ palette: (s) => s.tertiaryPalette,
2694
+ tone: (s) => {
2695
+ if (isMonochrome(s)) {
2696
+ return s.isDark ? 90 : 25;
2697
+ }
2698
+ return s.isDark ? 80 : 40;
2699
+ },
2700
+ isBackground: true,
2701
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2702
+ contrastCurve: new ContrastCurve(3, 4.5, 7, 7),
2703
+ toneDeltaPair: (s) => new ToneDeltaPair(MaterialDynamicColors.tertiaryContainer, MaterialDynamicColors.tertiary, 10, 'nearer', false),
2704
+ });
2705
+ MaterialDynamicColors.onTertiary = DynamicColor.fromPalette({
2706
+ name: 'on_tertiary',
2707
+ palette: (s) => s.tertiaryPalette,
2708
+ tone: (s) => {
2709
+ if (isMonochrome(s)) {
2710
+ return s.isDark ? 10 : 90;
2711
+ }
2712
+ return s.isDark ? 20 : 100;
2713
+ },
2714
+ background: (s) => MaterialDynamicColors.tertiary,
2715
+ contrastCurve: new ContrastCurve(4.5, 7, 11, 21),
2716
+ });
2717
+ MaterialDynamicColors.tertiaryContainer = DynamicColor.fromPalette({
2718
+ name: 'tertiary_container',
2719
+ palette: (s) => s.tertiaryPalette,
2720
+ tone: (s) => {
2721
+ if (isMonochrome(s)) {
2722
+ return s.isDark ? 60 : 49;
2723
+ }
2724
+ if (!isFidelity(s)) {
2725
+ return s.isDark ? 30 : 90;
2726
+ }
2727
+ const proposedHct = s.tertiaryPalette.getHct(s.sourceColorHct.tone);
2728
+ return DislikeAnalyzer.fixIfDisliked(proposedHct).tone;
2729
+ },
2730
+ isBackground: true,
2731
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2732
+ contrastCurve: new ContrastCurve(1, 1, 3, 4.5),
2733
+ toneDeltaPair: (s) => new ToneDeltaPair(MaterialDynamicColors.tertiaryContainer, MaterialDynamicColors.tertiary, 10, 'nearer', false),
2734
+ });
2735
+ MaterialDynamicColors.onTertiaryContainer = DynamicColor.fromPalette({
2736
+ name: 'on_tertiary_container',
2737
+ palette: (s) => s.tertiaryPalette,
2738
+ tone: (s) => {
2739
+ if (isMonochrome(s)) {
2740
+ return s.isDark ? 0 : 100;
2741
+ }
2742
+ if (!isFidelity(s)) {
2743
+ return s.isDark ? 90 : 30;
2744
+ }
2745
+ return DynamicColor.foregroundTone(MaterialDynamicColors.tertiaryContainer.tone(s), 4.5);
2746
+ },
2747
+ background: (s) => MaterialDynamicColors.tertiaryContainer,
2748
+ contrastCurve: new ContrastCurve(3, 4.5, 7, 11),
2749
+ });
2750
+ MaterialDynamicColors.error = DynamicColor.fromPalette({
2751
+ name: 'error',
2752
+ palette: (s) => s.errorPalette,
2753
+ tone: (s) => s.isDark ? 80 : 40,
2754
+ isBackground: true,
2755
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2756
+ contrastCurve: new ContrastCurve(3, 4.5, 7, 7),
2757
+ toneDeltaPair: (s) => new ToneDeltaPair(MaterialDynamicColors.errorContainer, MaterialDynamicColors.error, 10, 'nearer', false),
2758
+ });
2759
+ MaterialDynamicColors.onError = DynamicColor.fromPalette({
2760
+ name: 'on_error',
2761
+ palette: (s) => s.errorPalette,
2762
+ tone: (s) => s.isDark ? 20 : 100,
2763
+ background: (s) => MaterialDynamicColors.error,
2764
+ contrastCurve: new ContrastCurve(4.5, 7, 11, 21),
2765
+ });
2766
+ MaterialDynamicColors.errorContainer = DynamicColor.fromPalette({
2767
+ name: 'error_container',
2768
+ palette: (s) => s.errorPalette,
2769
+ tone: (s) => s.isDark ? 30 : 90,
2770
+ isBackground: true,
2771
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2772
+ contrastCurve: new ContrastCurve(1, 1, 3, 4.5),
2773
+ toneDeltaPair: (s) => new ToneDeltaPair(MaterialDynamicColors.errorContainer, MaterialDynamicColors.error, 10, 'nearer', false),
2774
+ });
2775
+ MaterialDynamicColors.onErrorContainer = DynamicColor.fromPalette({
2776
+ name: 'on_error_container',
2777
+ palette: (s) => s.errorPalette,
2778
+ tone: (s) => {
2779
+ if (isMonochrome(s)) {
2780
+ return s.isDark ? 90 : 10;
2781
+ }
2782
+ return s.isDark ? 90 : 30;
2783
+ },
2784
+ background: (s) => MaterialDynamicColors.errorContainer,
2785
+ contrastCurve: new ContrastCurve(3, 4.5, 7, 11),
2786
+ });
2787
+ MaterialDynamicColors.primaryFixed = DynamicColor.fromPalette({
2788
+ name: 'primary_fixed',
2789
+ palette: (s) => s.primaryPalette,
2790
+ tone: (s) => isMonochrome(s) ? 40.0 : 90.0,
2791
+ isBackground: true,
2792
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2793
+ contrastCurve: new ContrastCurve(1, 1, 3, 4.5),
2794
+ toneDeltaPair: (s) => new ToneDeltaPair(MaterialDynamicColors.primaryFixed, MaterialDynamicColors.primaryFixedDim, 10, 'lighter', true),
2795
+ });
2796
+ MaterialDynamicColors.primaryFixedDim = DynamicColor.fromPalette({
2797
+ name: 'primary_fixed_dim',
2798
+ palette: (s) => s.primaryPalette,
2799
+ tone: (s) => isMonochrome(s) ? 30.0 : 80.0,
2800
+ isBackground: true,
2801
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2802
+ contrastCurve: new ContrastCurve(1, 1, 3, 4.5),
2803
+ toneDeltaPair: (s) => new ToneDeltaPair(MaterialDynamicColors.primaryFixed, MaterialDynamicColors.primaryFixedDim, 10, 'lighter', true),
2804
+ });
2805
+ MaterialDynamicColors.onPrimaryFixed = DynamicColor.fromPalette({
2806
+ name: 'on_primary_fixed',
2807
+ palette: (s) => s.primaryPalette,
2808
+ tone: (s) => isMonochrome(s) ? 100.0 : 10.0,
2809
+ background: (s) => MaterialDynamicColors.primaryFixedDim,
2810
+ secondBackground: (s) => MaterialDynamicColors.primaryFixed,
2811
+ contrastCurve: new ContrastCurve(4.5, 7, 11, 21),
2812
+ });
2813
+ MaterialDynamicColors.onPrimaryFixedVariant = DynamicColor.fromPalette({
2814
+ name: 'on_primary_fixed_variant',
2815
+ palette: (s) => s.primaryPalette,
2816
+ tone: (s) => isMonochrome(s) ? 90.0 : 30.0,
2817
+ background: (s) => MaterialDynamicColors.primaryFixedDim,
2818
+ secondBackground: (s) => MaterialDynamicColors.primaryFixed,
2819
+ contrastCurve: new ContrastCurve(3, 4.5, 7, 11),
2820
+ });
2821
+ MaterialDynamicColors.secondaryFixed = DynamicColor.fromPalette({
2822
+ name: 'secondary_fixed',
2823
+ palette: (s) => s.secondaryPalette,
2824
+ tone: (s) => isMonochrome(s) ? 80.0 : 90.0,
2825
+ isBackground: true,
2826
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2827
+ contrastCurve: new ContrastCurve(1, 1, 3, 4.5),
2828
+ toneDeltaPair: (s) => new ToneDeltaPair(MaterialDynamicColors.secondaryFixed, MaterialDynamicColors.secondaryFixedDim, 10, 'lighter', true),
2829
+ });
2830
+ MaterialDynamicColors.secondaryFixedDim = DynamicColor.fromPalette({
2831
+ name: 'secondary_fixed_dim',
2832
+ palette: (s) => s.secondaryPalette,
2833
+ tone: (s) => isMonochrome(s) ? 70.0 : 80.0,
2834
+ isBackground: true,
2835
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2836
+ contrastCurve: new ContrastCurve(1, 1, 3, 4.5),
2837
+ toneDeltaPair: (s) => new ToneDeltaPair(MaterialDynamicColors.secondaryFixed, MaterialDynamicColors.secondaryFixedDim, 10, 'lighter', true),
2838
+ });
2839
+ MaterialDynamicColors.onSecondaryFixed = DynamicColor.fromPalette({
2840
+ name: 'on_secondary_fixed',
2841
+ palette: (s) => s.secondaryPalette,
2842
+ tone: (s) => 10.0,
2843
+ background: (s) => MaterialDynamicColors.secondaryFixedDim,
2844
+ secondBackground: (s) => MaterialDynamicColors.secondaryFixed,
2845
+ contrastCurve: new ContrastCurve(4.5, 7, 11, 21),
2846
+ });
2847
+ MaterialDynamicColors.onSecondaryFixedVariant = DynamicColor.fromPalette({
2848
+ name: 'on_secondary_fixed_variant',
2849
+ palette: (s) => s.secondaryPalette,
2850
+ tone: (s) => isMonochrome(s) ? 25.0 : 30.0,
2851
+ background: (s) => MaterialDynamicColors.secondaryFixedDim,
2852
+ secondBackground: (s) => MaterialDynamicColors.secondaryFixed,
2853
+ contrastCurve: new ContrastCurve(3, 4.5, 7, 11),
2854
+ });
2855
+ MaterialDynamicColors.tertiaryFixed = DynamicColor.fromPalette({
2856
+ name: 'tertiary_fixed',
2857
+ palette: (s) => s.tertiaryPalette,
2858
+ tone: (s) => isMonochrome(s) ? 40.0 : 90.0,
2859
+ isBackground: true,
2860
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2861
+ contrastCurve: new ContrastCurve(1, 1, 3, 4.5),
2862
+ toneDeltaPair: (s) => new ToneDeltaPair(MaterialDynamicColors.tertiaryFixed, MaterialDynamicColors.tertiaryFixedDim, 10, 'lighter', true),
2863
+ });
2864
+ MaterialDynamicColors.tertiaryFixedDim = DynamicColor.fromPalette({
2865
+ name: 'tertiary_fixed_dim',
2866
+ palette: (s) => s.tertiaryPalette,
2867
+ tone: (s) => isMonochrome(s) ? 30.0 : 80.0,
2868
+ isBackground: true,
2869
+ background: (s) => MaterialDynamicColors.highestSurface(s),
2870
+ contrastCurve: new ContrastCurve(1, 1, 3, 4.5),
2871
+ toneDeltaPair: (s) => new ToneDeltaPair(MaterialDynamicColors.tertiaryFixed, MaterialDynamicColors.tertiaryFixedDim, 10, 'lighter', true),
2872
+ });
2873
+ MaterialDynamicColors.onTertiaryFixed = DynamicColor.fromPalette({
2874
+ name: 'on_tertiary_fixed',
2875
+ palette: (s) => s.tertiaryPalette,
2876
+ tone: (s) => isMonochrome(s) ? 100.0 : 10.0,
2877
+ background: (s) => MaterialDynamicColors.tertiaryFixedDim,
2878
+ secondBackground: (s) => MaterialDynamicColors.tertiaryFixed,
2879
+ contrastCurve: new ContrastCurve(4.5, 7, 11, 21),
2880
+ });
2881
+ MaterialDynamicColors.onTertiaryFixedVariant = DynamicColor.fromPalette({
2882
+ name: 'on_tertiary_fixed_variant',
2883
+ palette: (s) => s.tertiaryPalette,
2884
+ tone: (s) => isMonochrome(s) ? 90.0 : 30.0,
2885
+ background: (s) => MaterialDynamicColors.tertiaryFixedDim,
2886
+ secondBackground: (s) => MaterialDynamicColors.tertiaryFixed,
2887
+ contrastCurve: new ContrastCurve(3, 4.5, 7, 11),
2888
+ });
2889
+
2890
+ /**
2891
+ * @license
2892
+ * Copyright 2022 Google LLC
2893
+ *
2894
+ * Licensed under the Apache License, Version 2.0 (the "License");
2895
+ * you may not use this file except in compliance with the License.
2896
+ * You may obtain a copy of the License at
2897
+ *
2898
+ * http://www.apache.org/licenses/LICENSE-2.0
2899
+ *
2900
+ * Unless required by applicable law or agreed to in writing, software
2901
+ * distributed under the License is distributed on an "AS IS" BASIS,
2902
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2903
+ * See the License for the specific language governing permissions and
2904
+ * limitations under the License.
2905
+ */
2906
+ /**
2907
+ * Constructed by a set of values representing the current UI state (such as
2908
+ * whether or not its dark theme, what the theme style is, etc.), and
2909
+ * provides a set of TonalPalettes that can create colors that fit in
2910
+ * with the theme style. Used by DynamicColor to resolve into a color.
2911
+ */
2912
+ class DynamicScheme {
2913
+ constructor(args) {
2914
+ this.sourceColorArgb = args.sourceColorArgb;
2915
+ this.variant = args.variant;
2916
+ this.contrastLevel = args.contrastLevel;
2917
+ this.isDark = args.isDark;
2918
+ this.sourceColorHct = Hct.fromInt(args.sourceColorArgb);
2919
+ this.primaryPalette = args.primaryPalette;
2920
+ this.secondaryPalette = args.secondaryPalette;
2921
+ this.tertiaryPalette = args.tertiaryPalette;
2922
+ this.neutralPalette = args.neutralPalette;
2923
+ this.neutralVariantPalette = args.neutralVariantPalette;
2924
+ this.errorPalette = TonalPalette.fromHueAndChroma(25.0, 84.0);
2925
+ }
2926
+ /**
2927
+ * Support design spec'ing Dynamic Color by schemes that specify hue
2928
+ * rotations that should be applied at certain breakpoints.
2929
+ * @param sourceColor the source color of the theme, in HCT.
2930
+ * @param hues The "breakpoints", i.e. the hues at which a rotation should
2931
+ * be apply.
2932
+ * @param rotations The rotation that should be applied when source color's
2933
+ * hue is >= the same index in hues array, and <= the hue at the next index
2934
+ * in hues array.
2935
+ */
2936
+ static getRotatedHue(sourceColor, hues, rotations) {
2937
+ const sourceHue = sourceColor.hue;
2938
+ if (hues.length !== rotations.length) {
2939
+ throw new Error(`mismatch between hue length ${hues.length} & rotations ${rotations.length}`);
2940
+ }
2941
+ if (rotations.length === 1) {
2942
+ return sanitizeDegreesDouble(sourceColor.hue + rotations[0]);
2943
+ }
2944
+ const size = hues.length;
2945
+ for (let i = 0; i <= size - 2; i++) {
2946
+ const thisHue = hues[i];
2947
+ const nextHue = hues[i + 1];
2948
+ if (thisHue < sourceHue && sourceHue < nextHue) {
2949
+ return sanitizeDegreesDouble(sourceHue + rotations[i]);
2950
+ }
2951
+ }
2952
+ // If this statement executes, something is wrong, there should have been a
2953
+ // rotation found using the arrays.
2954
+ return sourceHue;
2955
+ }
2956
+ getArgb(dynamicColor) {
2957
+ return dynamicColor.getArgb(this);
2958
+ }
2959
+ getHct(dynamicColor) {
2960
+ return dynamicColor.getHct(this);
2961
+ }
2962
+ get primaryPaletteKeyColor() {
2963
+ return this.getArgb(MaterialDynamicColors.primaryPaletteKeyColor);
2964
+ }
2965
+ get secondaryPaletteKeyColor() {
2966
+ return this.getArgb(MaterialDynamicColors.secondaryPaletteKeyColor);
2967
+ }
2968
+ get tertiaryPaletteKeyColor() {
2969
+ return this.getArgb(MaterialDynamicColors.tertiaryPaletteKeyColor);
2970
+ }
2971
+ get neutralPaletteKeyColor() {
2972
+ return this.getArgb(MaterialDynamicColors.neutralPaletteKeyColor);
2973
+ }
2974
+ get neutralVariantPaletteKeyColor() {
2975
+ return this.getArgb(MaterialDynamicColors.neutralVariantPaletteKeyColor);
2976
+ }
2977
+ get background() {
2978
+ return this.getArgb(MaterialDynamicColors.background);
2979
+ }
2980
+ get onBackground() {
2981
+ return this.getArgb(MaterialDynamicColors.onBackground);
2982
+ }
2983
+ get surface() {
2984
+ return this.getArgb(MaterialDynamicColors.surface);
2985
+ }
2986
+ get surfaceDim() {
2987
+ return this.getArgb(MaterialDynamicColors.surfaceDim);
2988
+ }
2989
+ get surfaceBright() {
2990
+ return this.getArgb(MaterialDynamicColors.surfaceBright);
2991
+ }
2992
+ get surfaceContainerLowest() {
2993
+ return this.getArgb(MaterialDynamicColors.surfaceContainerLowest);
2994
+ }
2995
+ get surfaceContainerLow() {
2996
+ return this.getArgb(MaterialDynamicColors.surfaceContainerLow);
2997
+ }
2998
+ get surfaceContainer() {
2999
+ return this.getArgb(MaterialDynamicColors.surfaceContainer);
3000
+ }
3001
+ get surfaceContainerHigh() {
3002
+ return this.getArgb(MaterialDynamicColors.surfaceContainerHigh);
3003
+ }
3004
+ get surfaceContainerHighest() {
3005
+ return this.getArgb(MaterialDynamicColors.surfaceContainerHighest);
3006
+ }
3007
+ get onSurface() {
3008
+ return this.getArgb(MaterialDynamicColors.onSurface);
3009
+ }
3010
+ get surfaceVariant() {
3011
+ return this.getArgb(MaterialDynamicColors.surfaceVariant);
3012
+ }
3013
+ get onSurfaceVariant() {
3014
+ return this.getArgb(MaterialDynamicColors.onSurfaceVariant);
3015
+ }
3016
+ get inverseSurface() {
3017
+ return this.getArgb(MaterialDynamicColors.inverseSurface);
3018
+ }
3019
+ get inverseOnSurface() {
3020
+ return this.getArgb(MaterialDynamicColors.inverseOnSurface);
3021
+ }
3022
+ get outline() {
3023
+ return this.getArgb(MaterialDynamicColors.outline);
3024
+ }
3025
+ get outlineVariant() {
3026
+ return this.getArgb(MaterialDynamicColors.outlineVariant);
3027
+ }
3028
+ get shadow() {
3029
+ return this.getArgb(MaterialDynamicColors.shadow);
3030
+ }
3031
+ get scrim() {
3032
+ return this.getArgb(MaterialDynamicColors.scrim);
3033
+ }
3034
+ get surfaceTint() {
3035
+ return this.getArgb(MaterialDynamicColors.surfaceTint);
3036
+ }
3037
+ get primary() {
3038
+ return this.getArgb(MaterialDynamicColors.primary);
3039
+ }
3040
+ get onPrimary() {
3041
+ return this.getArgb(MaterialDynamicColors.onPrimary);
3042
+ }
3043
+ get primaryContainer() {
3044
+ return this.getArgb(MaterialDynamicColors.primaryContainer);
3045
+ }
3046
+ get onPrimaryContainer() {
3047
+ return this.getArgb(MaterialDynamicColors.onPrimaryContainer);
3048
+ }
3049
+ get inversePrimary() {
3050
+ return this.getArgb(MaterialDynamicColors.inversePrimary);
3051
+ }
3052
+ get secondary() {
3053
+ return this.getArgb(MaterialDynamicColors.secondary);
3054
+ }
3055
+ get onSecondary() {
3056
+ return this.getArgb(MaterialDynamicColors.onSecondary);
3057
+ }
3058
+ get secondaryContainer() {
3059
+ return this.getArgb(MaterialDynamicColors.secondaryContainer);
3060
+ }
3061
+ get onSecondaryContainer() {
3062
+ return this.getArgb(MaterialDynamicColors.onSecondaryContainer);
3063
+ }
3064
+ get tertiary() {
3065
+ return this.getArgb(MaterialDynamicColors.tertiary);
3066
+ }
3067
+ get onTertiary() {
3068
+ return this.getArgb(MaterialDynamicColors.onTertiary);
3069
+ }
3070
+ get tertiaryContainer() {
3071
+ return this.getArgb(MaterialDynamicColors.tertiaryContainer);
3072
+ }
3073
+ get onTertiaryContainer() {
3074
+ return this.getArgb(MaterialDynamicColors.onTertiaryContainer);
3075
+ }
3076
+ get error() {
3077
+ return this.getArgb(MaterialDynamicColors.error);
3078
+ }
3079
+ get onError() {
3080
+ return this.getArgb(MaterialDynamicColors.onError);
3081
+ }
3082
+ get errorContainer() {
3083
+ return this.getArgb(MaterialDynamicColors.errorContainer);
3084
+ }
3085
+ get onErrorContainer() {
3086
+ return this.getArgb(MaterialDynamicColors.onErrorContainer);
3087
+ }
3088
+ get primaryFixed() {
3089
+ return this.getArgb(MaterialDynamicColors.primaryFixed);
3090
+ }
3091
+ get primaryFixedDim() {
3092
+ return this.getArgb(MaterialDynamicColors.primaryFixedDim);
3093
+ }
3094
+ get onPrimaryFixed() {
3095
+ return this.getArgb(MaterialDynamicColors.onPrimaryFixed);
3096
+ }
3097
+ get onPrimaryFixedVariant() {
3098
+ return this.getArgb(MaterialDynamicColors.onPrimaryFixedVariant);
3099
+ }
3100
+ get secondaryFixed() {
3101
+ return this.getArgb(MaterialDynamicColors.secondaryFixed);
3102
+ }
3103
+ get secondaryFixedDim() {
3104
+ return this.getArgb(MaterialDynamicColors.secondaryFixedDim);
3105
+ }
3106
+ get onSecondaryFixed() {
3107
+ return this.getArgb(MaterialDynamicColors.onSecondaryFixed);
3108
+ }
3109
+ get onSecondaryFixedVariant() {
3110
+ return this.getArgb(MaterialDynamicColors.onSecondaryFixedVariant);
3111
+ }
3112
+ get tertiaryFixed() {
3113
+ return this.getArgb(MaterialDynamicColors.tertiaryFixed);
3114
+ }
3115
+ get tertiaryFixedDim() {
3116
+ return this.getArgb(MaterialDynamicColors.tertiaryFixedDim);
3117
+ }
3118
+ get onTertiaryFixed() {
3119
+ return this.getArgb(MaterialDynamicColors.onTertiaryFixed);
3120
+ }
3121
+ get onTertiaryFixedVariant() {
3122
+ return this.getArgb(MaterialDynamicColors.onTertiaryFixedVariant);
3123
+ }
3124
+ }
3125
+
3126
+ /**
3127
+ * @license
3128
+ * Copyright 2021 Google LLC
3129
+ *
3130
+ * Licensed under the Apache License, Version 2.0 (the "License");
3131
+ * you may not use this file except in compliance with the License.
3132
+ * You may obtain a copy of the License at
3133
+ *
3134
+ * http://www.apache.org/licenses/LICENSE-2.0
3135
+ *
3136
+ * Unless required by applicable law or agreed to in writing, software
3137
+ * distributed under the License is distributed on an "AS IS" BASIS,
3138
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3139
+ * See the License for the specific language governing permissions and
3140
+ * limitations under the License.
3141
+ */
3142
+ /**
3143
+ * An intermediate concept between the key color for a UI theme, and a full
3144
+ * color scheme. 5 sets of tones are generated, all except one use the same hue
3145
+ * as the key color, and all vary in chroma.
3146
+ */
3147
+ class CorePalette {
3148
+ /**
3149
+ * @param argb ARGB representation of a color
3150
+ */
3151
+ static of(argb) {
3152
+ return new CorePalette(argb, false);
3153
+ }
3154
+ /**
3155
+ * @param argb ARGB representation of a color
3156
+ */
3157
+ static contentOf(argb) {
3158
+ return new CorePalette(argb, true);
3159
+ }
3160
+ /**
3161
+ * Create a [CorePalette] from a set of colors
3162
+ */
3163
+ static fromColors(colors) {
3164
+ return CorePalette.createPaletteFromColors(false, colors);
3165
+ }
3166
+ /**
3167
+ * Create a content [CorePalette] from a set of colors
3168
+ */
3169
+ static contentFromColors(colors) {
3170
+ return CorePalette.createPaletteFromColors(true, colors);
3171
+ }
3172
+ static createPaletteFromColors(content, colors) {
3173
+ const palette = new CorePalette(colors.primary, content);
3174
+ if (colors.secondary) {
3175
+ const p = new CorePalette(colors.secondary, content);
3176
+ palette.a2 = p.a1;
3177
+ }
3178
+ if (colors.tertiary) {
3179
+ const p = new CorePalette(colors.tertiary, content);
3180
+ palette.a3 = p.a1;
3181
+ }
3182
+ if (colors.error) {
3183
+ const p = new CorePalette(colors.error, content);
3184
+ palette.error = p.a1;
3185
+ }
3186
+ if (colors.neutral) {
3187
+ const p = new CorePalette(colors.neutral, content);
3188
+ palette.n1 = p.n1;
3189
+ }
3190
+ if (colors.neutralVariant) {
3191
+ const p = new CorePalette(colors.neutralVariant, content);
3192
+ palette.n2 = p.n2;
3193
+ }
3194
+ return palette;
3195
+ }
3196
+ constructor(argb, isContent) {
3197
+ const hct = Hct.fromInt(argb);
3198
+ const hue = hct.hue;
3199
+ const chroma = hct.chroma;
3200
+ if (isContent) {
3201
+ this.a1 = TonalPalette.fromHueAndChroma(hue, chroma);
3202
+ this.a2 = TonalPalette.fromHueAndChroma(hue, chroma / 3);
3203
+ this.a3 = TonalPalette.fromHueAndChroma(hue + 60, chroma / 2);
3204
+ this.n1 = TonalPalette.fromHueAndChroma(hue, Math.min(chroma / 12, 4));
3205
+ this.n2 = TonalPalette.fromHueAndChroma(hue, Math.min(chroma / 6, 8));
3206
+ }
3207
+ else {
3208
+ this.a1 = TonalPalette.fromHueAndChroma(hue, Math.max(48, chroma));
3209
+ this.a2 = TonalPalette.fromHueAndChroma(hue, 16);
3210
+ this.a3 = TonalPalette.fromHueAndChroma(hue + 60, 24);
3211
+ this.n1 = TonalPalette.fromHueAndChroma(hue, 4);
3212
+ this.n2 = TonalPalette.fromHueAndChroma(hue, 8);
3213
+ }
3214
+ this.error = TonalPalette.fromHueAndChroma(25, 84);
3215
+ }
3216
+ }
3217
+
3218
+ /**
3219
+ * @license
3220
+ * Copyright 2022 Google LLC
3221
+ *
3222
+ * Licensed under the Apache License, Version 2.0 (the "License");
3223
+ * you may not use this file except in compliance with the License.
3224
+ * You may obtain a copy of the License at
3225
+ *
3226
+ * http://www.apache.org/licenses/LICENSE-2.0
3227
+ *
3228
+ * Unless required by applicable law or agreed to in writing, software
3229
+ * distributed under the License is distributed on an "AS IS" BASIS,
3230
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3231
+ * See the License for the specific language governing permissions and
3232
+ * limitations under the License.
3233
+ */
3234
+ /**
3235
+ * A Dynamic Color theme that is intentionally detached from the source color.
3236
+ */
3237
+ class SchemeExpressive extends DynamicScheme {
3238
+ constructor(sourceColorHct, isDark, contrastLevel) {
3239
+ super({
3240
+ sourceColorArgb: sourceColorHct.toInt(),
3241
+ variant: Variant.EXPRESSIVE,
3242
+ contrastLevel,
3243
+ isDark,
3244
+ primaryPalette: TonalPalette.fromHueAndChroma(sanitizeDegreesDouble(sourceColorHct.hue + 240.0), 40.0),
3245
+ secondaryPalette: TonalPalette.fromHueAndChroma(DynamicScheme.getRotatedHue(sourceColorHct, SchemeExpressive.hues, SchemeExpressive.secondaryRotations), 24.0),
3246
+ tertiaryPalette: TonalPalette.fromHueAndChroma(DynamicScheme.getRotatedHue(sourceColorHct, SchemeExpressive.hues, SchemeExpressive.tertiaryRotations), 32.0),
3247
+ neutralPalette: TonalPalette.fromHueAndChroma(sourceColorHct.hue + 15, 8.0),
3248
+ neutralVariantPalette: TonalPalette.fromHueAndChroma(sourceColorHct.hue + 15, 12.0),
3249
+ });
3250
+ }
3251
+ }
3252
+ /**
3253
+ * Hues (in degrees) used at breakpoints such that designers can specify a
3254
+ * hue rotation that occurs at a given break point.
3255
+ */
3256
+ SchemeExpressive.hues = [
3257
+ 0.0,
3258
+ 21.0,
3259
+ 51.0,
3260
+ 121.0,
3261
+ 151.0,
3262
+ 191.0,
3263
+ 271.0,
3264
+ 321.0,
3265
+ 360.0,
3266
+ ];
3267
+ /**
3268
+ * Hue rotations (in degrees) of the Secondary [TonalPalette],
3269
+ * corresponding to the breakpoints in [hues].
3270
+ */
3271
+ SchemeExpressive.secondaryRotations = [
3272
+ 45.0,
3273
+ 95.0,
3274
+ 45.0,
3275
+ 20.0,
3276
+ 45.0,
3277
+ 90.0,
3278
+ 45.0,
3279
+ 45.0,
3280
+ 45.0,
3281
+ ];
3282
+ /**
3283
+ * Hue rotations (in degrees) of the Tertiary [TonalPalette],
3284
+ * corresponding to the breakpoints in [hues].
3285
+ */
3286
+ SchemeExpressive.tertiaryRotations = [
3287
+ 120.0,
3288
+ 120.0,
3289
+ 20.0,
3290
+ 45.0,
3291
+ 20.0,
3292
+ 15.0,
3293
+ 20.0,
3294
+ 120.0,
3295
+ 120.0,
3296
+ ];
3297
+
3298
+ /**
3299
+ * @license
3300
+ * Copyright 2022 Google LLC
3301
+ *
3302
+ * Licensed under the Apache License, Version 2.0 (the "License");
3303
+ * you may not use this file except in compliance with the License.
3304
+ * You may obtain a copy of the License at
3305
+ *
3306
+ * http://www.apache.org/licenses/LICENSE-2.0
3307
+ *
3308
+ * Unless required by applicable law or agreed to in writing, software
3309
+ * distributed under the License is distributed on an "AS IS" BASIS,
3310
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3311
+ * See the License for the specific language governing permissions and
3312
+ * limitations under the License.
3313
+ */
3314
+ /**
3315
+ * A Dynamic Color theme that maxes out colorfulness at each position in the
3316
+ * Primary Tonal Palette.
3317
+ */
3318
+ class SchemeVibrant extends DynamicScheme {
3319
+ constructor(sourceColorHct, isDark, contrastLevel) {
3320
+ super({
3321
+ sourceColorArgb: sourceColorHct.toInt(),
3322
+ variant: Variant.VIBRANT,
3323
+ contrastLevel,
3324
+ isDark,
3325
+ primaryPalette: TonalPalette.fromHueAndChroma(sourceColorHct.hue, 200.0),
3326
+ secondaryPalette: TonalPalette.fromHueAndChroma(DynamicScheme.getRotatedHue(sourceColorHct, SchemeVibrant.hues, SchemeVibrant.secondaryRotations), 24.0),
3327
+ tertiaryPalette: TonalPalette.fromHueAndChroma(DynamicScheme.getRotatedHue(sourceColorHct, SchemeVibrant.hues, SchemeVibrant.tertiaryRotations), 32.0),
3328
+ neutralPalette: TonalPalette.fromHueAndChroma(sourceColorHct.hue, 10.0),
3329
+ neutralVariantPalette: TonalPalette.fromHueAndChroma(sourceColorHct.hue, 12.0),
3330
+ });
3331
+ }
3332
+ }
3333
+ /**
3334
+ * Hues (in degrees) used at breakpoints such that designers can specify a
3335
+ * hue rotation that occurs at a given break point.
3336
+ */
3337
+ SchemeVibrant.hues = [
3338
+ 0.0,
3339
+ 41.0,
3340
+ 61.0,
3341
+ 101.0,
3342
+ 131.0,
3343
+ 181.0,
3344
+ 251.0,
3345
+ 301.0,
3346
+ 360.0,
3347
+ ];
3348
+ /**
3349
+ * Hue rotations (in degrees) of the Secondary [TonalPalette],
3350
+ * corresponding to the breakpoints in [hues].
3351
+ */
3352
+ SchemeVibrant.secondaryRotations = [
3353
+ 18.0,
3354
+ 15.0,
3355
+ 10.0,
3356
+ 12.0,
3357
+ 15.0,
3358
+ 18.0,
3359
+ 15.0,
3360
+ 12.0,
3361
+ 12.0,
3362
+ ];
3363
+ /**
3364
+ * Hue rotations (in degrees) of the Tertiary [TonalPalette],
3365
+ * corresponding to the breakpoints in [hues].
3366
+ */
3367
+ SchemeVibrant.tertiaryRotations = [
3368
+ 35.0,
3369
+ 30.0,
3370
+ 20.0,
3371
+ 25.0,
3372
+ 30.0,
3373
+ 35.0,
3374
+ 30.0,
3375
+ 25.0,
3376
+ 25.0,
3377
+ ];
3378
+
3379
+ /**
3380
+ * @license
3381
+ * Copyright 2021 Google LLC
3382
+ *
3383
+ * Licensed under the Apache License, Version 2.0 (the "License");
3384
+ * you may not use this file except in compliance with the License.
3385
+ * You may obtain a copy of the License at
3386
+ *
3387
+ * http://www.apache.org/licenses/LICENSE-2.0
3388
+ *
3389
+ * Unless required by applicable law or agreed to in writing, software
3390
+ * distributed under the License is distributed on an "AS IS" BASIS,
3391
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3392
+ * See the License for the specific language governing permissions and
3393
+ * limitations under the License.
3394
+ */
3395
+ /**
3396
+ * Utility methods for hexadecimal representations of colors.
3397
+ */
3398
+ /**
3399
+ * @param argb ARGB representation of a color.
3400
+ * @return Hex string representing color, ex. #ff0000 for red.
3401
+ */
3402
+ function hexFromArgb(argb) {
3403
+ const r = redFromArgb(argb);
3404
+ const g = greenFromArgb(argb);
3405
+ const b = blueFromArgb(argb);
3406
+ const outParts = [r.toString(16), g.toString(16), b.toString(16)];
3407
+ // Pad single-digit output values
3408
+ for (const [i, part] of outParts.entries()) {
3409
+ if (part.length === 1) {
3410
+ outParts[i] = '0' + part;
3411
+ }
3412
+ }
3413
+ return '#' + outParts.join('');
3414
+ }
3415
+ /**
3416
+ * @param hex String representing color as hex code. Accepts strings with or
3417
+ * without leading #, and string representing the color using 3, 6, or 8
3418
+ * hex characters.
3419
+ * @return ARGB representation of color.
3420
+ */
3421
+ function argbFromHex(hex) {
3422
+ hex = hex.replace('#', '');
3423
+ const isThree = hex.length === 3;
3424
+ const isSix = hex.length === 6;
3425
+ const isEight = hex.length === 8;
3426
+ if (!isThree && !isSix && !isEight) {
3427
+ throw new Error('unexpected hex ' + hex);
3428
+ }
3429
+ let r = 0;
3430
+ let g = 0;
3431
+ let b = 0;
3432
+ if (isThree) {
3433
+ r = parseIntHex(hex.slice(0, 1).repeat(2));
3434
+ g = parseIntHex(hex.slice(1, 2).repeat(2));
3435
+ b = parseIntHex(hex.slice(2, 3).repeat(2));
3436
+ }
3437
+ else if (isSix) {
3438
+ r = parseIntHex(hex.slice(0, 2));
3439
+ g = parseIntHex(hex.slice(2, 4));
3440
+ b = parseIntHex(hex.slice(4, 6));
3441
+ }
3442
+ else if (isEight) {
3443
+ r = parseIntHex(hex.slice(2, 4));
3444
+ g = parseIntHex(hex.slice(4, 6));
3445
+ b = parseIntHex(hex.slice(6, 8));
3446
+ }
3447
+ return (((255 << 24) | ((r & 0x0ff) << 16) | ((g & 0x0ff) << 8) | (b & 0x0ff)) >>>
3448
+ 0);
3449
+ }
3450
+ function parseIntHex(value) {
3451
+ // tslint:disable-next-line:ban
3452
+ return parseInt(value, 16);
3453
+ }
3454
+
3455
+ var _M3eThemeElement_instances, _M3eThemeElement_styleSheet, _M3eThemeElement_firstUpdated, _M3eThemeElement_light, _M3eThemeElement_dark, _M3eThemeElement_forcedColor, _M3eThemeElement_colorSchemeChangeHandler, _M3eThemeElement_apply, _M3eThemeElement_getVariant, _M3eThemeElement_getContrastLevel;
3456
+ /**
3457
+ * @summary
3458
+ * A non-visual element responsible for application-level theming.
3459
+ *
3460
+ * @description
3461
+ * The `m3e-theme` component is a non-visual element used to apply dynamic color, expressive motion, density, and strong focus indicators
3462
+ * to nested, theme-aware elements.
3463
+ *
3464
+ * When `m3e-theme` is nested directly beneath the `<body>` of a document, the `<body>`'s `background-color` is set to the computed
3465
+ * value of `--md-sys-color-background` and `color` is set to the computed value of `--md-sys-color-on-background`. In addition,
3466
+ * the document's `scrollbar-color` is set to the computed values of `--m3e-scrollbar-thumb-color` and `--m3e-scrollbar-track-color` which,
3467
+ * when supported, cascades to all viewport scrollbars.
3468
+ *
3469
+ * @example
3470
+ * The following example adds a top-level `m3e-theme` directly beneath a document's `<body>` element to
3471
+ * apply application-level theming. In this example, `color` and `scheme` are used to create a dynamic color
3472
+ * palette which automatically adjusts to a user's light or dark color preference. In addition, expressive motion
3473
+ * and strong focus indicators are enabled.
3474
+ *
3475
+ * ```html
3476
+ * <body>
3477
+ * <m3e-theme color="#7D67BE" scheme="auto" motion="expressive" strong-focus>
3478
+ * <!-- Normal body content here. -->
3479
+ * </m3e-theme>
3480
+ * <body/>
3481
+ * ```
3482
+ * @tag m3e-theme
3483
+ *
3484
+ * @attr color - The hex color from which to derive dynamic color palettes.
3485
+ * @attr contrast - The contrast level of the theme.
3486
+ * @attr density - The density scale (0, -1, -2).
3487
+ * @attr scheme - The color scheme of the theme.
3488
+ * @attr strong-focus - Whether to enable strong focus indicators.
3489
+ * @attr variant - The color variant of the theme.
3490
+ *
3491
+ * @fires change - Dispatched when the theme changes.
3492
+ */
3493
+ let M3eThemeElement = class M3eThemeElement extends Role(LitElement, "none") {
3494
+ constructor() {
3495
+ super(...arguments);
3496
+ _M3eThemeElement_instances.add(this);
3497
+ /** @private */ _M3eThemeElement_styleSheet.set(this, new CSSStyleSheet());
3498
+ /** @private */ _M3eThemeElement_firstUpdated.set(this, false);
3499
+ /** @private */ _M3eThemeElement_light.set(this, void 0);
3500
+ /** @private */ _M3eThemeElement_dark.set(this, void 0);
3501
+ /** @private */ _M3eThemeElement_forcedColor.set(this, void 0);
3502
+ /** @private */ _M3eThemeElement_colorSchemeChangeHandler.set(this, () => __classPrivateFieldGet(this, _M3eThemeElement_instances, "m", _M3eThemeElement_apply).call(this));
3503
+ /**
3504
+ * The hex color from which to derive dynamic color palettes.
3505
+ * @default "#7D67BE"
3506
+ */
3507
+ this.color = "#7D67BE";
3508
+ /**
3509
+ * The color variant of the theme.
3510
+ * @default "vibrant"
3511
+ */
3512
+ this.variant = "vibrant";
3513
+ /**
3514
+ * The color scheme of the theme.
3515
+ * @default "auto"
3516
+ */
3517
+ this.scheme = "auto";
3518
+ /**
3519
+ * The contrast level of the theme.
3520
+ * @default "standard"
3521
+ */
3522
+ this.contrast = "standard";
3523
+ /**
3524
+ * Whether to enable strong focus indicators.
3525
+ * @default false
3526
+ */
3527
+ this.strongFocus = false;
3528
+ /**
3529
+ * The density scale (0, -1, -2).
3530
+ * @default 0
3531
+ */
3532
+ this.density = 0;
3533
+ /** The motion scheme.
3534
+ * @default "standard"
3535
+ */
3536
+ this.motion = "standard";
3537
+ }
3538
+ /** Whether a dark theme is applied. */
3539
+ get isDark() {
3540
+ switch (this.scheme) {
3541
+ case "light":
3542
+ return false;
3543
+ case "dark":
3544
+ return true;
3545
+ default: // auto
3546
+ return __classPrivateFieldGet(this, _M3eThemeElement_dark, "f")?.matches ?? false;
3547
+ }
3548
+ }
3549
+ /** @inheritdoc */
3550
+ connectedCallback() {
3551
+ super.connectedCallback();
3552
+ if (this.shadowRoot && !this.shadowRoot.adoptedStyleSheets.includes(__classPrivateFieldGet(this, _M3eThemeElement_styleSheet, "f"))) {
3553
+ this.shadowRoot.adoptedStyleSheets = [...this.shadowRoot.adoptedStyleSheets, __classPrivateFieldGet(this, _M3eThemeElement_styleSheet, "f")];
3554
+ }
3555
+ __classPrivateFieldSet(this, _M3eThemeElement_light, matchMedia("(prefers-color-scheme: light)"), "f");
3556
+ __classPrivateFieldSet(this, _M3eThemeElement_dark, matchMedia("(prefers-color-scheme: dark)"), "f");
3557
+ __classPrivateFieldSet(this, _M3eThemeElement_forcedColor, matchMedia("(forced-colors: active)"), "f");
3558
+ [__classPrivateFieldGet(this, _M3eThemeElement_light, "f"), __classPrivateFieldGet(this, _M3eThemeElement_dark, "f"), __classPrivateFieldGet(this, _M3eThemeElement_forcedColor, "f")].forEach((x) => x.addEventListener("change", __classPrivateFieldGet(this, _M3eThemeElement_colorSchemeChangeHandler, "f")));
3559
+ }
3560
+ /** @inheritdoc */
3561
+ disconnectedCallback() {
3562
+ super.disconnectedCallback();
3563
+ [__classPrivateFieldGet(this, _M3eThemeElement_light, "f"), __classPrivateFieldGet(this, _M3eThemeElement_dark, "f"), __classPrivateFieldGet(this, _M3eThemeElement_forcedColor, "f")].forEach((x) => x?.removeEventListener("change", __classPrivateFieldGet(this, _M3eThemeElement_colorSchemeChangeHandler, "f")));
3564
+ __classPrivateFieldSet(this, _M3eThemeElement_light, __classPrivateFieldSet(this, _M3eThemeElement_dark, __classPrivateFieldSet(this, _M3eThemeElement_forcedColor, undefined, "f"), "f"), "f");
3565
+ }
3566
+ /** @inheritdoc */
3567
+ updated(_changedProperties) {
3568
+ super.updated(_changedProperties);
3569
+ __classPrivateFieldGet(this, _M3eThemeElement_instances, "m", _M3eThemeElement_apply).call(this);
3570
+ }
3571
+ /** @inheritdoc */
3572
+ firstUpdated(_changedProperties) {
3573
+ super.firstUpdated(_changedProperties);
3574
+ __classPrivateFieldSet(this, _M3eThemeElement_firstUpdated, true, "f");
3575
+ }
3576
+ /** @inheritdoc */
3577
+ render() {
3578
+ return html `<slot></slot>`;
3579
+ }
3580
+ };
3581
+ _M3eThemeElement_styleSheet = new WeakMap();
3582
+ _M3eThemeElement_firstUpdated = new WeakMap();
3583
+ _M3eThemeElement_light = new WeakMap();
3584
+ _M3eThemeElement_dark = new WeakMap();
3585
+ _M3eThemeElement_forcedColor = new WeakMap();
3586
+ _M3eThemeElement_colorSchemeChangeHandler = new WeakMap();
3587
+ _M3eThemeElement_instances = new WeakSet();
3588
+ _M3eThemeElement_apply = function _M3eThemeElement_apply() {
3589
+ const color = argbFromHex(this.color);
3590
+ const palette = CorePalette.of(color);
3591
+ const scheme = new DynamicScheme({
3592
+ sourceColorArgb: color,
3593
+ variant: __classPrivateFieldGet(this, _M3eThemeElement_instances, "m", _M3eThemeElement_getVariant).call(this),
3594
+ contrastLevel: __classPrivateFieldGet(this, _M3eThemeElement_instances, "m", _M3eThemeElement_getContrastLevel).call(this),
3595
+ isDark: this.isDark,
3596
+ primaryPalette: palette.a1,
3597
+ secondaryPalette: palette.a2,
3598
+ tertiaryPalette: palette.a3,
3599
+ neutralPalette: palette.n1,
3600
+ neutralVariantPalette: palette.n2,
3601
+ });
3602
+ let css = "";
3603
+ for (const key in MaterialDynamicColors) {
3604
+ if (!key.endsWith("PaletteKeyColor")) {
3605
+ const dynamicColor = MaterialDynamicColors[key];
3606
+ if (dynamicColor instanceof DynamicColor) {
3607
+ css += `--md-sys-color-${key.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase()}: ${hexFromArgb(dynamicColor.getArgb(scheme))};`;
3608
+ }
3609
+ }
3610
+ }
3611
+ if (this.motion === "expressive") {
3612
+ css += "--md-sys-motion-spring-fast-spatial: 350ms cubic-bezier(0.42, 1.67, 0.21, 0.90);";
3613
+ css += "--md-sys-motion-spring-default-spatial: 500ms cubic-bezier(0.38, 1.21, 0.22, 1.00);";
3614
+ css += "--md-sys-motion-spring-slow-spatial: 650ms cubic-bezier(0.39, 1.29, 0.35, 0.98);";
3615
+ css += "--md-sys-motion-spring-fast-effects: 150ms cubic-bezier(0.31, 0.94, 0.34, 1.00);";
3616
+ css += "--md-sys-motion-spring-default-effects: 200ms cubic-bezier(0.34, 0.80, 0.34, 1.00);";
3617
+ css += "--md-sys-motion-spring-slow-effects: 300ms cubic-bezier(0.34, 0.88, 0.34, 1.00);";
3618
+ }
3619
+ css += `--md-sys-density-scale: ${this.density};`;
3620
+ css += `--m3e-scrollbar-thumb-color: ${hexFromArgb(palette.n1.tone(60))};`;
3621
+ css += `--m3e-focus-ring-visibility: ${this.strongFocus ? "visible" : "hidden"};`;
3622
+ __classPrivateFieldGet(this, _M3eThemeElement_styleSheet, "f").replaceSync(`:host { ${css} }`);
3623
+ if (this.parentElement instanceof HTMLBodyElement) {
3624
+ const computedStyle = getComputedStyle(this);
3625
+ if (__classPrivateFieldGet(this, _M3eThemeElement_forcedColor, "f")?.matches) {
3626
+ this.parentElement.style.backgroundColor =
3627
+ this.parentElement.style.color =
3628
+ this.parentElement.ownerDocument.documentElement.style.scrollbarColor =
3629
+ this.parentElement.style.scrollbarColor =
3630
+ "";
3631
+ }
3632
+ else {
3633
+ this.parentElement.style.backgroundColor = computedStyle.getPropertyValue("--md-sys-color-background");
3634
+ this.parentElement.style.color = computedStyle.getPropertyValue("--md-sys-color-on-background");
3635
+ this.parentElement.ownerDocument.documentElement.style.scrollbarColor =
3636
+ this.parentElement.style.scrollbarColor = `${computedStyle.getPropertyValue("--m3e-scrollbar-thumb-color")} ${computedStyle.getPropertyValue("--m3e-scrollbar-track-color")}`;
3637
+ }
3638
+ }
3639
+ if (__classPrivateFieldGet(this, _M3eThemeElement_firstUpdated, "f")) {
3640
+ this.dispatchEvent(new Event("change", { bubbles: true }));
3641
+ }
3642
+ };
3643
+ _M3eThemeElement_getVariant = function _M3eThemeElement_getVariant() {
3644
+ switch (this.variant) {
3645
+ case "monochrome":
3646
+ return 0;
3647
+ case "neutral":
3648
+ return 1;
3649
+ case "tonal-spot":
3650
+ return 2;
3651
+ case "vibrant":
3652
+ return 3;
3653
+ case "expressive":
3654
+ return 4;
3655
+ case "fidelity":
3656
+ return 5;
3657
+ case "rainbow":
3658
+ return 7;
3659
+ case "fruit-salad":
3660
+ return 8;
3661
+ default: // content
3662
+ return 6;
3663
+ }
3664
+ };
3665
+ _M3eThemeElement_getContrastLevel = function _M3eThemeElement_getContrastLevel() {
3666
+ switch (this.contrast) {
3667
+ case "high":
3668
+ return 1;
3669
+ case "medium":
3670
+ return 0.5;
3671
+ default: // standard
3672
+ return 0;
3673
+ }
3674
+ };
3675
+ /** The styles of the element. */
3676
+ M3eThemeElement.styles = css `
3677
+ :host {
3678
+ display: contents;
3679
+ font-size: ${DesignToken.typescale.standard.body.large.fontSize};
3680
+ font-weight: ${DesignToken.typescale.standard.body.large.fontWeight};
3681
+ line-height: ${DesignToken.typescale.standard.body.large.lineHeight};
3682
+ letter-spacing: ${DesignToken.typescale.standard.body.large.tracking};
3683
+ }
3684
+ `;
3685
+ __decorate([
3686
+ n()
3687
+ ], M3eThemeElement.prototype, "color", void 0);
3688
+ __decorate([
3689
+ n()
3690
+ ], M3eThemeElement.prototype, "variant", void 0);
3691
+ __decorate([
3692
+ n()
3693
+ ], M3eThemeElement.prototype, "scheme", void 0);
3694
+ __decorate([
3695
+ n()
3696
+ ], M3eThemeElement.prototype, "contrast", void 0);
3697
+ __decorate([
3698
+ n({ attribute: "strong-focus", type: Boolean })
3699
+ ], M3eThemeElement.prototype, "strongFocus", void 0);
3700
+ __decorate([
3701
+ n({ type: Number })
3702
+ ], M3eThemeElement.prototype, "density", void 0);
3703
+ __decorate([
3704
+ n()
3705
+ ], M3eThemeElement.prototype, "motion", void 0);
3706
+ M3eThemeElement = __decorate([
3707
+ t$1("m3e-theme")
3708
+ ], M3eThemeElement);
3709
+
3710
+ export { M3eThemeElement };
3711
+ //# sourceMappingURL=index.js.map