@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 3.16 kB
JavaScript
import{hsla2rgba as t,isNamedColor as r,getNamedColor as s}from"./colorUtils.js";import{clamp as i}from"./core/mathUtils.js";import{isSome as e}from"./core/maybe.js";import{ensureInteger as a}from"./core/accessorSupport/ensureType.js";function n(t){return i(a(t),0,255)}function o(t,r,s){return t=Number(t),isNaN(t)?s:t<r?r:t>s?s:t}class h{static blendColors(t,r,s,i=new h){return i.r=Math.round(t.r+(r.r-t.r)*s),i.g=Math.round(t.g+(r.g-t.g)*s),i.b=Math.round(t.b+(r.b-t.b)*s),i.a=t.a+(r.a-t.a)*s,i._sanitize()}static fromRgb(r,s){const i=r.toLowerCase().match(/^(rgba?|hsla?)\(([\s\.\-,%0-9]+)\)/);if(i){const r=i[2].split(/\s*,\s*/),e=i[1];if("rgb"===e&&3===r.length||"rgba"===e&&4===r.length){const t=r[0];if("%"===t.charAt(t.length-1)){const t=r.map((t=>2.56*parseFloat(t)));return 4===r.length&&(t[3]=parseFloat(r[3])),h.fromArray(t,s)}return h.fromArray(r.map((t=>parseFloat(t))),s)}if("hsl"===e&&3===r.length||"hsla"===e&&4===r.length)return h.fromArray(t(parseFloat(r[0]),parseFloat(r[1])/100,parseFloat(r[2])/100,parseFloat(r[3])),s)}return null}static fromHex(t,r=new h){if(4!==t.length&&7!==t.length||"#"!==t[0])return null;const s=4===t.length?4:8,i=(1<<s)-1;let e=Number("0x"+t.substr(1));return isNaN(e)?null:(["b","g","r"].forEach((t=>{const a=e&i;e>>=s,r[t]=4===s?17*a:a})),r.a=1,r)}static fromArray(t,r=new h){return r._set(Number(t[0]),Number(t[1]),Number(t[2]),Number(t[3])),isNaN(r.a)&&(r.a=1),r._sanitize()}static fromString(t,i){const e=r(t)?s(t):null;return e&&h.fromArray(e,i)||h.fromRgb(t,i)||h.fromHex(t,i)}static fromJSON(t){return t&&new h([t[0],t[1],t[2],t[3]/255])}static toUnitRGB(t){return e(t)?[t.r/255,t.g/255,t.b/255]:null}static toUnitRGBA(t){return e(t)?[t.r/255,t.g/255,t.b/255,null!=t.a?t.a:1]:null}constructor(t){this.r=255,this.g=255,this.b=255,this.a=1,t&&this.setColor(t)}get isBright(){return.299*this.r+.587*this.g+.114*this.b>=127}setColor(t){return"string"==typeof t?h.fromString(t,this):Array.isArray(t)?h.fromArray(t,this):(this._set(t.r??0,t.g??0,t.b??0,t.a??1),t instanceof h||this._sanitize()),this}toRgb(){return[this.r,this.g,this.b]}toRgba(){return[this.r,this.g,this.b,this.a]}toHex(){const t=this.r.toString(16),r=this.g.toString(16),s=this.b.toString(16);return`#${t.length<2?"0"+t:t}${r.length<2?"0"+r:r}${s.length<2?"0"+s:s}`}toCss(t=!1){const r=this.r+", "+this.g+", "+this.b;return t?`rgba(${r}, ${this.a})`:`rgb(${r})`}toString(){return this.toCss(!0)}toJSON(){return this.toArray()}toArray(t=h.AlphaMode.ALWAYS){const r=n(this.r),s=n(this.g),i=n(this.b);return t===h.AlphaMode.ALWAYS||1!==this.a?[r,s,i,n(255*this.a)]:[r,s,i]}clone(){return new h(this.toRgba())}hash(){return this.r<<24|this.g<<16|this.b<<8|255*this.a}equals(t){return e(t)&&t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}_sanitize(){return this.r=Math.round(o(this.r,0,255)),this.g=Math.round(o(this.g,0,255)),this.b=Math.round(o(this.b,0,255)),this.a=o(this.a,0,1),this}_set(t,r,s,i){this.r=t,this.g=r,this.b=s,this.a=i}}h.prototype.declaredClass="esri.Color",function(t){var r;(r=t.AlphaMode||(t.AlphaMode={}))[r.ALWAYS=0]="ALWAYS",r[r.UNLESS_OPAQUE=1]="UNLESS_OPAQUE"}(h||(h={}));const l=h;export{l as default};