UNPKG

cpt2js

Version:

Color palette text parser to a function, input compatible with GMT, GDAL, GRASS, PostGIS, ArcGIS

10 lines (9 loc) 20 kB
/*! * Copyright (c) 2022 WeatherLayers.com * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).cpt2js={})}(this,(function(e){"use strict";function t(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var r,n,o,a,f,s,l,i,u,c,h,d,g,b,p,m,y,w;function k(){return n?r:(n=1,r=(e,t=0,r=1)=>e<t?t:e>r?r:e)}function v(){if(a)return o;a=1;const e=k();return o=t=>{t._clipped=!1,t._unclipped=t.slice(0);for(let r=0;r<=3;r++)r<3?((t[r]<0||t[r]>255)&&(t._clipped=!0),t[r]=e(t[r],0,255)):3===r&&(t[r]=e(t[r],0,1));return t},o}function _(){if(s)return f;s=1;const e={};for(let t of["Boolean","Number","String","Function","Array","Date","RegExp","Undefined","Null"])e[`[object ${t}]`]=t.toLowerCase();return f=function(t){return e[Object.prototype.toString.call(t)]||"object"}}function $(){if(i)return l;i=1;const e=_();return l=(t,r=null)=>t.length>=3?Array.prototype.slice.call(t):"object"==e(t[0])&&r?r.split("").filter((e=>void 0!==t[0][e])).map((e=>t[0][e])):t[0]}function N(){if(c)return u;c=1;const e=_();return u=t=>{if(t.length<2)return null;const r=t.length-1;return"string"==e(t[r])?t[r].toLowerCase():null}}function x(){if(d)return h;d=1;const e=Math.PI;return h={clip_rgb:v(),limit:k(),type:_(),unpack:$(),last:N(),PI:e,TWOPI:2*e,PITHIRD:e/3,DEG2RAD:e/180,RAD2DEG:180/e}}function M(){return b?g:(b=1,g={format:{},autodetect:[]})}function F(){if(m)return p;m=1;const{last:e,clip_rgb:t,type:r}=x(),n=M();return p=class{constructor(...o){const a=this;if("object"===r(o[0])&&o[0].constructor&&o[0].constructor===this.constructor)return o[0];let f=e(o),s=!1;if(!f){s=!0,n.sorted||(n.autodetect=n.autodetect.sort(((e,t)=>t.p-e.p)),n.sorted=!0);for(let e of n.autodetect)if(f=e.test(...o),f)break}if(!n.format[f])throw new Error("unknown format: "+o);{const e=n.format[f].apply(null,s?o:o.slice(0,-1));a._rgb=t(e)}3===a._rgb.length&&a._rgb.push(1)}toString(){return"function"==r(this.hex)?this.hex():`[${this._rgb.join(",")}]`}},p}function S(){if(w)return y;w=1;const e=(...t)=>new e.Color(...t);return e.Color=F(),e.version="@@version",y=e}var j,A,C,E,L,O,I,R,q,D,P,z=t(S()),T={};function W(){if(E)return C;E=1;const{unpack:e}=x();return C=(...t)=>{t=e(t,"rgba");let[r,n,o]=t;r/=255,n/=255,o/=255;const a=Math.min(r,n,o),f=Math.max(r,n,o),s=(f+a)/2;let l,i;return f===a?(l=0,i=Number.NaN):l=s<.5?(f-a)/(f+a):(f-a)/(2-f-a),r==f?i=(n-o)/(f-a):n==f?i=2+(o-r)/(f-a):o==f&&(i=4+(r-n)/(f-a)),i*=60,i<0&&(i+=360),t.length>3&&void 0!==t[3]?[i,l,s,t[3]]:[i,l,s]}}function B(){if(O)return L;O=1;const{unpack:e,last:t}=x(),r=function(){if(A)return j;A=1;const{unpack:e,last:t}=x(),r=e=>Math.round(100*e)/100;return j=(...n)=>{const o=e(n,"hsla");let a=t(n)||"lsa";return o[0]=r(o[0]||0),o[1]=r(100*o[1])+"%",o[2]=r(100*o[2])+"%","hsla"===a||o.length>3&&o[3]<1?(o[3]=o.length>3?o[3]:1,a="hsla"):o.length=3,`${a}(${o.join(",")})`}}(),n=W(),{round:o}=Math;return L=(...a)=>{const f=e(a,"rgba");let s=t(a)||"rgb";return"hsl"==s.substr(0,3)?r(n(f),s):(f[0]=o(f[0]),f[1]=o(f[1]),f[2]=o(f[2]),("rgba"===s||f.length>3&&f[3]<1)&&(f[3]=f.length>3?f[3]:1,s="rgba"),`${s}(${f.slice(0,"rgb"===s?3:4).join(",")})`)}}function G(){if(R)return I;R=1;const{unpack:e}=x(),{round:t}=Math;return I=(...r)=>{r=e(r,"hsl");const[n,o,a]=r;let f,s,l;if(0===o)f=s=l=255*a;else{const e=[0,0,0],r=[0,0,0],i=a<.5?a*(1+o):a+o-a*o,u=2*a-i,c=n/360;e[0]=c+1/3,e[1]=c,e[2]=c-1/3;for(let t=0;t<3;t++)e[t]<0&&(e[t]+=1),e[t]>1&&(e[t]-=1),6*e[t]<1?r[t]=u+6*(i-u)*e[t]:2*e[t]<1?r[t]=i:3*e[t]<2?r[t]=u+(i-u)*(2/3-e[t])*6:r[t]=u;[f,s,l]=[t(255*r[0]),t(255*r[1]),t(255*r[2])]}return r.length>3?[f,s,l,r[3]]:[f,s,l,1]}}!function(){if(P)return T;P=1;const e=S(),t=F(),r=M(),{type:n}=x(),o=B(),a=function(){if(D)return q;D=1;const e=G(),t=M(),r=/^rgb\(\s*(-?\d+),\s*(-?\d+)\s*,\s*(-?\d+)\s*\)$/,n=/^rgba\(\s*(-?\d+),\s*(-?\d+)\s*,\s*(-?\d+)\s*,\s*([01]|[01]?\.\d+)\)$/,o=/^rgb\(\s*(-?\d+(?:\.\d+)?)%,\s*(-?\d+(?:\.\d+)?)%\s*,\s*(-?\d+(?:\.\d+)?)%\s*\)$/,a=/^rgba\(\s*(-?\d+(?:\.\d+)?)%,\s*(-?\d+(?:\.\d+)?)%\s*,\s*(-?\d+(?:\.\d+)?)%\s*,\s*([01]|[01]?\.\d+)\)$/,f=/^hsl\(\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)%\s*,\s*(-?\d+(?:\.\d+)?)%\s*\)$/,s=/^hsla\(\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)%\s*,\s*(-?\d+(?:\.\d+)?)%\s*,\s*([01]|[01]?\.\d+)\)$/,{round:l}=Math,i=i=>{let u;if(i=i.toLowerCase().trim(),t.format.named)try{return t.format.named(i)}catch(e){}if(u=i.match(r)){const e=u.slice(1,4);for(let t=0;t<3;t++)e[t]=+e[t];return e[3]=1,e}if(u=i.match(n)){const e=u.slice(1,5);for(let t=0;t<4;t++)e[t]=+e[t];return e}if(u=i.match(o)){const e=u.slice(1,4);for(let t=0;t<3;t++)e[t]=l(2.55*e[t]);return e[3]=1,e}if(u=i.match(a)){const e=u.slice(1,5);for(let t=0;t<3;t++)e[t]=l(2.55*e[t]);return e[3]=+e[3],e}if(u=i.match(f)){const t=u.slice(1,4);t[1]*=.01,t[2]*=.01;const r=e(t);return r[3]=1,r}if(u=i.match(s)){const t=u.slice(1,4);t[1]*=.01,t[2]*=.01;const r=e(t);return r[3]=+u[4],r}};return i.test=e=>r.test(e)||n.test(e)||o.test(e)||a.test(e)||f.test(e)||s.test(e),q=i}();t.prototype.css=function(e){return o(this._rgb,e)},e.css=(...e)=>new t(...e,"css"),r.format.css=a,r.autodetect.push({p:5,test:(e,...t)=>{if(!t.length&&"string"===n(e)&&a.test(e))return"css"}})}();var H,U,Z,J,K,Q={};function V(){if(U)return H;U=1;const{unpack:e,last:t}=x(),{round:r}=Math;return H=(...n)=>{let[o,a,f,s]=e(n,"rgba"),l=t(n)||"auto";void 0===s&&(s=1),"auto"===l&&(l=s<1?"rgba":"rgb"),o=r(o),a=r(a),f=r(f);let i="000000"+(o<<16|a<<8|f).toString(16);i=i.substr(i.length-6);let u="0"+r(255*s).toString(16);switch(u=u.substr(u.length-2),l.toLowerCase()){case"rgba":return`#${i}${u}`;case"argb":return`#${u}${i}`;default:return`#${i}`}}}function X(){if(J)return Z;J=1;const e=/^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/,t=/^#?([A-Fa-f0-9]{8}|[A-Fa-f0-9]{4})$/;return Z=r=>{if(r.match(e)){4!==r.length&&7!==r.length||(r=r.substr(1)),3===r.length&&(r=(r=r.split(""))[0]+r[0]+r[1]+r[1]+r[2]+r[2]);const e=parseInt(r,16);return[e>>16,e>>8&255,255&e,1]}if(r.match(t)){5!==r.length&&9!==r.length||(r=r.substr(1)),4===r.length&&(r=(r=r.split(""))[0]+r[0]+r[1]+r[1]+r[2]+r[2]+r[3]+r[3]);const e=parseInt(r,16);return[e>>24&255,e>>16&255,e>>8&255,Math.round((255&e)/255*100)/100]}throw new Error(`unknown hex color: ${r}`)},Z}!function(){if(K)return Q;K=1;const e=S(),t=F(),{type:r}=x(),n=M(),o=V();t.prototype.hex=function(e){return o(this._rgb,e)},e.hex=(...e)=>new t(...e,"hex"),n.format.hex=X(),n.autodetect.push({p:4,test:(e,...t)=>{if(!t.length&&"string"===r(e)&&[3,4,5,6,7,8,9].indexOf(e.length)>=0)return"hex"}})}();var Y,ee={};function te(){if(Y)return ee;Y=1;const{unpack:e,type:t}=x(),r=S(),n=F(),o=M(),a=W();return n.prototype.hsl=function(){return a(this._rgb)},r.hsl=(...e)=>new n(...e,"hsl"),o.format.hsl=G(),o.autodetect.push({p:2,test:(...r)=>{if(r=e(r,"hsl"),"array"===t(r)&&3===r.length)return"hsl"}}),ee}te();var re,ne,oe,ae,fe,se={};function le(){if(ne)return re;ne=1;const{unpack:e}=x(),{min:t,max:r}=Math;return re=(...n)=>{n=e(n,"rgb");let[o,a,f]=n;const s=t(o,a,f),l=r(o,a,f),i=l-s;let u,c,h;return h=l/255,0===l?(u=Number.NaN,c=0):(c=i/l,o===l&&(u=(a-f)/i),a===l&&(u=2+(f-o)/i),f===l&&(u=4+(o-a)/i),u*=60,u<0&&(u+=360)),[u,c,h]}}function ie(){if(fe)return se;fe=1;const{unpack:e,type:t}=x(),r=S(),n=F(),o=M(),a=le();return n.prototype.hsv=function(){return a(this._rgb)},r.hsv=(...e)=>new n(...e,"hsv"),o.format.hsv=function(){if(ae)return oe;ae=1;const{unpack:e}=x(),{floor:t}=Math;return oe=(...r)=>{r=e(r,"hsv");let n,o,a,[f,s,l]=r;if(l*=255,0===s)n=o=a=l;else{360===f&&(f=0),f>360&&(f-=360),f<0&&(f+=360),f/=60;const e=t(f),r=f-e,i=l*(1-s),u=l*(1-s*r),c=l*(1-s*(1-r));switch(e){case 0:[n,o,a]=[l,c,i];break;case 1:[n,o,a]=[u,l,i];break;case 2:[n,o,a]=[i,l,c];break;case 3:[n,o,a]=[i,u,l];break;case 4:[n,o,a]=[c,i,l];break;case 5:[n,o,a]=[l,i,u]}}return[n,o,a,r.length>3?r[3]:1]}}(),o.autodetect.push({p:2,test:(...r)=>{if(r=e(r,"hsv"),"array"===t(r)&&3===r.length)return"hsv"}}),se}ie();var ue,ce,he,de={};!function(){if(he)return de;he=1;const e=F(),t=M(),{type:r}=x(),n=ce?ue:(ce=1,ue={aliceblue:"#f0f8ff",antiquewhite:"#faebd7",aqua:"#00ffff",aquamarine:"#7fffd4",azure:"#f0ffff",beige:"#f5f5dc",bisque:"#ffe4c4",black:"#000000",blanchedalmond:"#ffebcd",blue:"#0000ff",blueviolet:"#8a2be2",brown:"#a52a2a",burlywood:"#deb887",cadetblue:"#5f9ea0",chartreuse:"#7fff00",chocolate:"#d2691e",coral:"#ff7f50",cornflower:"#6495ed",cornflowerblue:"#6495ed",cornsilk:"#fff8dc",crimson:"#dc143c",cyan:"#00ffff",darkblue:"#00008b",darkcyan:"#008b8b",darkgoldenrod:"#b8860b",darkgray:"#a9a9a9",darkgreen:"#006400",darkgrey:"#a9a9a9",darkkhaki:"#bdb76b",darkmagenta:"#8b008b",darkolivegreen:"#556b2f",darkorange:"#ff8c00",darkorchid:"#9932cc",darkred:"#8b0000",darksalmon:"#e9967a",darkseagreen:"#8fbc8f",darkslateblue:"#483d8b",darkslategray:"#2f4f4f",darkslategrey:"#2f4f4f",darkturquoise:"#00ced1",darkviolet:"#9400d3",deeppink:"#ff1493",deepskyblue:"#00bfff",dimgray:"#696969",dimgrey:"#696969",dodgerblue:"#1e90ff",firebrick:"#b22222",floralwhite:"#fffaf0",forestgreen:"#228b22",fuchsia:"#ff00ff",gainsboro:"#dcdcdc",ghostwhite:"#f8f8ff",gold:"#ffd700",goldenrod:"#daa520",gray:"#808080",green:"#008000",greenyellow:"#adff2f",grey:"#808080",honeydew:"#f0fff0",hotpink:"#ff69b4",indianred:"#cd5c5c",indigo:"#4b0082",ivory:"#fffff0",khaki:"#f0e68c",laserlemon:"#ffff54",lavender:"#e6e6fa",lavenderblush:"#fff0f5",lawngreen:"#7cfc00",lemonchiffon:"#fffacd",lightblue:"#add8e6",lightcoral:"#f08080",lightcyan:"#e0ffff",lightgoldenrod:"#fafad2",lightgoldenrodyellow:"#fafad2",lightgray:"#d3d3d3",lightgreen:"#90ee90",lightgrey:"#d3d3d3",lightpink:"#ffb6c1",lightsalmon:"#ffa07a",lightseagreen:"#20b2aa",lightskyblue:"#87cefa",lightslategray:"#778899",lightslategrey:"#778899",lightsteelblue:"#b0c4de",lightyellow:"#ffffe0",lime:"#00ff00",limegreen:"#32cd32",linen:"#faf0e6",magenta:"#ff00ff",maroon:"#800000",maroon2:"#7f0000",maroon3:"#b03060",mediumaquamarine:"#66cdaa",mediumblue:"#0000cd",mediumorchid:"#ba55d3",mediumpurple:"#9370db",mediumseagreen:"#3cb371",mediumslateblue:"#7b68ee",mediumspringgreen:"#00fa9a",mediumturquoise:"#48d1cc",mediumvioletred:"#c71585",midnightblue:"#191970",mintcream:"#f5fffa",mistyrose:"#ffe4e1",moccasin:"#ffe4b5",navajowhite:"#ffdead",navy:"#000080",oldlace:"#fdf5e6",olive:"#808000",olivedrab:"#6b8e23",orange:"#ffa500",orangered:"#ff4500",orchid:"#da70d6",palegoldenrod:"#eee8aa",palegreen:"#98fb98",paleturquoise:"#afeeee",palevioletred:"#db7093",papayawhip:"#ffefd5",peachpuff:"#ffdab9",peru:"#cd853f",pink:"#ffc0cb",plum:"#dda0dd",powderblue:"#b0e0e6",purple:"#800080",purple2:"#7f007f",purple3:"#a020f0",rebeccapurple:"#663399",red:"#ff0000",rosybrown:"#bc8f8f",royalblue:"#4169e1",saddlebrown:"#8b4513",salmon:"#fa8072",sandybrown:"#f4a460",seagreen:"#2e8b57",seashell:"#fff5ee",sienna:"#a0522d",silver:"#c0c0c0",skyblue:"#87ceeb",slateblue:"#6a5acd",slategray:"#708090",slategrey:"#708090",snow:"#fffafa",springgreen:"#00ff7f",steelblue:"#4682b4",tan:"#d2b48c",teal:"#008080",thistle:"#d8bfd8",tomato:"#ff6347",turquoise:"#40e0d0",violet:"#ee82ee",wheat:"#f5deb3",white:"#ffffff",whitesmoke:"#f5f5f5",yellow:"#ffff00",yellowgreen:"#9acd32"}),o=X(),a=V();e.prototype.name=function(){const e=a(this._rgb,"rgb");for(let t of Object.keys(n))if(n[t]===e)return t.toLowerCase();return e},t.format.named=e=>{if(e=e.toLowerCase(),n[e])return o(n[e]);throw new Error("unknown color name: "+e)},t.autodetect.push({p:5,test:(e,...t)=>{if(!t.length&&"string"===r(e)&&n[e.toLowerCase()])return"named"}})}();var ge,be={};!function(){if(ge)return be;ge=1;const e=S(),t=F(),r=M(),{unpack:n,type:o}=x(),{round:a}=Math;t.prototype.rgb=function(e=!0){return!1===e?this._rgb.slice(0,3):this._rgb.slice(0,3).map(a)},t.prototype.rgba=function(e=!0){return this._rgb.slice(0,4).map(((t,r)=>r<3?!1===e?t:a(t):t))},e.rgb=(...e)=>new t(...e,"rgb"),r.format.rgb=(...e)=>{const t=n(e,"rgba");return void 0===t[3]&&(t[3]=1),t},r.autodetect.push({p:3,test:(...e)=>{if(e=n(e,"rgba"),"array"===o(e)&&(3===e.length||4===e.length&&"number"==o(e[3])&&e[3]>=0&&e[3]<=1))return"rgb"}})}();var pe,me,ye,we,ke,ve,_e,$e,Ne,xe,Me,Fe,Se,je={};function Ae(){if(ye)return me;ye=1;const e=F();return me=(t,r,n,o)=>{let a,f,s,l,i,u,c,h,d,g,b,p;return"hsl"===o?(a=t.hsl(),f=r.hsl()):"hsv"===o?(a=t.hsv(),f=r.hsv()):"hcg"===o?(a=t.hcg(),f=r.hcg()):"hsi"===o?(a=t.hsi(),f=r.hsi()):"lch"===o||"hcl"===o?(o="hcl",a=t.hcl(),f=r.hcl()):"oklch"===o&&(a=t.oklch().reverse(),f=r.oklch().reverse()),"h"!==o.substr(0,1)&&"oklch"!==o||([s,i,c]=a,[l,u,h]=f),isNaN(s)||isNaN(l)?isNaN(s)?isNaN(l)?g=Number.NaN:(g=l,1!=c&&0!=c||"hsv"==o||(d=u)):(g=s,1!=h&&0!=h||"hsv"==o||(d=i)):(p=l>s&&l-s>180?l-(s+360):l<s&&s-l>180?l+360-s:l-s,g=s+n*p),void 0===d&&(d=i+n*(u-i)),b=c+n*(h-c),new e("oklch"===o?[b,d,g]:[g,d,b],o)}}function Ce(){return ke?we:(ke=1,we={})}!function(){if(pe)return je;pe=1;const e=F(),{type:t}=x();e.prototype.alpha=function(r,n=!1){return void 0!==r&&"number"===t(r)?n?(this._rgb[3]=r,this):new e([this._rgb[0],this._rgb[1],this._rgb[2],r],"rgb"):this._rgb[3]}}(),function(){if(_e)return ve;_e=1,te();const e=Ae(),t=(t,r,n)=>e(t,r,n,"hsl");Ce().hsl=t,ve=t}(),function(){if(Ne)return $e;Ne=1,ie();const e=Ae(),t=(t,r,n)=>e(t,r,n,"hsv");Ce().hsv=t,$e=t}(),function(){if(Me)return xe;Me=1;const e=F(),t=(t,r,n)=>{const o=t._rgb,a=r._rgb;return new e(o[0]+n*(a[0]-o[0]),o[1]+n*(a[1]-o[1]),o[2]+n*(a[2]-o[2]),"rgb")};Ce().rgb=t,xe=t}();var Ee,Le,Oe=function(){if(Se)return Fe;Se=1;const e=F(),{type:t}=x(),r=Ce();return Fe=(n,o,a=.5,...f)=>{let s=f[0]||"lrgb";if(r[s]||f.length||(s=Object.keys(r)[0]),!r[s])throw new Error(`interpolation mode ${s} is not defined`);return"object"!==t(n)&&(n=new e(n)),"object"!==t(o)&&(o=new e(o)),r[s](n,o,a).alpha(n.alpha()+a*(o.alpha()-n.alpha()))}}(),Ie=t(Oe);var Re=function(){if(Le)return Ee;Le=1;const e=S(),{type:t}=x(),{pow:r}=Math;return Ee=function(n){let o="rgb",a=e("#ccc"),f=0,s=[0,1],l=[],i=[0,0],u=!1,c=[],h=!1,d=0,g=1,b=!1,p={},m=!0,y=1;const w=function(r){if((r=r||["#fff","#000"])&&"string"===t(r)&&e.brewer&&e.brewer[r.toLowerCase()]&&(r=e.brewer[r.toLowerCase()]),"array"===t(r)){1===r.length&&(r=[r[0],r[0]]),r=r.slice(0);for(let t=0;t<r.length;t++)r[t]=e(r[t]);l.length=0;for(let e=0;e<r.length;e++)l.push(e/(r.length-1))}return $(),c=r};let k=e=>e,v=e=>e;const _=function(n,f){let s,h;if(null==f&&(f=!1),isNaN(n)||null===n)return a;if(f)h=n;else if(u&&u.length>2){h=function(e){if(null!=u){const t=u.length-1;let r=0;for(;r<t&&e>=u[r];)r++;return r-1}return 0}(n)/(u.length-2)}else h=g!==d?(n-d)/(g-d):1;h=v(h),f||(h=k(h)),1!==y&&(h=r(h,y)),h=i[0]+h*(1-i[0]-i[1]),h=Math.min(1,Math.max(0,h));const b=Math.floor(1e4*h);if(m&&p[b])s=p[b];else{if("array"===t(c))for(let t=0;t<l.length;t++){const r=l[t];if(h<=r){s=c[t];break}if(h>=r&&t===l.length-1){s=c[t];break}if(h>r&&h<l[t+1]){h=(h-r)/(l[t+1]-r),s=e.interpolate(c[t],c[t+1],h,o);break}}else"function"===t(c)&&(s=c(h));m&&(p[b]=s)}return s};var $=()=>p={};w(n);const N=function(t){const r=e(_(t));return h&&r[h]?r[h]():r};return N.classes=function(r){if(null!=r){if("array"===t(r))u=r,s=[r[0],r[r.length-1]];else{const t=e.analyze(s);u=0===r?[t.min,t.max]:e.limits(t,"e",r)}return N}return u},N.domain=function(e){if(!arguments.length)return s;d=e[0],g=e[e.length-1],l=[];const t=c.length;if(e.length===t&&d!==g)for(let t of Array.from(e))l.push((t-d)/(g-d));else{for(let e=0;e<t;e++)l.push(e/(t-1));if(e.length>2){const t=e.map(((t,r)=>r/(e.length-1))),r=e.map((e=>(e-d)/(g-d)));r.every(((e,r)=>t[r]===e))||(v=e=>{if(e<=0||e>=1)return e;let n=0;for(;e>=r[n+1];)n++;const o=(e-r[n])/(r[n+1]-r[n]);return t[n]+o*(t[n+1]-t[n])})}}return s=[d,g],N},N.mode=function(e){return arguments.length?(o=e,$(),N):o},N.range=function(e,t){return w(e),N},N.out=function(e){return h=e,N},N.spread=function(e){return arguments.length?(f=e,N):f},N.correctLightness=function(e){return null==e&&(e=!0),b=e,$(),k=b?function(e){const t=_(0,!0).lab()[0],r=_(1,!0).lab()[0],n=t>r;let o=_(e,!0).lab()[0];const a=t+(r-t)*e;let f=o-a,s=0,l=1,i=20;for(;Math.abs(f)>.01&&i-- >0;)n&&(f*=-1),f<0?(s=e,e+=.5*(l-e)):(l=e,e+=.5*(s-e)),o=_(e,!0).lab()[0],f=o-a;return e}:e=>e,N},N.padding=function(e){return null!=e?("number"===t(e)&&(e=[e,e]),i=e,N):i},N.colors=function(t,r){arguments.length<2&&(r="hex");let o=[];if(0===arguments.length)o=c.slice(0);else if(1===t)o=[N(.5)];else if(t>1){const e=s[0],r=s[1]-e;o=function(e,t,r){let n=[],o=e<t,a=t;for(let t=e;o?t<a:t>a;o?t++:t--)n.push(t);return n}(0,t).map((n=>N(e+n/(t-1)*r)))}else{n=[];let e=[];if(u&&u.length>2)for(let t=1,r=u.length,n=1<=r;n?t<r:t>r;n?t++:t--)e.push(.5*(u[t-1]+u[t]));else e=s;o=e.map((e=>N(e)))}return e[r]&&(o=o.map((e=>e[r]()))),o},N.cache=function(e){return null!=e?(m=e,N):m},N.gamma=function(e){return null!=e?(y=e,N):y},N.nodata=function(t){return null!=t?(a=e(t),N):a},N},Ee}(),qe=t(Re);z.mix=z.interpolate=Ie,z.scale=qe;const De=/[ ,\t:]+/g,Pe=/[\-\/]/g;function ze(e){return e.startsWith("#")}function Te(e){const t=e.split(Pe);return 1===t.length?t[0]:t}function We(e){const t=e.trim().split("\n"),r=function(e){return e.some((e=>!ze(e)&&e.split(De).length>=8))}(t),n=function(e){return e.some((e=>!(ze(e)||!e.match(/\d+\-\d+\-\d+/)&&!e.match(/\d+\/\d+\/\d+/))))}(t),o=function(e){const t=e.find((e=>ze(e)&&e.includes("COLOR_MODEL = ")));if(t){const e=t.match(/COLOR_MODEL = ([a-zA-Z]+)/);if(e)return e[1].toLowerCase()}}(t),a=t.filter((e=>!!e&&!e.startsWith("#"))),f=[];for(let e of a){const t=e.split(De);r?8===t.length||9===t.length?(f.push([t[0],[t[1],t[2],t[3]]]),f.push([t[4],[t[5],t[6],t[7]]])):4!==t.length&&5!==t.length||f.push([t[0],[t[1],t[2],t[3]]]):n?4===t.length||5===t.length?(f.push([t[0],Te(t[1])]),f.push([t[2],Te(t[3])])):2!==t.length&&3!==t.length||f.push([t[0],Te(t[1])]):5===t.length?f.push([t[0],[t[1],t[2],t[3],t[4]]]):4===t.length?f.push([t[0],[t[1],t[2],t[3]]]):2===t.length&&f.push([t[0],t[1]])}return{paletteArray:f,mode:o}}const Be="rgb";function Ge(e,t){if("string"==typeof e){if("%"===e[e.length-1]){const r=parseFloat(e)/100;if(r<0||r>1)throw new Error(`Invalid value for a percentage ${e}`);return t[0]+(t[1]-t[0])*r}return"N"===e?null:"B"===e||"F"===e?void 0:"nv"===e?null:"default"===e?void 0:"null"===e||"nodata"===e?null:parseFloat(e)}if("number"==typeof e)return e;throw new Error("Invalid state")}function He(e,t){if(Array.isArray(e)){if(4===e.length)return{[t[0]]:parseFloat(e[0].toString()),[t[1]]:parseFloat(e[1].toString()),[t[2]]:parseFloat(e[2].toString()),a:parseFloat(e[3].toString())/255};if(3===e.length)return{[t[0]]:parseFloat(e[0].toString()),[t[1]]:parseFloat(e[1].toString()),[t[2]]:parseFloat(e[2].toString())};throw new Error(`Invalid color ${e}`)}if("string"==typeof e||"number"==typeof e)return e.toString().match(/^\d+$/)||"number"==typeof e?{[t[0]]:parseFloat(e.toString()),[t[1]]:parseFloat(e.toString()),[t[2]]:parseFloat(e.toString())}:e;throw new Error(`Invalid color ${e}`)}function Ue(e,{bounds:t=[0,1],mode:r=Be}={}){const n=[],o=[];let a;for(let[f,s]of e){const e=Ge(f,t),l=He(s,r);null!=e?(n.push(l),o.push(e)):null===e&&(a=l)}let f=z.scale(n).domain(o).mode(r);return void 0!==a&&(f=f.nodata(a)),f}e.colorRampCanvas=function(e,{width:t=256,height:r=1}={}){const n=e.colors(t,"css"),o=document.createElement("canvas");o.width=t,o.height=r,o.style.imageRendering="-moz-crisp-edges",o.style.imageRendering="pixelated";const a=o.getContext("2d");for(let e=0;e<t;e++)a.fillStyle=n[e],a.fillRect(e,0,1,r);return o},e.parsePalette=function(e,{bounds:t=[0,1]}={}){if("string"==typeof e)return function(e,{bounds:t=[0,1]}={}){const{paletteArray:r,mode:n}=We(e);return Ue(r,{bounds:t,mode:n})}(e,{bounds:t});if(Array.isArray(e))return Ue(e,{bounds:t});throw new Error("Invalid format")}})); //# sourceMappingURL=cpt2js.umd.min.js.map