File size: 16,587 Bytes
1c8d1ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
 * @license
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
function t(t){return t<0?-1:0===t?0:1}function n(t,n,r){return(1-r)*t+r*n}function r(t){return(t%=360)<0&&(t+=360),t}
/**
 * @license
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
const a=[[.41233895,.35762064,.18051042],[.2126,.7152,.0722],[.01932141,.11916382,.95034478]],s=[[3.2413774792388685,-1.5376652402851851,-.49885366846268053],[-.9691452513005321,1.8758853451067872,.04156585616912061],[.05562093689691305,-.20395524564742123,1.0571799111220335]],e=[95.047,100,108.883];function o(t){return t>>16&255}function h(t){return t>>8&255}function i(t){return 255&t}function c(t,n,r){const a=s,e=a[0][0]*t+a[0][1]*n+a[0][2]*r,o=a[1][0]*t+a[1][1]*n+a[1][2]*r,h=a[2][0]*t+a[2][1]*n+a[2][2]*r,i=w(e),c=w(o),u=w(h);return(255<<24|(255&i)<<16|(255&c)<<8|255&u)>>>0}function u(t){const n=(t+16)/116,r=24389/27,a=n*n*n>216/24389;return c((a?n*n*n:t/r)*e[0],(t>8?n*n*n:t/r)*e[1],(a?n*n*n:t/r)*e[2])}function M(t){const n=function(t){const n=f(o(t)),r=f(h(t)),s=f(i(t));return[(e=[n,r,s])[0]*(c=a)[0][0]+e[1]*c[0][1]+e[2]*c[0][2],e[0]*c[1][0]+e[1]*c[1][1]+e[2]*c[1][2],e[0]*c[2][0]+e[1]*c[2][1]+e[2]*c[2][2]];var e,c}(t)[1]/100;if(n<=216/24389)return 24389/27*n;return 116*Math.pow(n,1/3)-16}function l(t){return t>8?100*Math.pow((t+16)/116,3):t/(24389/27)*100}function f(t){const n=t/255;return n<=.040449936?n/12.92*100:100*Math.pow((n+.055)/1.055,2.4)}function w(t){const n=t/100;let r=0;return r=n<=.0031308?12.92*n:1.055*Math.pow(n,1/2.4)-.055,a=0,s=255,(e=Math.round(255*r))<a?a:e>s?s:e;var a,s,e}
/**
 * @license
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
class m{
/**
   * Parameters are intermediate values of the CAM16 conversion process. Their
   * names are shorthand for technical color science terminology, this class
   * would not benefit from documenting them individually. A brief overview
   * is available in the CAM16 specification, and a complete overview requires
   * a color science textbook, such as Fairchild's Color Appearance Models.
   */
constructor(t,n,r,a,s,e,o,h,i,c){this.n=t,this.aw=n,this.nbb=r,this.ncb=a,this.c=s,this.nc=e,this.rgbD=o,this.fl=h,this.fLRoot=i,this.z=c}
/**
   * Create ViewingConditions from a simple, physically relevant, set of
   * parameters.
   *
   * @param whitePoint White point, measured in the XYZ color space.
   *     default = D65, or sunny day afternoon
   * @param adaptingLuminance The luminance of the adapting field. Informally,
   *     how bright it is in the room where the color is viewed. Can be
   *     calculated from lux by multiplying lux by 0.0586. default = 11.72,
   *     or 200 lux.
   * @param backgroundLstar The lightness of the area surrounding the color.
   *     measured by L* in L*a*b*. default = 50.0
   * @param surround A general description of the lighting surrounding the
   *     color. 0 is pitch dark, like watching a movie in a theater. 1.0 is a
   *     dimly light room, like watching TV at home at night. 2.0 means there
   *     is no difference between the lighting on the color and around it.
   *     default = 2.0
   * @param discountingIlluminant Whether the eye accounts for the tint of the
   *     ambient lighting, such as knowing an apple is still red in green light.
   *     default = false, the eye does not perform this process on
   *       self-luminous objects like displays.
   */static make(t=function(){return e}(),r=200/Math.PI*l(50)/100,a=50,s=2,o=!1){const h=t,i=.401288*h[0]+.650173*h[1]+-.051461*h[2],c=-.250268*h[0]+1.204414*h[1]+.045854*h[2],u=-.002079*h[0]+.048952*h[1]+.953127*h[2],M=.8+s/10,f=M>=.9?n(.59,.69,10*(M-.9)):n(.525,.59,10*(M-.8));let w=o?1:M*(1-1/3.6*Math.exp((-r-42)/92));w=w>1?1:w<0?0:w;const b=M,p=[w*(100/i)+1-w,w*(100/c)+1-w,w*(100/u)+1-w],I=1/(5*r+1),g=I*I*I*I,d=1-g,C=g*r+.1*d*d*Math.cbrt(5*r),D=l(a)/t[1],q=1.48+Math.sqrt(D),L=.725/Math.pow(D,.2),T=L,j=[Math.pow(C*p[0]*i/100,.42),Math.pow(C*p[1]*c/100,.42),Math.pow(C*p[2]*u/100,.42)],U=[400*j[0]/(j[0]+27.13),400*j[1]/(j[1]+27.13),400*j[2]/(j[2]+27.13)];return new m(D,(2*U[0]+U[1]+.05*U[2])*L,L,T,f,b,p,C,Math.pow(C,.25),q)}}m.DEFAULT=m.make();
