-
Notifications
You must be signed in to change notification settings - Fork 3
/
colorFormat.min.js
1 lines (1 loc) · 5.42 KB
/
colorFormat.min.js
1
!function(undefined){"use strict";var _global,colorFormat=function(options){return new _colorFormat(options)},_colorFormat=function(options){var result,color=options&&options.color&&options.color.replace(/\s/g,"").toLowerCase()||"#f00",format=options&&options.format&&options.format.replace(/\s/g,"").toLowerCase()||"rgb",rgbType=-1==format.indexOf("rgba")?0:1,hslType=-1==format.indexOf("hsla")?0:1;if(color.indexOf("#")>-1)"hex"==format?(result=this.hexToRgb(color),result=this.rgbToHex(result)):format.indexOf("rgb")>-1?result=this.hexToRgb(color,rgbType):format.indexOf("hsl")>-1&&(result=this.hexToRgb(color),result=this.rgbToHsl(result,hslType));else if(color.indexOf("rgb")>-1)result=this.getRgb(color,rgbType),"hex"==format?result=this.rgbToHex(result):format.indexOf("hsl")>-1&&(result=this.rgbToHsl(result,hslType));else if(color.indexOf("hsl")>-1)result=this.getHsl(color,hslType),result=this.hslToRgb(result,rgbType),"hex"==format?result=this.rgbToHex(result):format.indexOf("hsl")>-1&&(result=this.rgbToHsl(result,hslType));else{for(var defineColor=this.defineColor,resultRgb,i=0,len=defineColor.length;i<len;i++)if(color==defineColor[i].name){resultRgb=defineColor[i].hex;break}resultRgb&&resultRgb.length>0?"hex"==format?(result=this.hexToRgb(resultRgb),result=this.rgbToHex(result)):format.indexOf("rgb")>-1?result=this.hexToRgb(resultRgb,rgbType):format.indexOf("hsl")>-1&&(result=this.hexToRgb(resultRgb),result=this.rgbToHsl(result,hslType)):alert("参数color暂未定义")}return result};_colorFormat.prototype={constructor:this,defineColor:[{name:"red",hex:"#f00"},{name:"orange",hex:"#ffa500"},{name:"yellow",hex:"#ff0"},{name:"green",hex:"#0f0"},{name:"cyan",hex:"#0ff"},{name:"blue",hex:"#00f"},{name:"violet",hex:"#ee82ee"},{name:"black",hex:"#000"},{name:"white",hex:"#fff"}],getRgb:function(rgb,type){var flag=-1==(rgb=rgb.toLowerCase()).indexOf("rgba")?0:1;rgb=(rgb=flag?rgb.replace("rgba",""):rgb.replace("rgb","")).replace(/\s/g,"").split(",");var red=Number(rgb[0].slice(1)),green=Number(rgb[1]),blue=flag?Number(rgb[2]):Number(rgb[2].slice(0,-1)),opacity=flag?Number(rgb[3].slice(0,-1))>1?1:Number(rgb[3].slice(0,-1)):1;return{r:red,g:green,b:blue,o:opacity,complete:type?"rgba("+[red,green,blue,opacity].join(",")+")":"rgb("+[red,green,blue].join(",")+")"}},getHsl:function(hsl,type){var flag=-1==(hsl=hsl.toLowerCase()).indexOf("hsla")?0:1;hsl=(hsl=flag?hsl.replace("hsla",""):hsl.replace("hsl","")).replace(/\s/g,"").split(",");var h=Number(hsl[0].slice(1)),s=parseInt(hsl[1]),l=flag?parseInt(hsl[2]):parseInt(hsl[2].slice(0,-1)),opacity=flag?Number(hsl[3].slice(0,-1))>1?1:Number(hsl[3].slice(0,-1)):1;return{h:h,s:s/100,l:l/100,o:opacity,complete:type?"hsla("+[h,s,l,opacity].join(",")+")":"hsl("+[h,s,l].join(",")+")"}},rgbToHex:function(rgb){var red=Number(rgb.r).toString(16),green=Number(rgb.g).toString(16),blue=Number(rgb.b).toString(16),opacity=Math.round(255*rgb.o).toString(16),simpleType=0;return red.length<2&&(red=0+red),green.length<2&&(green=0+green),blue.length<2&&(blue=0+blue),opacity.length<2&&(opacity=0+opacity),red[0]==red[1]&&green[0]==green[1]&&blue[0]==blue[1]&&opacity[0]==opacity[1]&&(simpleType=1),{r:red,g:green,b:blue,o:opacity,complete:simpleType?"#"+red[0]+green[0]+blue[0]+(1==rgb.o?"":opacity):"#"+red+green+blue+(1==rgb.o?"":opacity)}},rgbToHsl:function(rgb,type){var r=Number(rgb.r)/255,g=Number(rgb.g)/255,b=Number(rgb.b)/255,o=Number(rgb.o),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/(max+min):d/(2-max-min),max){case r:h=(g-b)/d;break;case g:h=(b-r)/d+2;break;case b:h=(r-g)/d+4}h=(h*=60)<0?h+360:h}return{h:h=Math.round(h),s:s=Math.round(100*s)+"%",l:l=Math.round(100*l)+"%",o:o,complete:type?"hsla("+[h,s,l,o].join(",")+")":"hsl("+[h,s,l].join(",")+")"}},hexToRgb:function(hex,type){var red,green,blue,opacity,hexsplit=(hex=hex.replace("#","")).split("");return 3==hex.length?(red=parseInt(hexsplit[0]+hexsplit[0],16),green=parseInt(hexsplit[1]+hexsplit[1],16),blue=parseInt(hexsplit[2]+hexsplit[2],16),opacity=1):4==hex.length?(red=parseInt(hexsplit[0]+hexsplit[0],16),green=parseInt(hexsplit[1]+hexsplit[1],16),blue=parseInt(hexsplit[2]+hexsplit[2],16),opacity=Math.round(parseInt(hexsplit[3]+hexsplit[3],16)/255*100)/100):6==hex.length?(red=parseInt(hexsplit[0]+hexsplit[1],16),green=parseInt(hexsplit[2]+hexsplit[3],16),blue=parseInt(hexsplit[4]+hexsplit[5],16),opacity=1):8==hex.length&&(red=parseInt(hexsplit[0]+hexsplit[1],16),green=parseInt(hexsplit[2]+hexsplit[3],16),blue=parseInt(hexsplit[4]+hexsplit[5],16),opacity=Math.round(parseInt(hexsplit[6]+hexsplit[7],16)/255*100)/100),{r:red,g:green,b:blue,o:opacity,complete:type?"rgba("+[red,green,blue,opacity].join(",")+")":"rgb("+[red,green,blue].join(",")+")"}},hslToRgb:function(hsl,type){var h=Number(hsl.h),s=Number(hsl.s),l=Number(hsl.l),o=Number(hsl.o),r,g,b;if(0==s)r=g=b=l;else{var temp2=l<.5?l*(1+s):l+s-l*s,temp1=2*l-temp2,hue2rgb=function(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};r=hue2rgb(temp1,temp2,(h/=360)+1/3),g=hue2rgb(temp1,temp2,h),b=hue2rgb(temp1,temp2,h-1/3)}return{r:r=Math.round(255*r),g:g=Math.round(255*g),b:b=Math.round(255*b),o:o,complete:type?"rgba("+[r,g,b,o].join(",")+")":"rgb("+[r,g,b].join(",")+")"}}},_global=function(){return this||(0,eval)("this")}(),"undefined"!=typeof module&&module.exports?module.exports=colorFormat:"function"==typeof define&&define.amd?define(function(){return colorFormat}):!("colorFormat"in _global)&&(_global.colorFormat=colorFormat)}();