@@ -97,17 +97,17 @@ function labInvf(ft) {
9797}
9898
9999class ViewingConditions {
100- constructor ( n , aw , nbb , ncb , c , nc , rgbD , fl , fLRoot , z ) {
101- this . n = n , this . aw = aw , this . nbb = nbb , this . ncb = ncb , this . c = c , this . nc = nc ,
102- this . rgbD = rgbD , this . fl = fl , this . fLRoot = fLRoot , this . z = z ;
103- }
104100 static make ( whitePoint = whitePointD65 ( ) , adaptingLuminance = 200 / Math . PI * yFromLstar ( 50 ) / 100 , backgroundLstar = 50 , surround = 2 , discountingIlluminant = ! 1 ) {
105101 const xyz = whitePoint , rW = .401288 * xyz [ 0 ] + .650173 * xyz [ 1 ] + - .051461 * xyz [ 2 ] , gW = - .250268 * xyz [ 0 ] + 1.204414 * xyz [ 1 ] + .045854 * xyz [ 2 ] , bW = - .002079 * xyz [ 0 ] + .048952 * xyz [ 1 ] + .953127 * xyz [ 2 ] , f = .8 + surround / 10 , c = f >= .9 ? lerp ( .59 , .69 , 10 * ( f - .9 ) ) : lerp ( .525 , .59 , 10 * ( f - .8 ) ) ;
106102 let d = discountingIlluminant ? 1 : f * ( 1 - 1 / 3.6 * Math . exp ( ( - adaptingLuminance - 42 ) / 92 ) ) ;
107103 d = d > 1 ? 1 : d < 0 ? 0 : d ;
108104 const nc = f , rgbD = [ d * ( 100 / rW ) + 1 - d , d * ( 100 / gW ) + 1 - d , d * ( 100 / bW ) + 1 - d ] , k = 1 / ( 5 * adaptingLuminance + 1 ) , k4 = k * k * k * k , k4F = 1 - k4 , fl = k4 * adaptingLuminance + .1 * k4F * k4F * Math . cbrt ( 5 * adaptingLuminance ) , n = yFromLstar ( backgroundLstar ) / whitePoint [ 1 ] , z = 1.48 + Math . sqrt ( n ) , nbb = .725 / Math . pow ( n , .2 ) , ncb = nbb , rgbAFactors = [ Math . pow ( fl * rgbD [ 0 ] * rW / 100 , .42 ) , Math . pow ( fl * rgbD [ 1 ] * gW / 100 , .42 ) , Math . pow ( fl * rgbD [ 2 ] * bW / 100 , .42 ) ] , rgbA = [ 400 * rgbAFactors [ 0 ] / ( rgbAFactors [ 0 ] + 27.13 ) , 400 * rgbAFactors [ 1 ] / ( rgbAFactors [ 1 ] + 27.13 ) , 400 * rgbAFactors [ 2 ] / ( rgbAFactors [ 2 ] + 27.13 ) ] ;
109105 return new ViewingConditions ( n , ( 2 * rgbA [ 0 ] + rgbA [ 1 ] + .05 * rgbA [ 2 ] ) * nbb , nbb , ncb , c , nc , rgbD , fl , Math . pow ( fl , .25 ) , z ) ;
110106 }
107+ constructor ( n , aw , nbb , ncb , c , nc , rgbD , fl , fLRoot , z ) {
108+ this . n = n , this . aw = aw , this . nbb = nbb , this . ncb = ncb , this . c = c , this . nc = nc ,
109+ this . rgbD = rgbD , this . fl = fl , this . fLRoot = fLRoot , this . z = z ;
110+ }
111111}
112112
113113ViewingConditions . DEFAULT = ViewingConditions . make ( ) ;
@@ -274,12 +274,6 @@ HctSolver.LINRGB_FROM_SCALED_DISCOUNT = [ [ 1373.2198709594231, -1100.4251190754
274274HctSolver . Y_FROM_LINRGB = [ .2126 , .7152 , .0722 ] , HctSolver . CRITICAL_PLANES = [ .015176349177441876 , .045529047532325624 , .07588174588720938 , .10623444424209313 , .13658714259697685 , .16693984095186062 , .19729253930674434 , .2276452376616281 , .2579979360165119 , .28835063437139563 , .3188300904430532 , .350925934958123 , .3848314933096426 , .42057480301049466 , .458183274052838 , .4976837250274023 , .5391024159806381 , .5824650784040898 , .6277969426914107 , .6751227633498623 , .7244668422128921 , .775853049866786 , .829304845476233 , .8848452951698498 , .942497089126609 , 1.0022825574869039 , 1.0642236851973577 , 1.1283421258858297 , 1.1946592148522128 , 1.2631959812511864 , 1.3339731595349034 , 1.407011200216447 , 1.4823302800086415 , 1.5599503113873272 , 1.6398909516233677 , 1.7221716113234105 , 1.8068114625156377 , 1.8938294463134073 , 1.9832442801866852 , 2.075074464868551 , 2.1693382909216234 , 2.2660538449872063 , 2.36523901573795 , 2.4669114995532007 , 2.5710888059345764 , 2.6777882626779785 , 2.7870270208169257 , 2.898822059350997 , 3.0131901897720907 , 3.1301480604002863 , 3.2497121605402226 , 3.3718988244681087 , 3.4967242352587946 , 3.624204428461639 , 3.754355295633311 , 3.887192587735158 , 4.022731918402185 , 4.160988767090289 , 4.301978482107941 , 4.445716283538092 , 4.592217266055746 , 4.741496401646282 , 4.893568542229298 , 5.048448422192488 , 5.20615066083972 , 5.3666897647573375 , 5.5300801301023865 , 5.696336044816294 , 5.865471690767354 , 6.037501145825082 , 6.212438385869475 , 6.390297286737924 , 6.571091626112461 , 6.7548350853498045 , 6.941541251256611 , 7.131223617812143 , 7.323895587840543 , 7.5195704746346665 , 7.7182615035334345 , 7.919981813454504 , 8.124744458384042 , 8.332562408825165 , 8.543448553206703 , 8.757415699253682 , 8.974476575321063 , 9.194643831691977 , 9.417930041841839 , 9.644347703669503 , 9.873909240696694 , 10.106627003236781 , 10.342513269534024 , 10.58158024687427 , 10.8238400726681 , 11.069304815507364 , 11.317986476196008 , 11.569896988756009 , 11.825048221409341 , 12.083451977536606 , 12.345119996613247 , 12.610063955123938 , 12.878295467455942 , 13.149826086772048 , 13.42466730586372 , 13.702830557985108 , 13.984327217668513 , 14.269168601521828 , 14.55736596900856 , 14.848930523210871 , 15.143873411576273 , 15.44220572664832 , 15.743938506781891 , 16.04908273684337 , 16.35764934889634 , 16.66964922287304 , 16.985093187232053 , 17.30399201960269 , 17.62635644741625 , 17.95219714852476 , 18.281524751807332 , 18.614349837764564 , 18.95068293910138 , 19.290534541298456 , 19.633915083172692 , 19.98083495742689 , 20.331304511189067 , 20.685334046541502 , 21.042933821039977 , 21.404114048223256 , 21.76888489811322 , 22.137256497705877 , 22.50923893145328 , 22.884842241736916 , 23.264076429332462 , 23.6469514538663 , 24.033477234264016 , 24.42366364919083 , 24.817520537484558 , 25.21505769858089 , 25.61628489293138 , 26.021211842414342 , 26.429848230738664 , 26.842203703840827 , 27.258287870275353 , 27.678110301598522 , 28.10168053274597 , 28.529008062403893 , 28.96010235337422 , 29.39497283293396 , 29.83362889318845 , 30.276079891419332 , 30.722335150426627 , 31.172403958865512 , 31.62629557157785 , 32.08401920991837 , 32.54558406207592 , 33.010999283389665 , 33.4802739966603 , 33.953417292456834 , 34.430438229418264 , 34.911345834551085 , 35.39614910352207 , 35.88485700094671 , 36.37747846067349 , 36.87402238606382 , 37.37449765026789 , 37.87891309649659 , 38.38727753828926 , 38.89959975977785 , 39.41588851594697 , 39.93615253289054 , 40.460400508064545 , 40.98864111053629 , 41.520882981230194 , 42.05713473317016 , 42.597404951718396 , 43.141702194811224 , 43.6900349931913 , 44.24241185063697 , 44.798841244188324 , 45.35933162437017 , 45.92389141541209 , 46.49252901546552 , 47.065252796817916 , 47.64207110610409 , 48.22299226451468 , 48.808024568002054 , 49.3971762874833 , 49.9904556690408 , 50.587870934119984 , 51.189430279724725 , 51.79514187861014 , 52.40501387947288 , 53.0190544071392 , 53.637271562750364 , 54.259673423945976 , 54.88626804504493 , 55.517063457223934 , 56.15206766869424 , 56.79128866487574 , 57.43473440856916 , 58.08241284012621 , 58.734331877617365 , 59.39049941699807 , 60.05092333227251 , 60.715611475655585 , 61.38457167773311 , 62.057811747619894 , 62.7353394731159 , 63.417162620860914 , 64.10328893648692 , 64.79372614476921 , 65.48848194977529 , 66.18756403501224 , 66.89098006357258 , 67.59873767827808 , 68.31084450182222 , 69.02730813691093 , 69.74813616640164 , 70.47333615344107 , 71.20291564160104 , 71.93688215501312 , 72.67524319850172 , 73.41800625771542 , 74.16517879925733 , 74.9167682708136 , 75.67278210128072 , 76.43322770089146 , 77.1981124613393 , 77.96744375590167 , 78.74122893956174 , 79.51947534912904 , 80.30219030335869 , 81.08938110306934 , 81.88105503125999 , 82.67721935322541 , 83.4778813166706 , 84.28304815182372 , 85.09272707154808 , 85.90692527145302 , 86.72564993000343 , 87.54890820862819 , 88.3767072518277 , 89.2090541872801 , 90.04595612594655 , 90.88742016217518 , 91.73345337380438 , 92.58406282226491 , 93.43925555268066 , 94.29903859396902 , 95.16341895893969 , 96.03240364439274 , 96.9059996312159 , 97.78421388448044 , 98.6670533535366 , 99.55452497210776 ] ;
275275
276276class Hct {
277- constructor ( argb ) {
278- this . argb = argb ;
279- const cam = Cam16 . fromInt ( argb ) ;
280- this . internalHue = cam . hue , this . internalChroma = cam . chroma , this . internalTone = lstarFromArgb ( argb ) ,
281- this . argb = argb ;
282- }
283277 static from ( hue , chroma , tone ) {
284278 return new Hct ( HctSolver . solveToInt ( hue , chroma , tone ) ) ;
285279 }
@@ -307,6 +301,12 @@ class Hct {
307301 set tone ( newTone ) {
308302 this . setInternalState ( HctSolver . solveToInt ( this . internalHue , this . internalChroma , newTone ) ) ;
309303 }
304+ constructor ( argb ) {
305+ this . argb = argb ;
306+ const cam = Cam16 . fromInt ( argb ) ;
307+ this . internalHue = cam . hue , this . internalChroma = cam . chroma , this . internalTone = lstarFromArgb ( argb ) ,
308+ this . argb = argb ;
309+ }
310310 setInternalState ( argb ) {
311311 const cam = Cam16 . fromInt ( argb ) ;
312312 this . internalHue = cam . hue , this . internalChroma = cam . chroma , this . internalTone = lstarFromArgb ( argb ) ,
@@ -330,16 +330,16 @@ class Blend {
330330}
331331
332332class TonalPalette {
333- constructor ( hue , chroma ) {
334- this . hue = hue , this . chroma = chroma , this . cache = new Map ;
335- }
336333 static fromInt ( argb ) {
337334 const hct = Hct . fromInt ( argb ) ;
338335 return TonalPalette . fromHueAndChroma ( hct . hue , hct . chroma ) ;
339336 }
340337 static fromHueAndChroma ( hue , chroma ) {
341338 return new TonalPalette ( hue , chroma ) ;
342339 }
340+ constructor ( hue , chroma ) {
341+ this . hue = hue , this . chroma = chroma , this . cache = new Map ;
342+ }
343343 tone ( tone ) {
344344 let argb = this . cache . get ( tone ) ;
345345 return void 0 === argb && ( argb = Hct . from ( this . hue , this . chroma , tone ) . toInt ( ) ,
@@ -348,6 +348,42 @@ class TonalPalette {
348348}
349349
350350class CorePalette {
351+ static of ( argb ) {
352+ return new CorePalette ( argb , ! 1 ) ;
353+ }
354+ static contentOf ( argb ) {
355+ return new CorePalette ( argb , ! 0 ) ;
356+ }
357+ static fromColors ( colors ) {
358+ return CorePalette . createPaletteFromColors ( ! 1 , colors ) ;
359+ }
360+ static contentFromColors ( colors ) {
361+ return CorePalette . createPaletteFromColors ( ! 0 , colors ) ;
362+ }
363+ static createPaletteFromColors ( content , colors ) {
364+ const palette = new CorePalette ( colors . primary , content ) ;
365+ if ( colors . secondary ) {
366+ const p = new CorePalette ( colors . secondary , content ) ;
367+ palette . a2 = p . a1 ;
368+ }
369+ if ( colors . tertiary ) {
370+ const p = new CorePalette ( colors . tertiary , content ) ;
371+ palette . a3 = p . a1 ;
372+ }
373+ if ( colors . error ) {
374+ const p = new CorePalette ( colors . error , content ) ;
375+ palette . error = p . a1 ;
376+ }
377+ if ( colors . neutral ) {
378+ const p = new CorePalette ( colors . neutral , content ) ;
379+ palette . n1 = p . n1 ;
380+ }
381+ if ( colors . neutralVariant ) {
382+ const p = new CorePalette ( colors . neutralVariant , content ) ;
383+ palette . n2 = p . n2 ;
384+ }
385+ return palette ;
386+ }
351387 constructor ( argb , isContent ) {
352388 const hct = Hct . fromInt ( argb ) , hue = hct . hue , chroma = hct . chroma ;
353389 isContent ? ( this . a1 = TonalPalette . fromHueAndChroma ( hue , chroma ) , this . a2 = TonalPalette . fromHueAndChroma ( hue , chroma / 3 ) ,
@@ -357,18 +393,9 @@ class CorePalette {
357393 this . n1 = TonalPalette . fromHueAndChroma ( hue , 4 ) , this . n2 = TonalPalette . fromHueAndChroma ( hue , 8 ) ) ,
358394 this . error = TonalPalette . fromHueAndChroma ( 25 , 84 ) ;
359395 }
360- static of ( argb ) {
361- return new CorePalette ( argb , ! 1 ) ;
362- }
363- static contentOf ( argb ) {
364- return new CorePalette ( argb , ! 0 ) ;
365- }
366396}
367397
368398class Scheme {
369- constructor ( props ) {
370- this . props = props ;
371- }
372399 get primary ( ) {
373400 return this . props . primary ;
374401 }
@@ -534,24 +561,31 @@ class Scheme {
534561 inversePrimary : core . a1 . tone ( 40 )
535562 } ) ;
536563 }
564+ constructor ( props ) {
565+ this . props = props ;
566+ }
537567 toJSON ( ) {
538- return Object . assign ( { } , this . props ) ;
568+ return {
569+ ...this . props
570+ } ;
539571 }
540572}
541573
542- const hexFromArgb = argb => {
574+ function hexFromArgb ( argb ) {
543575 const r = redFromArgb ( argb ) , g = greenFromArgb ( argb ) , b = blueFromArgb ( argb ) , outParts = [ r . toString ( 16 ) , g . toString ( 16 ) , b . toString ( 16 ) ] ;
544576 for ( const [ i , part ] of outParts . entries ( ) ) 1 === part . length && ( outParts [ i ] = "0" + part ) ;
545577 return "#" + outParts . join ( "" ) ;
546- } , argbFromHex = hex => {
578+ }
579+
580+ function argbFromHex ( hex ) {
547581 const isThree = 3 === ( hex = hex . replace ( "#" , "" ) ) . length , isSix = 6 === hex . length , isEight = 8 === hex . length ;
548582 if ( ! isThree && ! isSix && ! isEight ) throw new Error ( "unexpected hex " + hex ) ;
549583 let r = 0 , g = 0 , b = 0 ;
550584 return isThree ? ( r = parseIntHex ( hex . slice ( 0 , 1 ) . repeat ( 2 ) ) , g = parseIntHex ( hex . slice ( 1 , 2 ) . repeat ( 2 ) ) ,
551585 b = parseIntHex ( hex . slice ( 2 , 3 ) . repeat ( 2 ) ) ) : isSix ? ( r = parseIntHex ( hex . slice ( 0 , 2 ) ) ,
552586 g = parseIntHex ( hex . slice ( 2 , 4 ) ) , b = parseIntHex ( hex . slice ( 4 , 6 ) ) ) : isEight && ( r = parseIntHex ( hex . slice ( 2 , 4 ) ) ,
553587 g = parseIntHex ( hex . slice ( 4 , 6 ) ) , b = parseIntHex ( hex . slice ( 6 , 8 ) ) ) , ( 255 << 24 | ( 255 & r ) << 16 | ( 255 & g ) << 8 | 255 & b ) >>> 0 ;
554- } ;
588+ }
555589
556590function parseIntHex ( value ) {
557591 return parseInt ( value , 16 ) ;
0 commit comments