mirror of
https://github.com/aurora-dot/pastel.codes.git
synced 2024-12-23 07:02:26 +00:00
1 line
29 KiB
JavaScript
1 line
29 KiB
JavaScript
|
function w3color(t,e){return this instanceof w3color?"object"==typeof t?t:(this.attachValues(toColorObject(t)),void(e&&(e.style.backgroundColor=this.toRgbString()))):new w3color(t,e)}function toColorObject(t){var e,r,a,n,i,s,o,h,f,u,l,c=[],b=[],d=[];if(e=(t=w3trim(t.toLowerCase())).substr(0,1).toUpperCase(),r=t.substr(1),h=1,"R"!=e&&"Y"!=e&&"G"!=e&&"C"!=e&&"B"!=e&&"M"!=e&&"W"!=e||isNaN(r)||(6!=t.length||-1!=t.indexOf(","))&&(t="ncol("+t+")"),3==t.length||6==t.length||isNaN(t)||(t="ncol("+t+")"),t.indexOf(",")>0&&-1==t.indexOf("(")&&(t="ncol("+t+")"),"rgb"==t.substr(0,3)||"hsl"==t.substr(0,3)||"hwb"==t.substr(0,3)||"ncol"==t.substr(0,4)||"cmyk"==t.substr(0,4)){if("ncol"==t.substr(0,4)?(4==t.split(",").length&&-1==t.indexOf("ncola")&&(t=t.replace("ncol","ncola")),a="ncol",t=t.substr(4)):"cmyk"==t.substr(0,4)?(a="cmyk",t=t.substr(4)):(a=t.substr(0,3),t=t.substr(3)),n=3,s=!1,"a"==t.substr(0,1).toLowerCase()?(n=4,s=!0,t=t.substr(1)):"cmyk"==a&&(n=4,5==t.split(",").length&&(n=5,s=!0)),c=(t=(t=t.replace("(","")).replace(")","")).split(","),"rgb"==a){if(c.length!=n)return emptyObject();for(i=0;i<n;i++){if((""==c[i]||" "==c[i])&&(c[i]="0"),c[i].indexOf("%")>-1&&(c[i]=c[i].replace("%",""),c[i]=Number(c[i]/100),i<3&&(c[i]=Math.round(255*c[i]))),isNaN(c[i]))return emptyObject();parseInt(c[i])>255&&(c[i]=255),i<3&&(c[i]=parseInt(c[i])),3==i&&Number(c[i])>1&&(c[i]=1)}l={r:c[0],g:c[1],b:c[2]},1==s&&(h=Number(c[3]))}if("hsl"==a||"hwb"==a||"ncol"==a){for(;c.length<n;)c.push("0");for(("hsl"==a||"hwb"==a)&&parseInt(c[0])>=360&&(c[0]=0),i=1;i<n;i++){if(c[i].indexOf("%")>-1){if(c[i]=c[i].replace("%",""),c[i]=Number(c[i]),isNaN(c[i]))return emptyObject();c[i]=c[i]/100}else c[i]=Number(c[i]);Number(c[i])>1&&(c[i]=1),0>Number(c[i])&&(c[i]=0)}"hsl"==a&&(l=hslToRgb(c[0],c[1],c[2]),f=Number(c[0]),u=Number(c[1])),"hwb"==a&&(l=hwbToRgb(c[0],c[1],c[2])),"ncol"==a&&(l=ncolToRgb(c[0],c[1],c[2])),1==s&&(h=Number(c[3]))}if("cmyk"==a){for(;c.length<n;)c.push("0");for(i=0;i<n;i++){if(c[i].indexOf("%")>-1){if(c[i]=c[i].replace("%",""),c[i]=Number(c[i]),isNaN(c[i]))return emptyObject();c[i]=c[i]/100}else c[i]=Number(c[i]);Number(c[i])>1&&(c[i]=1),0>Number(c[i])&&(c[i]=0)}l=cmykToRgb(c[0],c[1],c[2],c[3]),1==s&&(h=Number(c[4]))}}else if("ncs"==t.substr(0,3))l=ncsToRgb(t);else{for(i=0,o=!1,b=getColorArr("names");i<b.length;i++)if(t.toLowerCase()==b[i].toLowerCase()){d=getColorArr("hexs"),o=!0,l={r:parseInt(d[i].substr(0,2),16),g:parseInt(d[i].substr(2,2),16),b:parseInt(d[i].substr(4,2),16)};break}if(0==o){for(3==(t=t.replace("#","")).length&&(t=t.substr(0,1)+t.substr(0,1)+t.substr(1,1)+t.substr(1,1)+t.substr(2,1)+t.substr(2,1)),i=0;i<t.length;i++)if(!isHex(t.substr(i,1)))return emptyObject();for(i=0,c[0]=parseInt(t.substr(0,2),16),c[1]=parseInt(t.substr(2,2),16),c[2]=parseInt(t.substr(4,2),16);i<3;i++)if(isNaN(c[i]))return emptyObject();l={r:c[0],g:c[1],b:c[2]}}}return colorObject(l,h,f,u)}function colorObject(t,e,r,a){var n,i,s,o,h,f;return t?(null===e&&(e=1),n=rgbToHsl(t.r,t.g,t.b),i=rgbToHwb(t.r,t.g,t.b),s=rgbToCmyk(t.r,t.g,t.b),h=r||n.h,f=a||n.s,o=hueToNcol(h),roundDecimals({red:t.r,green:t.g,blue:t.b,hue:h,sat:f,lightness:n.l,whiteness:i.w,blackness:i.b,cyan:s.c,magenta:s.m,yellow:s.y,black:s.k,ncol:o,opacity:e,valid:!0})):emptyObject()}function emptyObject(){return{red:0,green:0,blue:0,hue:0,sat:0,lightness:0,whiteness:0,blackness:0,cyan:0,magenta:0,yellow:0,black:0,ncol:"R",opacity:1,valid:!1}}function getColorArr(t){return"names"==t?["AliceBlue","AntiqueWhite","Aqua","Aquamarine","Azure","Beige","Bisque","Black","BlanchedAlmond","Blue","BlueViolet","Brown","BurlyWood","CadetBlue","Chartreuse","Chocolate","Coral","CornflowerBlue","Cornsilk","Crimson","Cyan","DarkBlue","DarkCyan","DarkGoldenRod","DarkGray","DarkGrey","DarkGreen","DarkKhaki","DarkMagenta","DarkOliveGreen","DarkOrange","DarkOrchid","DarkRed","DarkSalmon","DarkSeaGreen","DarkSlateBlue","DarkSlateGray","DarkSlateGrey","DarkTurquoise","DarkViolet","DeepPink","DeepSkyBlue","DimGray","DimGrey","DodgerBlue","FireBrick","FloralWhite","ForestGreen","Fuchsia","Gainsboro","GhostWhite","Gold","Golden
|