fabric
Version:
Object model for HTML5 canvas, and SVG-to-canvas parser. Backed by jsdom and node-canvas.
3 lines (2 loc) • 2.99 kB
JavaScript
import{defineProperty as t}from"../../_virtual/_rollupPluginBabelHelpers.min.mjs";import{radiansToDegrees as r}from"../util/misc/radiansDegreesConversion.min.mjs";import{ColorNameMap as e}from"./color_map.min.mjs";import{reRGBa as o,reHSLa as s,reHex as c}from"./constants.min.mjs";import{rgb2Hsl as n,hexify as a,greyAverage as i,fromAlphaToFloat as u,hue2rgb as l}from"./util.min.mjs";class m{constructor(r){if(t(this,"isUnrecognised",!1),r)if(r instanceof m)this.setSource([...r._source]);else if(Array.isArray(r)){const[t,e,o,s=1]=r;this.setSource([t,e,o,s])}else this.setSource(this._tryParsingColor(r));else this.setSource([0,0,0,1])}_tryParsingColor(t){return(t=t.toLowerCase())in e&&(t=e[t]),"transparent"===t?[255,255,255,0]:m.sourceFromHex(t)||m.sourceFromRgb(t)||m.sourceFromHsl(t)||(this.isUnrecognised=!0)&&[0,0,0,1]}getSource(){return this._source}setSource(t){this._source=t}toRgb(){const[t,r,e]=this.getSource();return"rgb(".concat(t,",").concat(r,",").concat(e,")")}toRgba(){return"rgba(".concat(this.getSource().join(","),")")}toHsl(){const[t,r,e]=n(...this.getSource());return"hsl(".concat(t,",").concat(r,"%,").concat(e,"%)")}toHsla(){const[t,r,e,o]=n(...this.getSource());return"hsla(".concat(t,",").concat(r,"%,").concat(e,"%,").concat(o,")")}toHex(){return this.toHexa().slice(0,6)}toHexa(){const[t,r,e,o]=this.getSource();return"".concat(a(t)).concat(a(r)).concat(a(e)).concat(a(Math.round(255*o)))}getAlpha(){return this.getSource()[3]}setAlpha(t){return this._source[3]=t,this}toGrayscale(){return this.setSource(i(this.getSource())),this}toBlackWhite(t){const[r,,,e]=i(this.getSource()),o=r<(t||127)?0:255;return this.setSource([o,o,o,e]),this}overlayWith(t){t instanceof m||(t=new m(t));const r=this.getSource(),e=t.getSource(),[o,s,c]=r.map(((t,r)=>Math.round(.5*t+.5*e[r])));return this.setSource([o,s,c,r[3]]),this}static fromRgb(t){return m.fromRgba(t)}static fromRgba(t){return new m(m.sourceFromRgb(t))}static sourceFromRgb(t){const r=t.match(o());if(r){const[t,e,o]=r.slice(1,4).map((t=>{const r=parseFloat(t);return t.endsWith("%")?Math.round(2.55*r):r}));return[t,e,o,u(r[4])]}}static fromHsl(t){return m.fromHsla(t)}static fromHsla(t){return new m(m.sourceFromHsl(t))}static sourceFromHsl(t){const r=t.match(s());if(!r)return;const e=(m.parseAngletoDegrees(r[1])%360+360)%360/360,o=parseFloat(r[2])/100,c=parseFloat(r[3])/100;let n,a,i;if(0===o)n=a=i=c;else{const t=c<=.5?c*(o+1):c+o-c*o,r=2*c-t;n=l(r,t,e+1/3),a=l(r,t,e),i=l(r,t,e-1/3)}return[Math.round(255*n),Math.round(255*a),Math.round(255*i),u(r[4])]}static fromHex(t){return new m(m.sourceFromHex(t))}static sourceFromHex(t){if(t.match(c())){const r=t.slice(t.indexOf("#")+1);let e;e=r.length<=4?r.split("").map((t=>t+t)):r.match(/.{2}/g);const[o,s,c,n=255]=e.map((t=>parseInt(t,16)));return[o,s,c,n/255]}}static parseAngletoDegrees(t){const e=t.toLowerCase(),o=parseFloat(e);return e.includes("rad")?r(o):e.includes("turn")?360*o:o}}export{m as Color};
//# sourceMappingURL=Color.min.mjs.map