@thednp/color-picker 0.0.2-alpha4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,2 +1,2 @@
1
- // ColorPicker v0.0.2alpha4 | thednp © 2022 | MIT-License
2
- const t={};function e(e){const o=this,{type:r}=e;(t[r]?[...t[r]]:[]).forEach(t=>{const[s,a]=t;[...a].forEach(t=>{if(s===o){const[o,a]=t;o.apply(s,[e]),a&&a.once&&n(s,r,o,a)}})})}const o=(o,n,r,s)=>{t[n]||(t[n]=new Map);const a=t[n];a.has(o)||a.set(o,new Map);const i=a.get(o),{size:l}=i;i&&i.set(r,s),l||o.addEventListener(n,e,s)},n=(o,n,r,s)=>{const a=t[n],i=a&&a.get(o),l=i&&i.get(r),{options:c}=void 0!==l?l:{options:s};i&&i.has(r)&&i.delete(r),!a||i&&i.size||a.delete(o),a&&a.size||delete t[n],i&&i.size||o.removeEventListener(n,e,c)},r="ArrowDown",s="ArrowUp",a="ArrowLeft",i="ArrowRight",l="Enter",c="Space";function u(t){return t instanceof HTMLElement?t.ownerDocument:t instanceof Window?t.document:window.document}function h(t){return u(t).documentElement}function g(t,e){const o=getComputedStyle(t);return e in o?o[e]:""}let p=0,d=0;const b=new Map;function f(t,e){const{width:o,height:n,top:r,right:s,bottom:a,left:i}=t.getBoundingClientRect();let l=1,c=1;if(e&&t instanceof HTMLElement){const{offsetWidth:e,offsetHeight:r}=t;l=e>0&&Math.round(o)/e||1,c=r>0&&Math.round(n)/r||1}return{width:o/l,height:n/c,top:r/c,right:s/l,bottom:a/c,left:i/l,x:i/l,y:r/c}}const m=[Document,Element,HTMLElement],v=[Element,HTMLElement];function w(t,e){const o=m.some(t=>e instanceof t)?e:u();return v.some(e=>t instanceof e)?t:o.querySelector(t)}function x(t,e){return(e&&m.some(t=>e instanceof t)?e:u()).getElementsByClassName(t)}const $=(t,e)=>Object.assign(t,e),y=new Map,k={set:(t,e,o)=>{const n=w(t);if(!n)return;y.has(e)||y.set(e,new Map);y.get(e).set(n,o)},getAllFor:t=>y.get(t)||null,get:(t,e)=>{const o=w(t),n=k.getAllFor(e);return o&&n&&n.get(o)||null},remove:(t,e)=>{const o=w(t),n=y.get(e);n&&o&&(n.delete(o),0===n.size&&y.delete(e))}},S=(t,e)=>{$(t.style,e)},H=(t,e)=>t.getAttribute(e);function A(t){return"true"===t||"false"!==t&&(Number.isNaN(+t)?""===t||"null"===t?null:t:+t)}const L=t=>Object.keys(t),E=t=>t.toLowerCase();const N=t=>t.focus();function P(t,e){return t.classList.contains(e)}function C(t,e){t.classList.add(e)}function M(t,e){t.classList.remove(e)}const T=(t,e,o)=>t.setAttribute(e,o),R=(t,e)=>t.removeAttribute(e),{head:F}=document,D=["transparent","currentColor","inherit","revert","initial"];function K(t){const e=Math.floor(t);return t-e<.5?e:Math.round(t)}const O=["rgb","hex","hsl","hsv","hwb"],I="[-\\+]?\\d*\\.?\\d+(?:deg|rad|grad|turn)?",V="(?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?)",U=`(?:${V})|(?:${I})`,B="(?:[,|\\s]+)",_=`(?:[\\s|\\(\\s|\\s\\(\\s]+)?(${U})${B}(${V})${B}(${V})(?:[,|\\/\\s]*)?(${V})?(?:[\\s|\\)\\s]+)?`,j={CSS_UNIT:new RegExp(U),hwb:new RegExp("hwb"+_),rgb:new RegExp("rgb(?:a)?"+_),hsl:new RegExp("hsl(?:a)?"+_),hsv:new RegExp("hsv(?:a)?"+_),hex3:/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,hex6:/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/,hex4:/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,hex8:/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/};function W(t){return(""+t).includes(".")&&1===parseFloat(t)}function z(t){return(""+t).includes("%")}function G(t){return!D.includes(t)&&!["#",...O].some(e=>t.includes(e))&&(!!["black","white"].includes(t)||["rgb(255, 255, 255)","rgb(0, 0, 0)"].every(e=>{S(F,{color:t});const o=g(F,"color");return S(F,{color:""}),o!==e}))}function X(t){return Boolean(j.CSS_UNIT.exec(String(t)))}function Y(t,e){let o=t;if("number"==typeof t&&0===Math.min(t,0)&&1===Math.max(t,1))return t;W(t)&&(o="100%");const n=z(o);return o=360===e?parseFloat(o):Math.min(e,Math.max(0,parseFloat(o))),n&&(o=o*e/100),Math.abs(o-e)<1e-6?1:(o=360===e?(o<0?o%e+e:o%e)/e:o%e/e,o)}function J(t){let e=parseFloat(""+t);return(Number.isNaN(e)||e<0||e>1)&&(e=1),e}function q(t){return Math.min(1,Math.max(0,t))}function Z(t){S(F,{color:t});const e=g(F,"color");return S(F,{color:""}),e}function Q(t){return K(255*t).toString(16)}function tt(t){return et(t)/255}function et(t){return parseInt(t,16)}function ot(t){return 1===t.length?"0"+t:String(t)}function nt(t,e,o){const n=Math.max(t,e,o),r=Math.min(t,e,o);let s=0,a=0;const i=(n+r)/2;if(n===r)a=0,s=0;else{const l=n-r;a=i>.5?l/(2-n-r):l/(n+r),n===t&&(s=(e-o)/l+(e<o?6:0)),n===e&&(s=(o-t)/l+2),n===o&&(s=(t-e)/l+4),s/=6}return{h:s,s:a,l:i}}function rt(t,e,o){let n=o;return n<0&&(n+=1),n>1&&(n-=1),n<1/6?t+6*n*(e-t):n<.5?e:n<2/3?t+(e-t)*(2/3-n)*6:t}function st(t,e,o){let n=0,r=0,s=0;if(0===e)r=o,s=o,n=o;else{const a=o<.5?o*(1+e):o+e-o*e,i=2*o-a;n=rt(i,a,t+1/3),r=rt(i,a,t),s=rt(i,a,t-1/3)}return{r:n,g:r,b:s}}function at(t,e,o){let n=0,r=0;const s=Math.min(t,e,o),a=Math.max(t,e,o),i=1-a;if(a===s)return{h:0,w:s,b:i};t===s?(n=e-o,r=3):(n=e===s?o-t:t-e,r=e===s?5:1);const l=(r-n/(a-s))/6;return{h:1===l?0:l,w:s,b:i}}function it(t,e,o){if(e+o>=1){const t=e/(e+o);return{r:t,g:t,b:t}}let{r:n,g:r,b:s}=st(t,1,.5);return[n,r,s]=[n,r,s].map(t=>t*(1-e-o)+e),{r:n,g:r,b:s}}function lt(t,e,o){const n=Math.max(t,e,o),r=Math.min(t,e,o);let s=0;const a=n-r;return n===r?s=0:(t===n&&(s=(e-o)/a+(e<o?6:0)),e===n&&(s=(o-t)/a+2),o===n&&(s=(t-e)/a+4),s/=6),{h:s,s:0===n?0:a/n,v:n}}function ct(t,e,o){const n=6*t,r=e,s=o,a=Math.floor(n),i=n-a,l=s*(1-r),c=s*(1-i*r),u=s*(1-(1-i)*r),h=a%6;return{r:[s,c,l,l,u,s][h],g:[u,s,s,c,l,l][h],b:[l,l,u,s,s,c][h]}}function ut(t,e,o,n){const r=[ot(K(t).toString(16)),ot(K(e).toString(16)),ot(K(o).toString(16))];return n&&r[0].charAt(0)===r[0].charAt(1)&&r[1].charAt(0)===r[1].charAt(1)&&r[2].charAt(0)===r[2].charAt(1)?r[0].charAt(0)+r[1].charAt(0)+r[2].charAt(0):r.join("")}function ht(t,e,o,n,r){const s=[ot(K(t).toString(16)),ot(K(e).toString(16)),ot(K(o).toString(16)),ot(Q(n))];return r&&s[0].charAt(0)===s[0].charAt(1)&&s[1].charAt(0)===s[1].charAt(1)&&s[2].charAt(0)===s[2].charAt(1)&&s[3].charAt(0)===s[3].charAt(1)?s[0].charAt(0)+s[1].charAt(0)+s[2].charAt(0)+s[3].charAt(0):s.join("")}function gt(t){let e=E(t.trim());if(0===e.length)return{r:0,g:0,b:0,a:1};if(G(e))e=Z(e);else if(D.includes(e)){return{r:0,g:0,b:0,a:"transparent"===e?0:1,format:"rgb",ok:!0}}let[,o,n,r,s]=j.rgb.exec(e)||[];return o&&n&&r?{r:o,g:n,b:r,a:void 0!==s?s:1,format:"rgb"}:([,o,n,r,s]=j.hsl.exec(e)||[],o&&n&&r?{h:o,s:n,l:r,a:void 0!==s?s:1,format:"hsl"}:([,o,n,r,s]=j.hsv.exec(e)||[],o&&n&&r?{h:o,s:n,v:r,a:void 0!==s?s:1,format:"hsv"}:([,o,n,r,s]=j.hwb.exec(e)||[],o&&n&&r?{h:o,w:n,b:r,a:void 0!==s?s:1,format:"hwb"}:([,o,n,r,s]=j.hex8.exec(e)||[],o&&n&&r&&s?{r:et(o),g:et(n),b:et(r),a:tt(s),format:"hex"}:([,o,n,r]=j.hex6.exec(e)||[],o&&n&&r?{r:et(o),g:et(n),b:et(r),format:"hex"}:([,o,n,r,s]=j.hex4.exec(e)||[],o&&n&&r&&s?{r:et(o+o),g:et(n+n),b:et(r+r),a:tt(s+s),format:"hex"}:([,o,n,r]=j.hex3.exec(e)||[],!!(o&&n&&r)&&{r:et(o+o),g:et(n+n),b:et(r+r),format:"hex"})))))))}function pt(t){let e={r:0,g:0,b:0},o=t,n=1,r=null,s=null,a=null,i=null,l=null,c=null,u=null,h=null,g=!1;const p="object"==typeof o&&o.format;let d=p&&O.includes(p)?p:"rgb";return"string"==typeof t&&(o=gt(t),o&&(g=!0)),"object"==typeof o&&(X(o.r)&&X(o.g)&&X(o.b)&&(({r:u,g:h,b:l}=o),[u,h,l]=[u,h,l].map(t=>Y(t,z(t)?100:255)),e={r:u,g:h,b:l},g=!0,d=o.format||"rgb"),X(o.h)&&X(o.s)&&X(o.v)&&(({h:c,s:r,v:s}=o),c=Y(c,360),r=Y(r,100),s=Y(s,100),e=ct(c,r,s),g=!0,d="hsv"),X(o.h)&&X(o.s)&&X(o.l)&&(({h:c,s:r,l:a}=o),c=Y(c,360),r=Y(r,100),a=Y(a,100),e=st(c,r,a),g=!0,d="hsl"),X(o.h)&&X(o.w)&&X(o.b)&&(({h:c,w:i,b:l}=o),c=Y(c,360),i=Y(i,100),l=Y(l,100),e=it(c,i,l),g=!0,d="hwb"),X(o.a)&&(n=o.a,n=z(""+n)||parseFloat(n)>1?Y(n,100):n)),void 0===o&&(g=!0),{ok:g,format:d,r:e.r,g:e.g,b:e.b,a:J(n)}}class dt{constructor(t,e){let o=t;const n=e&&O.includes(e)?e:"";o instanceof dt&&(o=pt(o));const{r:r,g:s,b:a,a:i,ok:l,format:c}=pt(o);this.originalInput=t,this.r=r,this.g=s,this.b=a,this.a=i,this.ok=l,this.format=n||c}get isValid(){return this.ok}get isDark(){return this.brightness<120}get luminance(){const{r:t,g:e,b:o}=this;let n=0,r=0,s=0;return n=t<=.03928?t/12.92:((t+.055)/1.055)**2.4,r=e<=.03928?e/12.92:((e+.055)/1.055)**2.4,s=o<=.03928?o/12.92:((o+.055)/1.055)**2.4,.2126*n+.7152*r+.0722*s}get brightness(){const{r:t,g:e,b:o}=this.toRgb();return(299*t+587*e+114*o)/1e3}toRgb(){let{r:t,g:e,b:o,a:n}=this;return[t,e,o]=[t,e,o].map(t=>K(255*t*100)/100),n=K(100*n)/100,{r:t,g:e,b:o,a:n}}toRgbString(){const{r:t,g:e,b:o,a:n}=this.toRgb(),[r,s,a]=[t,e,o].map(K);return 1===n?`rgb(${r}, ${s}, ${a})`:`rgba(${r}, ${s}, ${a}, ${n})`}toRgbCSS4String(){const{r:t,g:e,b:o,a:n}=this.toRgb(),[r,s,a]=[t,e,o].map(K);return`rgb(${r} ${s} ${a}${1===n?"":` / ${K(100*n)}%`})`}toHex(t){const{r:e,g:o,b:n,a:r}=this.toRgb();return 1===r?ut(e,o,n,t):ht(e,o,n,r,t)}toHexString(t){return"#"+this.toHex(t)}toHex8(t){const{r:e,g:o,b:n,a:r}=this.toRgb();return ht(e,o,n,r,t)}toHex8String(t){return"#"+this.toHex8(t)}toHsv(){const{r:t,g:e,b:o,a:n}=this,{h:r,s:s,v:a}=lt(t,e,o);return{h:r,s:s,v:a,a:n}}toHsl(){const{r:t,g:e,b:o,a:n}=this,{h:r,s:s,l:a}=nt(t,e,o);return{h:r,s:s,l:a,a:n}}toHslString(){let{h:t,s:e,l:o,a:n}=this.toHsl();return t=K(360*t),e=K(100*e),o=K(100*o),n=K(100*n)/100,1===n?`hsl(${t}, ${e}%, ${o}%)`:`hsla(${t}, ${e}%, ${o}%, ${n})`}toHslCSS4String(){let{h:t,s:e,l:o,a:n}=this.toHsl();t=K(360*t),e=K(100*e),o=K(100*o),n=K(100*n);return`hsl(${t}deg ${e}% ${o}%${n<100?` / ${K(n)}%`:""})`}toHwb(){const{r:t,g:e,b:o,a:n}=this,{h:r,w:s,b:a}=at(t,e,o);return{h:r,w:s,b:a,a:n}}toHwbString(){let{h:t,w:e,b:o,a:n}=this.toHwb();t=K(360*t),e=K(100*e),o=K(100*o),n=K(100*n);return`hwb(${t}deg ${e}% ${o}%${n<100?` / ${K(n)}%`:""})`}setAlpha(t){return"number"!=typeof t||(this.a=J(t)),this}saturate(t){if("number"!=typeof t)return this;const{h:e,s:o,l:n}=this.toHsl(),{r:r,g:s,b:a}=st(e,q(o+t/100),n);return $(this,{r:r,g:s,b:a}),this}desaturate(t){return"number"==typeof t?this.saturate(-t):this}greyscale(){return this.saturate(-100)}lighten(t){if("number"!=typeof t)return this;const{h:e,s:o,l:n}=this.toHsl(),{r:r,g:s,b:a}=st(e,o,q(n+t/100));return $(this,{r:r,g:s,b:a}),this}darken(t){return"number"==typeof t?this.lighten(-t):this}spin(t){if("number"!=typeof t)return this;const{h:e,s:o,l:n}=this.toHsl(),{r:r,g:s,b:a}=st(q((360*e+t)%360/360),o,n);return $(this,{r:r,g:s,b:a}),this}clone(){return new dt(this)}toString(t){const{format:e}=this;return"hex"===e?this.toHexString(t):"hsl"===e?this.toHslString():"hwb"===e?this.toHwbString():this.toRgbString()}}$(dt,{ANGLES:"deg|rad|grad|turn",CSS_ANGLE:I,CSS_INTEGER:"[-\\+]?\\d+%?",CSS_NUMBER:"[-\\+]?\\d*\\.\\d+%?",CSS_UNIT:V,CSS_UNIT2:U,PERMISSIVE_MATCH:_,matchers:j,isOnePointZero:W,isPercentage:z,isValidCSSUnit:X,isColorName:G,pad2:ot,clamp01:q,bound01:Y,boundAlpha:J,getRGBFromName:Z,convertHexToDecimal:tt,convertDecimalToHex:Q,rgbToHsl:nt,rgbToHex:ut,rgbToHsv:lt,rgbToHwb:at,rgbaToHex:ht,hslToRgb:st,hsvToRgb:ct,hueToRgb:rt,hwbToRgb:it,parseIntFromHex:et,stringInputToObject:gt,inputToRGB:pt,roundPart:K,getElementStyle:g,setElementStyle:S,ObjectAssign:$});class bt{constructor(...t){let e=0,o=12,n=10,r=[.5];if(3===t.length)[e,o,n]=t;else if(2===t.length&&([o,n]=t,[o,n].some(t=>t<1)))throw TypeError("ColorPalette: both arguments must be higher than 0.");const s=[],a=360/o,i=K((n-(n%2?1:0))/2),l=[[1,2,3],[4,5],[6,7],[8,9],[10,11],[12,13]],c=l.find(t=>t.includes(n)),u=c?[.25,.2,.15,.11,.09,.075][l.indexOf(c)]:100/(n+(n%2?0:1))/100;for(let t=1;t<i+1;t+=1)r=[...r,.5+u*t];for(let t=1;t<n-i;t+=1)r=[.5-u*t,...r];for(let t=0;t<o;t+=1){const o=(e+t*a)%360/360;r.forEach(t=>{s.push(new dt({h:o,s:1,l:t}))})}this.hue=e,this.hueSteps=o,this.lightSteps=n,this.colors=s}}$(bt,{Color:dt});const ft={pickerLabel:"Colour Picker",appearanceLabel:"Colour Appearance",valueLabel:"Colour Value",toggleLabel:"Select Colour",presetsLabel:"Colour Presets",defaultsLabel:"Colour Defaults",formatLabel:"Format",alphaLabel:"Alpha",hexLabel:"Hexadecimal",hueLabel:"Hue",whitenessLabel:"Whiteness",blacknessLabel:"Blackness",saturationLabel:"Saturation",lightnessLabel:"Lightness",redLabel:"Red",greenLabel:"Green",blueLabel:"Blue"},mt=["white","black","grey","red","orange","brown","gold","olive","yellow","lime","green","teal","cyan","blue","violet","magenta","pink"];function vt(t){try{JSON.parse(t)}catch(t){return!1}return!0}const wt=t=>t.toUpperCase();function xt(t){if("string"==typeof t)return u().createElement(t);const{tagName:e}=t,o={...t},n=xt(e);return delete o.tagName,$(n,o),n}function $t(t,e){if("string"==typeof e)return u().createElementNS(t,e);const{tagName:o}=e,n={...e},r=$t(t,o);return delete n.tagName,$(r,n),r}function yt(t,e,o){const{input:n,format:r,componentLabels:s}=t,{defaultsLabel:a,presetsLabel:i}=s,l="color-options"===o,c=e instanceof bt,u=l?i:a;let h=c?e.colors:e;h=h instanceof Array?h:[];const g=h.length,{lightSteps:p}=c?e:{lightSteps:null},d=p||[9,10].find(t=>g>2*t&&!(g%t))||5,b=l&&g>d;let f=2;f=b&&g>=2*d?3:f,f=g>=3*d?4:f,f=g>=4*d?5:f;const m=f-(g<3*d?1:2),v=b&&g>m*d;let w=o;w+=v?" scrollable":"",w+=b?" multiline":"";const x=b?"1px":"0.25rem";let $=b?1.75:2;$=d>5&&b?1.5:$;const y=(m||1)*$+"rem",k=`calc(${f} * ${$}rem + ${f-1} * ${x})`,A=xt({tagName:"ul",className:w});var E,N;return T(A,"role","listbox"),T(A,"aria-label",u),v&&(E=A,L(N={"--grid-item-size":$+"rem","--grid-fit":d,"--grid-gap":x,"--grid-height":y,"--grid-hover-height":k}).forEach(t=>{E.style.setProperty(t,N[t])})),h.forEach(t=>{let[e,o]="string"==typeof t?t.trim().split(":"):[];t instanceof dt&&(e=t.toHexString(),o=e);const s=new dt(t instanceof dt?t:e,r).toString()===H(n,"value"),a=xt({tagName:"li",className:"color-option"+(s?" active":""),innerText:""+(o||e)});T(a,"tabindex","0"),T(a,"data-value",""+e),T(a,"role","option"),T(a,"aria-selected",s?"true":"false"),l&&S(a,{backgroundColor:e}),A.append(a)}),A}function kt(t){const{input:e,parent:o,format:n,id:r,componentLabels:s,colorKeywords:a,colorPresets:i}=t,l=H(e,"value")||"#fff",{toggleLabel:c,pickerLabel:u,formatLabel:h,hexLabel:g}=s,p=D.includes(l)?"#fff":l;t.color=new dt(p,n);const d="hex"===n?g:wt(n),b=xt({id:"picker-btn-"+r,tagName:"button",className:"picker-toggle btn-appearance"});T(b,"aria-expanded","false"),T(b,"aria-haspopup","true"),b.append(xt({tagName:"span",className:"v-hidden",innerText:`${u}. ${h}: ${d}`}));const f=xt({tagName:"div",className:"color-dropdown picker"});T(f,"aria-labelledby","picker-btn-"+r),T(f,"role","group");const m=function(t){const{format:e,componentLabels:o}=t,{hueLabel:n,alphaLabel:r,lightnessLabel:s,saturationLabel:a,whitenessLabel:i,blacknessLabel:l}=o,c="hsl"===e?360:100,u="hsl"===e?100:360;let h="hsl"===e?`${n} & ${s}`:`${s} & ${a}`;h="hwb"===e?`${i} & ${l}`:h;const g="hsl"===e?""+a:""+n,p=xt({tagName:"div",className:"color-controls "+e});return[{i:1,c:"color-pointer",l:h,min:0,max:c},{i:2,c:"color-slider",l:g,min:0,max:u},{i:3,c:"color-slider",l:r,min:0,max:100}].forEach(t=>{const{i:e,c:o,l:n,min:r,max:s}=t,a=xt({tagName:"div",className:"color-control"});T(a,"role","presentation"),a.append(xt({tagName:"div",className:"visual-control visual-control"+e}));const i=xt({tagName:"div",className:o+" knob",ariaLive:"polite"});T(i,"aria-label",n),T(i,"role","slider"),T(i,"tabindex","0"),T(i,"aria-valuemin",""+r),T(i,"aria-valuemax",""+s),a.append(i),p.append(a)}),p}(t),v=function(t){const{format:e,id:o,componentLabels:n}=t,r=xt({tagName:"div",className:"color-form "+e});let s=["hex"];return"rgb"===e?s=["red","green","blue","alpha"]:"hsl"===e?s=["hue","saturation","lightness","alpha"]:"hwb"===e&&(s=["hue","whiteness","blackness","alpha"]),s.forEach(t=>{const[s]="hex"===e?["#"]:wt(t).split(""),a=`color_${e}_${t}_${o}`,i=n[t+"Label"],l=xt({tagName:"label"});T(l,"for",a),l.append(xt({tagName:"span",ariaHidden:"true",innerText:s+":"}),xt({tagName:"span",className:"v-hidden",innerText:i}));const c=xt({tagName:"input",id:a,type:"hex"===e?"text":"number",value:"alpha"===t?"100":"0",className:"color-input "+t});T(c,"autocomplete","off"),T(c,"spellcheck","false");let u="100",h="1";"alpha"!==t&&("rgb"===e?(u="255",h="1"):"hue"===t&&(u="360",h="1")),$(c,{min:"0",max:u,step:h}),r.append(l,c)}),r}(t);if(f.append(m,v),e.before(b),o.append(f),a||i){const e=xt({tagName:"div",className:"color-dropdown scrollable menu"});if(i instanceof Array&&i.length||i instanceof bt&&i.colors){const o=yt(t,i,"color-options");e.append(o)}if(a&&a.length){const o=yt(t,a,"color-defaults");e.append(o)}const n=xt({tagName:"button",className:"menu-toggle btn-appearance"});T(n,"tabindex","-1"),T(n,"aria-expanded","false"),T(n,"aria-haspopup","true");const r=encodeURI("http://www.w3.org/2000/svg"),s=$t(r,{tagName:"svg"});T(s,"xmlns",r),T(s,"viewBox","0 0 512 512"),T(s,"aria-hidden","true");const l=$t(r,{tagName:"path"});T(l,"d","M98,158l157,156L411,158l27,27L255,368L71,185L98,158z"),T(l,"fill","#fff"),s.append(l),n.append(xt({tagName:"span",className:"v-hidden",innerText:""+c}),s),o.append(n,e)}a&&D.includes(l)&&(t.value=l),T(e,"tabindex","-1")}const St={componentLabels:ft,colorLabels:mt,format:"rgb",colorPresets:!1,colorKeywords:!1};function Ht(t,e){const r=e?o:n,{input:s,pickerToggle:a,menuToggle:i}=t;r(s,"focusin",t.showPicker),r(a,"click",t.togglePicker),i&&r(i,"click",t.toggleMenu)}function At(t,e){const r=e?o:n,{input:s,colorMenu:a,parent:i}=t,l=u(s),c=function(t){if(null==t)return window;if(!(t instanceof Window)){const{ownerDocument:e}=t;return e&&e.defaultView||window}return t}(s),h="ontouchstart"in l?{down:"touchstart",move:"touchmove",up:"touchend"}:{down:"mousedown",move:"mousemove",up:"mouseup"};r(t.controls,h.down,t.pointerDown),t.controlKnobs.forEach(e=>r(e,"keydown",t.handleKnobs)),r(c,"scroll",t.handleScroll),r(c,"resize",t.update),[s,...t.inputs].forEach(e=>r(e,"change",t.changeHandler)),a&&(r(a,"click",t.menuClickHandler),r(a,"keydown",t.menuKeyHandler)),r(l,h.move,t.pointerMove),r(l,h.up,t.pointerUp),r(i,"focusout",t.handleFocusOut),r(l,"keyup",t.handleDismiss)}function Lt(t){var e,o;e=t.input,o=new CustomEvent("colorpicker.change"),e.dispatchEvent(o)}function Et(t){t&&["bottom","top"].forEach(e=>M(t,e))}function Nt(t,e){const{colorPicker:o,colorMenu:n,menuToggle:r,pickerToggle:s,parent:a}=t,i=e===o,l=i?n:o,c=i?r:s,u=i?s:r;P(a,"open")||C(a,"open"),l&&(M(l,"show"),Et(l)),C(e,"bottom"),e.offsetHeight,C(e,"show"),i&&t.update(),t.isOpen||(At(t,!0),t.updateDropdownPosition(),t.isOpen=!0,T(t.input,"tabindex","0"),r&&T(r,"tabindex","0")),T(u,"aria-expanded","true"),c&&T(c,"aria-expanded","false")}class Pt{constructor(t,e){const o=this,n=w(t);if(!n)throw new TypeError(`ColorPicker target "${t}" cannot be found.`);o.input=n;const r=function t(e,o){return e?e.closest(o)||t(e.getRootNode().host,o):null}(n,".color-picker,color-picker");if(!r)throw new TypeError("ColorPicker requires a specific markup to work.");o.parent=r,o.id=function t(e,o){let n=o?p:d;if(o){const r=t(e),s=b.get(r)||new Map;b.has(r)||b.set(r,s),s.has(o)?n=s.get(o):(s.set(o,n),p+=1)}else{const t=e.id||e;b.has(t)?n=b.get(t):(b.set(t,n),d+=1)}return n}(n,"color-picker"),o.dragElement=null,o.isOpen=!1,o.controlPositions={c1x:0,c1y:0,c2y:0,c3y:0},o.colorLabels={},o.colorKeywords=void 0,o.colorPresets=void 0;const{format:s,componentLabels:a,colorLabels:i,colorKeywords:l,colorPresets:c}=function(t,e,o,n){const r={...t.dataset},s={},a={};return L(r).forEach(t=>{const e=n&&t.includes(n)?t.replace(n,"").replace(/[A-Z]/,t=>E(t)):t;a[e]=A(r[t])}),L(o).forEach(t=>{o[t]=A(o[t])}),L(e).forEach(n=>{s[n]=n in o?o[n]:n in a?a[n]:"title"===n?H(t,"title"):e[n]}),s}(this.isCE?r:n,St,e||{});let u=mt;i instanceof Array&&17===i.length?u=i:i&&17===i.split(",").length&&(u=i.split(",")),mt.forEach((t,e)=>{o.colorLabels[t]=u[e].trim()});const h=a&&vt(a)?JSON.parse(a):a;if(o.componentLabels=$(ft,h),o.color=new dt(n.value||"#fff",s),o.format=s,l instanceof Array?o.colorKeywords=l:"string"==typeof l&&l.length&&(o.colorKeywords=l.split(",").map(t=>t.trim())),c instanceof Array)o.colorPresets=c;else if("string"==typeof c&&c.length)if(vt(c)){const{hue:t,hueSteps:e,lightSteps:n}=JSON.parse(c);o.colorPresets=new bt(t,e,n)}else o.colorPresets=c.split(",").map(t=>t.trim());o.showPicker=o.showPicker.bind(o),o.togglePicker=o.togglePicker.bind(o),o.toggleMenu=o.toggleMenu.bind(o),o.menuClickHandler=o.menuClickHandler.bind(o),o.menuKeyHandler=o.menuKeyHandler.bind(o),o.pointerDown=o.pointerDown.bind(o),o.pointerMove=o.pointerMove.bind(o),o.pointerUp=o.pointerUp.bind(o),o.update=o.update.bind(o),o.handleScroll=o.handleScroll.bind(o),o.handleFocusOut=o.handleFocusOut.bind(o),o.changeHandler=o.changeHandler.bind(o),o.handleDismiss=o.handleDismiss.bind(o),o.handleKnobs=o.handleKnobs.bind(o),kt(o);const[g,f]=x("color-dropdown",r);o.pickerToggle=w(".picker-toggle",r),o.menuToggle=w(".menu-toggle",r),o.colorPicker=g,o.colorMenu=f,o.inputs=[...x("color-input",r)];const[m]=x("color-controls",r);o.controls=m,o.controlKnobs=[...x("knob",m)],o.visuals=[...x("visual-control",m)],o.update(),Ht(o,!0),k.set(n,"color-picker",o)}get value(){return this.input.value}set value(t){this.input.value=t}get hasNonColor(){return this.colorKeywords instanceof Array&&this.colorKeywords.some(t=>D.includes(t))}get isCE(){return"color-picker"===this.parent.localName}get hex(){return this.color.toHex(!0)}get hsv(){return this.color.toHsv()}get hsl(){return this.color.toHsl()}get hwb(){return this.color.toHwb()}get rgb(){return this.color.toRgb()}get brightness(){return this.color.brightness}get luminance(){return this.color.luminance}get isDark(){const{color:t,brightness:e}=this;return e<120&&t.a>.33}get isValid(){const t=this.input.value;return""!==t&&new dt(t).isValid}get appearance(){const{colorLabels:t,hsl:e,hsv:o,format:n}=this,r=K(360*e.h),s=K(100*("hsl"===n?e.s:o.s)),a=K(100*e.l),i=100*o.v;let l;if(100===a&&0===s)l=t.white;else if(0===a)l=t.black;else if(0===s)l=t.grey;else if(r<15||r>=345)l=t.red;else if(r>=15&&r<45)l=i>80&&s>80?t.orange:t.brown;else if(r>=45&&r<75){const e=r>=54&&r<75&&i<80;l=r>46&&r<54&&i<80&&s>90?t.gold:t.yellow,l=e?t.olive:l}else r>=75&&r<155?l=i<68?t.green:t.lime:r>=155&&r<175?l=t.teal:r>=175&&r<195?l=t.cyan:r>=195&&r<255?l=t.blue:r>=255&&r<270?l=t.violet:r>=270&&r<295?l=t.magenta:r>=295&&r<345&&(l=t.pink);return l}updateVisuals(){const{controlPositions:t,visuals:e}=this,[o,n,r]=e,{offsetHeight:s}=o,a=t.c2y/s,{r:i,g:l,b:c}=new dt({h:a,s:1,l:.5}).toRgb(),u=1-t.c3y/s,h=K(100*u)/100,g=new dt({h:a,s:1,l:.5,a:u}).toRgbString();S(o,{background:`linear-gradient(rgba(0,0,0,0) 0%, rgba(0,0,0,${h}) 100%),\n linear-gradient(to right, rgba(255,255,255,${h}) 0%, ${g} 100%),\n linear-gradient(rgb(255,255,255) 0%, rgb(255,255,255) 100%)`}),S(n,{background:"linear-gradient(\n rgb(255,0,0) 0%, rgb(255,255,0) 16.67%,\n rgb(0,255,0) 33.33%, rgb(0,255,255) 50%,\n rgb(0,0,255) 66.67%, rgb(255,0,255) 83.33%,\n rgb(255,0,0) 100%)"}),S(r,{background:`linear-gradient(rgba(${i},${l},${c},1) 0%,rgba(${i},${l},${c},0) 100%)`})}handleFocusOut({relatedTarget:t}){t&&!this.parent.contains(t)&&this.hide(!0)}handleDismiss({code:t}){const e=this;e.isOpen&&"Escape"===t&&e.hide()}handleScroll(t){const{activeElement:e}=u(this.input);("touchmove"===t.type&&this.dragElement||e&&this.controlKnobs.includes(e))&&(t.stopPropagation(),t.preventDefault()),this.updateDropdownPosition()}menuKeyHandler(t){const{target:e,code:o}=t,{previousElementSibling:n,nextElementSibling:u,parentElement:h}=e,p=h&&P(h,"color-options"),d=[...h.children],b=p&&g(h,"grid-template-columns").split(" ").length,f=d.indexOf(e),m=f>-1&&b&&d[f-b],v=f>-1&&b&&d[f+b];[r,s,c].includes(o)&&t.preventDefault(),p?m&&o===s?N(m):v&&o===r?N(v):n&&o===a?N(n):u&&o===i&&N(u):n&&[a,s].includes(o)?N(n):u&&[i,r].includes(o)&&N(u),[l,c].includes(o)&&this.menuClickHandler({target:e})}menuClickHandler(t){const e=this,{target:o}=t,{colorMenu:n}=e,r=(H(o,"data-value")||"").trim();if(!r.length)return;const s=w("li.active",n);let a=D.includes(r)?"white":r;a="transparent"===r?"rgba(0,0,0,0)":r;const{r:i,g:l,b:c,a:u}=new dt(a);$(e.color,{r:i,g:l,b:c,a:u}),e.update(),s!==o&&(s&&(M(s,"active"),R(s,"aria-selected")),C(o,"active"),T(o,"aria-selected","true"),D.includes(r)&&(e.value=r),Lt(e))}pointerDown(t){const e=this,{type:o,target:n,touches:r,pageX:s,pageY:a}=t,{colorMenu:i,visuals:l,controlKnobs:c}=e,[u,g,p]=l,[d,b,m]=c,v=c.includes(n)?n.previousElementSibling:n,x=f(v),$=h(u),y="touchstart"===o?r[0].pageX:s,k="touchstart"===o?r[0].pageY:a,S=y-$.scrollLeft-x.left,H=k-$.scrollTop-x.top;if(n===u||n===d?(e.dragElement=v,e.changeControl1(S,H)):n===g||n===b?(e.dragElement=v,e.changeControl2(H)):n!==p&&n!==m||(e.dragElement=v,e.changeAlpha(H)),i){const t=w("li.active",i);t&&(M(t,"active"),R(t,"aria-selected"))}t.preventDefault()}pointerUp({target:t}){const e=this,{parent:o}=e,n=u(o),r=null!==w(".color-picker,color-picker.open",n),s=n.getSelection();e.dragElement||s.toString().length||o.contains(t)||e.hide(r),e.dragElement=null}pointerMove(t){const e=this,{dragElement:o,visuals:n}=e,[r,s,a]=n,{type:i,touches:l,pageX:c,pageY:u}=t;if(!o)return;const g=f(o),p=h(r),d="touchmove"===i?l[0].pageX:c,b="touchmove"===i?l[0].pageY:u,m=d-p.scrollLeft-g.left,v=b-p.scrollTop-g.top;o===r&&e.changeControl1(m,v),o===s&&e.changeControl2(v),o===a&&e.changeAlpha(v)}handleKnobs(t){const{target:e,code:o}=t,n=this;if(![s,r,a,i].includes(o))return;t.preventDefault();const{controlKnobs:l,visuals:c}=n,{offsetWidth:h,offsetHeight:g}=c[0],[p,d,b]=l,{activeElement:f}=u(p),m=g/360;if(l.find(t=>t===f)){let l=0,c=0;if(e===p){const t=h/100;[a,i].includes(o)?n.controlPositions.c1x+=o===i?t:-t:[s,r].includes(o)&&(n.controlPositions.c1y+=o===r?m:-m),l=n.controlPositions.c1x,c=n.controlPositions.c1y,n.changeControl1(l,c)}else e===d?(n.controlPositions.c2y+=[r,i].includes(o)?m:-m,c=n.controlPositions.c2y,n.changeControl2(c)):e===b&&(n.controlPositions.c3y+=[r,i].includes(o)?m:-m,c=n.controlPositions.c3y,n.changeAlpha(c));n.handleScroll(t)}}changeHandler(){const t=this;let e;const{inputs:o,format:n,value:r,input:s,controlPositions:a,visuals:i}=t,{activeElement:l}=u(s),{offsetHeight:c}=i[0],[h,,,g]=o,[p,d,b,f]="rgb"===n?o.map(t=>parseFloat(t.value)/(t===g?100:1)):o.map(t=>parseFloat(t.value)/(t!==h?100:360)),m=t.hasNonColor&&D.includes(r),v=g?f:1-a.c3y/c;if(l===s||l&&o.includes(l)){e=l===s?m?"transparent"===r?"rgba(0,0,0,0)":"rgb(0,0,0)":r:"hex"===n?h.value:"hsl"===n?{h:p,s:d,l:b,a:v}:"hwb"===n?{h:p,w:d,b:b,a:v}:{r:p,g:d,b:b,a:v};const{r:o,g:a,b:i,a:c}=new dt(e);$(t.color,{r:o,g:a,b:i,a:c}),t.setControlPositions(),t.updateAppearance(),t.updateInputs(),t.updateControls(),t.updateVisuals(),l===s&&m&&(t.value=r)}}changeControl1(t,e){let[o,n]=[0,0];const{controlPositions:r,visuals:s}=this,{offsetHeight:a,offsetWidth:i}=s[0];t>i?o=i:t>=0&&(o=t),e>a?n=a:e>=0&&(n=e);const l=r.c2y/a,c=o/i,u=1-n/a,h=1-r.c3y/a,{r:g,g:p,b:d,a:b}=new dt({h:l,s:c,v:u,a:h});$(this.color,{r:g,g:p,b:d,a:b}),this.controlPositions.c1x=o,this.controlPositions.c1y=n,this.updateAppearance(),this.updateInputs(),this.updateControls(),this.updateVisuals()}changeControl2(t){const{controlPositions:e,visuals:o}=this,{offsetHeight:n,offsetWidth:r}=o[0];let s=0;t>n?s=n:t>=0&&(s=t);const a=s/n,i=e.c1x/r,l=1-e.c1y/n,c=1-e.c3y/n,{r:u,g:h,b:g,a:p}=new dt({h:a,s:i,v:l,a:c});$(this.color,{r:u,g:h,b:g,a:p}),this.controlPositions.c2y=s,this.updateAppearance(),this.updateInputs(),this.updateControls(),this.updateVisuals()}changeAlpha(t){const{visuals:e}=this,{offsetHeight:o}=e[0];let n=0;t>o?n=o:t>=0&&(n=t);const r=1-n/o;this.color.setAlpha(r),this.controlPositions.c3y=n,this.updateAppearance(),this.updateInputs(),this.updateControls(),this.updateVisuals()}update(){this.updateDropdownPosition(),this.updateAppearance(),this.setControlPositions(),this.updateInputs(!0),this.updateControls(),this.updateVisuals()}updateDropdownPosition(){const{input:t,colorPicker:e,colorMenu:o}=this,n=f(t),{top:r,bottom:s}=n,{offsetHeight:a}=t,i=h(t).clientHeight,l=P(e,"show")?e:o;if(!l)return;const{offsetHeight:c}=l,u=i-s,g=r,p=r+c+a>i,d=r-c<0;(P(l,"bottom")||!d)&&u<g&&p?(M(l,"bottom"),C(l,"top")):(M(l,"top"),C(l,"bottom"))}setControlPositions(){const{visuals:t,color:e,hsv:o}=this,{offsetHeight:n,offsetWidth:r}=t[0],s=e.a,a=o.h,i=o.s,l=o.v;this.controlPositions.c1x=i*r,this.controlPositions.c1y=(1-l)*n,this.controlPositions.c2y=a*n,this.controlPositions.c3y=(1-s)*n}updateAppearance(){const t=this,{componentLabels:e,color:o,parent:n,hsv:r,hex:s,format:a,controlKnobs:i}=t,{appearanceLabel:l,hexLabel:c,valueLabel:u}=e;let{r:h,g:g,b:p}=o.toRgb();const[d,b,f]=i,m=K(360*r.h),v=o.a,w=K(100*r.s),x=K(100*r.v),$=t.appearance;let y=`${c} ${s.split("").join(" ")}`;if("hwb"===a){const{hwb:e}=t,o=K(100*e.w),n=K(100*e.b);y=`HWB: ${m}°, ${o}%, ${n}%`,T(d,"aria-valuetext",`${o}% & ${n}%`),T(d,"aria-valuenow",""+o),T(b,"aria-description",`${u}: ${y}. ${l}: ${$}.`),T(b,"aria-valuetext",m+"%"),T(b,"aria-valuenow",""+m)}else[h,g,p]=[h,g,p].map(K),y="hsl"===a?`HSL: ${m}°, ${w}%, ${x}%`:y,y="rgb"===a?`RGB: ${h}, ${g}, ${p}`:y,T(d,"aria-valuetext",`${x}% & ${w}%`),T(d,"aria-valuenow",""+x),T(b,"aria-description",`${u}: ${y}. ${l}: ${$}.`),T(b,"aria-valuetext",m+"°"),T(b,"aria-valuenow",""+m);const k=K(100*v);T(f,"aria-valuetext",k+"%"),T(f,"aria-valuenow",""+k);const H=o.toString();S(t.input,{backgroundColor:H}),t.isDark?(P(n,"txt-light")&&M(n,"txt-light"),P(n,"txt-dark")||C(n,"txt-dark")):(P(n,"txt-dark")&&M(n,"txt-dark"),P(n,"txt-light")||C(n,"txt-light"))}updateControls(){const{controlKnobs:t,controlPositions:e}=this;let{c1x:o,c1y:n,c2y:r,c3y:s}=e;const[a,i,l]=t;[o,n,r,s]=[o,n,r,s].map(K),S(a,{transform:`translate3d(${o-4}px,${n-4}px,0)`}),S(i,{transform:`translate3d(0,${r-4}px,0)`}),S(l,{transform:`translate3d(0,${s-4}px,0)`})}updateInputs(t){const e=this,{value:o,format:n,inputs:r,color:s,hsl:a}=e,[i,l,c,u]=r,h=K(100*s.a),g=K(360*a.h);let p;if("hex"===n)p=e.color.toHexString(!0),i.value=e.hex;else if("hsl"===n){const t=K(100*a.l),o=K(100*a.s);p=e.color.toHslString(),i.value=""+g,l.value=""+o,c.value=""+t,u.value=""+h}else if("hwb"===n){const{w:t,b:o}=e.hwb,n=K(100*t),r=K(100*o);p=e.color.toHwbString(),i.value=""+g,l.value=""+n,c.value=""+r,u.value=""+h}else if("rgb"===n){let{r:t,g:o,b:n}=e.rgb;[t,o,n]=[t,o,n].map(K),p=e.color.toRgbString(),i.value=""+t,l.value=""+o,c.value=""+n,u.value=""+h}e.value=""+p,t||p===o||Lt(e)}togglePicker(t){t&&t.preventDefault();const e=this,{colorPicker:o}=e;e.isOpen&&P(o,"show")?e.hide(!0):Nt(e,o)}showPicker(){const t=this,{colorPicker:e}=t;["top","bottom"].some(t=>P(e,t))||Nt(t,e)}toggleMenu(t){t&&t.preventDefault();const e=this,{colorMenu:o}=e;e.isOpen&&P(o,"show")?e.hide(!0):Nt(e,o)}hide(t){const e=this;if(e.isOpen){const{pickerToggle:o,menuToggle:n,colorPicker:r,colorMenu:s,parent:a,input:i}=e,l=P(r,"show"),c=l?r:s,u=l?o:n,h=c&&function(t){const e=g(t,"transitionProperty"),o=g(t,"transitionDuration"),n=o.includes("ms")?1:1e3,r=e&&"none"!==e?parseFloat(o)*n:0;return Number.isNaN(r)?0:r}(c);e.value=e.color.toString(!0),c&&(M(c,"show"),T(u,"aria-expanded","false"),setTimeout(()=>{Et(c),w(".show",a)||(M(a,"open"),At(e),e.isOpen=!1)},h)),t||N(o),T(i,"tabindex","-1"),n&&T(n,"tabindex","-1")}}dispose(){const{input:t,parent:e}=this;this.hide(!0),Ht(this),[...e.children].forEach(e=>{e!==t&&e.remove()}),R(t,"tabindex"),S(t,{backgroundColor:""}),["txt-light","txt-dark"].forEach(t=>M(e,t)),k.remove(t,"color-picker")}}$(Pt,{Color:dt,ColorPalette:bt,Version:"0.0.2alpha4",getInstance:t=>{return e=t,o="color-picker",k.get(e,o);var e,o},init:t=>new Pt(t),selector:'[data-function="color-picker"]',roundPart:K,setElementStyle:S,setAttribute:T,getBoundingClientRect:f});export{Pt as default};
1
+ // ColorPicker v1.0.0 | thednp © 2022 | MIT-License
2
+ const t={};function e(e){const o=this,{type:r,target:s}=e;[...t[r]].forEach(t=>{const[a,i]=t;[s,o].some(t=>a===t)&&[...i].forEach(t=>{const[o,s]=t;o.apply(a,[e]),s&&s.once&&n(a,r,o,s)})})}const o=(o,n,r,s)=>{t[n]||(t[n]=new Map);const a=t[n];a.has(o)||a.set(o,new Map);const i=a.get(o),{size:l}=i;i.set(r,s),l||o.addEventListener(n,e,s)},n=(o,n,r,s)=>{const a=t[n],i=a&&a.get(o),l=i&&i.get(r),{options:c}=void 0!==l?l:{options:s};i&&i.has(r)&&i.delete(r),!a||i&&i.size||a.delete(o),a&&a.size||delete t[n],i&&i.size||o.removeEventListener(n,e,c)},r="ArrowDown",s="ArrowUp",a="ArrowLeft",i="ArrowRight",l="Enter",c="Space",u="touchmove",h="pointermove";function g(t){return t instanceof HTMLElement?t.ownerDocument:t instanceof Window?t.document:window.document}function p(t){return g(t).documentElement}function d(t,e){const o=getComputedStyle(t);return e in o?o[e]:""}let b=0,f=0;const m=new Map;function v(t,e){const{width:o,height:n,top:r,right:s,bottom:a,left:i}=t.getBoundingClientRect();let l=1,c=1;if(e&&t instanceof HTMLElement){const{offsetWidth:e,offsetHeight:r}=t;l=e>0&&Math.round(o)/e||1,c=r>0&&Math.round(n)/r||1}return{width:o/l,height:n/c,top:r/c,right:s/l,bottom:a/c,left:i/l,x:i/l,y:r/c}}const w=[Document,Element,HTMLElement],x=[Element,HTMLElement];function $(t,e){const o=w.some(t=>e instanceof t)?e:g();return x.some(e=>t instanceof e)?t:o.querySelector(t)}function y(t,e){return(e&&w.some(t=>e instanceof t)?e:g()).getElementsByClassName(t)}const k=(t,e)=>Object.assign(t,e),S=new Map,H={set:(t,e,o)=>{const n=$(t);if(!n)return;S.has(e)||S.set(e,new Map);S.get(e).set(n,o)},getAllFor:t=>S.get(t)||null,get:(t,e)=>{const o=$(t),n=H.getAllFor(e);return o&&n&&n.get(o)||null},remove:(t,e)=>{const o=$(t),n=S.get(e);n&&o&&(n.delete(o),0===n.size&&S.delete(e))}},A=(t,e)=>{k(t.style,e)},L=(t,e)=>t.getAttribute(e);function E(t){return"true"===t||"false"!==t&&(Number.isNaN(+t)?""===t||"null"===t?null:t:+t)}const N=t=>Object.keys(t),P=t=>t.toLowerCase();const C=t=>t.focus();function M(t,e){return t.classList.contains(e)}function T(t,e){t.classList.add(e)}function R(t,e){t.classList.remove(e)}const F=(t,e,o)=>t.setAttribute(e,o),D=(t,e)=>t.removeAttribute(e),{head:K}=document,O=["transparent","currentColor","inherit","revert","initial"];function I(t){const e=Math.floor(t);return t-e<.5?e:Math.round(t)}const V=["rgb","hex","hsl","hsv","hwb"],U="[-\\+]?\\d*\\.?\\d+(?:deg|rad|grad|turn)?",B="(?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?)",_=`(?:${B})|(?:${U})`,j="(?:[,|\\s]+)",z=`(?:[\\s|\\(\\s|\\s\\(\\s]+)?(${_})${j}(${B})${j}(${B})(?:[,|\\/\\s]*)?(${B})?(?:[\\s|\\)\\s]+)?`,W={CSS_UNIT:new RegExp(_),hwb:new RegExp("hwb"+z),rgb:new RegExp("rgb(?:a)?"+z),hsl:new RegExp("hsl(?:a)?"+z),hsv:new RegExp("hsv(?:a)?"+z),hex3:/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,hex6:/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/,hex4:/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/,hex8:/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/};function G(t){return(""+t).includes(".")&&1===parseFloat(t)}function J(t){return(""+t).includes("%")}function q(t){return!O.includes(t)&&!["#",...V].some(e=>t.includes(e))&&(!!["black","white"].includes(t)||["rgb(255, 255, 255)","rgb(0, 0, 0)"].every(e=>{A(K,{color:t});const o=d(K,"color");return A(K,{color:""}),o!==e}))}function X(t){return Boolean(W.CSS_UNIT.exec(String(t)))}function Y(t,e){let o=t;if("number"==typeof t&&0===Math.min(t,0)&&1===Math.max(t,1))return t;G(t)&&(o="100%");const n=J(o);return o=360===e?parseFloat(o):Math.min(e,Math.max(0,parseFloat(o))),n&&(o=o*e/100),Math.abs(o-e)<1e-6?1:(o=360===e?(o<0?o%e+e:o%e)/e:o%e/e,o)}function Z(t){let e=parseFloat(""+t);return(Number.isNaN(e)||e<0||e>1)&&(e=1),e}function Q(t){return Math.min(1,Math.max(0,t))}function tt(t){A(K,{color:t});const e=d(K,"color");return A(K,{color:""}),e}function et(t){return I(255*t).toString(16)}function ot(t){return nt(t)/255}function nt(t){return parseInt(t,16)}function rt(t){return 1===t.length?"0"+t:String(t)}function st(t,e,o){const n=Math.max(t,e,o),r=Math.min(t,e,o);let s=0,a=0;const i=(n+r)/2;if(n===r)a=0,s=0;else{const l=n-r;a=i>.5?l/(2-n-r):l/(n+r),n===t&&(s=(e-o)/l+(e<o?6:0)),n===e&&(s=(o-t)/l+2),n===o&&(s=(t-e)/l+4),s/=6}return{h:s,s:a,l:i}}function at(t,e,o){let n=o;return n<0&&(n+=1),n>1&&(n-=1),n<1/6?t+6*n*(e-t):n<.5?e:n<2/3?t+(e-t)*(2/3-n)*6:t}function it(t,e,o){let n=0,r=0,s=0;if(0===e)r=o,s=o,n=o;else{const a=o<.5?o*(1+e):o+e-o*e,i=2*o-a;n=at(i,a,t+1/3),r=at(i,a,t),s=at(i,a,t-1/3)}return{r:n,g:r,b:s}}function lt(t,e,o){let n=0,r=0;const s=Math.min(t,e,o),a=Math.max(t,e,o),i=1-a;if(a===s)return{h:0,w:s,b:i};t===s?(n=e-o,r=3):(n=e===s?o-t:t-e,r=e===s?5:1);const l=(r-n/(a-s))/6;return{h:1===l?0:l,w:s,b:i}}function ct(t,e,o){if(e+o>=1){const t=e/(e+o);return{r:t,g:t,b:t}}let{r:n,g:r,b:s}=it(t,1,.5);return[n,r,s]=[n,r,s].map(t=>t*(1-e-o)+e),{r:n,g:r,b:s}}function ut(t,e,o){const n=Math.max(t,e,o),r=Math.min(t,e,o);let s=0;const a=n-r;return n===r?s=0:(t===n&&(s=(e-o)/a+(e<o?6:0)),e===n&&(s=(o-t)/a+2),o===n&&(s=(t-e)/a+4),s/=6),{h:s,s:0===n?0:a/n,v:n}}function ht(t,e,o){const n=6*t,r=e,s=o,a=Math.floor(n),i=n-a,l=s*(1-r),c=s*(1-i*r),u=s*(1-(1-i)*r),h=a%6;return{r:[s,c,l,l,u,s][h],g:[u,s,s,c,l,l][h],b:[l,l,u,s,s,c][h]}}function gt(t,e,o,n){const r=[rt(I(t).toString(16)),rt(I(e).toString(16)),rt(I(o).toString(16))];return n&&r[0].charAt(0)===r[0].charAt(1)&&r[1].charAt(0)===r[1].charAt(1)&&r[2].charAt(0)===r[2].charAt(1)?r[0].charAt(0)+r[1].charAt(0)+r[2].charAt(0):r.join("")}function pt(t,e,o,n,r){const s=[rt(I(t).toString(16)),rt(I(e).toString(16)),rt(I(o).toString(16)),rt(et(n))];return r&&s[0].charAt(0)===s[0].charAt(1)&&s[1].charAt(0)===s[1].charAt(1)&&s[2].charAt(0)===s[2].charAt(1)&&s[3].charAt(0)===s[3].charAt(1)?s[0].charAt(0)+s[1].charAt(0)+s[2].charAt(0)+s[3].charAt(0):s.join("")}function dt(t){let e=P(t.trim());if(0===e.length)return{r:0,g:0,b:0,a:1};if(q(e))e=tt(e);else if(O.includes(e)){return{r:0,g:0,b:0,a:"transparent"===e?0:1,format:"rgb",ok:!0}}let[,o,n,r,s]=W.rgb.exec(e)||[];return o&&n&&r?{r:o,g:n,b:r,a:void 0!==s?s:1,format:"rgb"}:([,o,n,r,s]=W.hsl.exec(e)||[],o&&n&&r?{h:o,s:n,l:r,a:void 0!==s?s:1,format:"hsl"}:([,o,n,r,s]=W.hsv.exec(e)||[],o&&n&&r?{h:o,s:n,v:r,a:void 0!==s?s:1,format:"hsv"}:([,o,n,r,s]=W.hwb.exec(e)||[],o&&n&&r?{h:o,w:n,b:r,a:void 0!==s?s:1,format:"hwb"}:([,o,n,r,s]=W.hex8.exec(e)||[],o&&n&&r&&s?{r:nt(o),g:nt(n),b:nt(r),a:ot(s),format:"hex"}:([,o,n,r]=W.hex6.exec(e)||[],o&&n&&r?{r:nt(o),g:nt(n),b:nt(r),format:"hex"}:([,o,n,r,s]=W.hex4.exec(e)||[],o&&n&&r&&s?{r:nt(o+o),g:nt(n+n),b:nt(r+r),a:ot(s+s),format:"hex"}:([,o,n,r]=W.hex3.exec(e)||[],!!(o&&n&&r)&&{r:nt(o+o),g:nt(n+n),b:nt(r+r),format:"hex"})))))))}function bt(t){let e={r:0,g:0,b:0},o=t,n=1,r=null,s=null,a=null,i=null,l=null,c=null,u=null,h=null,g=!1;const p="object"==typeof o&&o.format;let d=p&&V.includes(p)?p:"rgb";return"string"==typeof t&&(o=dt(t),o&&(g=!0)),"object"==typeof o&&(X(o.r)&&X(o.g)&&X(o.b)&&(({r:u,g:h,b:l}=o),[u,h,l]=[u,h,l].map(t=>Y(t,J(t)?100:255)),e={r:u,g:h,b:l},g=!0,d=o.format||"rgb"),X(o.h)&&X(o.s)&&X(o.v)&&(({h:c,s:r,v:s}=o),c=Y(c,360),r=Y(r,100),s=Y(s,100),e=ht(c,r,s),g=!0,d="hsv"),X(o.h)&&X(o.s)&&X(o.l)&&(({h:c,s:r,l:a}=o),c=Y(c,360),r=Y(r,100),a=Y(a,100),e=it(c,r,a),g=!0,d="hsl"),X(o.h)&&X(o.w)&&X(o.b)&&(({h:c,w:i,b:l}=o),c=Y(c,360),i=Y(i,100),l=Y(l,100),e=ct(c,i,l),g=!0,d="hwb"),X(o.a)&&(n=o.a,n=J(""+n)||parseFloat(n)>1?Y(n,100):n)),void 0===o&&(g=!0),{ok:g,format:d,r:e.r,g:e.g,b:e.b,a:Z(n)}}class ft{constructor(t,e){let o=t;const n=e&&V.includes(e)?e:"";o instanceof ft&&(o=bt(o));const{r:r,g:s,b:a,a:i,ok:l,format:c}=bt(o);this.originalInput=t,this.r=r,this.g=s,this.b=a,this.a=i,this.ok=l,this.format=n||c}get isValid(){return this.ok}get isDark(){return this.brightness<120}get luminance(){const{r:t,g:e,b:o}=this;let n=0,r=0,s=0;return n=t<=.03928?t/12.92:((t+.055)/1.055)**2.4,r=e<=.03928?e/12.92:((e+.055)/1.055)**2.4,s=o<=.03928?o/12.92:((o+.055)/1.055)**2.4,.2126*n+.7152*r+.0722*s}get brightness(){const{r:t,g:e,b:o}=this.toRgb();return(299*t+587*e+114*o)/1e3}toRgb(){let{r:t,g:e,b:o,a:n}=this;return[t,e,o]=[t,e,o].map(t=>I(255*t*100)/100),n=I(100*n)/100,{r:t,g:e,b:o,a:n}}toRgbString(){const{r:t,g:e,b:o,a:n}=this.toRgb(),[r,s,a]=[t,e,o].map(I);return 1===n?`rgb(${r}, ${s}, ${a})`:`rgba(${r}, ${s}, ${a}, ${n})`}toRgbCSS4String(){const{r:t,g:e,b:o,a:n}=this.toRgb(),[r,s,a]=[t,e,o].map(I);return`rgb(${r} ${s} ${a}${1===n?"":` / ${I(100*n)}%`})`}toHex(t){const{r:e,g:o,b:n,a:r}=this.toRgb();return 1===r?gt(e,o,n,t):pt(e,o,n,r,t)}toHexString(t){return"#"+this.toHex(t)}toHex8(t){const{r:e,g:o,b:n,a:r}=this.toRgb();return pt(e,o,n,r,t)}toHex8String(t){return"#"+this.toHex8(t)}toHsv(){const{r:t,g:e,b:o,a:n}=this,{h:r,s:s,v:a}=ut(t,e,o);return{h:r,s:s,v:a,a:n}}toHsl(){const{r:t,g:e,b:o,a:n}=this,{h:r,s:s,l:a}=st(t,e,o);return{h:r,s:s,l:a,a:n}}toHslString(){let{h:t,s:e,l:o,a:n}=this.toHsl();return t=I(360*t),e=I(100*e),o=I(100*o),n=I(100*n)/100,1===n?`hsl(${t}, ${e}%, ${o}%)`:`hsla(${t}, ${e}%, ${o}%, ${n})`}toHslCSS4String(){let{h:t,s:e,l:o,a:n}=this.toHsl();t=I(360*t),e=I(100*e),o=I(100*o),n=I(100*n);return`hsl(${t}deg ${e}% ${o}%${n<100?` / ${I(n)}%`:""})`}toHwb(){const{r:t,g:e,b:o,a:n}=this,{h:r,w:s,b:a}=lt(t,e,o);return{h:r,w:s,b:a,a:n}}toHwbString(){let{h:t,w:e,b:o,a:n}=this.toHwb();t=I(360*t),e=I(100*e),o=I(100*o),n=I(100*n);return`hwb(${t}deg ${e}% ${o}%${n<100?` / ${I(n)}%`:""})`}setAlpha(t){return"number"!=typeof t||(this.a=Z(t)),this}saturate(t){if("number"!=typeof t)return this;const{h:e,s:o,l:n}=this.toHsl(),{r:r,g:s,b:a}=it(e,Q(o+t/100),n);return k(this,{r:r,g:s,b:a}),this}desaturate(t){return"number"==typeof t?this.saturate(-t):this}greyscale(){return this.saturate(-100)}lighten(t){if("number"!=typeof t)return this;const{h:e,s:o,l:n}=this.toHsl(),{r:r,g:s,b:a}=it(e,o,Q(n+t/100));return k(this,{r:r,g:s,b:a}),this}darken(t){return"number"==typeof t?this.lighten(-t):this}spin(t){if("number"!=typeof t)return this;const{h:e,s:o,l:n}=this.toHsl(),{r:r,g:s,b:a}=it(Q((360*e+t)%360/360),o,n);return k(this,{r:r,g:s,b:a}),this}clone(){return new ft(this)}toString(t){const{format:e}=this;return"hex"===e?this.toHexString(t):"hsl"===e?this.toHslString():"hwb"===e?this.toHwbString():this.toRgbString()}}k(ft,{ANGLES:"deg|rad|grad|turn",CSS_ANGLE:U,CSS_INTEGER:"[-\\+]?\\d+%?",CSS_NUMBER:"[-\\+]?\\d*\\.\\d+%?",CSS_UNIT:B,CSS_UNIT2:_,PERMISSIVE_MATCH:z,matchers:W,isOnePointZero:G,isPercentage:J,isValidCSSUnit:X,isColorName:q,pad2:rt,clamp01:Q,bound01:Y,boundAlpha:Z,getRGBFromName:tt,convertHexToDecimal:ot,convertDecimalToHex:et,rgbToHsl:st,rgbToHex:gt,rgbToHsv:ut,rgbToHwb:lt,rgbaToHex:pt,hslToRgb:it,hsvToRgb:ht,hueToRgb:at,hwbToRgb:ct,parseIntFromHex:nt,stringInputToObject:dt,inputToRGB:bt,roundPart:I,getElementStyle:d,setElementStyle:A,ObjectAssign:k});class mt{constructor(...t){let e=0,o=12,n=10,r=[.5];if(3===t.length)[e,o,n]=t;else if(2===t.length&&([o,n]=t,[o,n].some(t=>t<1)))throw TypeError("ColorPalette: both arguments must be higher than 0.");const s=[],a=360/o,i=I((n-(n%2?1:0))/2),l=[[1,2,3],[4,5],[6,7],[8,9],[10,11],[12,13]],c=l.find(t=>t.includes(n)),u=c?[.25,.2,.15,.11,.09,.075][l.indexOf(c)]:100/(n+(n%2?0:1))/100;for(let t=1;t<i+1;t+=1)r=[...r,.5+u*t];for(let t=1;t<n-i;t+=1)r=[.5-u*t,...r];for(let t=0;t<o;t+=1){const o=(e+t*a)%360/360;r.forEach(t=>{s.push(new ft({h:o,s:1,l:t}))})}this.hue=e,this.hueSteps=o,this.lightSteps=n,this.colors=s}}k(mt,{Color:ft});const vt={pickerLabel:"Colour Picker",appearanceLabel:"Colour Appearance",valueLabel:"Colour Value",toggleLabel:"Select Colour",presetsLabel:"Colour Presets",defaultsLabel:"Colour Defaults",formatLabel:"Format",alphaLabel:"Alpha",hexLabel:"Hexadecimal",hueLabel:"Hue",whitenessLabel:"Whiteness",blacknessLabel:"Blackness",saturationLabel:"Saturation",lightnessLabel:"Lightness",redLabel:"Red",greenLabel:"Green",blueLabel:"Blue"},wt=["white","black","grey","red","orange","brown","gold","olive","yellow","lime","green","teal","cyan","blue","violet","magenta","pink"];function xt(t){try{JSON.parse(t)}catch(t){return!1}return!0}const $t=t=>t.toUpperCase();function yt(t){if("string"==typeof t)return g().createElement(t);const{tagName:e}=t,o={...t},n=yt(e);return delete o.tagName,k(n,o),n}function kt(t,e){if("string"==typeof e)return g().createElementNS(t,e);const{tagName:o}=e,n={...e},r=kt(t,o);return delete n.tagName,k(r,n),r}function St(t,e,o){const{input:n,format:r,componentLabels:s}=t,{defaultsLabel:a,presetsLabel:i}=s,l="color-options"===o,c=e instanceof mt,u=l?i:a,h=c?e.colors:e,g=h.length,{lightSteps:p}=c?e:{lightSteps:null},d=p||[9,10].find(t=>g>=2*t&&!(g%t))||5,b=l&&g>d;let f=2;f=b&&g>2*d?3:f,f=b&&g>3*d?4:f,f=b&&g>4*d?5:f;const m=f-(g<=3*d?1:2),v=b&&g>m*d;let w=o;w+=v?" scrollable":"",w+=b?" multiline":"";const x=b?"1px":"0.25rem";let $=b?1.75:2;$=d>5&&b?1.5:$;const y=m*$+"rem",k=`calc(${f} * ${$}rem + ${f-1} * ${x})`,S=yt({tagName:"ul",className:w});var H,E;return F(S,"role","listbox"),F(S,"aria-label",u),v&&(H=S,N(E={"--grid-item-size":$+"rem","--grid-fit":d,"--grid-gap":x,"--grid-height":y,"--grid-hover-height":k}).forEach(t=>{H.style.setProperty(t,E[t])})),h.forEach(t=>{let[e,o]="string"==typeof t?t.trim().split(":"):[];t instanceof ft&&(e=t.toHexString(),o=e);const s=new ft(t instanceof ft?t:e,r).toString()===L(n,"value"),a=yt({tagName:"li",className:"color-option"+(s?" active":""),innerText:""+(o||e)});F(a,"tabindex","0"),F(a,"data-value",""+e),F(a,"role","option"),F(a,"aria-selected",s?"true":"false"),l&&A(a,{backgroundColor:e}),S.append(a)}),S}function Ht(t){const{input:e,parent:o,format:n,id:r,componentLabels:s,colorKeywords:a,colorPresets:i}=t,l=L(e,"value")||"#fff",{toggleLabel:c,pickerLabel:u,formatLabel:h,hexLabel:g}=s,p=O.includes(l)?"#fff":l;t.color=new ft(p,n);const d="hex"===n?g:$t(n),b=yt({id:"picker-btn-"+r,tagName:"button",className:"picker-toggle btn-appearance"});F(b,"aria-expanded","false"),F(b,"aria-haspopup","true"),b.append(yt({tagName:"span",className:"v-hidden",innerText:`${u}. ${h}: ${d}`}));const f=yt({tagName:"div",className:"color-dropdown picker"});F(f,"aria-labelledby","picker-btn-"+r),F(f,"role","group");const m=function(t){const{format:e,componentLabels:o}=t,{hueLabel:n,alphaLabel:r,lightnessLabel:s,saturationLabel:a,whitenessLabel:i,blacknessLabel:l}=o,c="hsl"===e?360:100,u="hsl"===e?100:360;let h="hsl"===e?`${n} & ${s}`:`${s} & ${a}`;h="hwb"===e?`${i} & ${l}`:h;const g="hsl"===e?""+a:""+n,p=yt({tagName:"div",className:"color-controls "+e});return[{i:1,c:"color-pointer",l:h,min:0,max:c},{i:2,c:"color-slider",l:g,min:0,max:u},{i:3,c:"color-slider",l:r,min:0,max:100}].forEach(t=>{const{i:e,c:o,l:n,min:r,max:s}=t,a=yt({tagName:"div",className:"color-control"});F(a,"role","presentation"),a.append(yt({tagName:"div",className:"visual-control visual-control"+e}));const i=yt({tagName:"div",className:o+" knob",ariaLive:"polite"});F(i,"aria-label",n),F(i,"role","slider"),F(i,"tabindex","0"),F(i,"aria-valuemin",""+r),F(i,"aria-valuemax",""+s),a.append(i),p.append(a)}),p}(t),v=function(t){const{format:e,id:o,componentLabels:n}=t,r=yt({tagName:"div",className:"color-form "+e});let s=["hex"];return"rgb"===e?s=["red","green","blue","alpha"]:"hsl"===e?s=["hue","saturation","lightness","alpha"]:"hwb"===e&&(s=["hue","whiteness","blackness","alpha"]),s.forEach(t=>{const[s]="hex"===e?["#"]:$t(t).split(""),a=`color_${e}_${t}_${o}`,i=n[t+"Label"],l=yt({tagName:"label"});F(l,"for",a),l.append(yt({tagName:"span",ariaHidden:"true",innerText:s+":"}),yt({tagName:"span",className:"v-hidden",innerText:i}));const c=yt({tagName:"input",id:a,type:"hex"===e?"text":"number",value:"alpha"===t?"100":"0",className:"color-input "+t});F(c,"autocomplete","off"),F(c,"spellcheck","false");let u="100",h="1";"alpha"!==t&&("rgb"===e?(u="255",h="1"):"hue"===t&&(u="360",h="1")),k(c,{min:"0",max:u,step:h}),r.append(l,c)}),r}(t);if(f.append(m,v),e.before(b),o.append(f),a||i){const e=yt({tagName:"div",className:"color-dropdown scrollable menu"});i&&e.append(St(t,i,"color-options")),a&&a.length&&e.append(St(t,a,"color-defaults"));const n=yt({tagName:"button",className:"menu-toggle btn-appearance"});F(n,"tabindex","-1"),F(n,"aria-expanded","false"),F(n,"aria-haspopup","true");const r=encodeURI("http://www.w3.org/2000/svg"),s=kt(r,{tagName:"svg"});F(s,"xmlns",r),F(s,"viewBox","0 0 512 512"),F(s,"aria-hidden","true");const l=kt(r,{tagName:"path"});F(l,"d","M98,158l157,156L411,158l27,27L255,368L71,185L98,158z"),F(l,"fill","#fff"),s.append(l),n.append(yt({tagName:"span",className:"v-hidden",innerText:""+c}),s),o.append(n,e)}a&&O.includes(l)&&(t.value=l),F(e,"tabindex","-1")}const At={componentLabels:vt,colorLabels:wt,format:"rgb",colorPresets:!1,colorKeywords:!1};function Lt(t,e){const r=e?o:n,{input:s,pickerToggle:a,menuToggle:i}=t;r(s,"focusin",t.showPicker),r(a,"click",t.togglePicker),i&&r(i,"click",t.toggleMenu)}function Et(t,e){const r=e?o:n,{input:s,colorMenu:a,parent:i}=t,l=g(s),c=l.defaultView;r(t.controls,"pointerdown",t.pointerDown),t.controlKnobs.forEach(e=>r(e,"keydown",t.handleKnobs)),r(c,"scroll",t.handleScroll),r(c,"resize",t.update),[s,...t.inputs].forEach(e=>r(e,"change",t.changeHandler)),a&&(r(a,"click",t.menuClickHandler),r(a,"keydown",t.menuKeyHandler)),r(l,h,t.pointerMove),r(l,"pointerup",t.pointerUp),r(i,"focusout",t.handleFocusOut),r(l,"keyup",t.handleDismiss)}function Nt(t){var e,o;e=t.input,o=new CustomEvent("colorpicker.change"),e.dispatchEvent(o)}function Pt(t){t&&["bottom","top"].forEach(e=>R(t,e))}function Ct(t,e){const{colorPicker:o,colorMenu:n,menuToggle:r,pickerToggle:s,parent:a}=t,i=e===o,l=i?n:o,c=i?r:s,u=i?s:r;M(a,"open")||T(a,"open"),l&&(R(l,"show"),Pt(l)),T(e,"bottom"),e.offsetHeight,T(e,"show"),i&&t.update(),t.isOpen||(Et(t,!0),t.updateDropdownPosition(),t.isOpen=!0,F(t.input,"tabindex","0"),r&&F(r,"tabindex","0")),F(u,"aria-expanded","true"),c&&F(c,"aria-expanded","false")}class Mt{constructor(t,e){const o=this,n=$(t);if(!n)throw new TypeError(`ColorPicker target "${t}" cannot be found.`);o.input=n;const r=function t(e,o){return e?e.closest(o)||t(e.getRootNode().host,o):null}(n,".color-picker,color-picker");if(!r)throw new TypeError("ColorPicker requires a specific markup to work.");o.parent=r,o.id=function t(e,o){let n=o?b:f;if(o){const r=t(e),s=m.get(r)||new Map;m.has(r)||m.set(r,s),s.has(o)?n=s.get(o):(s.set(o,n),b+=1)}else{const t=e.id||e;m.has(t)?n=m.get(t):(m.set(t,n),f+=1)}return n}(n,"color-picker"),o.dragElement=null,o.isOpen=!1,o.controlPositions={c1x:0,c1y:0,c2y:0,c3y:0},o.colorLabels={},o.colorKeywords=void 0,o.colorPresets=void 0;const{format:s,componentLabels:a,colorLabels:i,colorKeywords:l,colorPresets:c}=function(t,e,o,n){const r={...t.dataset},s={},a={};return N(r).forEach(t=>{const e=n&&t.includes(n)?t.replace(n,"").replace(/[A-Z]/,t=>P(t)):t;a[e]=E(r[t])}),N(o).forEach(t=>{o[t]=E(o[t])}),N(e).forEach(n=>{s[n]=n in o?o[n]:n in a?a[n]:"title"===n?L(t,"title"):e[n]}),s}(this.isCE?r:n,At,e||{});let u=wt;i instanceof Array&&17===i.length?u=i:i&&17===i.split(",").length&&(u=i.split(",")),wt.forEach((t,e)=>{o.colorLabels[t]=u[e].trim()});const h=a&&xt(a)?JSON.parse(a):a;if(o.componentLabels=k({...vt},h),o.color=new ft(n.value||"#fff",s),o.format=s,l instanceof Array&&l.length?o.colorKeywords=l:"string"==typeof l&&l.length&&(o.colorKeywords=l.split(",").map(t=>t.trim())),c instanceof Array&&c.length)o.colorPresets=c;else if("string"==typeof c&&c.length)if(xt(c)){const{hue:t,hueSteps:e,lightSteps:n}=JSON.parse(c);o.colorPresets=new mt(t,e,n)}else o.colorPresets=c.split(",").map(t=>t.trim());o.showPicker=o.showPicker.bind(o),o.togglePicker=o.togglePicker.bind(o),o.toggleMenu=o.toggleMenu.bind(o),o.menuClickHandler=o.menuClickHandler.bind(o),o.menuKeyHandler=o.menuKeyHandler.bind(o),o.pointerDown=o.pointerDown.bind(o),o.pointerMove=o.pointerMove.bind(o),o.pointerUp=o.pointerUp.bind(o),o.update=o.update.bind(o),o.handleScroll=o.handleScroll.bind(o),o.handleFocusOut=o.handleFocusOut.bind(o),o.changeHandler=o.changeHandler.bind(o),o.handleDismiss=o.handleDismiss.bind(o),o.handleKnobs=o.handleKnobs.bind(o),Ht(o);const[g,p]=y("color-dropdown",r);o.pickerToggle=$(".picker-toggle",r),o.menuToggle=$(".menu-toggle",r),o.colorPicker=g,o.colorMenu=p,o.inputs=[...y("color-input",r)];const[d]=y("color-controls",r);o.controls=d,o.controlKnobs=[...y("knob",d)],o.visuals=[...y("visual-control",d)],o.update(),Lt(o,!0),H.set(n,"color-picker",o)}get value(){return this.input.value}set value(t){this.input.value=t}get hasNonColor(){return this.colorKeywords instanceof Array&&this.colorKeywords.some(t=>O.includes(t))}get isCE(){return"color-picker"===this.parent.localName}get hex(){return this.color.toHex(!0)}get hsv(){return this.color.toHsv()}get hsl(){return this.color.toHsl()}get hwb(){return this.color.toHwb()}get rgb(){return this.color.toRgb()}get brightness(){return this.color.brightness}get luminance(){return this.color.luminance}get isDark(){const{color:t,brightness:e}=this;return e<120&&t.a>.33}get isValid(){const t=this.input.value;return""!==t&&new ft(t).isValid}get appearance(){const{colorLabels:t,hsl:e,hsv:o,format:n}=this,r=I(360*e.h),s=I(100*("hsl"===n?e.s:o.s)),a=I(100*e.l),i=100*o.v;let l;if(100===a&&0===s)l=t.white;else if(0===a)l=t.black;else if(0===s)l=t.grey;else if(r<15||r>=345)l=t.red;else if(r>=15&&r<45)l=i>80&&s>80?t.orange:t.brown;else if(r>=45&&r<75){const e=r>=54&&r<75&&i<80;l=r>46&&r<54&&i<80&&s>90?t.gold:t.yellow,l=e?t.olive:l}else r>=75&&r<155?l=i<68?t.green:t.lime:r>=155&&r<175?l=t.teal:r>=175&&r<195?l=t.cyan:r>=195&&r<255?l=t.blue:r>=255&&r<270?l=t.violet:r>=270&&r<295?l=t.magenta:r>=295&&r<345&&(l=t.pink);return l}updateVisuals(){const{controlPositions:t,visuals:e}=this,[o,n,r]=e,{offsetHeight:s}=o,a=t.c2y/s,{r:i,g:l,b:c}=new ft({h:a,s:1,l:.5}).toRgb(),u=1-t.c3y/s,h=I(100*u)/100,g=new ft({h:a,s:1,l:.5,a:u}).toRgbString();A(o,{background:`linear-gradient(rgba(0,0,0,0) 0%, rgba(0,0,0,${h}) 100%),\n linear-gradient(to right, rgba(255,255,255,${h}) 0%, ${g} 100%),\n linear-gradient(rgb(255,255,255) 0%, rgb(255,255,255) 100%)`}),A(n,{background:"linear-gradient(\n rgb(255,0,0) 0%, rgb(255,255,0) 16.67%,\n rgb(0,255,0) 33.33%, rgb(0,255,255) 50%,\n rgb(0,0,255) 66.67%, rgb(255,0,255) 83.33%,\n rgb(255,0,0) 100%)"}),A(r,{background:`linear-gradient(rgba(${i},${l},${c},1) 0%,rgba(${i},${l},${c},0) 100%)`})}handleFocusOut({relatedTarget:t}){t&&!this.parent.contains(t)&&this.hide(!0)}handleDismiss({code:t}){const e=this;e.isOpen&&"Escape"===t&&e.hide()}handleScroll(t){const{activeElement:e}=g(this.input);this.updateDropdownPosition(),([h,u].includes(t.type)&&this.dragElement||e&&this.controlKnobs.includes(e))&&(t.stopPropagation(),t.preventDefault())}menuKeyHandler(t){const{target:e,code:o}=t,{previousElementSibling:n,nextElementSibling:u,parentElement:h}=e,g=h&&M(h,"color-options"),p=[...h.children],b=g&&d(h,"grid-template-columns").split(" ").length,f=p.indexOf(e),m=f>-1&&b&&p[f-b],v=f>-1&&b&&p[f+b];[r,s,c].includes(o)&&t.preventDefault(),g?m&&o===s?C(m):v&&o===r?C(v):n&&o===a?C(n):u&&o===i&&C(u):n&&[a,s].includes(o)?C(n):u&&[i,r].includes(o)&&C(u),[l,c].includes(o)&&this.menuClickHandler({target:e})}menuClickHandler(t){const e=this,{target:o}=t,{colorMenu:n}=e,r=(L(o,"data-value")||"").trim();if(!r.length)return;const s=$("li.active",n);let a=O.includes(r)?"white":r;a="transparent"===r?"rgba(0,0,0,0)":r;const{r:i,g:l,b:c,a:u}=new ft(a);k(e.color,{r:i,g:l,b:c,a:u}),e.update(),s!==o&&(s&&(R(s,"active"),D(s,"aria-selected")),T(o,"active"),F(o,"aria-selected","true"),O.includes(r)&&(e.value=r),Nt(e))}pointerDown(t){const e=this,{target:o,pageX:n,pageY:r}=t,{colorMenu:s,visuals:a,controlKnobs:i}=e,[l,c,u]=a,[h,g,d]=i,b=i.includes(o)?o.previousElementSibling:o,f=v(b),m=p(l),w=n-m.scrollLeft-f.left,x=r-m.scrollTop-f.top;if(o===l||o===h?(e.dragElement=b,e.changeControl1(w,x)):o===c||o===g?(e.dragElement=b,e.changeControl2(x)):o!==u&&o!==d||(e.dragElement=b,e.changeAlpha(x)),s){const t=$("li.active",s);t&&(R(t,"active"),D(t,"aria-selected"))}t.preventDefault()}pointerUp({target:t}){const e=this,{parent:o}=e,n=g(o),r=null!==$(".color-picker,color-picker.open",n),s=n.getSelection();e.dragElement||s.toString().length||o.contains(t)||e.hide(r),e.dragElement=null}pointerMove(t){const e=this,{dragElement:o,visuals:n}=e,[r,s,a]=n,{pageX:i,pageY:l}=t;if(!o)return;const c=v(o),u=p(r),h=i-u.scrollLeft-c.left,g=l-u.scrollTop-c.top;o===r&&e.changeControl1(h,g),o===s&&e.changeControl2(g),o===a&&e.changeAlpha(g)}handleKnobs(t){const{target:e,code:o}=t,n=this;if(![s,r,a,i].includes(o))return;t.preventDefault();const{controlKnobs:l,visuals:c}=n,{offsetWidth:u,offsetHeight:h}=c[0],[p,d,b]=l,{activeElement:f}=g(p),m=h/360;if(l.find(t=>t===f)){let l=0,c=0;if(e===p){const t=u/100;[a,i].includes(o)?n.controlPositions.c1x+=o===i?t:-t:[s,r].includes(o)&&(n.controlPositions.c1y+=o===r?m:-m),l=n.controlPositions.c1x,c=n.controlPositions.c1y,n.changeControl1(l,c)}else e===d?(n.controlPositions.c2y+=[r,i].includes(o)?m:-m,c=n.controlPositions.c2y,n.changeControl2(c)):e===b&&(n.controlPositions.c3y+=[r,i].includes(o)?m:-m,c=n.controlPositions.c3y,n.changeAlpha(c));n.handleScroll(t)}}changeHandler(){const t=this;let e;const{inputs:o,format:n,value:r,input:s,controlPositions:a,visuals:i}=t,{activeElement:l}=g(s),{offsetHeight:c}=i[0],[u,,,h]=o,[p,d,b,f]="rgb"===n?o.map(t=>parseFloat(t.value)/(t===h?100:1)):o.map(t=>parseFloat(t.value)/(t!==u?100:360)),m=t.hasNonColor&&O.includes(r),v=h?f:1-a.c3y/c;if(l===s||l&&o.includes(l)){e=l===s?m?"transparent"===r?"rgba(0,0,0,0)":"rgb(0,0,0)":r:"hex"===n?u.value:"hsl"===n?{h:p,s:d,l:b,a:v}:"hwb"===n?{h:p,w:d,b:b,a:v}:{r:p,g:d,b:b,a:v};const{r:o,g:a,b:i,a:c}=new ft(e);k(t.color,{r:o,g:a,b:i,a:c}),t.setControlPositions(),t.updateAppearance(),t.updateInputs(),t.updateControls(),t.updateVisuals(),l===s&&m&&(t.value=r)}}changeControl1(t,e){let[o,n]=[0,0];const{controlPositions:r,visuals:s}=this,{offsetHeight:a,offsetWidth:i}=s[0];t>i?o=i:t>=0&&(o=t),e>a?n=a:e>=0&&(n=e);const l=r.c2y/a,c=o/i,u=1-n/a,h=1-r.c3y/a,{r:g,g:p,b:d,a:b}=new ft({h:l,s:c,v:u,a:h});k(this.color,{r:g,g:p,b:d,a:b}),this.controlPositions.c1x=o,this.controlPositions.c1y=n,this.updateAppearance(),this.updateInputs(),this.updateControls(),this.updateVisuals()}changeControl2(t){const{controlPositions:e,visuals:o}=this,{offsetHeight:n,offsetWidth:r}=o[0];let s=0;t>n?s=n:t>=0&&(s=t);const a=s/n,i=e.c1x/r,l=1-e.c1y/n,c=1-e.c3y/n,{r:u,g:h,b:g,a:p}=new ft({h:a,s:i,v:l,a:c});k(this.color,{r:u,g:h,b:g,a:p}),this.controlPositions.c2y=s,this.updateAppearance(),this.updateInputs(),this.updateControls(),this.updateVisuals()}changeAlpha(t){const{visuals:e}=this,{offsetHeight:o}=e[0];let n=0;t>o?n=o:t>=0&&(n=t);const r=1-n/o;this.color.setAlpha(r),this.controlPositions.c3y=n,this.updateAppearance(),this.updateInputs(),this.updateControls(),this.updateVisuals()}update(){this.updateDropdownPosition(),this.updateAppearance(),this.setControlPositions(),this.updateInputs(!0),this.updateControls(),this.updateVisuals()}updateDropdownPosition(){const{input:t,colorPicker:e,colorMenu:o}=this,n=v(t),{top:r,bottom:s}=n,{offsetHeight:a}=t,i=p(t).clientHeight,l=M(e,"show")?e:o;if(!l)return;const{offsetHeight:c}=l,u=i-s,h=r,g=r+c+a>i,d=r-c<0;(M(l,"bottom")||!d)&&u<h&&g?(R(l,"bottom"),T(l,"top")):(R(l,"top"),T(l,"bottom"))}setControlPositions(){const{visuals:t,color:e,hsv:o}=this,{offsetHeight:n,offsetWidth:r}=t[0],s=e.a,a=o.h,i=o.s,l=o.v;this.controlPositions.c1x=i*r,this.controlPositions.c1y=(1-l)*n,this.controlPositions.c2y=a*n,this.controlPositions.c3y=(1-s)*n}updateAppearance(){const t=this,{componentLabels:e,color:o,parent:n,hsv:r,hex:s,format:a,controlKnobs:i}=t,{appearanceLabel:l,hexLabel:c,valueLabel:u}=e;let{r:h,g:g,b:p}=o.toRgb();const[d,b,f]=i,m=I(360*r.h),v=o.a,w=I(100*r.s),x=I(100*r.v),$=t.appearance;let y=`${c} ${s.split("").join(" ")}`;if("hwb"===a){const{hwb:e}=t,o=I(100*e.w),n=I(100*e.b);y=`HWB: ${m}°, ${o}%, ${n}%`,F(d,"aria-valuetext",`${o}% & ${n}%`),F(d,"aria-valuenow",""+o),F(b,"aria-description",`${u}: ${y}. ${l}: ${$}.`),F(b,"aria-valuetext",m+"%"),F(b,"aria-valuenow",""+m)}else[h,g,p]=[h,g,p].map(I),y="hsl"===a?`HSL: ${m}°, ${w}%, ${x}%`:y,y="rgb"===a?`RGB: ${h}, ${g}, ${p}`:y,F(d,"aria-valuetext",`${x}% & ${w}%`),F(d,"aria-valuenow",""+x),F(b,"aria-description",`${u}: ${y}. ${l}: ${$}.`),F(b,"aria-valuetext",m+"°"),F(b,"aria-valuenow",""+m);const k=I(100*v);F(f,"aria-valuetext",k+"%"),F(f,"aria-valuenow",""+k);const S=o.toString();A(t.input,{backgroundColor:S}),t.isDark?(M(n,"txt-light")&&R(n,"txt-light"),M(n,"txt-dark")||T(n,"txt-dark")):(M(n,"txt-dark")&&R(n,"txt-dark"),M(n,"txt-light")||T(n,"txt-light"))}updateControls(){const{controlKnobs:t,controlPositions:e}=this;let{c1x:o,c1y:n,c2y:r,c3y:s}=e;const[a,i,l]=t;[o,n,r,s]=[o,n,r,s].map(I),A(a,{transform:`translate3d(${o-4}px,${n-4}px,0)`}),A(i,{transform:`translate3d(0,${r-4}px,0)`}),A(l,{transform:`translate3d(0,${s-4}px,0)`})}updateInputs(t){const e=this,{value:o,format:n,inputs:r,color:s,hsl:a}=e,[i,l,c,u]=r,h=I(100*s.a),g=I(360*a.h);let p;if("hex"===n)p=e.color.toHexString(!0),i.value=e.hex;else if("hsl"===n){const t=I(100*a.l),o=I(100*a.s);p=e.color.toHslString(),i.value=""+g,l.value=""+o,c.value=""+t,u.value=""+h}else if("hwb"===n){const{w:t,b:o}=e.hwb,n=I(100*t),r=I(100*o);p=e.color.toHwbString(),i.value=""+g,l.value=""+n,c.value=""+r,u.value=""+h}else if("rgb"===n){let{r:t,g:o,b:n}=e.rgb;[t,o,n]=[t,o,n].map(I),p=e.color.toRgbString(),i.value=""+t,l.value=""+o,c.value=""+n,u.value=""+h}e.value=""+p,t||p===o||Nt(e)}togglePicker(t){t&&t.preventDefault();const e=this,{colorPicker:o}=e;e.isOpen&&M(o,"show")?e.hide(!0):Ct(e,o)}showPicker(){const t=this,{colorPicker:e}=t;["top","bottom"].some(t=>M(e,t))||Ct(t,e)}toggleMenu(t){t&&t.preventDefault();const e=this,{colorMenu:o}=e;e.isOpen&&M(o,"show")?e.hide(!0):Ct(e,o)}hide(t){const e=this;if(e.isOpen){const{pickerToggle:o,menuToggle:n,colorPicker:r,colorMenu:s,parent:a,input:i}=e,l=M(r,"show"),c=l?r:s,u=l?o:n,h=c&&function(t){const e=d(t,"transitionProperty"),o=d(t,"transitionDuration"),n=o.includes("ms")?1:1e3,r=e&&"none"!==e?parseFloat(o)*n:0;return Number.isNaN(r)?0:r}(c);e.value=e.color.toString(!0),c&&(R(c,"show"),F(u,"aria-expanded","false"),setTimeout(()=>{Pt(c),$(".show",a)||(R(a,"open"),Et(e),e.isOpen=!1)},h)),t||C(o),F(i,"tabindex","-1"),u===n&&F(n,"tabindex","-1")}}dispose(){const{input:t,parent:e}=this;this.hide(!0),Lt(this),[...e.children].forEach(e=>{e!==t&&e.remove()}),D(t,"tabindex"),A(t,{backgroundColor:""}),["txt-light","txt-dark"].forEach(t=>R(e,t)),H.remove(t,"color-picker")}}k(Mt,{Color:ft,ColorPalette:mt,Version:"1.0.0",getInstance:t=>{return e=t,o="color-picker",H.get(e,o);var e,o},init:t=>new Mt(t),selector:'[data-function="color-picker"]',roundPart:I,setElementStyle:A,setAttribute:F,getBoundingClientRect:v});export{Mt as default};
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * ColorPicker v0.0.2alpha4 (http://thednp.github.io/color-picker)
2
+ * ColorPicker v1.0.0 (http://thednp.github.io/color-picker)
3
3
  * Copyright 2022 © thednp
4
4
  * Licensed under MIT (https://github.com/thednp/color-picker/blob/master/LICENSE)
5
5
  */
@@ -15,27 +15,26 @@
15
15
  /**
16
16
  * The global event listener.
17
17
  *
18
- * @this {Element | HTMLElement | Window | Document}
19
- * @param {Event} e
20
- * @returns {void}
18
+ * @type {EventListener}
19
+ * @this {EventTarget}
21
20
  */
22
21
  function globalListener(e) {
23
22
  const that = this;
24
- const { type } = e;
25
- const oneEvMap = EventRegistry[type] ? [...EventRegistry[type]] : [];
23
+ const { type, target } = e;
26
24
 
27
- oneEvMap.forEach((elementsMap) => {
25
+ [...EventRegistry[type]].forEach((elementsMap) => {
28
26
  const [element, listenersMap] = elementsMap;
29
- [...listenersMap].forEach((listenerMap) => {
30
- if (element === that) {
27
+ /* istanbul ignore else */
28
+ if ([target, that].some((el) => element === el)) {
29
+ [...listenersMap].forEach((listenerMap) => {
31
30
  const [listener, options] = listenerMap;
32
31
  listener.apply(element, [e]);
33
32
 
34
33
  if (options && options.once) {
35
34
  removeListener(element, type, listener, options);
36
35
  }
37
- }
38
- });
36
+ });
37
+ }
39
38
  });
40
39
  }
41
40
 
@@ -43,10 +42,7 @@
43
42
  * Register a new listener with its options and attach the `globalListener`
44
43
  * to the target if this is the first listener.
45
44
  *
46
- * @param {Element | HTMLElement | Window | Document} element
47
- * @param {string} eventType
48
- * @param {EventListenerObject['handleEvent']} listener
49
- * @param {AddEventListenerOptions=} options
45
+ * @type {Listener.ListenerAction<EventTarget>}
50
46
  */
51
47
  const addListener = (element, eventType, listener, options) => {
52
48
  // get element listeners first
@@ -64,9 +60,7 @@
64
60
  const { size } = oneElementMap;
65
61
 
66
62
  // register listener with its options
67
- if (oneElementMap) {
68
- oneElementMap.set(listener, options);
69
- }
63
+ oneElementMap.set(listener, options);
70
64
 
71
65
  // add listener last
72
66
  if (!size) {
@@ -78,10 +72,7 @@
78
72
  * Remove a listener from registry and detach the `globalListener`
79
73
  * if no listeners are found in the registry.
80
74
  *
81
- * @param {Element | HTMLElement | Window | Document} element
82
- * @param {string} eventType
83
- * @param {EventListenerObject['handleEvent']} listener
84
- * @param {AddEventListenerOptions=} options
75
+ * @type {Listener.ListenerAction<EventTarget>}
85
76
  */
86
77
  const removeListener = (element, eventType, listener, options) => {
87
78
  // get listener first
@@ -100,6 +91,7 @@
100
91
  if (!oneEventMap || !oneEventMap.size) delete EventRegistry[eventType];
101
92
 
102
93
  // remove listener last
94
+ /* istanbul ignore else */
103
95
  if (!oneElementMap || !oneElementMap.size) {
104
96
  element.removeEventListener(eventType, globalListener, eventOptions);
105
97
  }
@@ -201,12 +193,6 @@
201
193
  */
202
194
  const changeEvent = 'change';
203
195
 
204
- /**
205
- * A global namespace for `touchstart` event.
206
- * @type {string}
207
- */
208
- const touchstartEvent = 'touchstart';
209
-
210
196
  /**
211
197
  * A global namespace for `touchmove` event.
212
198
  * @type {string}
@@ -214,28 +200,22 @@
214
200
  const touchmoveEvent = 'touchmove';
215
201
 
216
202
  /**
217
- * A global namespace for `touchend` event.
218
- * @type {string}
219
- */
220
- const touchendEvent = 'touchend';
221
-
222
- /**
223
- * A global namespace for `mousedown` event.
203
+ * A global namespace for `pointerdown` event.
224
204
  * @type {string}
225
205
  */
226
- const mousedownEvent = 'mousedown';
206
+ const pointerdownEvent = 'pointerdown';
227
207
 
228
208
  /**
229
- * A global namespace for `mousemove` event.
209
+ * A global namespace for `pointermove` event.
230
210
  * @type {string}
231
211
  */
232
- const mousemoveEvent = 'mousemove';
212
+ const pointermoveEvent = 'pointermove';
233
213
 
234
214
  /**
235
- * A global namespace for `mouseup` event.
215
+ * A global namespace for `pointerup` event.
236
216
  * @type {string}
237
217
  */
238
- const mouseupEvent = 'mouseup';
218
+ const pointerupEvent = 'pointerup';
239
219
 
240
220
  /**
241
221
  * A global namespace for `scroll` event.
@@ -283,27 +263,6 @@
283
263
  return getDocument(node).documentElement;
284
264
  }
285
265
 
286
- /**
287
- * Returns the `Window` object of a target node.
288
- * @see https://github.com/floating-ui/floating-ui
289
- *
290
- * @param {(Node | HTMLElement | Element | Window)=} node target node
291
- * @returns {globalThis}
292
- */
293
- function getWindow(node) {
294
- if (node == null) {
295
- return window;
296
- }
297
-
298
- if (!(node instanceof Window)) {
299
- const { ownerDocument } = node;
300
- return ownerDocument ? ownerDocument.defaultView || window : window;
301
- }
302
-
303
- // @ts-ignore
304
- return node;
305
- }
306
-
307
266
  /**
308
267
  * Shortcut for `window.getComputedStyle(element).propertyName`
309
268
  * static method.
@@ -2252,17 +2211,16 @@
2252
2211
  const isOptionsMenu = menuClass === 'color-options';
2253
2212
  const isPalette = colorsSource instanceof ColorPalette;
2254
2213
  const menuLabel = isOptionsMenu ? presetsLabel : defaultsLabel;
2255
- let colorsArray = isPalette ? colorsSource.colors : colorsSource;
2256
- colorsArray = colorsArray instanceof Array ? colorsArray : [];
2214
+ const colorsArray = isPalette ? colorsSource.colors : colorsSource;
2257
2215
  const colorsCount = colorsArray.length;
2258
2216
  const { lightSteps } = isPalette ? colorsSource : { lightSteps: null };
2259
- const fit = lightSteps || [9, 10].find((x) => colorsCount > x * 2 && !(colorsCount % x)) || 5;
2217
+ const fit = lightSteps || [9, 10].find((x) => colorsCount >= x * 2 && !(colorsCount % x)) || 5;
2260
2218
  const isMultiLine = isOptionsMenu && colorsCount > fit;
2261
2219
  let rowCountHover = 2;
2262
- rowCountHover = isMultiLine && colorsCount >= fit * 2 ? 3 : rowCountHover;
2263
- rowCountHover = colorsCount >= fit * 3 ? 4 : rowCountHover;
2264
- rowCountHover = colorsCount >= fit * 4 ? 5 : rowCountHover;
2265
- const rowCount = rowCountHover - (colorsCount < fit * 3 ? 1 : 2);
2220
+ rowCountHover = isMultiLine && colorsCount > fit * 2 ? 3 : rowCountHover;
2221
+ rowCountHover = isMultiLine && colorsCount > fit * 3 ? 4 : rowCountHover;
2222
+ rowCountHover = isMultiLine && colorsCount > fit * 4 ? 5 : rowCountHover;
2223
+ const rowCount = rowCountHover - (colorsCount <= fit * 3 ? 1 : 2);
2266
2224
  const isScrollable = isMultiLine && colorsCount > rowCount * fit;
2267
2225
  let finalClass = menuClass;
2268
2226
  finalClass += isScrollable ? ' scrollable' : '';
@@ -2270,7 +2228,7 @@
2270
2228
  const gap = isMultiLine ? '1px' : '0.25rem';
2271
2229
  let optionSize = isMultiLine ? 1.75 : 2;
2272
2230
  optionSize = fit > 5 && isMultiLine ? 1.5 : optionSize;
2273
- const menuHeight = `${(rowCount || 1) * optionSize}rem`;
2231
+ const menuHeight = `${rowCount * optionSize}rem`;
2274
2232
  const menuHeightHover = `calc(${rowCountHover} * ${optionSize}rem + ${rowCountHover - 1} * ${gap})`;
2275
2233
  /** @type {HTMLUListElement} */
2276
2234
  // @ts-ignore -- <UL> is an `HTMLElement`
@@ -2377,16 +2335,14 @@
2377
2335
  });
2378
2336
 
2379
2337
  // color presets
2380
- if ((colorPresets instanceof Array && colorPresets.length)
2381
- || (colorPresets instanceof ColorPalette && colorPresets.colors)) {
2382
- const presetsMenu = getColorMenu(self, colorPresets, 'color-options');
2383
- presetsDropdown.append(presetsMenu);
2338
+ if (colorPresets) {
2339
+ presetsDropdown.append(getColorMenu(self, colorPresets, 'color-options'));
2384
2340
  }
2385
2341
 
2386
2342
  // explicit defaults [reset, initial, inherit, transparent, currentColor]
2343
+ // also custom defaults [default: #069, complementary: #930]
2387
2344
  if (colorKeywords && colorKeywords.length) {
2388
- const keywordsMenu = getColorMenu(self, colorKeywords, 'color-defaults');
2389
- presetsDropdown.append(keywordsMenu);
2345
+ presetsDropdown.append(getColorMenu(self, colorKeywords, 'color-defaults'));
2390
2346
  }
2391
2347
 
2392
2348
  const presetsBtn = createElement({
@@ -2423,7 +2379,7 @@
2423
2379
  setAttribute(input, tabIndex, '-1');
2424
2380
  }
2425
2381
 
2426
- var version = "0.0.2alpha4";
2382
+ var version = "1.0.0";
2427
2383
 
2428
2384
  // @ts-ignore
2429
2385
 
@@ -2445,7 +2401,7 @@
2445
2401
  // ColorPicker Static Methods
2446
2402
  // ==========================
2447
2403
 
2448
- /** @type {CP.GetInstance<ColorPicker>} */
2404
+ /** @type {CP.GetInstance<ColorPicker, HTMLInputElement>} */
2449
2405
  const getColorPickerInstance = (element) => getInstance(element, colorPickerString);
2450
2406
 
2451
2407
  /** @type {CP.InitCallback<ColorPicker>} */
@@ -2480,12 +2436,10 @@
2480
2436
  const fn = action ? addListener : removeListener;
2481
2437
  const { input, colorMenu, parent } = self;
2482
2438
  const doc = getDocument(input);
2483
- const win = getWindow(input);
2484
- const pointerEvents = `on${touchstartEvent}` in doc
2485
- ? { down: touchstartEvent, move: touchmoveEvent, up: touchendEvent }
2486
- : { down: mousedownEvent, move: mousemoveEvent, up: mouseupEvent };
2439
+ // const win = getWindow(input);
2440
+ const win = doc.defaultView;
2487
2441
 
2488
- fn(self.controls, pointerEvents.down, self.pointerDown);
2442
+ fn(self.controls, pointerdownEvent, self.pointerDown);
2489
2443
  self.controlKnobs.forEach((x) => fn(x, keydownEvent, self.handleKnobs));
2490
2444
 
2491
2445
  // @ts-ignore -- this is `Window`
@@ -2500,8 +2454,8 @@
2500
2454
  fn(colorMenu, keydownEvent, self.menuKeyHandler);
2501
2455
  }
2502
2456
 
2503
- fn(doc, pointerEvents.move, self.pointerMove);
2504
- fn(doc, pointerEvents.up, self.pointerUp);
2457
+ fn(doc, pointermoveEvent, self.pointerMove);
2458
+ fn(doc, pointerupEvent, self.pointerUp);
2505
2459
  fn(parent, focusoutEvent, self.handleFocusOut);
2506
2460
  fn(doc, keyupEvent, self.handleDismiss);
2507
2461
  }
@@ -2520,6 +2474,7 @@
2520
2474
  * @returns {void}
2521
2475
  */
2522
2476
  function removePosition(element) {
2477
+ /* istanbul ignore else */
2523
2478
  if (element) {
2524
2479
  ['bottom', 'top'].forEach((x) => removeClass(element, x));
2525
2480
  }
@@ -2622,6 +2577,7 @@
2622
2577
  } = normalizeOptions(this.isCE ? parent : input, colorPickerDefaults, config || {});
2623
2578
 
2624
2579
  let translatedColorLabels = colorNames;
2580
+ /* istanbul ignore else */
2625
2581
  if (colorLabels instanceof Array && colorLabels.length === 17) {
2626
2582
  translatedColorLabels = colorLabels;
2627
2583
  } else if (colorLabels && colorLabels.split(',').length === 17) {
@@ -2638,7 +2594,7 @@
2638
2594
  ? JSON.parse(componentLabels) : componentLabels;
2639
2595
 
2640
2596
  /** @type {Record<string, string>} */
2641
- self.componentLabels = ObjectAssign(colorPickerLabels, tempComponentLabels);
2597
+ self.componentLabels = ObjectAssign({ ...colorPickerLabels }, tempComponentLabels);
2642
2598
 
2643
2599
  /** @type {Color} */
2644
2600
  self.color = new Color(input.value || '#fff', format);
@@ -2647,14 +2603,14 @@
2647
2603
  self.format = format;
2648
2604
 
2649
2605
  // set colour defaults
2650
- if (colorKeywords instanceof Array) {
2606
+ if (colorKeywords instanceof Array && colorKeywords.length) {
2651
2607
  self.colorKeywords = colorKeywords;
2652
2608
  } else if (typeof colorKeywords === 'string' && colorKeywords.length) {
2653
2609
  self.colorKeywords = colorKeywords.split(',').map((x) => x.trim());
2654
2610
  }
2655
2611
 
2656
2612
  // set colour presets
2657
- if (colorPresets instanceof Array) {
2613
+ if (colorPresets instanceof Array && colorPresets.length) {
2658
2614
  self.colorPresets = colorPresets;
2659
2615
  } else if (typeof colorPresets === 'string' && colorPresets.length) {
2660
2616
  if (isValidJSON(colorPresets)) {
@@ -2785,6 +2741,7 @@
2785
2741
  let colorName;
2786
2742
 
2787
2743
  // determine color appearance
2744
+ /* istanbul ignore else */
2788
2745
  if (lightness === 100 && saturation === 0) {
2789
2746
  colorName = colorLabels.white;
2790
2747
  } else if (lightness === 0) {
@@ -2885,13 +2842,14 @@
2885
2842
  const self = this;
2886
2843
  const { activeElement } = getDocument(self.input);
2887
2844
 
2888
- if ((e.type === touchmoveEvent && self.dragElement)
2845
+ self.updateDropdownPosition();
2846
+
2847
+ /* istanbul ignore next */
2848
+ if (([pointermoveEvent, touchmoveEvent].includes(e.type) && self.dragElement)
2889
2849
  || (activeElement && self.controlKnobs.includes(activeElement))) {
2890
2850
  e.stopPropagation();
2891
2851
  e.preventDefault();
2892
2852
  }
2893
-
2894
- self.updateDropdownPosition();
2895
2853
  }
2896
2854
 
2897
2855
  /**
@@ -2965,7 +2923,9 @@
2965
2923
 
2966
2924
  self.update();
2967
2925
 
2926
+ /* istanbul ignore else */
2968
2927
  if (currentActive !== target) {
2928
+ /* istanbul ignore else */
2969
2929
  if (currentActive) {
2970
2930
  removeClass(currentActive, 'active');
2971
2931
  removeAttribute(currentActive, ariaSelected);
@@ -2983,15 +2943,13 @@
2983
2943
 
2984
2944
  /**
2985
2945
  * The `ColorPicker` *touchstart* / *mousedown* events listener for control knobs.
2986
- * @param {TouchEvent} e
2946
+ * @param {PointerEvent} e
2987
2947
  * @this {ColorPicker}
2988
2948
  */
2989
2949
  pointerDown(e) {
2990
2950
  const self = this;
2991
2951
  /** @type {*} */
2992
- const {
2993
- type, target, touches, pageX, pageY,
2994
- } = e;
2952
+ const { target, pageX, pageY } = e;
2995
2953
  const { colorMenu, visuals, controlKnobs } = self;
2996
2954
  const [v1, v2, v3] = visuals;
2997
2955
  const [c1, c2, c3] = controlKnobs;
@@ -2999,11 +2957,10 @@
2999
2957
  const visual = controlKnobs.includes(target) ? target.previousElementSibling : target;
3000
2958
  const visualRect = getBoundingClientRect(visual);
3001
2959
  const html = getDocumentElement(v1);
3002
- const X = type === 'touchstart' ? touches[0].pageX : pageX;
3003
- const Y = type === 'touchstart' ? touches[0].pageY : pageY;
3004
- const offsetX = X - html.scrollLeft - visualRect.left;
3005
- const offsetY = Y - html.scrollTop - visualRect.top;
2960
+ const offsetX = pageX - html.scrollLeft - visualRect.left;
2961
+ const offsetY = pageY - html.scrollTop - visualRect.top;
3006
2962
 
2963
+ /* istanbul ignore else */
3007
2964
  if (target === v1 || target === c1) {
3008
2965
  self.dragElement = visual;
3009
2966
  self.changeControl1(offsetX, offsetY);
@@ -3027,7 +2984,7 @@
3027
2984
 
3028
2985
  /**
3029
2986
  * The `ColorPicker` *touchend* / *mouseup* events listener for control knobs.
3030
- * @param {TouchEvent} e
2987
+ * @param {PointerEvent} e
3031
2988
  * @this {ColorPicker}
3032
2989
  */
3033
2990
  pointerUp({ target }) {
@@ -3036,9 +2993,8 @@
3036
2993
  const doc = getDocument(parent);
3037
2994
  const currentOpen = querySelector(`${colorPickerParentSelector}.open`, doc) !== null;
3038
2995
  const selection = doc.getSelection();
3039
- // @ts-ignore
2996
+
3040
2997
  if (!self.dragElement && !selection.toString().length
3041
- // @ts-ignore
3042
2998
  && !parent.contains(target)) {
3043
2999
  self.hide(currentOpen);
3044
3000
  }
@@ -3048,25 +3004,20 @@
3048
3004
 
3049
3005
  /**
3050
3006
  * The `ColorPicker` *touchmove* / *mousemove* events listener for control knobs.
3051
- * @param {TouchEvent} e
3007
+ * @param {PointerEvent} e
3052
3008
  */
3053
3009
  pointerMove(e) {
3054
3010
  const self = this;
3055
3011
  const { dragElement, visuals } = self;
3056
3012
  const [v1, v2, v3] = visuals;
3057
- const {
3058
- // @ts-ignore
3059
- type, touches, pageX, pageY,
3060
- } = e;
3013
+ const { pageX, pageY } = e;
3061
3014
 
3062
3015
  if (!dragElement) return;
3063
3016
 
3064
3017
  const controlRect = getBoundingClientRect(dragElement);
3065
3018
  const win = getDocumentElement(v1);
3066
- const X = type === touchmoveEvent ? touches[0].pageX : pageX;
3067
- const Y = type === touchmoveEvent ? touches[0].pageY : pageY;
3068
- const offsetX = X - win.scrollLeft - controlRect.left;
3069
- const offsetY = Y - win.scrollTop - controlRect.top;
3019
+ const offsetX = pageX - win.scrollLeft - controlRect.left;
3020
+ const offsetY = pageY - win.scrollTop - controlRect.top;
3070
3021
 
3071
3022
  if (dragElement === v1) {
3072
3023
  self.changeControl1(offsetX, offsetY);
@@ -3100,13 +3051,16 @@
3100
3051
  const currentKnob = controlKnobs.find((x) => x === activeElement);
3101
3052
  const yRatio = offsetHeight / 360;
3102
3053
 
3054
+ /* istanbul ignore else */
3103
3055
  if (currentKnob) {
3104
3056
  let offsetX = 0;
3105
3057
  let offsetY = 0;
3106
3058
 
3059
+ /* istanbul ignore else */
3107
3060
  if (target === c1) {
3108
3061
  const xRatio = offsetWidth / 100;
3109
3062
 
3063
+ /* istanbul ignore else */
3110
3064
  if ([keyArrowLeft, keyArrowRight].includes(code)) {
3111
3065
  self.controlPositions.c1x += code === keyArrowRight ? xRatio : -xRatio;
3112
3066
  } else if ([keyArrowUp, keyArrowDown].includes(code)) {
@@ -3152,6 +3106,7 @@
3152
3106
  const isNonColorValue = self.hasNonColor && nonColors.includes(currentValue);
3153
3107
  const alpha = i4 ? v4 : (1 - controlPositions.c3y / offsetHeight);
3154
3108
 
3109
+ /* istanbul ignore else */
3155
3110
  if (activeElement === input || (activeElement && inputs.includes(activeElement))) {
3156
3111
  if (activeElement === input) {
3157
3112
  if (isNonColorValue) {
@@ -3466,6 +3421,7 @@
3466
3421
  const hue = roundPart(hsl.h * 360);
3467
3422
  let newColor;
3468
3423
 
3424
+ /* istanbul ignore else */
3469
3425
  if (format === 'hex') {
3470
3426
  newColor = self.color.toHexString(true);
3471
3427
  i1.value = self.hex;
@@ -3566,15 +3522,15 @@
3566
3522
  const relatedBtn = openPicker ? pickerToggle : menuToggle;
3567
3523
  const animationDuration = openDropdown && getElementTransitionDuration(openDropdown);
3568
3524
 
3569
- // if (!self.isValid) {
3570
3525
  self.value = self.color.toString(true);
3571
- // }
3572
3526
 
3527
+ /* istanbul ignore else */
3573
3528
  if (openDropdown) {
3574
3529
  removeClass(openDropdown, 'show');
3575
3530
  setAttribute(relatedBtn, ariaExpanded, 'false');
3576
3531
  setTimeout(() => {
3577
3532
  removePosition(openDropdown);
3533
+ /* istanbul ignore else */
3578
3534
  if (!querySelector('.show', parent)) {
3579
3535
  removeClass(parent, 'open');
3580
3536
  toggleEventsOnShown(self);
@@ -3587,7 +3543,7 @@
3587
3543
  focus(pickerToggle);
3588
3544
  }
3589
3545
  setAttribute(input, tabIndex, '-1');
3590
- if (menuToggle) {
3546
+ if (relatedBtn === menuToggle) {
3591
3547
  setAttribute(menuToggle, tabIndex, '-1');
3592
3548
  }
3593
3549
  }