@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 2.63 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.33/esri/copyright.txt for details.
*/
import{cssToRgba as t,cssHexToRgba as r,parseRgba as s,sanitizeRgbComponent as i,sanitizeAlpha as n}from"./colorUtils.js";import{clamp as o}from"./core/mathUtils.js";import{ensureInteger as a}from"./core/accessorSupport/ensureType.js";function e(t){return o(a(t),0,255)}function h(t,r){const s=t.toString(16).padStart(2,"0");return r?s.slice(0,1):s}class u{static blendColors(t,r,s,i=new u){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=t(r);return i?u.fromArray(i,s):null}static fromHex(t,s=new u){const i=r(t);return i?u.fromArray(i,s):null}static fromArray(t,r=new u){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,r){const i=s(t);return i?u.fromArray(i,r):null}static fromJSON(t){return null!=t?new u([t[0],t[1],t[2],(t[3]??255)/255]):void 0}static toUnitRGB(t){return null!=t?[t.r/255,t.g/255,t.b/255]:null}static toUnitRGBA(t){return null!=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?u.fromString(t,this):Array.isArray(t)?u.fromArray(t,this):(this._set(t.r??0,t.g??0,t.b??0,t.a??1),t instanceof u||this._sanitize()),this}toRgb(){return[this.r,this.g,this.b]}toRgba(){return[this.r,this.g,this.b,this.a]}toHex(t){const r=t?.capitalize??!1,s=t?.digits??6,i=3===s||4===s,n=4===s||8===s,o=`#${h(this.r,i)}${h(this.g,i)}${h(this.b,i)}${n?h(Math.round(255*this.a),i):""}`;return r?o.toUpperCase():o}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=u.AlphaMode.ALWAYS){const r=e(this.r),s=e(this.g),i=e(this.b);return t===u.AlphaMode.ALWAYS||1!==this.a?[r,s,i,e(255*this.a)]:[r,s,i]}clone(){return new u(this.toRgba())}hash(){return this.r<<24|this.g<<16|this.b<<8|255*this.a}equals(t){return null!=t&&t.r===this.r&&t.g===this.g&&t.b===this.b&&t.a===this.a}_sanitize(){return this.r=i(this.r),this.g=i(this.g),this.b=i(this.b),this.a=n(this.a),this}_set(t,r,s,i){this.r=t,this.g=r,this.b=s,this.a=i}}u.prototype.declaredClass="esri.Color",function(t){var r;(r=t.AlphaMode||(t.AlphaMode={}))[r.ALWAYS=0]="ALWAYS",r[r.UNLESS_OPAQUE=1]="UNLESS_OPAQUE"}(u||(u={}));const l=u;export{l as default};