/**
 * @license
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
class b{
/**
   * All of the CAM16 dimensions can be calculated from 3 of the dimensions, in
   * the following combinations:
   *      -  {j or q} and {c, m, or s} and hue
   *      - jstar, astar, bstar
   * Prefer using a static method that constructs from 3 of those dimensions.
   * This constructor is intended for those methods to use to return all
   * possible dimensions.
   *
   * @param hue
   * @param chroma informally, colorfulness / color intensity. like saturation
   *     in HSL, except perceptually accurate.
   * @param j lightness
   * @param q brightness; ratio of lightness to white point's lightness
   * @param m colorfulness
   * @param s saturation; ratio of chroma to white point's chroma
   * @param jstar CAM16-UCS J coordinate
   * @param astar CAM16-UCS a coordinate
   * @param bstar CAM16-UCS b coordinate
   */
constructor(t,n,r,a,s,e,o,h,i){this.hue=t,this.chroma=n,this.j=r,this.q=a,this.m=s,this.s=e,this.jstar=o,this.astar=h,this.bstar=i}
/**
   * CAM16 instances also have coordinates in the CAM16-UCS space, called J*,
   * a*, b*, or jstar, astar, bstar in code. CAM16-UCS is included in the CAM16
   * specification, and is used to measure distances between colors.
   */distance(t){const n=this.jstar-t.jstar,r=this.astar-t.astar,a=this.bstar-t.bstar,s=Math.sqrt(n*n+r*r+a*a);return 1.41*Math.pow(s,.63)}
/**
   * @param argb ARGB representation of a color.
   * @return CAM16 color, assuming the color was viewed in default viewing
   *     conditions.
   */static fromInt(t){return b.fromIntInViewingConditions(t,m.DEFAULT)}
/**
   * @param argb ARGB representation of a color.
   * @param viewingConditions Information about the environment where the color
   *     was observed.
   * @return CAM16 color.
   */static fromIntInViewingConditions(n,r){const a=(65280&n)>>8,s=255&n,e=f((16711680&n)>>16),o=f(a),h=f(s),i=.41233895*e+.35762064*o+.18051042*h,c=.2126*e+.7152*o+.0722*h,u=.01932141*e+.11916382*o+.95034478*h,M=.401288*i+.650173*c-.051461*u,l=-.250268*i+1.204414*c+.045854*u,w=-.002079*i+.048952*c+.953127*u,m=r.rgbD[0]*M,p=r.rgbD[1]*l,I=r.rgbD[2]*w,g=Math.pow(r.fl*Math.abs(m)/100,.42),d=Math.pow(r.fl*Math.abs(p)/100,.42),C=Math.pow(r.fl*Math.abs(I)/100,.42),D=400*t(m)*g/(g+27.13),q=400*t(p)*d/(d+27.13),L=400*t(I)*C/(C+27.13),T=(11*D+-12*q+L)/11,j=(D+q-2*L)/9,U=(20*D+20*q+21*L)/20,x=(40*D+20*q+L)/20,S=180*Math.atan2(j,T)/Math.PI,v=S<0?S+360:S>=360?S-360:S,E=v*Math.PI/180,H=x*r.nbb,P=100*Math.pow(H/r.aw,r.c*r.z),V=4/r.c*Math.sqrt(P/100)*(r.aw+4)*r.fLRoot,A=v<20.14?v+360:v,F=5e4/13*(.25*(Math.cos(A*Math.PI/180+2)+3.8))*r.nc*r.ncb*Math.sqrt(T*T+j*j)/(U+.305),J=Math.pow(F,.9)*Math.pow(1.64-Math.pow(.29,r.n),.73),R=J*Math.sqrt(P/100),k=R*r.fLRoot,z=50*Math.sqrt(J*r.c/(r.aw+4)),y=(1+100*.007)*P/(1+.007*P),B=1/.0228*Math.log(1+.0228*k),G=B*Math.cos(E),K=B*Math.sin(E);return new b(v,R,P,V,k,z,y,G,K)}
