UNPKG

@legumeinfo/taxon-symbology

Version:

A TypeScript library for consistent coloring of visualizations by genus & species

34 lines (33 loc) 24.5 kB
/** * @license * * js - JavaScript library for color conversions * * Copyright (c) 2011-2017, Gregor Aisch * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. The name Gregor Aisch may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL GREGOR AISCH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ const{abs:t,atan2:r,cos:n,floor:e,log:i,min:s,max:o,round:u,sign:c,sin:a,sqrt:h,cbrt:l,PI:f,hypot:g}=Math;function d(t,r,n){return t+(r-t)*n}function b(t,r=0,n=1){return t<r?r:t>n?n:t}function m(r,n,e,i=1e-8){let s,o=n;for(;e--&&t(s=r(o))>i;){const t=(r(o+i)-s)/i;console.log("fx / dfdx",s/t,"fx",s,"x",o),o-=s/t}return o}const p=f/180,y=180/f;function w(...t){return t[0]instanceof k?t[0]:t.length>1&&"string"==typeof t[t.length-1]?S(t.slice(0,t.length-1),t[t.length-1]):Array.isArray(t[0])?S(t[0]):S(t)}class k{constructor(t,r,n,e=1){this.r=t,this.g=r,this.b=n,this.a=e}mix(t,r,n="rgb"){return R(this,t,r,n)}rgb(t=!0,r=!0){const n=n=>(t&&(n=u(n)),r&&(n=b(n,0,255)),n),{r:e,g:i,b:s}=this;return[n(e),n(i),n(s)]}rgba(t=!0,r=!0){const n=n=>(t&&(n=u(n)),r&&(n=b(n,0,255)),n),{r:e,g:i,b:s,a:o}=this;return[n(e),n(i),n(s),o]}hex(t="rgb"){const{r,g:n,b:e,a:i}=this;return function(t,r,n,e,i="rgb"){t=b(u(t),0,255),r=b(u(r),0,255),n=b(u(n),0,255);const s=t<<16|r<<8|n,o=s.toString(16).padStart(6,"0"),c=u(255*b(e)).toString(16).padStart(2,"0");return"#"+("argb"==i?c+o:"rgba"==i?o+c:o)}(r,n,e,i,t)}hsl(){const{r:t,g:r,b:n}=this;return function(t,r,n){const[e,i,s]=W(t/255,r/255,n/255),o=(s+i)/2;let u;u=s==i?0:o<.5?(s-i)/(s+i):(s-i)/(2-s-i);return[e,u,o]}(t,r,n)}hsv(){const{r:t,g:r,b:n}=this;return function(t,r,n){const[e,i,s]=W(t,r,n),o=s-i;return[e,0==s?0:o/s,s/255]}(t,r,n)}hcg(){const{r:t,g:r,b:n}=this;return function(t,r,n){const[e,i,s]=W(t,r,n),o=(s-i)/255;return[e,o,o<1?i/255/(1-o):0]}(t,r,n)}css(t="rgb"){if("rgb"==t){const{r:t,g:r,b:n,a:e}=this;return function(t,r,n,e=1){return e>=1?"rgb("+[t,r,n].map(u).join(",")+")":"rgba("+[t,r,n].map(u).join(",")+","+e+")"}(t,r,n,e)}if("hsl"==t)return function([t,r,n],e){const i=e<1?"hsla":"hsl";return i+"("+Q(t)+","+Q(100*r)+"%,"+Q(100*n)+"%"+("hsla"==i?","+Q(e):"")+")"}(this.hsl(),this.alpha());throw new Error}name(t=!1){const r=this.num(),n=Object.keys(E).find((t=>E[t]==r));if(!n&&t){const[t,r,n]=this.lab();return e=Object.keys(E),i=e=>{const[i,s,o]=P(E[e]).lab();return-g(t-i,r-s,n-o)},e[lt(e,i)]}var e,i;return n}cmyk(){const{r:t,g:r,b:n}=this;return function(t,r,n){const e=1-o(t/=255,r/=255,n/=255);if(1==e)return[0,0,0,1];return[(1-t-e)/(1-e),(1-r-e)/(1-e),(1-n-e)/(1-e),e]}(t,r,n)}gl(){const{r:t,g:r,b:n,a:e}=this;return[t/255,r/255,n/255,e]}luminance(t){const{r,g:n,b:e,a:i}=this,[,s]=at(r,n,e);if(void 0===t)return s;const o=(t,r,n)=>(n-t)/(r-t);return t>s?R(this,A,o(s,1,t),"xyz").alpha(i):R(v,this,o(0,s,t),"xyz").alpha(i)}temperature(){const{r:t,g:r,b:n}=this;return e=t,s=r,o=n,console.log(o-e),s+o<158.61?(console.log("0 < t < 20"),u(100*m((t=>s-(-155.25485562709179-.44596950469579133*(t-2)+104.49216199393888*i(t-2))),15,4))):o-e<0?(console.log("20 < t < 66"),u(100*m((t=>o-(.8274096064007395*(t-10)-254.76935184120902+115.67994401066147*i(t-10))),43,4))):(console.log("0 < t < 400, start= "+(-1.4*(e+s)+755)),u(100*m((t=>e-(351.97690566805693+.114206453784165*(t-55)-40.25366309332127*i(t-55))),-1.4*(e+s)+755,8)));var e,s,o}set(t,r){const[n,e]=t.split("."),i=this[n](),s=n.indexOf(e);if(-1==s)throw new Error("invalid channel");return i[s]="number"==typeof r?r:r(i[s]),w(i,n).alpha(this.a)}clipped(){const{r:t,g:r,b:n}=this;return!(0<=t&&t<=255&&0<=r&&r<=255&&0<=n&&n<=255)}textColor(){return this.luminance()>.5?v:A}alpha(t){if(void 0===t)return this.a;const{r,g:n,b:e}=this;return B(r,n,e,t)}darker(t=1){const[r,n,e]=this.lab();return function(...t){return S(t,"lab")}(r-rt*t,n,e,this.alpha())}brighter(t=1){return this.darker(-t)}saturate(t=1){const[r,n,e]=this.lch();return function(...t){return S(t,"lch")}(r,o(0,n+t*rt),e,this.alpha())}desaturate(t=1){return this.saturate(-t)}premultiplied(){const{r:t,g:r,b:n,a:e}=this;return B(t*e,r*e,n*e,e)}hsi(){const{r:t,g:n,b:e}=this;return function(t,n,e){const i=t/255,o=n/255,u=e/255,c=(i+o+u)/3;if(i==o&&o==u)return[0,0,c];{const t=.5*(2*i-o-u),n=h(3)/2*(o-u),e=r(n,t),a=s(i,o,u);return[(e<0?2*f+e:e)*y,1-a/c,c]}}(t,n,e)}lab(){const{r:t,g:r,b:n}=this;return ct(t,r,n)}num(t="rgb"){const{r,g:n,b:e,a:i}=this;return function(t,r,n,e=1,i="rgb"){const s=u(t)<<16|u(r)<<8|u(n);return"rgb"===i?s:"rgba"===i?s<<8|u(255*e)<<24:u(255*e)<<24|s}(r,n,e,i,t)}lch(){const{r:t,g:n,b:e}=this;return function(t,n,e){const[i,s,o]=ct(t,n,e);return function(t,n,e){const i=g(n,e),s=(r(e,n)*y+360)%360;return[t,i,s]}(i,s,o)}(t,n,e)}xyz(){const{r:t,g:r,b:n}=this;return at(t,r,n)}equals(t){const{r,g:n,b:e,a:i}=this,{r:s,g:o,b:u,a:c}=t;return r==s&&n==o&&e==u&&i==c}hashCode(){return this.num("rgba")}toSource(){const{r:t,g:r,b:n,a:e}=this;return"chroma.rgb("+t+", "+r+", "+n+(1===e?")":", "+e+")")}}k.prototype.toString=k.prototype.css,k.prototype.kelvin=k.prototype.temperature;const v=new k(0,0,0,1),A=new k(255,255,255,1),x={OrRd:[16775148,16705736,16635038,16628612,16551257,15689032,14102559,11730944,8323072],PuBu:[16775163,15525874,13685222,10927579,7645647,3576e3,356528,285325,145496],BuPu:[16252157,14740724,12571622,10403034,9213638,9202609,8929693,8458108,5046347],Oranges:[16774635,16705230,16634018,16625259,16616764,15821075,14239745,10892803,8333060],BuGn:[16252157,15070713,13429990,10082505,6734500,4304502,2329413,27948,17435],YlOrBr:[16777189,16775100,16704401,16696399,16685353,15495188,13388802,10040324,6694150],YlGn:[16777189,16252089,14282915,11394446,7915129,4303709,2327619,26679,17705],Reds:[16774640,16703698,16563105,16552562,16476746,15678252,13309981,10817301,6750221],RdPu:[16775155,16638173,16565696,16424885,16214177,14496919,11403646,7995767,4784234],Greens:[16252149,15070688,13101504,10607003,7652470,4303709,2329413,27948,17435],YlGnBu:[16777177,15595697,13101492,8375739,4306628,1937856,2252456,2438292,531800],Purples:[16579581,15724021,14342891,12369372,10394312,8420794,6967715,5515151,4128893],GnBu:[16252144,14742491,13429701,11066805,8113348,5157843,2854078,551084,540801],Greys:[16777215,15790320,14277081,12434877,9868950,7566195,5395026,2434341,0],YlOrRd:[16777164,16772512,16701814,16691788,16616764,16535082,14883356,12386342,8388646],PuRd:[16250105,15196655,13941210,13210823,14640560,15149450,13505110,9961539,6750239],Blues:[16251903,14609399,13032431,10406625,7057110,4362950,2191797,545180,536683],PuBuGn:[16775163,15524592,13685222,10927579,6793679,3576e3,164234,93273,83510],Viridis:[4456788,4728695,4147850,3237774,2524047,2071946,7130714,11984427,16705573],Spectral:[10355010,13975119,16018755,16625249,16703627,16777151,15136152,11263396,6734501,3311805,6180770],RdYlGn:[10813478,14102567,16018755,16625249,16703627,16777151,14282635,10934634,6733155,1742928,26679],RdBu:[6750239,11671595,14049357,16033154,16636871,16250871,13755888,9618910,4428739,2188972,340065],PiYG:[9306450,12917629,14579630,15840986,16638191,16250871,15136208,12116358,8371265,5083681,2581529],PRGn:[4194379,7744131,10055851,12756431,15193320,16250871,14282963,10935200,5942881,1800247,17435],RdYlBu:[10813478,14102567,16018755,16625249,16703632,16777151,14742520,11262441,7646673,4552116,3225237],BrBG:[5517317,9195786,12550445,14664317,16181443,16119285,13101797,8441281,3512207,91742,15408],RdGy:[6750239,11671595,14049357,16033154,16636871,16777215,14737632,12237498,8882055,5066061,1710618],PuOr:[8338184,11753478,14713364,16627811,16703670,16250871,14211819,11709394,8418220,5515144,2949195],Set2:[6734501,16551266,9281739,15174339,10934356,16767279,15058068,11776947],Accent:[8374655,12496596,16629894,16777113,3697840,15729279,12540695,6710886],Set1:[14948892,3636920,5091146,9981603,16744192,16777011,10901032,16220607,10066329],Set3:[9294791,16777139,12499674,16482418,8434131,16626786,11787881,16567781,14277081,12353725,13429701,16772463],Dark2:[1810039,14245634,7696563,15149450,6727198,15117058,10909213,6710886],Paired:[10931939,2062516,11722634,3383340,16489113,14883356,16629615,16744192,13284054,6962586,16777113,11622696],Pastel2:[11789005,16633260,13358568,16042724,15136201,16773806,15852236,13421772],Pastel1:[16495790,11783651,13429701,14601188,16701862,16777164,15063229,16636652,15921906]},E={aliceblue:15792383,antiquewhite:16444375,aqua:65535,aquamarine:8388564,azure:15794175,beige:16119260,bisque:16770244,black:0,blanchedalmond:16772045,blue:255,blueviolet:9055202,brown:10824234,burlywood:14596231,cadetblue:6266528,chartreuse:8388352,chocolate:13789470,coral:16744272,cornflower:6591981,cornflowerblue:6591981,cornsilk:16775388,crimson:14423100,cyan:65535,darkblue:139,darkcyan:35723,darkgoldenrod:12092939,darkgray:11119017,darkgreen:25600,darkgrey:11119017,darkkhaki:12433259,darkmagenta:9109643,darkolivegreen:5597999,darkorange:16747520,darkorchid:10040012,darkred:9109504,darksalmon:15308410,darkseagreen:9419919,darkslateblue:4734347,darkslategray:3100495,darkslategrey:3100495,darkturquoise:52945,darkviolet:9699539,deeppink:16716947,deepskyblue:49151,dimgray:6908265,dimgrey:6908265,dodgerblue:2003199,firebrick:11674146,floralwhite:16775920,forestgreen:2263842,fuchsia:16711935,gainsboro:14474460,ghostwhite:16316671,gold:16766720,goldenrod:14329120,gray:8421504,green:32768,greenyellow:11403055,grey:8421504,honeydew:15794160,hotpink:16738740,indianred:13458524,indigo:4915330,ivory:16777200,khaki:15787660,laserlemon:16777044,lavender:15132410,lavenderblush:16773365,lawngreen:8190976,lemonchiffon:16775885,lightblue:11393254,lightcoral:15761536,lightcyan:14745599,lightgoldenrod:16448210,lightgoldenrodyellow:16448210,lightgray:13882323,lightgreen:9498256,lightgrey:13882323,lightpink:16758465,lightsalmon:16752762,lightseagreen:2142890,lightskyblue:8900346,lightslategray:7833753,lightslategrey:7833753,lightsteelblue:11584734,lightyellow:16777184,lime:65280,limegreen:3329330,linen:16445670,magenta:16711935,maroon:8388608,maroon2:8323072,maroon3:11546720,mediumaquamarine:6737322,mediumblue:205,mediumorchid:12211667,mediumpurple:9662683,mediumseagreen:3978097,mediumslateblue:8087790,mediumspringgreen:64154,mediumturquoise:4772300,mediumvioletred:13047173,midnightblue:1644912,mintcream:16121850,mistyrose:16770273,moccasin:16770229,navajowhite:16768685,navy:128,oldlace:16643558,olive:8421376,olivedrab:7048739,orange:16753920,orangered:16729344,orchid:14315734,palegoldenrod:15657130,palegreen:10025880,paleturquoise:11529966,palevioletred:14381203,papayawhip:16773077,peachpuff:16767673,peru:13468991,pink:16761035,plum:14524637,powderblue:11591910,purple:8388736,purple2:8323199,purple3:10494192,rebeccapurple:6697881,red:16711680,rosybrown:12357519,royalblue:4286945,saddlebrown:9127187,salmon:16416882,sandybrown:16032864,seagreen:3050327,seashell:16774638,sienna:10506797,silver:12632256,skyblue:8900331,slateblue:6970061,slategray:7372944,slategrey:7372944,snow:16775930,springgreen:65407,steelblue:4620980,tan:13808780,teal:32896,thistle:14204888,tomato:16737095,turquoise:4251856,violet:15631086,wheat:16113331,white:16777215,whitesmoke:16119285,yellow:16776960,yellowgreen:10145074};function R(t,r,n=.5,e="rgb"){const i=w(t),s=w(r),o=$[e]&&$[e](i,s,n,e);if(!o)throw new Error("color mode "+e+" is not supported");return o.alpha(d(i.alpha(),s.alpha(),n))}function q(...t){return S(t,"hsl")}function P(t){const[r,n,e]=M(t);return new k(r,n,e)}function B(...t){return S(t,"rgb")}function G(...t){const r=t=>r._at(t);return Object.getOwnPropertyNames(O.prototype).forEach((t=>r[t]=O.prototype[t])),Array.isArray(t[0])&&(t=t[0]),1==t.length&&"string"==typeof t[0]&&(t=x[t[0]]),r._init("function"==typeof t[0]?t[0]:t.map((t=>w(t)))),r}class O{classes(t){if(void 0===t)return this._classes;if(Array.isArray(t))this._classes=t,this.domain(t[0],t[t.length-1]);else{if(t%1!=0||t<1)throw new Error("invalid classes param");this._classes=I(this.domain(),"e",t)}return this}domain(...t){if(void 0===t[0])return"function"!=typeof this._colors?this._pos.map((t=>d(this._min,this._max,t))):[this._min,this._max];if(this._min=t[0],this._max=t[t.length-1],2==t.length)"function"!=typeof this._colors&&(this._pos=this._colors.map(((t,r)=>r/(this._colors.length-1))));else{if("function"==typeof this._colors||t.length!=this._colors.length)throw new Error("invalid domain "+t);this._pos=t.map((t=>{return r=this._min,n=this._max,(t-r)/(n-r);var r,n}))}return this}mode(t){return void 0===t?this._mode:(this._mode=t,this._resetCache(),this)}out(t){return this._out=t,this}correctLightness(t=!0){if(this._correctLightness!=t){this._resetCache();const r=this._colors;if(t&&"function"!=typeof r){let t=0;for(let n=1;n<r.length;n++){const e=r[n].lab()[0]-r[n-1].lab()[0];if(0==t)t=e;else if(t*e<0)throw new Error("scale color lightnesses must be monotonic")}}}return this._correctLightness=t,this}padding(t,r=t){return t?(this._paddingLeft=t,this._paddingRight=r,this):[this._paddingLeft,this._paddingRight]}colors(t,r="hex"){let n;if(void 0===t)n=this._colors.slice();else if(1==t)n=[this._color((this._min+this._max)/2)];else if(t>1)n=Array.from({length:t},((r,n)=>this._color(d(this._min,this._max,n/(t-1)))));else{let t;t=this._classes&&this._classes.length>2?Array.from({length:this._classes.length-1},((t,r)=>.5*(this._classes[r]+this._classes[r+1]))):this.domain(),n=t.map((t=>this._color(t)))}return"color"!=r?n.map((t=>t[r]())):n}cache(t){return void 0===t?!!this._cache:(this._cache=t?this._cache||new Map:void 0,this)}gamma(t){return void 0===t?this._gamma:(this._gamma=t,this)}_at(t){const r=this._color(t);return this._out?r[this._out]():r}_init(t){this._colors=t,"function"!=typeof t&&(this._pos=t.map(((r,n)=>n/(t.length-1)))),this._mode="rgb",this.domain(0,1),this._paddingLeft=0,this._paddingRight=0,this._correctLightness=!1,this._cache=new Map,this._gamma=1}_getClass(t){return this._classes.findIndex((r=>t<=r))-1}_color(t,r=!1){let n;if(r)n=t;else{const r=this._min,e=this._max;if(this._classes&&this._classes.length>2){n=this._getClass(t)/(this._classes.length-2)}else n=e!==r?(t-r)/(e-r):1;this._correctLightness&&(n=this._tCorrectedLightness(n))}n=n**this._gamma,n=(this._paddingLeft+n)/(1+this._paddingLeft+this._paddingRight),n=b(n,0,1);const e=n,i=this._cache&&this._cache.get(e);if(i)return i;{let t;if(Array.isArray(this._colors))for(let r=0;r<this._pos.length;r++){const e=this._pos[r];if(n<=e){t=this._colors[r];break}if(n>=e&&r==this._pos.length-1){t=this._colors[r];break}if(n>e&&n<this._pos[r+1]){n=(n-e)/(this._pos[r+1]-e),t=R(this._colors[r],this._colors[r+1],n,this._mode);break}}else t=this._colors(n);return this._cache&&this._cache.set(e,t),t}}_tCorrectedLightness(t){const r=d(this._color(0,!0).lab()[0],this._color(1,!0).lab()[0],t);return function(t,r,n,e){let i=t(r);for(;e--;){const e=(r+n)/2,s=t(e);c(i)==c(s)?(r=e,i=s):n=e}return(r+n)/2}((t=>this._color(t,!0).lab()[0]-r),0,1,8)}_resetCache(){this._cache&&this._cache.clear()}}var j;function F(t){const r={min:1/0,max:-1/0,sum:0,values:[],count:0};return t.forEach((t=>function(t){null==t||isNaN(t)||(r.values.push(t),r.sum+=t,t<r.min&&(r.min=t),t>r.max&&(r.max=t),r.count+=1)}(t))),r.domain=[r.min,r.max],r.limits=function(t,r){return I(this,t,r)},r}function I(r,n="e",s=7){const o=Array.isArray(r)?F(r):r,{min:u,max:c,values:a}=o;if(a.sort(((t,r)=>t-r)),1==s)return[u,c];if(n.startsWith("c"))return[u,c];if(n.startsWith("e"))return Array.from({length:s+1},((t,r)=>d(u,c,r/s)));if(n.startsWith("l")){if(u<=0)throw new Error("Logarithmic scales are only possible for values > 0");const t=Math.LOG10E*i(u),r=Math.LOG10E*i(c);return Array.from({length:s+1},((n,e)=>10**d(t,r,e/s)))}if(n.startsWith("q"))return Array.from({length:s+1},((t,r)=>{const n=(a.length-1)*r/s,i=e(n);return i==n?a[i]:d(a[i],a[i+1],n-i)}));if(n.startsWith("k")){const r=a.length,n=new Array(r),e=new Array(s);let i=!0,o=0,h=Array.from({length:s+1},((t,r)=>d(u,c,r/s)));do{e.fill(0);for(let r=0;r<a.length;r++){const i=a[r],s=lt(h,(r=>-t(r-i)));e[s]++,n[r]=s}const r=new Array(s).fill(0);for(let t=0;t<n.length;t++){r[n[t]]+=a[t]}for(let t=0;t<r.length;t++)r[t]/=e[t];i=r.some(((t,r)=>t!=h[r])),h=r}while(o++<200&&i);const l=Array.from({length:s},(()=>[]));for(let t=0;t<n.length;t++){l[n[t]].push(a[t])}const f=[];for(const t of l)f.push(t[0],t[t.length-1]);f.sort(((t,r)=>t-r));const g=[];g.push(f[0]);for(let t=1;t<f.length;t+=2){const r=f[t];isNaN(r)||-1!=g.indexOf(r)||g.push(r)}return g}throw new Error("unknown mode")}!function(t){t.cool=function(){return G([q(180,1,.9),q(250,.7,.4)])},t.hot=function(){return G(["#000","#f00","#ff0","#fff"]).mode("rgb")}}(j||(j={}));const $={},Y={};function S(t,r){if(Array.isArray(t[0])&&(t=t[0]),!r)if(1==t.length&&t[0]in E)r="name";else if(1==t.length&&"string"==typeof t[0])r="css";else if(3==t.length)r="rgb";else if(4==t.length&&"number"==typeof t[3]&&t[3]>=0&&t[3]<=1)r="rgb";else{if(!(1==t.length&&"number"==typeof t[0]&&t[0]>=0&&t[0]<=16777215))throw new Error("could not guess mode. args "+JSON.stringify(t));r="num"}const n=Y[r](...t);return new k(n[0],n[1],n[2],void 0!==n[3]?n[3]:1)}function _(t){let r;if(r=t.match(/^#?([A-F\d]{2})([A-F\d]{2})([A-F\d]{2})([A-F\d]{2})?$/i))return[parseInt(r[1],16),parseInt(r[2],16),parseInt(r[3],16),r[4]?parseInt(r[4],16)/255:1];if(r=t.match(/^#?([A-F\d])([A-F\d])([A-F\d])([A-F\d])?$/i))return[17*parseInt(r[1],16),17*parseInt(r[2],16),17*parseInt(r[3],16),r[4]?17*parseInt(r[4],16)/255:1];throw new Error("invalid hex color: "+t)}function C(t){return(t%360+360)%360}function M(t){if(!("number"==typeof t&&t>=0&&t<=16777215))throw new Error("unknown num color: "+t);return[t>>16,t>>8&255,255&t,1]}$.xyz=$.rgb=$.lab=function(t,r,n,e){const i=t[e](),s=r[e]();return S([d(i[0],s[0],n),d(i[1],s[1],n),d(i[2],s[2],n),d(t.alpha(),r.alpha(),n)],e)},$.num=function(t,r,n){return P(d(t.num(),r.num(),n))},$.lrgb=function(t,r,n){const[e,i,s,o]=t.rgba(!1,!1),[u,c,a,l]=r.rgba(!1,!1);return new k(h(e**2*(1-n)+u**2*n),h(i**2*(1-n)+c**2*n),h(s**2*(1-n)+a**2*n),d(o,l,n))},Y.lrgb=Y.rgb,Y.hex=_,Y.hsl=Z,Y.hsv=function(r,n,e,i=1){r=C(r);const s=e*n;return X(r,s,s*(1-t(r/60%2-1)),e-s,i)},Y.num=M;const N="\\s*",z="([+-]?(?:\\d*\\.?)?\\d+(?:[eE][+-]?\\d+)?)",L=new RegExp(["^rgba?\\(",z,",",z,",",z,"(?:,",z+"(%)?",")?\\)$"].join(N),"i"),D=new RegExp(["^rgba?\\(",z,z,z,"(?:/",z+"(%)?",")?\\)$"].join(N),"i"),J=new RegExp(["^rgba?\\(",z+"%",",",z+"%",",",z+"%","(?:,",z+"(%)?",")?\\)$"].join(N),"i"),V=new RegExp(["^rgba?\\(",z+"%",z+"%",z+"%","(?:/",z+"(%)?",")?\\)$"].join(N),"i"),H=new RegExp(["^hsla?\\(",z+"(deg|rad|turn)?",",",z+"%",",",z+"%","(?:,",z+"(%)?",")?\\)$"].join(N),"i"),K=new RegExp(["^hsla?\\(",z+"(deg|rad|turn)?\\s+"+z+"%",z+"%","(?:/",z+"(%)?",")?\\)$"].join(N),"i");function Q(t){return u(100*t)/100}function T(t){const r=t/255;return r<=.04045?r/12.92:((r+.055)/1.055)**2.4}function U(t){return t<=.0031308?12.92*t*255:255*(1.055*t**(1/2.4)-.055)}function W(t,r,n){const e=s(t,r,n),i=o(t,r,n),u=i-e;let c;return c=0==u?0:t==i?(r-n)/u+(r<n?6:0):r==i?2+(n-t)/u:4+(t-r)/u,[60*c,e,i]}function X(t,r,n,e,i){const s=255*e,o=255*r+s,u=255*n+s;return t<60?[o,u,s,i]:t<120?[u,o,s,i]:t<180?[s,o,u,i]:t<240?[s,u,o,i]:t<300?[u,s,o,i]:[o,s,u,i]}function Z(r,n,e,i=1){r=C(r);const s=(1-t(2*e-1))*n;return X(r,s,s*(1-t(r/60%2-1)),e-s/2,i)}function tt(t,r,n,e=1){const[i,s,o]=function(t,r,n){function e(t){return t>st?t**3:ot*(t-4/29)}return[nt*e((t+16)/116+r/500),et*e((t+16)/116),it*e((t+16)/116-n/200)]}(t,r,n);return ht(i,s,o,e)}Y.css=function(t){if(E&&E.hasOwnProperty(t))return M(E[t.toLowerCase()]);let r;if(r=t.match(L)||t.match(D))return[b(+r[1],0,255),b(+r[2],0,255),b(+r[3],0,255),r[4]?b(r[5]?+r[4]/100:+r[4]):1];if(r=t.match(J)||t.match(V))return[255*b(+r[1]/100),255*b(+r[2]/100),255*b(+r[3]/100),r[4]?b(r[5]?+r[4]/100:+r[4]):1];if(r=t.match(H)||t.match(K)){const t={deg:1,rad:y,turn:360},n=r[2]?r[2].toLowerCase():"deg";return Z((+r[1]*t[n]%360+360)%360,b(+r[3]/100),b(+r[4]/100),r[5]?b(r[6]?+r[5]/100:+r[5]):1)}return _(t)},Y.name=function(t){return M(E[t])},Y.lch=function(t,r,e,i=1){const[,s,o]=function(t,r,e){return[t,n(e*p)*r,a(e*p)*r]}(t,r,e);return tt(t,s,o,i)},Y.cmyk=function(t,r,n,e,i=1){return 1==e?[0,0,0,i]:[255*(1-t)*(1-e),255*(1-r)*(1-e),255*(1-n)*(1-e),i]},Y.gl=function(t,r,n,e=1){return[255*t,255*r,255*n,e]},Y.rgb=(...t)=>t,Y.temperature=Y.kelvin=Y.K=function(t){const r=t/100;let n,e,s;return r<66?(n=255,e=-155.25485562709179-.44596950469579133*(r-2)+104.49216199393888*i(r-2),s=r<20?0:.8274096064007395*(r-10)-254.76935184120902+115.67994401066147*i(r-10)):(n=351.97690566805693+.114206453784165*(r-55)-40.25366309332127*i(r-55),e=325.4494125711974+.07943456536662342*(r-50)-28.0852963507957*i(r-50),s=255),[n,e,s]},Y.hcg=function(r,n,e,i=1){const s=e*(1-n);return X(r=C(r),n,n*(1-t(r/60%2-1)),s,i)};const rt=18,nt=.95047,et=1,it=1.08883,st=.206896552,ot=.12841855,ut=.008856452;function ct(t,r,n){const[e,i,s]=at(t,r,n);return function(t,r,n){function e(t){return t>ut?l(t):t/ot+4/29}return[116*e(r/et)-16,500*(e(t/nt)-e(r/et)),200*(e(r/et)-e(n/it))]}(e,i,s)}function at(t,r,n){const e=T(t),i=T(r),s=T(n);return[.4124564*e+.3575761*i+.1804375*s,.2126729*e+.7151522*i+.072175*s,.0193339*e+.119192*i+.9503041*s]}function ht(t,r,n,e=1){const i=-.969266*t+1.8760108*r+.041556*n,s=.0556434*t-.2040259*r+1.0572252*n;return[U(3.2404542*t-1.5371385*r-.4985314*n),U(i),U(s),e]}function lt(t,r){let n=-1/0,e=-1;for(let i=0;i<t.length;i++){const s=r(t[i]);s>n&&(n=s,e=i)}return e}Y.xyz=ht,Y.lab=tt,Y.hsi=function(t,r,e,i=1){let s,o,u,c=t*p;return c<2*f/3?(u=(1-r)/3,s=(1+r*n(c)/n(f/3-c))/3,o=1-(u+s)):c<4*f/3?(c-=2*f/3,s=(1-r)/3,o=(1+r*n(c)/n(f/3-c))/3,u=1-(s+o)):(c-=4*f/3,o=(1-r)/3,u=(1+r*n(c)/n(f/3-c))/3,s=1-(o+u)),[3*e*s*255,3*e*o*255,3*e*u*255,i]},$.hsv=$.hsl=$.hsi=$.lch=$.hcg=function(t,r,n,e){const[i,s,o]=t[e](),[u,c,a]=r[e]();function h(t,r,n){return t+n*(C(r-t+180)-180)}return w(("h"==e.charAt(0)?h:d)(i,u,n),d(s,c,n),("h"==e.charAt(2)?h:d)(o,a,n),e)};const ft={acacia:"#ffc000",aeschynomene:"#ff4000",apios:"#00c0ff",arachis:"#ff8000",bauhinia:"#ff00c0",cajanus:"#0040ff",cercis:"#ff0040",chamaecrista:"#ffff00",cicer:"#80ff00",dalbergia:"#ff0000",glycine:"#4000ff",lablab:"#8000ff",lens:"#00ff80",lotus:"#00ffff",lupinus:"#c000ff",medicago:"#40ff00",phanera:"#ff0080",phaseolus:"#0000ff",pisum:"#00ff00",quillaja:"#666666",senna:"#c0ff00",sindora:"#ff00ff",trifolium:"#00ff40",vicia:"#00ffc0",vigna:"#0080ff"},gt=Object.keys(ft);class dt{constructor(t="#d3d3d3"){this.colorCache={},this.defaultColor=t}get(t,r={}){if(-1===t.indexOf(" "))throw'Error: required format is "genus species" with a space between';t=t.toLowerCase();const{lightnessFactor:n=dt.LIGHTNESS_FACTOR,overrides:e={}}=r,i=Object.fromEntries(Object.entries(e).map((([t,r])=>[t.toLowerCase(),r])));if(void 0!==i[t])return i[t];if(void 0!==this.colorCache[t])return this.colorCache[t];let s=this.defaultColor;const[o,u,...c]=t.split(" ");if(o in ft){const t=ft[o],[r,...e]=w(t).hsl();s=w(r,1,(dt.MIN_LIGHTNESS+function(t,r){let n=2166136261;for(let r=0;r<t.length;++r)n^=t.charCodeAt(r),n+=(n<<1)+(n<<4)+(n<<7)+(n<<8)+(n<<24);return(n>>>0)%r}(u,1e3)/1e3*(1-2*dt.MIN_LIGHTNESS))*n,"hsl").hex()}return this.colorCache[t]=s,s}}dt.LIGHTNESS_FACTOR=1,dt.MIN_LIGHTNESS=.3;const bt=new dt;export{dt as TaxonChroma,gt as genera,bt as taxonChroma};