forked from regorxxx/chroma.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chroma-ultra-light.min.js
58 lines (57 loc) · 8.89 KB
/
chroma-ultra-light.min.js
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
/**
* chroma.js - JavaScript library for color conversions
*
* Copyright (c) 2011-2019, 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.
*
* -------------------------------------------------------
*
* chroma.js includes colors from colorbrewer2.org, which are released under
* the following license:
*
* Copyright (c) 2002 Cynthia Brewer, Mark Harrower,
* and The Pennsylvania State University.
*
* 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.
*
* ------------------------------------------------------
*
* Named colors are taken from X11 Color Names.
* http://www.w3.org/TR/css3-color/#svg-color
*
* @preserve
*/
var t=(t,r=0,e=1)=>t<r?r:t>e?e:t;const r={};for(let t of["Boolean","Number","String","Function","Array","Date","RegExp","Undefined","Null"])r[`[object ${t}]`]=t.toLowerCase();function e(t){return r[Object.prototype.toString.call(t)]||"object"}var n=(t,r=null)=>t.length>=3?Array.prototype.slice.call(t):"object"==e(t[0])&&r?r.split("").filter((r=>void 0!==t[0][r])).map((r=>t[0][r])):t[0],s=t=>{if(t.length<2)return null;const r=t.length-1;return"string"==e(t[r])?t[r].toLowerCase():null},o={format:{},autodetect:[]};class a{constructor(...r){const n=this;if("object"===e(r[0])&&r[0].constructor&&r[0].constructor===this.constructor)return r[0];let a=s(r),c=!1;if(!a){c=!0,o.sorted||(o.autodetect=o.autodetect.sort(((t,r)=>r.p-t.p)),o.sorted=!0);for(let t of o.autodetect)if(a=t.test(...r),a)break}if(!o.format[a])throw new Error("unknown format: "+r);{const e=o.format[a].apply(null,c?r:r.slice(0,-1));n._rgb=(r=>{r._clipped=!1,r._unclipped=r.slice(0);for(let e=0;e<=3;e++)e<3?((r[e]<0||r[e]>255)&&(r._clipped=!0),r[e]=t(r[e],0,255)):3===e&&(r[e]=t(r[e],0,1));return r})(e)}3===n._rgb.length&&n._rgb.push(1)}toString(){return"function"==e(this.hex)?this.hex():`[${this._rgb.join(",")}]`}}const c=(...t)=>new c.Color(...t);c.Color=a,c.version="2.6.2";const l=t=>Math.round(100*t)/100,{round:i}=Math,u=(...t)=>{const r=n(t,"rgba");let e=s(t)||"rgb";return"hsl"==e.substr(0,3)?((...t)=>{const r=n(t,"hsla");let e=s(t)||"lsa";return r[0]=l(r[0]||0),r[1]=l(100*r[1])+"%",r[2]=l(100*r[2])+"%","hsla"===e||r.length>3&&r[3]<1?(r[3]=r.length>3?r[3]:1,e="hsla"):r.length=3,`${e}(${r.join(",")})`})(((...t)=>{t=n(t,"rgba");let[r,e,s]=t;r/=255,e/=255,s/=255;const o=Math.min(r,e,s),a=Math.max(r,e,s),c=(a+o)/2;let l,i;return a===o?(l=0,i=Number.NaN):l=c<.5?(a-o)/(a+o):(a-o)/(2-a-o),r==a?i=(e-s)/(a-o):e==a?i=2+(s-r)/(a-o):s==a&&(i=4+(r-e)/(a-o)),i*=60,i<0&&(i+=360),t.length>3&&void 0!==t[3]?[i,l,c,t[3]]:[i,l,c]})(r),e):(r[0]=i(r[0]),r[1]=i(r[1]),r[2]=i(r[2]),("rgba"===e||r.length>3&&r[3]<1)&&(r[3]=r.length>3?r[3]:1,e="rgba"),`${e}(${r.slice(0,"rgb"===e?3:4).join(",")})`)},{round:h}=Math,d=(...t)=>{t=n(t,"hsl");const[r,e,s]=t;let o,a,c;if(0===e)o=a=c=255*s;else{const t=[0,0,0],n=[0,0,0],l=s<.5?s*(1+e):s+e-s*e,i=2*s-l,u=r/360;t[0]=u+1/3,t[1]=u,t[2]=u-1/3;for(let r=0;r<3;r++)t[r]<0&&(t[r]+=1),t[r]>1&&(t[r]-=1),6*t[r]<1?n[r]=i+6*(l-i)*t[r]:2*t[r]<1?n[r]=l:3*t[r]<2?n[r]=i+(l-i)*(2/3-t[r])*6:n[r]=i;[o,a,c]=[h(255*n[0]),h(255*n[1]),h(255*n[2])]}return t.length>3?[o,a,c,t[3]]:[o,a,c,1]},g=/^rgb\(\s*(-?\d+),\s*(-?\d+)\s*,\s*(-?\d+)\s*\)$/,f=/^rgba\(\s*(-?\d+),\s*(-?\d+)\s*,\s*(-?\d+)\s*,\s*([01]|[01]?\.\d+)\)$/,b=/^rgb\(\s*(-?\d+(?:\.\d+)?)%,\s*(-?\d+(?:\.\d+)?)%\s*,\s*(-?\d+(?:\.\d+)?)%\s*\)$/,p=/^rgba\(\s*(-?\d+(?:\.\d+)?)%,\s*(-?\d+(?:\.\d+)?)%\s*,\s*(-?\d+(?:\.\d+)?)%\s*,\s*([01]|[01]?\.\d+)\)$/,m=/^hsl\(\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)%\s*,\s*(-?\d+(?:\.\d+)?)%\s*\)$/,w=/^hsla\(\s*(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)%\s*,\s*(-?\d+(?:\.\d+)?)%\s*,\s*([01]|[01]?\.\d+)\)$/,{round:$}=Math,y=t=>{let r;if(t=t.toLowerCase().trim(),o.format.named)try{return o.format.named(t)}catch(t){}if(r=t.match(g)){const t=r.slice(1,4);for(let r=0;r<3;r++)t[r]=+t[r];return t[3]=1,t}if(r=t.match(f)){const t=r.slice(1,5);for(let r=0;r<4;r++)t[r]=+t[r];return t}if(r=t.match(b)){const t=r.slice(1,4);for(let r=0;r<3;r++)t[r]=$(2.55*t[r]);return t[3]=1,t}if(r=t.match(p)){const t=r.slice(1,5);for(let r=0;r<3;r++)t[r]=$(2.55*t[r]);return t[3]=+t[3],t}if(r=t.match(m)){const t=r.slice(1,4);t[1]*=.01,t[2]*=.01;const e=d(t);return e[3]=1,e}if(r=t.match(w)){const t=r.slice(1,4);t[1]*=.01,t[2]*=.01;const e=d(t);return e[3]=+r[4],e}};y.test=t=>g.test(t)||f.test(t)||b.test(t)||p.test(t)||m.test(t)||w.test(t),a.prototype.css=function(t){return u(this._rgb,t)},c.css=(...t)=>new a(...t,"css"),o.format.css=y,o.autodetect.push({p:5,test:(t,...r)=>{if(!r.length&&"string"===e(t)&&y.test(t))return"css"}});const{round:x}=Math,_=/^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/,M=/^#?([A-Fa-f0-9]{8}|[A-Fa-f0-9]{4})$/;a.prototype.hex=function(t){return((...t)=>{let[r,e,o,a]=n(t,"rgba"),c=s(t)||"auto";void 0===a&&(a=1),"auto"===c&&(c=a<1?"rgba":"rgb"),r=x(r),e=x(e),o=x(o);let l="000000"+(r<<16|e<<8|o).toString(16);l=l.substr(l.length-6);let i="0"+x(255*a).toString(16);switch(i=i.substr(i.length-2),c.toLowerCase()){case"rgba":return`#${l}${i}`;case"argb":return`#${i}${l}`;default:return`#${l}`}})(this._rgb,t)},c.hex=(...t)=>new a(...t,"hex"),o.format.hex=t=>{if(t.match(_)){4!==t.length&&7!==t.length||(t=t.substr(1)),3===t.length&&(t=(t=t.split(""))[0]+t[0]+t[1]+t[1]+t[2]+t[2]);const r=parseInt(t,16);return[r>>16,r>>8&255,255&r,1]}if(t.match(M)){5!==t.length&&9!==t.length||(t=t.substr(1)),4===t.length&&(t=(t=t.split(""))[0]+t[0]+t[1]+t[1]+t[2]+t[2]+t[3]+t[3]);const r=parseInt(t,16);return[r>>24&255,r>>16&255,r>>8&255,Math.round((255&r)/255*100)/100]}throw new Error(`unknown hex color: ${t}`)},o.autodetect.push({p:4,test:(t,...r)=>{if(!r.length&&"string"===e(t)&&[3,4,5,6,7,8,9].indexOf(t.length)>=0)return"hex"}});const{round:v}=Math;a.prototype.rgb=function(t=!0){return!1===t?this._rgb.slice(0,3):this._rgb.slice(0,3).map(v)},a.prototype.rgba=function(t=!0){return this._rgb.slice(0,4).map(((r,e)=>e<3?!1===t?r:v(r):r))},c.rgb=(...t)=>new a(...t,"rgb"),o.format.rgb=(...t)=>{const r=n(t,"rgba");return void 0===r[3]&&(r[3]=1),r},o.autodetect.push({p:3,test:(...t)=>{if("array"===e(t=n(t,"rgba"))&&(3===t.length||4===t.length&&"number"==e(t[3])&&t[3]>=0&&t[3]<=1))return"rgb"}});const{round:j}=Math;a.prototype.android=function(t){return((...t)=>{const r=n(t,"rgba");let e=(1!==r[3]?s(t):"rgb")||"rgb";return r[0]=j(r[0]),r[1]=j(r[1]),r[2]=j(r[2]),("rgba"===e||r.length>3&&r[3]<1&&"rgb"!==s(t))&&(r[3]=r.length>3?r[3]:1,e="rgba"),"rgba"===e?r[3]<<24|r[0]<<16|r[1]<<8|r[2]:4278190080|r[0]<<16|r[1]<<8|r[2]})(this._rgb,t)},c.android=(...t)=>new a(...t,"android"),o.format.android=t=>{if("number"==e(t)){const r=(t>>24&255)/255;if(r>=0&&r<=1){const e=t-4278190080;return[e>>16&255,e>>8&255,255&e,r]}}throw new Error("unknown android color: "+t)},o.autodetect.push({p:5,test:(...t)=>{if(1===t.length&&"number"===e(t[0])){const r=(t[0]>>24&255)/255;if(r>=0&&r<=1){const r=t[0]-4278190080;if([r>>16&255,r>>8&255,255&r].every((t=>t>=0&&t<=255)))return"android"}}}}),a.prototype.get=function(t){const[r,e]=t.split("."),n=this[r]();if(e){const t=r.indexOf(e)-("ok"===r.substr(0,2)?2:0);if(t>-1)return n[t];throw new Error(`unknown channel ${e} in mode ${r}`)}return n};c.valid=(...t)=>{try{return new a(...t),!0}catch(t){return!1}};export{c as default};