/**
   * @param j CAM16 lightness
   * @param c CAM16 chroma
   * @param h CAM16 hue
   */static fromJch(t,n,r){return b.fromJchInViewingConditions(t,n,r,m.DEFAULT)}
/**
   * @param j CAM16 lightness
   * @param c CAM16 chroma
   * @param h CAM16 hue
   * @param viewingConditions Information about the environment where the color
   *     was observed.
   */static fromJchInViewingConditions(t,n,r,a){const s=4/a.c*Math.sqrt(t/100)*(a.aw+4)*a.fLRoot,e=n*a.fLRoot,o=n/Math.sqrt(t/100),h=50*Math.sqrt(o*a.c/(a.aw+4)),i=r*Math.PI/180,c=(1+100*.007)*t/(1+.007*t),u=1/.0228*Math.log(1+.0228*e),M=u*Math.cos(i),l=u*Math.sin(i);return new b(r,n,t,s,e,h,c,M,l)}
/**
   * @param jstar CAM16-UCS lightness.
   * @param astar CAM16-UCS a dimension. Like a* in L*a*b*, it is a Cartesian
   *     coordinate on the Y axis.
   * @param bstar CAM16-UCS b dimension. Like a* in L*a*b*, it is a Cartesian
   *     coordinate on the X axis.
   */static fromUcs(t,n,r){return b.fromUcsInViewingConditions(t,n,r,m.DEFAULT)}
/**
   * @param jstar CAM16-UCS lightness.
   * @param astar CAM16-UCS a dimension. Like a* in L*a*b*, it is a Cartesian
   *     coordinate on the Y axis.
   * @param bstar CAM16-UCS b dimension. Like a* in L*a*b*, it is a Cartesian
   *     coordinate on the X axis.
   * @param viewingConditions Information about the environment where the color
   *     was observed.
   */static fromUcsInViewingConditions(t,n,r,a){const s=n,e=r,o=Math.sqrt(s*s+e*e),h=(Math.exp(.0228*o)-1)/.0228/a.fLRoot;let i=Math.atan2(e,s)*(180/Math.PI);i<0&&(i+=360);const c=t/(1-.007*(t-100));return b.fromJchInViewingConditions(c,h,i,a)}
/**
   *  @return ARGB representation of color, assuming the color was viewed in
   *     default viewing conditions, which are near-identical to the default
   *     viewing conditions for sRGB.
   */toInt(){return this.viewed(m.DEFAULT)}
/**
   * @param viewingConditions Information about the environment where the color
   *     will be viewed.
   * @return ARGB representation of color
   */viewed(n){const r=0===this.chroma||0===this.j?0:this.chroma/Math.sqrt(this.j/100),a=Math.pow(r/Math.pow(1.64-Math.pow(.29,n.n),.73),1/.9),s=this.hue*Math.PI/180,e=.25*(Math.cos(s+2)+3.8),o=n.aw*Math.pow(this.j/100,1/n.c/n.z),h=e*(5e4/13)*n.nc*n.ncb,i=o/n.nbb,u=Math.sin(s),M=Math.cos(s),l=23*(i+.305)*a/(23*h+11*a*M+108*a*u),f=l*M,w=l*u,m=(460*i+451*f+288*w)/1403,b=(460*i-891*f-261*w)/1403,p=(460*i-220*f-6300*w)/1403,I=Math.max(0,27.13*Math.abs(m)/(400-Math.abs(m))),g=t(m)*(100/n.fl)*Math.pow(I,1/.42),d=Math.max(0,27.13*Math.abs(b)/(400-Math.abs(b))),C=t(b)*(100/n.fl)*Math.pow(d,1/.42),D=Math.max(0,27.13*Math.abs(p)/(400-Math.abs(p))),q=t(p)*(100/n.fl)*Math.pow(D,1/.42),L=g/n.rgbD[0],T=C/n.rgbD[1],j=q/n.rgbD[2];return c(1.86206786*L-1.01125463*T+.14918677*j,.38752654*L+.62144744*T-.00897398*j,-.0158415*L-.03412294*T+1.04996444*j)}}
