-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolorCode.min.js
More file actions
1 lines (1 loc) · 5.19 KB
/
colorCode.min.js
File metadata and controls
1 lines (1 loc) · 5.19 KB
1
var colorCode=class{constructor(...args){return"string"==typeof arguments[0]?this.hex(...args):this.set(...args)}set(red,green,blue,alpha){return this.red=void 0!==red?red:0,this.green=void 0!==green?green:0,this.blue=void 0!==blue?blue:0,this.alpha=void 0!==alpha?alpha:void 0,this}hex(hex){return void 0!==hex?this.parseHex(hex):this.getHex()}rgb(rgb){return void 0!==rgb?this.parseRgb(rgb):this.getRgb()}hsl(){return getHsl()}relativeHslTo(object){var a=this.rgbToHsl(this.red,this.green,this.blue),b=object.rgbToHsl(object.red,object.green,object.blue),diff=[];return diff[0]=parseInt(b[0])-parseInt(a[0])+"deg",diff[1]=parseInt(b[1])-parseInt(a[1])+"%",diff[2]=parseInt(b[2])-parseInt(a[2])+"%",diff}scssTransformTo(variationColor,colorVariable){var baseColor=this.hex(),diff=this.relativeHslTo(variationColor);if(void 0!==colorVariable)var output=colorVariable;else var output="#"+baseColor.replace("#","");return 0!==parseInt(diff[0])&&(output="adjust-hue("+output+","+diff[0]+")"),parseInt(diff[1])>0?output="saturate("+output+","+diff[1]+")":parseInt(diff[1])<0&&(output="desaturate("+output+","+diff[1].replace(/-/g,"")+")"),parseInt(diff[2])>0?output="lighten("+output+","+diff[2]+")":parseInt(diff[2])<0&&(output="darken("+output+","+diff[2].replace(/-/g,"")+")"),output+";"}scssTransformBy(diff,colorVariable){var baseColor=this.hex();if(void 0!==colorVariable)var output=colorVariable;else var output="#"+baseColor.replace("#","");return 0!==parseInt(diff[0])&&(output="adjust-hue("+output+","+diff[0]+")"),parseInt(diff[1])>0?output="saturate("+output+","+diff[1]+")":parseInt(diff[1])<0&&(output="desaturate("+output+","+diff[1].replace(/-/g,"")+")"),parseInt(diff[2])>0?output="lighten("+output+","+diff[2]+")":parseInt(diff[2])<0&&(output="darken("+output+","+diff[2].replace(/-/g,"")+")"),output+";"}parseHex(hex){if(6==(hex=hex.replace("#","")).length||8==hex.length){var channels=this.splitString(hex,2);this.red=this.hexToNumber(channels[0]),this.green=this.hexToNumber(channels[1]),this.blue=this.hexToNumber(channels[2]),this.alpha=void 0!==channels[3]?this.hexToOpacity(channels[3]):void 0}else{if(3!=hex.length&&4!=hex.length)throw"Invalid HEX string provided to parseHex()! Provided: "+hex;var channels=this.splitString(hex,1);this.red=this.hexToNumber(channels[0]+channels[0]),this.green=this.hexToNumber(channels[1]+channels[1]),this.blue=this.hexToNumber(channels[2]+channels[2]),this.alpha=void 0!==channels[3]?this.hexToOpacity(channels[3]):void 0}return this}parseRgb(rgb){rgb.indexOf("(")>-1&&(rgb=rgb.substring(rgb.lastIndexOf("(")+1,rgb.lastIndexOf(")")));var channels=(rgb=rgb.replace(/\s+/g,"")).split(",");if(3!=channels.length&&4!=channels.length)throw"Invalid RGB string provided to parseRgb()";return this.red=parseInt(channels[0]),this.green=parseInt(channels[1]),this.blue=parseInt(channels[2]),channels[3]=parseFloat(channels[3]),isNaN(channels[3])?this.alpha=void 0:this.alpha=this.round(channels[3],2),this}getHex(){var output="",colorChannels=[this.red,this.green,this.blue],dedupeFlag=!0;return colorChannels.forEach(value=>{output+=this.numberToHex(value),this.isDoubleChar(this.numberToHex(value))||(dedupeFlag=!1)}),void 0!==this.alpha&&(dedupeFlag=!1,output+=this.decToHexAlpha(this.alpha)),dedupeFlag&&(output=this.dedupe(output)),output="#"+output}getRgb(){var output=void 0!==this.alpha?"rgba(":"rgb(";return output+=[this.red,this.green,this.blue].join(","),output+=void 0!==this.alpha?","+this.alpha+")":")"}getHsl(){var hsl=this.rgbToHsl(this.red,this.green,this.blue),output=void 0!==this.alpha?"hsla(":"hsl(";return output+=hsl.join(","),output+=void 0!==this.alpha?","+this.alpha+")":")"}getHslArray(format="string"){var hsl=this.rgbToHsl(this.red,this.green,this.blue);if("string"==format)var output=hsl;else if("integer"==format){var output=[];hsl.forEach(element=>{output.push(parseInt(element))})}else if("decimal"==format){var output;(output=[])[0]=parseInt(hsl[0])/360,output[1]=parseInt(hsl[1])/100,output[2]=parseInt(hsl[2])/100}return output}numberToHex(number){var hex=number.toString(16);return 1==hex.length?"0"+hex:hex}decToHexAlpha(decimal){return this.numberToHex(this.round(100*decimal*2.55,0))}testHexAlpha(){for(var i=100;i>0;i-=1)console.log(i+"% - "+this.decToHexAlpha(i/100))}hexToOpacity(hex){var scale=Math.pow(16,hex.length)-1;return this.round(this.hexToNumber(hex)/scale,2)}hexToNumber(hex){return parseInt(hex,16)}round(value,precision){return Number(Math.round(value+"e"+precision)+"e-"+precision)}splitString(string,size){return string.match(new RegExp(".{1,"+size+"}","g"))}isDoubleChar(string){return string[0]==string[1]}dedupe(string){for(var chars=string.split(""),i=1;i<chars.length;i+=2)chars[i]="";return chars.join("")}rgbToHsl(r,g,b){r/=255,g/=255,b/=255;var max=Math.max(r,g,b),min=Math.min(r,g,b),h,s,l=(max+min)/2;if(max==min)h=s=0;else{var d=max-min;switch(s=l>.5?d/(2-max-min):d/(max+min),max){case r:h=(g-b)/d+(g<b?6:0);break;case g:h=(b-r)/d+2;break;case b:h=(r-g)/d+4}h/=6}return[100*h/100*360+.5|0,(100*s+.5|0)+"%",(100*l+.5|0)+"%"]}hslToRgb(h,s,l){var r,g,b;if(0==s)r=g=b=l;else{function hue2rgb(p,q,t){return t<0&&(t+=1),t>1&&(t-=1),t<1/6?p+6*(q-p)*t:t<.5?q:t<2/3?p+(q-p)*(2/3-t)*6:p}var q=l<.5?l*(1+s):l+s-l*s,p=2*l-q;r=hue2rgb(p,q,h+1/3),g=hue2rgb(p,q,h),b=hue2rgb(p,q,h-1/3)}return[255*r,255*g,255*b]}};