-
Notifications
You must be signed in to change notification settings - Fork 40
/
one-color.js
2 lines (2 loc) · 5.39 KB
/
one-color.js
1
2
!function(t,r){"object"==typeof exports&&"undefined"!=typeof module?module.exports=r():"function"==typeof define&&define.amd?define(r):((t=t||self).one=t.one||{},t.one.color=r())}(this,(function(){"use strict";var t=[],r=function(t){return void 0===t},e=/\s*(\.\d+|\d+(?:\.\d+)?)(%|deg)?\s*/,n=/\s*(\.\d+|100|\d?\d(?:\.\d+)?)%\s*/,a=new RegExp("^(rgb|hsl|hsv)a?\\("+e.source+"[, ]"+e.source+"[, ]"+e.source+"(?:[,/]"+e.source+")?\\)$","i");function o(t,r,e){return"%"===t?100:"deg"===t||e&&0===r?360:t?void 0:255}function s(t){if(Array.isArray(t)){if("string"==typeof t[0]&&"function"==typeof s[t[0]])return new s[t[0]](t.slice(1,t.length));if(4===t.length)return new s.RGB(t[0]/255,t[1]/255,t[2]/255,t[3]/255)}else if("string"==typeof t){var e=t.toLowerCase();s.namedColors[e]&&(t="#"+s.namedColors[e]),"transparent"===e&&(t="rgba(0,0,0,0)");var i=t.match(a);if(i){var u=i[1].toUpperCase(),h="H"===u[0];if(r(s[u]))throw new Error("color."+u+" is not installed.");let t=r(i[8])?1:void 0;return void 0===t&&(t="%"===i[9]?parseFloat(i[8])/100:parseFloat(i[8])),new s[u](parseFloat(i[2])/o(i[3],0,h),parseFloat(i[4])/o(i[5],1,h),parseFloat(i[6])/o(i[7],2,h),t)}t.length<6&&(t=t.replace(/^#?([0-9a-f])([0-9a-f])([0-9a-f])([0-9a-f])?$/i,"$1$1$2$2$3$3$4$4"));var c=t.match(/^#?([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])([0-9a-f][0-9a-f])?$/i);if(c)return new s.RGB(parseInt(c[1],16)/255,parseInt(c[2],16)/255,parseInt(c[3],16)/255,c[4]?parseInt(c[4],16)/255:1);if(s.CMYK){var f=t.match(new RegExp("^cmyk\\("+n.source+","+n.source+","+n.source+","+n.source+"\\)$","i"));if(f)return new s.CMYK(parseFloat(f[1])/100,parseFloat(f[2])/100,parseFloat(f[3])/100,parseFloat(f[4])/100)}}else if("object"==typeof t&&t.isColor)return t;return!1}s.namedColors={},s.installColorSpace=function(e,n,a){s[e]=function(t){var r=Array.isArray(t)?t:arguments;n.forEach((function(t,a){var o=r[a];if("alpha"===t)this._alpha=isNaN(o)||o>1?1:o<0?0:o;else{if(isNaN(o))throw new Error("["+e+"]: Invalid color: ("+n.join(",")+")");"hue"===t?this._hue=o<0?o-Math.floor(o):o%1:this["_"+t]=o<0?0:o>1?1:o}}),this)},s[e].propertyNames=n;var o=s[e].prototype;for(var i in["valueOf","hex","hexa","css","cssa"].forEach((function(t){o[t]=o[t]||("RGB"===e?o.hex:function(){return this.rgb()[t]()})})),o.isColor=!0,o.equals=function(t,a){r(a)&&(a=1e-10),t=t[e.toLowerCase()]();for(var o=0;o<n.length;o+=1)if(Math.abs(this["_"+n[o]]-t["_"+n[o]])>a)return!1;return!0},o.toJSON=function(){return[e].concat(n.map((function(t){return this["_"+t]}),this))},a)if(Object.prototype.hasOwnProperty.call(a,i)){var u=i.match(/^from(.*)$/);u?s[u[1].toUpperCase()].prototype[e.toLowerCase()]=a[i]:o[i]=a[i]}function h(t,r){var e={};for(var n in e[r.toLowerCase()]=function(){return this.rgb()[r.toLowerCase()]()},s[r].propertyNames.forEach((function(t){var n="black"===t?"k":t.charAt(0);e[t]=e[n]=function(e,n){return this[r.toLowerCase()]()[t](e,n)}})),e)Object.prototype.hasOwnProperty.call(e,n)&&void 0===s[t].prototype[n]&&(s[t].prototype[n]=e[n])}return o[e.toLowerCase()]=function(){return this},o.toString=function(){return"["+e+" "+n.map((function(t){return this["_"+t]}),this).join(", ")+"]"},n.forEach((function(t){var r="black"===t?"k":t.charAt(0);o[t]=o[r]=function(r,e){return void 0===r?this["_"+t]:e?new this.constructor(n.map((function(e){return this["_"+e]+(t===e?r:0)}),this)):new this.constructor(n.map((function(e){return t===e?r:this["_"+e]}),this))}})),t.forEach((function(t){h(e,t),h(t,e)})),t.push(e),s},s.pluginList=[],s.use=function(t){return-1===s.pluginList.indexOf(t)&&(this.pluginList.push(t),t(s)),s},s.installMethod=function(r,e){return t.forEach((function(t){s[t].prototype[r]=e})),this},s.installColorSpace("RGB",["red","green","blue","alpha"],{hex:function(){var t=(65536*Math.round(255*this._red)+256*Math.round(255*this._green)+Math.round(255*this._blue)).toString(16);return"#"+"00000".substr(0,6-t.length)+t},hexa:function(){var t=Math.round(255*this._alpha).toString(16);return this.hex()+"00".substr(0,2-t.length)+t},css:function(){return"rgb("+Math.round(255*this._red)+","+Math.round(255*this._green)+","+Math.round(255*this._blue)+")"},cssa:function(){return"rgba("+Math.round(255*this._red)+","+Math.round(255*this._green)+","+Math.round(255*this._blue)+","+this._alpha+")"}});var i=function(t){t.installColorSpace("HSV",["hue","saturation","value","alpha"],{rgb:function(){var r,e,n,a=this._hue,o=this._saturation,s=this._value,i=Math.min(5,Math.floor(6*a)),u=6*a-i,h=s*(1-o),c=s*(1-u*o),f=s*(1-(1-u)*o);switch(i){case 0:r=s,e=f,n=h;break;case 1:r=c,e=s,n=h;break;case 2:r=h,e=s,n=f;break;case 3:r=h,e=c,n=s;break;case 4:r=f,e=h,n=s;break;case 5:r=s,e=h,n=c}return new t.RGB(r,e,n,this._alpha)},hsl:function(){var r,e=(2-this._saturation)*this._value,n=this._saturation*this._value,a=e<=1?e:2-e;return r=a<1e-9?0:n/a,new t.HSL(this._hue,r,e/2,this._alpha)},fromRgb:function(){var r,e=this._red,n=this._green,a=this._blue,o=Math.max(e,n,a),s=o-Math.min(e,n,a),i=0===o?0:s/o,u=o;if(0===s)r=0;else switch(o){case e:r=(n-a)/s/6+(n<a?1:0);break;case n:r=(a-e)/s/6+1/3;break;case a:r=(e-n)/s/6+2/3}return new t.HSV(r,i,u,this._alpha)}})};return s.use(i).use((function(t){t.use(i),t.installColorSpace("HSL",["hue","saturation","lightness","alpha"],{hsv:function(){var r,e=2*this._lightness,n=this._saturation*(e<=1?e:2-e);return r=e+n<1e-9?0:2*n/(e+n),new t.HSV(this._hue,r,(e+n)/2,this._alpha)},rgb:function(){return this.hsv().rgb()},fromRgb:function(){return this.hsv().hsl()}})}))}));
//# sourceMappingURL=one-color.js.map