/**
 * @license
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */class p{constructor(t,n,r){this.internalHue=t,this.internalChroma=n,this.internalTone=r,this.setInternalState(this.toInt())}
/**
   * @param hue 0 <= hue < 360; invalid values are corrected.
   * @param chroma 0 <= chroma < ?; Informally, colorfulness. The color
   *     returned may be lower than the requested chroma. Chroma has a different
   *     maximum for any given hue and tone.
   * @param tone 0 <= tone <= 100; invalid values are corrected.
   * @return HCT representation of a color in default viewing conditions.
   */static from(t,n,r){return new p(t,n,r)}
/**
   * @param argb ARGB representation of a color.
   * @return HCT representation of a color in default viewing conditions
   */static fromInt(t){const n=b.fromInt(t),r=M(t);return new p(n.hue,n.chroma,r)}toInt(){return D(this.internalHue,this.internalChroma,this.internalTone)}
/**
   * A number, in degrees, representing ex. red, orange, yellow, etc.
   * Ranges from 0 <= hue < 360.
   */get hue(){return this.internalHue}
/**
   * @param newHue 0 <= newHue < 360; invalid values are corrected.
   * Chroma may decrease because chroma has a different maximum for any given
   * hue and tone.
   */set hue(t){this.setInternalState(D(r(t),this.internalChroma,this.internalTone))}get chroma(){return this.internalChroma}
/**
   * @param newChroma 0 <= newChroma < ?
   * Chroma may decrease because chroma has a different maximum for any given
   * hue and tone.
   */set chroma(t){this.setInternalState(D(this.internalHue,t,this.internalTone))}
/** Lightness. Ranges from 0 to 100. */get tone(){return this.internalTone}
/**
   * @param newTone 0 <= newTone <= 100; invalid valids are corrected.
   * Chroma may decrease because chroma has a different maximum for any given
   * hue and tone.
   */set tone(t){this.setInternalState(D(this.internalHue,this.internalChroma,t))}setInternalState(t){const n=b.fromInt(t),r=M(t);this.internalHue=n.hue,this.internalChroma=n.chroma,this.internalTone=r}}const I=.4,g=1,d=.2,C=.01;function D(t,n,a){return function(t,n,a,s){if(n<1||Math.round(a)<=0||Math.round(a)>=100)return u(a);t=r(t);let e=n,o=n,h=0,i=!0,c=null;for(;Math.abs(h-e)>=I;){const n=q(t,o,a);if(i){if(null!=n)return n.viewed(s);i=!1,o=h+(e-h)/2}else null===n?e=o:(c=n,h=o),o=h+(e-h)/2}if(null===c)return u(a);return c.viewed(s)}(r(t),n,(e=100,(o=a)<(s=0)?s:o>e?e:o),m.DEFAULT);var s,e,o}function q(t,n,r){let a=0,s=100,e=0,o=1e3,h=1e3,i=null;for(;Math.abs(a-s)>C;){e=a+(s-a)/2;const c=b.fromJch(e,n,t).toInt(),u=M(c),l=Math.abs(r-u);if(l<d){const n=b.fromInt(c),r=n.distance(b.fromJch(n.j,n.chroma,t));r<=g&&r<=h&&(o=l,h=r,i=n)}if(0===o&&0===h)break;u<r?a=e:s=e}return i}
/**
 * @license
 * Copyright 2021 Google LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */const L=t=>{const n=o(t),r=h(t),a=i(t),s=[n.toString(16),r.toString(16),a.toString(16)];for(const[e,o]of s.entries())1===o.length&&(s[e]="0"+o);return"#"+s.join("")},T=t=>{const n=3===(t=t.replace("#","")).length,r=6===t.length,a=8===t.length;if(!n&&!r&&!a)throw new Error("unexpected hex "+t);let s=0,e=0,o=0;return n?(s=j(t.slice(0,1).repeat(2)),e=j(t.slice(1,2).repeat(2)),o=j(t.slice(2,3).repeat(2))):r?(s=j(t.slice(0,2)),e=j(t.slice(2,4)),o=j(t.slice(4,6))):a&&(s=j(t.slice(2,4)),e=j(t.slice(4,6)),o=j(t.slice(6,8))),(255<<24|(255&s)<<16|(255&e)<<8|255&o)>>>0};function j(t){return parseInt(t,16)}export{p as H,T as a,L as h};