-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorFormat.min.js
1 lines (1 loc) · 4.06 KB
/
colorFormat.min.js
1
!function(){"use strict";function e(e){return new r(e)}function r(e){var r,t=e&&e.color&&e.color.replace(/\s/g,"").toLowerCase()||"#fff",n=-1==(e=e&&e.format&&e.format.replace(/\s/g,"").toLowerCase()||"rgb").indexOf("rgba")?0:1,o=-1==e.indexOf("hsla")?0:1;if(-1<t.indexOf("#"))"hex"==e?(r=this.hexToRgb(t),r=this.rgbToHex(r)):-1<e.indexOf("rgb")?r=this.hexToRgb(t,n):-1<e.indexOf("hsl")&&(r=this.hexToRgb(t),r=this.rgbToHsl(r,o));else if(-1<t.indexOf("rgb"))r=this.getRgb(t,n),"hex"==e?r=this.rgbToHex(r):-1<e.indexOf("hsl")&&(r=this.rgbToHsl(r,o));else if(-1<t.indexOf("hsl"))r=this.getHsl(t,o),r=this.hslToRgb(r,n),"hex"==e?r=this.rgbToHex(r):-1<e.indexOf("hsl")&&(r=this.rgbToHsl(r,o));else{for(var s,a=this.defineColor,i=0,h=a.length;i<h;i++)if(t==a[i].name){s=a[i].hex;break}s&&0<s.length?"hex"==e?(r=this.hexToRgb(s),r=this.rgbToHex(r)):-1<e.indexOf("rgb")?r=this.hexToRgb(s,n):-1<e.indexOf("hsl")&&(r=this.hexToRgb(s),r=this.rgbToHsl(r,o)):alert("参数color暂未定义")}return r}var t;r.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(e,r){var t=-1==(e=e.toLowerCase()).indexOf("rgba")?0:1,n=(e=(e=t?e.replace("rgba",""):e.replace("rgb","")).replace(/\s/g,"").split(","),Number(e[0].slice(1))),o=Number(e[1]),s=t?Number(e[2]):Number(e[2].slice(0,-1)),t=!t||1<Number(e[3].slice(0,-1))?1:Number(e[3].slice(0,-1));return{r:n,g:o,b:s,o:t,complete:r?"rgba("+[n,o,s,t].join(",")+")":"rgb("+[n,o,s].join(",")+")"}},getHsl:function(e,r){var t=-1==(e=e.toLowerCase()).indexOf("hsla")?0:1,n=(e=(e=t?e.replace("hsla",""):e.replace("hsl","")).replace(/\s/g,"").split(","),Number(e[0].slice(1))),o=parseInt(e[1]),s=t?parseInt(e[2]):parseInt(e[2].slice(0,-1)),t=!t||1<Number(e[3].slice(0,-1))?1:Number(e[3].slice(0,-1));return{h:n,s:o/100,l:s/100,o:t,complete:r?"hsla("+[n,o,s,t].join(",")+")":"hsl("+[n,o,s].join(",")+")"}},rgbToHex:function(e){var r=Number(e.r).toString(16),t=Number(e.g).toString(16),n=Number(e.b).toString(16),o=Math.round(255*e.o).toString(16),s=0;return{r:r=r.length<2?0+r:r,g:t=t.length<2?0+t:t,b:n=n.length<2?0+n:n,o:o=o.length<2?0+o:o,complete:(s=r[0]==r[1]&&t[0]==t[1]&&n[0]==n[1]&&o[0]==o[1]?1:s)?"#"+r[0]+t[0]+n[0]+(1==e.o?"":o):"#"+r+t+n+(1==e.o?"":o)}},rgbToHsl:function(e,r){var t,n=Number(e.r)/255,o=Number(e.g)/255,s=Number(e.b)/255,e=Number(e.o),a=Math.max(n,o,s),i=Math.min(n,o,s),h=(a+i)/2;if(a==i)t=b=0;else{var l=a-i,b=h<.5?l/(a+i):l/(2-a-i);switch(a){case n:t=(o-s)/l;break;case o:t=(s-n)/l+2;break;case s:t=(n-o)/l+4}t=(t*=60)<0?t+360:t}return{h:t=Math.round(t),s:b=Math.round(100*b)+"%",l:h=Math.round(100*h)+"%",o:e,complete:r?"hsla("+[t,b,h,e].join(",")+")":"hsl("+[t,b,h].join(",")+")"}},hexToRgb:function(e,r){var t,n,o,s,a=(e=e.replace("#","")).split("");return 3==e.length?(t=parseInt(a[0]+a[0],16),n=parseInt(a[1]+a[1],16),o=parseInt(a[2]+a[2],16),s=1):4==e.length?(t=parseInt(a[0]+a[0],16),n=parseInt(a[1]+a[1],16),o=parseInt(a[2]+a[2],16),s=Math.round(parseInt(a[3]+a[3],16)/255*100)/100):6==e.length?(t=parseInt(a[0]+a[1],16),n=parseInt(a[2]+a[3],16),o=parseInt(a[4]+a[5],16),s=1):8==e.length&&(t=parseInt(a[0]+a[1],16),n=parseInt(a[2]+a[3],16),o=parseInt(a[4]+a[5],16),s=Math.round(parseInt(a[6]+a[7],16)/255*100)/100),{r:t,g:n,b:o,o:s,complete:r?"rgba("+[t,n,o,s].join(",")+")":"rgb("+[t,n,o].join(",")+")"}},hslToRgb:function(e,r){var t,n,o,s,a=Number(e.h),i=Number(e.s),h=Number(e.l),e=Number(e.o);return 0==i?n=o=s=h:(n=(t=function(e,r,t){return t<0&&(t+=1),1<t&&--t,t<1/6?e+6*(r-e)*t:t<.5?r:t<2/3?e+(r-e)*(2/3-t)*6:e})(i=2*h-(h=h<.5?h*(1+i):h+i-h*i),h,(a/=360)+1/3),o=t(i,h,a),s=t(i,h,a-1/3)),{r:n=Math.round(255*n),g:o=Math.round(255*o),b:s=Math.round(255*s),o:e,complete:r?"rgba("+[n,o,s,e].join(",")+")":"rgb("+[n,o,s].join(",")+")"}}},t=function(){return this||(0,eval)("this")}(),"undefined"!=typeof module&&module.exports?module.exports=e:"function"==typeof define&&define.amd?define(function(){return e}):"colorFormat"in t||(t.colorFormat=e)}();