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