@thednp/color-picker 0.0.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- // ColorPickerElement v0.0.2 | thednp © 2022 | MIT-License
2
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).ColorPickerElement=e()}(this,(function(){"use strict";function t(t){return t instanceof HTMLElement?t.ownerDocument:t instanceof Window?t.document:window.document}const e=[Document,Element,HTMLElement];const o=(t,e)=>Object.assign(t,e);function n(e){if("string"==typeof e)return t().createElement(e);const{tagName:r}=e,s={...e},a=n(r);return delete s.tagName,o(a,s),a}const r=(t,e,o)=>t.setAttribute(e,o),s=(t,e)=>t.getAttribute(e),{head:a}=document;function i(t,e){const o=getComputedStyle(t);return e in o?o[e]:""}const l=(t,e)=>{o(t.style,e)},c=t=>t.toLowerCase(),u=["transparent","currentColor","inherit","revert","initial"];function h(t){const e=Math.floor(t);return t-e<.5?e:Math.round(t)}const g=["rgb","hex","hsl","hsv","hwb"],p="[-\\+]?\\d*\\.?\\d+(?:deg|rad|grad|turn)?",d="(?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?)",b=`(?:${d})|(?:${p})`,f="(?:[,|\\s]+)",m=`(?:[\\s|\\(\\s|\\s\\(\\s]+)?(${b})${f}(${d})${f}(${d})(?:[,|\\/\\s]*)?(${d})?(?:[\\s|\\)\\s]+)?`,v={CSS_UNIT:new RegExp(b),hwb:new RegExp("hwb"+m),rgb:new RegExp("rgb(?:a)?"+m),hsl:new RegExp("hsl(?:a)?"+m),hsv:new RegExp("hsv(?:a)?"+m),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 x(t){return(""+t).includes("%")}function y(t){return!u.includes(t)&&!["#",...g].some(e=>t.includes(e))&&(!!["black","white"].includes(t)||["rgb(255, 255, 255)","rgb(0, 0, 0)"].every(e=>{l(a,{color:t});const o=i(a,"color");return l(a,{color:""}),o!==e}))}function $(t){return Boolean(v.CSS_UNIT.exec(String(t)))}function k(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=x(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 S(t){let e=parseFloat(""+t);return(Number.isNaN(e)||e<0||e>1)&&(e=1),e}function H(t){return Math.min(1,Math.max(0,t))}function A(t){l(a,{color:t});const e=i(a,"color");return l(a,{color:""}),e}function E(t){return h(255*t).toString(16)}function L(t){return P(t)/255}function P(t){return parseInt(t,16)}function N(t){return 1===t.length?"0"+t:String(t)}function C(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 M(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 T(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=M(i,a,t+1/3),r=M(i,a,t),s=M(i,a,t-1/3)}return{r:n,g:r,b:s}}function R(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 F(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}=T(t,1,.5);return[n,r,s]=[n,r,s].map(t=>t*(1-e-o)+e),{r:n,g:r,b:s}}function D(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 K(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 O(t,e,o,n){const r=[N(h(t).toString(16)),N(h(e).toString(16)),N(h(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 I(t,e,o,n,r){const s=[N(h(t).toString(16)),N(h(e).toString(16)),N(h(o).toString(16)),N(E(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 V(t){let e=c(t.trim());if(0===e.length)return{r:0,g:0,b:0,a:1};if(y(e))e=A(e);else if(u.includes(e)){return{r:0,g:0,b:0,a:"transparent"===e?0:1,format:"rgb",ok:!0}}let[,o,n,r,s]=v.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]=v.hsl.exec(e)||[],o&&n&&r?{h:o,s:n,l:r,a:void 0!==s?s:1,format:"hsl"}:([,o,n,r,s]=v.hsv.exec(e)||[],o&&n&&r?{h:o,s:n,v:r,a:void 0!==s?s:1,format:"hsv"}:([,o,n,r,s]=v.hwb.exec(e)||[],o&&n&&r?{h:o,w:n,b:r,a:void 0!==s?s:1,format:"hwb"}:([,o,n,r,s]=v.hex8.exec(e)||[],o&&n&&r&&s?{r:P(o),g:P(n),b:P(r),a:L(s),format:"hex"}:([,o,n,r]=v.hex6.exec(e)||[],o&&n&&r?{r:P(o),g:P(n),b:P(r),format:"hex"}:([,o,n,r,s]=v.hex4.exec(e)||[],o&&n&&r&&s?{r:P(o+o),g:P(n+n),b:P(r+r),a:L(s+s),format:"hex"}:([,o,n,r]=v.hex3.exec(e)||[],!!(o&&n&&r)&&{r:P(o+o),g:P(n+n),b:P(r+r),format:"hex"})))))))}function B(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,p=!1;const d="object"==typeof o&&o.format;let b=d&&g.includes(d)?d:"rgb";return"string"==typeof t&&(o=V(t),o&&(p=!0)),"object"==typeof o&&($(o.r)&&$(o.g)&&$(o.b)&&(({r:u,g:h,b:l}=o),[u,h,l]=[u,h,l].map(t=>k(t,x(t)?100:255)),e={r:u,g:h,b:l},p=!0,b=o.format||"rgb"),$(o.h)&&$(o.s)&&$(o.v)&&(({h:c,s:r,v:s}=o),c=k(c,360),r=k(r,100),s=k(s,100),e=K(c,r,s),p=!0,b="hsv"),$(o.h)&&$(o.s)&&$(o.l)&&(({h:c,s:r,l:a}=o),c=k(c,360),r=k(r,100),a=k(a,100),e=T(c,r,a),p=!0,b="hsl"),$(o.h)&&$(o.w)&&$(o.b)&&(({h:c,w:i,b:l}=o),c=k(c,360),i=k(i,100),l=k(l,100),e=F(c,i,l),p=!0,b="hwb"),$(o.a)&&(n=o.a,n=x(""+n)||parseFloat(n)>1?k(n,100):n)),void 0===o&&(p=!0),{ok:p,format:b,r:e.r,g:e.g,b:e.b,a:S(n)}}class U{constructor(t,e){let o=t;const n=e&&g.includes(e)?e:"";o instanceof U&&(o=B(o));const{r:r,g:s,b:a,a:i,ok:l,format:c}=B(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=>h(255*t*100)/100),n=h(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(h);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(h);return`rgb(${r} ${s} ${a}${1===n?"":` / ${h(100*n)}%`})`}toHex(t){const{r:e,g:o,b:n,a:r}=this.toRgb();return 1===r?O(e,o,n,t):I(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 I(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}=D(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}=C(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=h(360*t),e=h(100*e),o=h(100*o),n=h(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=h(360*t),e=h(100*e),o=h(100*o),n=h(100*n);return`hsl(${t}deg ${e}% ${o}%${n<100?` / ${h(n)}%`:""})`}toHwb(){const{r:t,g:e,b:o,a:n}=this,{h:r,w:s,b:a}=R(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=h(360*t),e=h(100*e),o=h(100*o),n=h(100*n);return`hwb(${t}deg ${e}% ${o}%${n<100?` / ${h(n)}%`:""})`}setAlpha(t){return"number"!=typeof t||(this.a=S(t)),this}saturate(t){if("number"!=typeof t)return this;const{h:e,s:n,l:r}=this.toHsl(),{r:s,g:a,b:i}=T(e,H(n+t/100),r);return o(this,{r:s,g:a,b:i}),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:n,l:r}=this.toHsl(),{r:s,g:a,b:i}=T(e,n,H(r+t/100));return o(this,{r:s,g:a,b:i}),this}darken(t){return"number"==typeof t?this.lighten(-t):this}spin(t){if("number"!=typeof t)return this;const{h:e,s:n,l:r}=this.toHsl(),{r:s,g:a,b:i}=T(H((360*e+t)%360/360),n,r);return o(this,{r:s,g:a,b:i}),this}clone(){return new U(this)}toString(t){const{format:e}=this;return"hex"===e?this.toHexString(t):"hsl"===e?this.toHslString():"hwb"===e?this.toHwbString():this.toRgbString()}}o(U,{ANGLES:"deg|rad|grad|turn",CSS_ANGLE:p,CSS_INTEGER:"[-\\+]?\\d+%?",CSS_NUMBER:"[-\\+]?\\d*\\.\\d+%?",CSS_UNIT:d,CSS_UNIT2:b,PERMISSIVE_MATCH:m,matchers:v,isOnePointZero:w,isPercentage:x,isValidCSSUnit:$,isColorName:y,pad2:N,clamp01:H,bound01:k,boundAlpha:S,getRGBFromName:A,convertHexToDecimal:L,convertDecimalToHex:E,rgbToHsl:C,rgbToHex:O,rgbToHsv:D,rgbToHwb:R,rgbaToHex:I,hslToRgb:T,hsvToRgb:K,hueToRgb:M,hwbToRgb:F,parseIntFromHex:P,stringInputToObject:V,inputToRGB:B,roundPart:h,getElementStyle:i,setElementStyle:l,ObjectAssign:o});const _={};function j(t){const e=this,{type:o}=t;(_[o]?[..._[o]]:[]).forEach(n=>{const[r,s]=n;[...s].forEach(n=>{if(r===e){const[e,s]=n;e.apply(r,[t]),s&&s.once&&z(r,o,e,s)}})})}const W=(t,e,o,n)=>{_[e]||(_[e]=new Map);const r=_[e];r.has(t)||r.set(t,new Map);const s=r.get(t),{size:a}=s;s&&s.set(o,n),a||t.addEventListener(e,j,n)},z=(t,e,o,n)=>{const r=_[e],s=r&&r.get(t),a=s&&s.get(o),{options:i}=void 0!==a?a:{options:n};s&&s.has(o)&&s.delete(o),!r||s&&s.size||r.delete(t),r&&r.size||delete _[e],s&&s.size||t.removeEventListener(e,j,i)},G="ArrowDown",X="ArrowUp",Y="ArrowLeft",J="ArrowRight",q="Enter",Z="Space";function Q(e){return t(e).documentElement}let tt=0,et=0;const ot=new Map;function nt(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 rt=[Element,HTMLElement];function st(o,n){const r=e.some(t=>n instanceof t)?n:t();return rt.some(t=>o instanceof t)?o:r.querySelector(o)}function at(o,n){return(n&&e.some(t=>n instanceof t)?n:t()).getElementsByClassName(o)}const it=new Map,lt={set:(t,e,o)=>{const n=st(t);if(!n)return;it.has(e)||it.set(e,new Map);it.get(e).set(n,o)},getAllFor:t=>it.get(t)||null,get:(t,e)=>{const o=st(t),n=lt.getAllFor(e);return o&&n&&n.get(o)||null},remove:(t,e)=>{const o=st(t),n=it.get(e);n&&o&&(n.delete(o),0===n.size&&it.delete(e))}};function ct(t){return"true"===t||"false"!==t&&(Number.isNaN(+t)?""===t||"null"===t?null:t:+t)}const ut=t=>Object.keys(t);const ht=t=>t.focus();function gt(t,e){return t.classList.contains(e)}function pt(t,e){t.classList.add(e)}function dt(t,e){t.classList.remove(e)}const bt=(t,e)=>t.removeAttribute(e);class ft{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=h((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 U({h:o,s:1,l:t}))})}this.hue=e,this.hueSteps=o,this.lightSteps=n,this.colors=s}}o(ft,{Color:U});const mt={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"},vt=["white","black","grey","red","orange","brown","gold","olive","yellow","lime","green","teal","cyan","blue","violet","magenta","pink"];function wt(t){try{JSON.parse(t)}catch(t){return!1}return!0}const xt=t=>t.toUpperCase();function yt(e,n){if("string"==typeof n)return t().createElementNS(e,n);const{tagName:r}=n,s={...n},a=yt(e,r);return delete s.tagName,o(a,s),a}function $t(t,e,o){const{input:a,format:i,componentLabels:c}=t,{defaultsLabel:u,presetsLabel:h}=c,g="color-options"===o,p=e instanceof ft,d=g?h:u;let b=p?e.colors:e;b=b instanceof Array?b:[];const f=b.length,{lightSteps:m}=p?e:{lightSteps:null},v=m||[9,10].find(t=>f>2*t&&!(f%t))||5,w=g&&f>v;let x=2;x=w&&f>=2*v?3:x,x=f>=3*v?4:x,x=f>=4*v?5:x;const y=x-(f<3*v?1:2),$=w&&f>y*v;let k=o;k+=$?" scrollable":"",k+=w?" multiline":"";const S=w?"1px":"0.25rem";let H=w?1.75:2;H=v>5&&w?1.5:H;const A=(y||1)*H+"rem",E=`calc(${x} * ${H}rem + ${x-1} * ${S})`,L=n({tagName:"ul",className:k});var P,N;return r(L,"role","listbox"),r(L,"aria-label",d),$&&(P=L,ut(N={"--grid-item-size":H+"rem","--grid-fit":v,"--grid-gap":S,"--grid-height":A,"--grid-hover-height":E}).forEach(t=>{P.style.setProperty(t,N[t])})),b.forEach(t=>{let[e,o]="string"==typeof t?t.trim().split(":"):[];t instanceof U&&(e=t.toHexString(),o=e);const c=new U(t instanceof U?t:e,i).toString()===s(a,"value"),u=n({tagName:"li",className:"color-option"+(c?" active":""),innerText:""+(o||e)});r(u,"tabindex","0"),r(u,"data-value",""+e),r(u,"role","option"),r(u,"aria-selected",c?"true":"false"),g&&l(u,{backgroundColor:e}),L.append(u)}),L}function kt(t){const{input:e,parent:a,format:i,id:l,componentLabels:c,colorKeywords:h,colorPresets:g}=t,p=s(e,"value")||"#fff",{toggleLabel:d,pickerLabel:b,formatLabel:f,hexLabel:m}=c,v=u.includes(p)?"#fff":p;t.color=new U(v,i);const w="hex"===i?m:xt(i),x=n({id:"picker-btn-"+l,tagName:"button",className:"picker-toggle btn-appearance"});r(x,"aria-expanded","false"),r(x,"aria-haspopup","true"),x.append(n({tagName:"span",className:"v-hidden",innerText:`${b}. ${f}: ${w}`}));const y=n({tagName:"div",className:"color-dropdown picker"});r(y,"aria-labelledby","picker-btn-"+l),r(y,"role","group");const $=function(t){const{format:e,componentLabels:o}=t,{hueLabel:s,alphaLabel:a,lightnessLabel:i,saturationLabel:l,whitenessLabel:c,blacknessLabel:u}=o,h="hsl"===e?360:100,g="hsl"===e?100:360;let p="hsl"===e?`${s} & ${i}`:`${i} & ${l}`;p="hwb"===e?`${c} & ${u}`:p;const d="hsl"===e?""+l:""+s,b=n({tagName:"div",className:"color-controls "+e});return[{i:1,c:"color-pointer",l:p,min:0,max:h},{i:2,c:"color-slider",l:d,min:0,max:g},{i:3,c:"color-slider",l:a,min:0,max:100}].forEach(t=>{const{i:e,c:o,l:s,min:a,max:i}=t,l=n({tagName:"div",className:"color-control"});r(l,"role","presentation"),l.append(n({tagName:"div",className:"visual-control visual-control"+e}));const c=n({tagName:"div",className:o+" knob",ariaLive:"polite"});r(c,"aria-label",s),r(c,"role","slider"),r(c,"tabindex","0"),r(c,"aria-valuemin",""+a),r(c,"aria-valuemax",""+i),l.append(c),b.append(l)}),b}(t),k=function(t){const{format:e,id:s,componentLabels:a}=t,i=n({tagName:"div",className:"color-form "+e});let l=["hex"];return"rgb"===e?l=["red","green","blue","alpha"]:"hsl"===e?l=["hue","saturation","lightness","alpha"]:"hwb"===e&&(l=["hue","whiteness","blackness","alpha"]),l.forEach(t=>{const[l]="hex"===e?["#"]:xt(t).split(""),c=`color_${e}_${t}_${s}`,u=a[t+"Label"],h=n({tagName:"label"});r(h,"for",c),h.append(n({tagName:"span",ariaHidden:"true",innerText:l+":"}),n({tagName:"span",className:"v-hidden",innerText:u}));const g=n({tagName:"input",id:c,type:"hex"===e?"text":"number",value:"alpha"===t?"100":"0",className:"color-input "+t});r(g,"autocomplete","off"),r(g,"spellcheck","false");let p="100",d="1";"alpha"!==t&&("rgb"===e?(p="255",d="1"):"hue"===t&&(p="360",d="1")),o(g,{min:"0",max:p,step:d}),i.append(h,g)}),i}(t);if(y.append($,k),e.before(x),a.append(y),h||g){const e=n({tagName:"div",className:"color-dropdown scrollable menu"});if(g instanceof Array&&g.length||g instanceof ft&&g.colors){const o=$t(t,g,"color-options");e.append(o)}if(h&&h.length){const o=$t(t,h,"color-defaults");e.append(o)}const o=n({tagName:"button",className:"menu-toggle btn-appearance"});r(o,"tabindex","-1"),r(o,"aria-expanded","false"),r(o,"aria-haspopup","true");const s=encodeURI("http://www.w3.org/2000/svg"),i=yt(s,{tagName:"svg"});r(i,"xmlns",s),r(i,"viewBox","0 0 512 512"),r(i,"aria-hidden","true");const l=yt(s,{tagName:"path"});r(l,"d","M98,158l157,156L411,158l27,27L255,368L71,185L98,158z"),r(l,"fill","#fff"),i.append(l),o.append(n({tagName:"span",className:"v-hidden",innerText:""+d}),i),a.append(o,e)}h&&u.includes(p)&&(t.value=p),r(e,"tabindex","-1")}const St={componentLabels:mt,colorLabels:vt,format:"rgb",colorPresets:!1,colorKeywords:!1};function Ht(t,e){const o=e?W:z,{input:n,pickerToggle:r,menuToggle:s}=t;o(n,"focusin",t.showPicker),o(r,"click",t.togglePicker),s&&o(s,"click",t.toggleMenu)}function At(e,o){const n=o?W:z,{input:r,colorMenu:s,parent:a}=e,i=t(r),l=function(t){if(null==t)return window;if(!(t instanceof Window)){const{ownerDocument:e}=t;return e&&e.defaultView||window}return t}(r),c="ontouchstart"in i?{down:"touchstart",move:"touchmove",up:"touchend"}:{down:"mousedown",move:"mousemove",up:"mouseup"};n(e.controls,c.down,e.pointerDown),e.controlKnobs.forEach(t=>n(t,"keydown",e.handleKnobs)),n(l,"scroll",e.handleScroll),n(l,"resize",e.update),[r,...e.inputs].forEach(t=>n(t,"change",e.changeHandler)),s&&(n(s,"click",e.menuClickHandler),n(s,"keydown",e.menuKeyHandler)),n(i,c.move,e.pointerMove),n(i,c.up,e.pointerUp),n(a,"focusout",e.handleFocusOut),n(i,"keyup",e.handleDismiss)}function Et(t){var e,o;e=t.input,o=new CustomEvent("colorpicker.change"),e.dispatchEvent(o)}function Lt(t){t&&["bottom","top"].forEach(e=>dt(t,e))}function Pt(t,e){const{colorPicker:o,colorMenu:n,menuToggle:s,pickerToggle:a,parent:i}=t,l=e===o,c=l?n:o,u=l?s:a,h=l?a:s;gt(i,"open")||pt(i,"open"),c&&(dt(c,"show"),Lt(c)),pt(e,"bottom"),e.offsetHeight,pt(e,"show"),l&&t.update(),t.isOpen||(At(t,!0),t.updateDropdownPosition(),t.isOpen=!0,r(t.input,"tabindex","0"),s&&r(s,"tabindex","0")),r(h,"aria-expanded","true"),u&&r(u,"aria-expanded","false")}class Nt{constructor(t,e){const n=this,r=st(t);if(!r)throw new TypeError(`ColorPicker target "${t}" cannot be found.`);n.input=r;const a=function t(e,o){return e?e.closest(o)||t(e.getRootNode().host,o):null}(r,".color-picker,color-picker");if(!a)throw new TypeError("ColorPicker requires a specific markup to work.");n.parent=a,n.id=function t(e,o){let n=o?tt:et;if(o){const r=t(e),s=ot.get(r)||new Map;ot.has(r)||ot.set(r,s),s.has(o)?n=s.get(o):(s.set(o,n),tt+=1)}else{const t=e.id||e;ot.has(t)?n=ot.get(t):(ot.set(t,n),et+=1)}return n}(r,"color-picker"),n.dragElement=null,n.isOpen=!1,n.controlPositions={c1x:0,c1y:0,c2y:0,c3y:0},n.colorLabels={},n.colorKeywords=void 0,n.colorPresets=void 0;const{format:i,componentLabels:l,colorLabels:u,colorKeywords:h,colorPresets:g}=function(t,e,o,n){const r={...t.dataset},a={},i={};return ut(r).forEach(t=>{const e=n&&t.includes(n)?t.replace(n,"").replace(/[A-Z]/,t=>c(t)):t;i[e]=ct(r[t])}),ut(o).forEach(t=>{o[t]=ct(o[t])}),ut(e).forEach(n=>{a[n]=n in o?o[n]:n in i?i[n]:"title"===n?s(t,"title"):e[n]}),a}(this.isCE?a:r,St,e||{});let p=vt;u instanceof Array&&17===u.length?p=u:u&&17===u.split(",").length&&(p=u.split(",")),vt.forEach((t,e)=>{n.colorLabels[t]=p[e].trim()});const d=l&&wt(l)?JSON.parse(l):l;if(n.componentLabels=o(mt,d),n.color=new U(r.value||"#fff",i),n.format=i,h instanceof Array?n.colorKeywords=h:"string"==typeof h&&h.length&&(n.colorKeywords=h.split(",").map(t=>t.trim())),g instanceof Array)n.colorPresets=g;else if("string"==typeof g&&g.length)if(wt(g)){const{hue:t,hueSteps:e,lightSteps:o}=JSON.parse(g);n.colorPresets=new ft(t,e,o)}else n.colorPresets=g.split(",").map(t=>t.trim());n.showPicker=n.showPicker.bind(n),n.togglePicker=n.togglePicker.bind(n),n.toggleMenu=n.toggleMenu.bind(n),n.menuClickHandler=n.menuClickHandler.bind(n),n.menuKeyHandler=n.menuKeyHandler.bind(n),n.pointerDown=n.pointerDown.bind(n),n.pointerMove=n.pointerMove.bind(n),n.pointerUp=n.pointerUp.bind(n),n.update=n.update.bind(n),n.handleScroll=n.handleScroll.bind(n),n.handleFocusOut=n.handleFocusOut.bind(n),n.changeHandler=n.changeHandler.bind(n),n.handleDismiss=n.handleDismiss.bind(n),n.handleKnobs=n.handleKnobs.bind(n),kt(n);const[b,f]=at("color-dropdown",a);n.pickerToggle=st(".picker-toggle",a),n.menuToggle=st(".menu-toggle",a),n.colorPicker=b,n.colorMenu=f,n.inputs=[...at("color-input",a)];const[m]=at("color-controls",a);n.controls=m,n.controlKnobs=[...at("knob",m)],n.visuals=[...at("visual-control",m)],n.update(),Ht(n,!0),lt.set(r,"color-picker",n)}get value(){return this.input.value}set value(t){this.input.value=t}get hasNonColor(){return this.colorKeywords instanceof Array&&this.colorKeywords.some(t=>u.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 U(t).isValid}get appearance(){const{colorLabels:t,hsl:e,hsv:o,format:n}=this,r=h(360*e.h),s=h(100*("hsl"===n?e.s:o.s)),a=h(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:c,b:u}=new U({h:a,s:1,l:.5}).toRgb(),g=1-t.c3y/s,p=h(100*g)/100,d=new U({h:a,s:1,l:.5,a:g}).toRgbString();l(o,{background:`linear-gradient(rgba(0,0,0,0) 0%, rgba(0,0,0,${p}) 100%),\n linear-gradient(to right, rgba(255,255,255,${p}) 0%, ${d} 100%),\n linear-gradient(rgb(255,255,255) 0%, rgb(255,255,255) 100%)`}),l(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%)"}),l(r,{background:`linear-gradient(rgba(${i},${c},${u},1) 0%,rgba(${i},${c},${u},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(e){const{activeElement:o}=t(this.input);("touchmove"===e.type&&this.dragElement||o&&this.controlKnobs.includes(o))&&(e.stopPropagation(),e.preventDefault()),this.updateDropdownPosition()}menuKeyHandler(t){const{target:e,code:o}=t,{previousElementSibling:n,nextElementSibling:r,parentElement:s}=e,a=s&&gt(s,"color-options"),l=[...s.children],c=a&&i(s,"grid-template-columns").split(" ").length,u=l.indexOf(e),h=u>-1&&c&&l[u-c],g=u>-1&&c&&l[u+c];[G,X,Z].includes(o)&&t.preventDefault(),a?h&&o===X?ht(h):g&&o===G?ht(g):n&&o===Y?ht(n):r&&o===J&&ht(r):n&&[Y,X].includes(o)?ht(n):r&&[J,G].includes(o)&&ht(r),[q,Z].includes(o)&&this.menuClickHandler({target:e})}menuClickHandler(t){const e=this,{target:n}=t,{colorMenu:a}=e,i=(s(n,"data-value")||"").trim();if(!i.length)return;const l=st("li.active",a);let c=u.includes(i)?"white":i;c="transparent"===i?"rgba(0,0,0,0)":i;const{r:h,g:g,b:p,a:d}=new U(c);o(e.color,{r:h,g:g,b:p,a:d}),e.update(),l!==n&&(l&&(dt(l,"active"),bt(l,"aria-selected")),pt(n,"active"),r(n,"aria-selected","true"),u.includes(i)&&(e.value=i),Et(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,h,g]=l,[p,d,b]=c,f=c.includes(n)?n.previousElementSibling:n,m=nt(f),v=Q(u),w="touchstart"===o?r[0].pageX:s,x="touchstart"===o?r[0].pageY:a,y=w-v.scrollLeft-m.left,$=x-v.scrollTop-m.top;if(n===u||n===p?(e.dragElement=f,e.changeControl1(y,$)):n===h||n===d?(e.dragElement=f,e.changeControl2($)):n!==g&&n!==b||(e.dragElement=f,e.changeAlpha($)),i){const t=st("li.active",i);t&&(dt(t,"active"),bt(t,"aria-selected"))}t.preventDefault()}pointerUp({target:e}){const o=this,{parent:n}=o,r=t(n),s=null!==st(".color-picker,color-picker.open",r),a=r.getSelection();o.dragElement||a.toString().length||n.contains(e)||o.hide(s),o.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 h=nt(o),g=Q(r),p="touchmove"===i?l[0].pageX:c,d="touchmove"===i?l[0].pageY:u,b=p-g.scrollLeft-h.left,f=d-g.scrollTop-h.top;o===r&&e.changeControl1(b,f),o===s&&e.changeControl2(f),o===a&&e.changeAlpha(f)}handleKnobs(e){const{target:o,code:n}=e,r=this;if(![X,G,Y,J].includes(n))return;e.preventDefault();const{controlKnobs:s,visuals:a}=r,{offsetWidth:i,offsetHeight:l}=a[0],[c,u,h]=s,{activeElement:g}=t(c),p=l/360;if(s.find(t=>t===g)){let t=0,s=0;if(o===c){const e=i/100;[Y,J].includes(n)?r.controlPositions.c1x+=n===J?e:-e:[X,G].includes(n)&&(r.controlPositions.c1y+=n===G?p:-p),t=r.controlPositions.c1x,s=r.controlPositions.c1y,r.changeControl1(t,s)}else o===u?(r.controlPositions.c2y+=[G,J].includes(n)?p:-p,s=r.controlPositions.c2y,r.changeControl2(s)):o===h&&(r.controlPositions.c3y+=[G,J].includes(n)?p:-p,s=r.controlPositions.c3y,r.changeAlpha(s));r.handleScroll(e)}}changeHandler(){const e=this;let n;const{inputs:r,format:s,value:a,input:i,controlPositions:l,visuals:c}=e,{activeElement:h}=t(i),{offsetHeight:g}=c[0],[p,,,d]=r,[b,f,m,v]="rgb"===s?r.map(t=>parseFloat(t.value)/(t===d?100:1)):r.map(t=>parseFloat(t.value)/(t!==p?100:360)),w=e.hasNonColor&&u.includes(a),x=d?v:1-l.c3y/g;if(h===i||h&&r.includes(h)){n=h===i?w?"transparent"===a?"rgba(0,0,0,0)":"rgb(0,0,0)":a:"hex"===s?p.value:"hsl"===s?{h:b,s:f,l:m,a:x}:"hwb"===s?{h:b,w:f,b:m,a:x}:{r:b,g:f,b:m,a:x};const{r:t,g:r,b:l,a:c}=new U(n);o(e.color,{r:t,g:r,b:l,a:c}),e.setControlPositions(),e.updateAppearance(),e.updateInputs(),e.updateControls(),e.updateVisuals(),h===i&&w&&(e.value=a)}}changeControl1(t,e){let[n,r]=[0,0];const{controlPositions:s,visuals:a}=this,{offsetHeight:i,offsetWidth:l}=a[0];t>l?n=l:t>=0&&(n=t),e>i?r=i:e>=0&&(r=e);const c=s.c2y/i,u=n/l,h=1-r/i,g=1-s.c3y/i,{r:p,g:d,b:b,a:f}=new U({h:c,s:u,v:h,a:g});o(this.color,{r:p,g:d,b:b,a:f}),this.controlPositions.c1x=n,this.controlPositions.c1y=r,this.updateAppearance(),this.updateInputs(),this.updateControls(),this.updateVisuals()}changeControl2(t){const{controlPositions:e,visuals:n}=this,{offsetHeight:r,offsetWidth:s}=n[0];let a=0;t>r?a=r:t>=0&&(a=t);const i=a/r,l=e.c1x/s,c=1-e.c1y/r,u=1-e.c3y/r,{r:h,g:g,b:p,a:d}=new U({h:i,s:l,v:c,a:u});o(this.color,{r:h,g:g,b:p,a:d}),this.controlPositions.c2y=a,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=nt(t),{top:r,bottom:s}=n,{offsetHeight:a}=t,i=Q(t).clientHeight,l=gt(e,"show")?e:o;if(!l)return;const{offsetHeight:c}=l,u=i-s,h=r,g=r+c+a>i,p=r-c<0;(gt(l,"bottom")||!p)&&u<h&&g?(dt(l,"bottom"),pt(l,"top")):(dt(l,"top"),pt(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:s,hex:a,format:i,controlKnobs:c}=t,{appearanceLabel:u,hexLabel:g,valueLabel:p}=e;let{r:d,g:b,b:f}=o.toRgb();const[m,v,w]=c,x=h(360*s.h),y=o.a,$=h(100*s.s),k=h(100*s.v),S=t.appearance;let H=`${g} ${a.split("").join(" ")}`;if("hwb"===i){const{hwb:e}=t,o=h(100*e.w),n=h(100*e.b);H=`HWB: ${x}°, ${o}%, ${n}%`,r(m,"aria-valuetext",`${o}% & ${n}%`),r(m,"aria-valuenow",""+o),r(v,"aria-description",`${p}: ${H}. ${u}: ${S}.`),r(v,"aria-valuetext",x+"%"),r(v,"aria-valuenow",""+x)}else[d,b,f]=[d,b,f].map(h),H="hsl"===i?`HSL: ${x}°, ${$}%, ${k}%`:H,H="rgb"===i?`RGB: ${d}, ${b}, ${f}`:H,r(m,"aria-valuetext",`${k}% & ${$}%`),r(m,"aria-valuenow",""+k),r(v,"aria-description",`${p}: ${H}. ${u}: ${S}.`),r(v,"aria-valuetext",x+"°"),r(v,"aria-valuenow",""+x);const A=h(100*y);r(w,"aria-valuetext",A+"%"),r(w,"aria-valuenow",""+A);const E=o.toString();l(t.input,{backgroundColor:E}),t.isDark?(gt(n,"txt-light")&&dt(n,"txt-light"),gt(n,"txt-dark")||pt(n,"txt-dark")):(gt(n,"txt-dark")&&dt(n,"txt-dark"),gt(n,"txt-light")||pt(n,"txt-light"))}updateControls(){const{controlKnobs:t,controlPositions:e}=this;let{c1x:o,c1y:n,c2y:r,c3y:s}=e;const[a,i,c]=t;[o,n,r,s]=[o,n,r,s].map(h),l(a,{transform:`translate3d(${o-4}px,${n-4}px,0)`}),l(i,{transform:`translate3d(0,${r-4}px,0)`}),l(c,{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,g=h(100*s.a),p=h(360*a.h);let d;if("hex"===n)d=e.color.toHexString(!0),i.value=e.hex;else if("hsl"===n){const t=h(100*a.l),o=h(100*a.s);d=e.color.toHslString(),i.value=""+p,l.value=""+o,c.value=""+t,u.value=""+g}else if("hwb"===n){const{w:t,b:o}=e.hwb,n=h(100*t),r=h(100*o);d=e.color.toHwbString(),i.value=""+p,l.value=""+n,c.value=""+r,u.value=""+g}else if("rgb"===n){let{r:t,g:o,b:n}=e.rgb;[t,o,n]=[t,o,n].map(h),d=e.color.toRgbString(),i.value=""+t,l.value=""+o,c.value=""+n,u.value=""+g}e.value=""+d,t||d===o||Et(e)}togglePicker(t){t&&t.preventDefault();const e=this,{colorPicker:o}=e;e.isOpen&&gt(o,"show")?e.hide(!0):Pt(e,o)}showPicker(){const t=this,{colorPicker:e}=t;["top","bottom"].some(t=>gt(e,t))||Pt(t,e)}toggleMenu(t){t&&t.preventDefault();const e=this,{colorMenu:o}=e;e.isOpen&&gt(o,"show")?e.hide(!0):Pt(e,o)}hide(t){const e=this;if(e.isOpen){const{pickerToggle:o,menuToggle:n,colorPicker:s,colorMenu:a,parent:l,input:c}=e,u=gt(s,"show"),h=u?s:a,g=u?o:n,p=h&&function(t){const e=i(t,"transitionProperty"),o=i(t,"transitionDuration"),n=o.includes("ms")?1:1e3,r=e&&"none"!==e?parseFloat(o)*n:0;return Number.isNaN(r)?0:r}(h);e.value=e.color.toString(!0),h&&(dt(h,"show"),r(g,"aria-expanded","false"),setTimeout(()=>{Lt(h),st(".show",l)||(dt(l,"open"),At(e),e.isOpen=!1)},p)),t||ht(o),r(c,"tabindex","-1"),n&&r(n,"tabindex","-1")}}dispose(){const{input:t,parent:e}=this;this.hide(!0),Ht(this),[...e.children].forEach(e=>{e!==t&&e.remove()}),bt(t,"tabindex"),l(t,{backgroundColor:""}),["txt-light","txt-dark"].forEach(t=>dt(e,t)),lt.remove(t,"color-picker")}}o(Nt,{Color:U,ColorPalette:ft,Version:"0.0.2",getInstance:t=>{return e=t,o="color-picker",lt.get(e,o);var e,o},init:t=>new Nt(t),selector:'[data-function="color-picker"]',roundPart:h,setElementStyle:l,setAttribute:r,getBoundingClientRect:nt});let Ct=0;class Mt extends HTMLElement{constructor(){super(),this.attachShadow({mode:"open"})}get value(){return this.input&&this.input.value}connectedCallback(){if(this.input)return;let[o]=(a="input",((i=this)&&e.some(t=>i instanceof t)?i:t()).getElementsByTagName(a));var a,i;const l=o&&s(o,"value")||s(this,"data-value")||"#fff",c=o&&s(o,"format")||s(this,"data-format")||"rgb";let u=o&&s(o,"id")||s(this,"data-id");u||(u=`color-picker-${c}-${Ct}`,Ct+=1),o||(o=n({tagName:"input",type:"text",className:"color-preview btn-appearance"}),r(o,"id",u),r(o,"name",u),r(o,"autocomplete","off"),r(o,"spellcheck","false"),r(o,"value",l),this.append(o)),this.input=o,this.colorPicker=new Nt(o),this.shadowRoot.append(n("slot"))}disconnectedCallback(){const{input:t,colorPicker:e,shadowRoot:n}=this;e&&e.dispose(),t&&t.remove(),n&&(n.innerHTML=""),o(this,{colorPicker:void 0,input:void 0})}}return o(Mt,{Color:U,ColorPicker:Nt,ColorPalette:ft,getInstance:Nt.getInstance,Version:"0.0.2"}),customElements.define("color-picker",Mt),Mt}));
1
+ // ColorPickerElement v1.0.0 | thednp © 2022 | MIT-License
2
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).ColorPickerElement=e()}(this,(function(){"use strict";function t(t){return t instanceof HTMLElement?t.ownerDocument:t instanceof Window?t.document:window.document}const e=(t,e)=>Object.assign(t,e);function o(n){if("string"==typeof n)return t().createElement(n);const{tagName:r}=n,s={...n},a=o(r);return delete s.tagName,e(a,s),a}const n=(t,e,o)=>t.setAttribute(e,o),r=(t,e)=>t.getAttribute(e),{head:s}=document;function a(t,e){const o=getComputedStyle(t);return e in o?o[e]:""}const i=(t,o)=>{e(t.style,o)},l=t=>t.toLowerCase(),c=["transparent","currentColor","inherit","revert","initial"];function u(t){const e=Math.floor(t);return t-e<.5?e:Math.round(t)}const h=["rgb","hex","hsl","hsv","hwb"],p="[-\\+]?\\d*\\.?\\d+(?:deg|rad|grad|turn)?",g="(?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?)",d=`(?:${g})|(?:${p})`,b="(?:[,|\\s]+)",f=`(?:[\\s|\\(\\s|\\s\\(\\s]+)?(${d})${b}(${g})${b}(${g})(?:[,|\\/\\s]*)?(${g})?(?:[\\s|\\)\\s]+)?`,m={CSS_UNIT:new RegExp(d),hwb:new RegExp("hwb"+f),rgb:new RegExp("rgb(?:a)?"+f),hsl:new RegExp("hsl(?:a)?"+f),hsv:new RegExp("hsv(?:a)?"+f),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 v(t){return(""+t).includes(".")&&1===parseFloat(t)}function w(t){return(""+t).includes("%")}function x(t){return!c.includes(t)&&!["#",...h].some(e=>t.includes(e))&&(!!["black","white"].includes(t)||["rgb(255, 255, 255)","rgb(0, 0, 0)"].every(e=>{i(s,{color:t});const o=a(s,"color");return i(s,{color:""}),o!==e}))}function y(t){return Boolean(m.CSS_UNIT.exec(String(t)))}function k(t,e){let o=t;if("number"==typeof t&&0===Math.min(t,0)&&1===Math.max(t,1))return t;v(t)&&(o="100%");const n=w(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 $(t){let e=parseFloat(""+t);return(Number.isNaN(e)||e<0||e>1)&&(e=1),e}function S(t){return Math.min(1,Math.max(0,t))}function H(t){i(s,{color:t});const e=a(s,"color");return i(s,{color:""}),e}function L(t){return u(255*t).toString(16)}function P(t){return A(t)/255}function A(t){return parseInt(t,16)}function E(t){return 1===t.length?"0"+t:String(t)}function N(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 C(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 M(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=C(i,a,t+1/3),r=C(i,a,t),s=C(i,a,t-1/3)}return{r:n,g:r,b:s}}function T(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 R(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}=M(t,1,.5);return[n,r,s]=[n,r,s].map(t=>t*(1-e-o)+e),{r:n,g:r,b:s}}function F(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 D(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 K(t,e,o,n){const r=[E(u(t).toString(16)),E(u(e).toString(16)),E(u(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 O(t,e,o,n,r){const s=[E(u(t).toString(16)),E(u(e).toString(16)),E(u(o).toString(16)),E(L(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 I(t){let e=l(t.trim());if(0===e.length)return{r:0,g:0,b:0,a:1};if(x(e))e=H(e);else if(c.includes(e)){return{r:0,g:0,b:0,a:"transparent"===e?0:1,format:"rgb",ok:!0}}let[,o,n,r,s]=m.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]=m.hsl.exec(e)||[],o&&n&&r?{h:o,s:n,l:r,a:void 0!==s?s:1,format:"hsl"}:([,o,n,r,s]=m.hsv.exec(e)||[],o&&n&&r?{h:o,s:n,v:r,a:void 0!==s?s:1,format:"hsv"}:([,o,n,r,s]=m.hwb.exec(e)||[],o&&n&&r?{h:o,w:n,b:r,a:void 0!==s?s:1,format:"hwb"}:([,o,n,r,s]=m.hex8.exec(e)||[],o&&n&&r&&s?{r:A(o),g:A(n),b:A(r),a:P(s),format:"hex"}:([,o,n,r]=m.hex6.exec(e)||[],o&&n&&r?{r:A(o),g:A(n),b:A(r),format:"hex"}:([,o,n,r,s]=m.hex4.exec(e)||[],o&&n&&r&&s?{r:A(o+o),g:A(n+n),b:A(r+r),a:P(s+s),format:"hex"}:([,o,n,r]=m.hex3.exec(e)||[],!!(o&&n&&r)&&{r:A(o+o),g:A(n+n),b:A(r+r),format:"hex"})))))))}function V(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,p=null,g=!1;const d="object"==typeof o&&o.format;let b=d&&h.includes(d)?d:"rgb";return"string"==typeof t&&(o=I(t),o&&(g=!0)),"object"==typeof o&&(y(o.r)&&y(o.g)&&y(o.b)&&(({r:u,g:p,b:l}=o),[u,p,l]=[u,p,l].map(t=>k(t,w(t)?100:255)),e={r:u,g:p,b:l},g=!0,b=o.format||"rgb"),y(o.h)&&y(o.s)&&y(o.v)&&(({h:c,s:r,v:s}=o),c=k(c,360),r=k(r,100),s=k(s,100),e=D(c,r,s),g=!0,b="hsv"),y(o.h)&&y(o.s)&&y(o.l)&&(({h:c,s:r,l:a}=o),c=k(c,360),r=k(r,100),a=k(a,100),e=M(c,r,a),g=!0,b="hsl"),y(o.h)&&y(o.w)&&y(o.b)&&(({h:c,w:i,b:l}=o),c=k(c,360),i=k(i,100),l=k(l,100),e=R(c,i,l),g=!0,b="hwb"),y(o.a)&&(n=o.a,n=w(""+n)||parseFloat(n)>1?k(n,100):n)),void 0===o&&(g=!0),{ok:g,format:b,r:e.r,g:e.g,b:e.b,a:$(n)}}class j{constructor(t,e){let o=t;const n=e&&h.includes(e)?e:"";o instanceof j&&(o=V(o));const{r:r,g:s,b:a,a:i,ok:l,format:c}=V(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=>u(255*t*100)/100),n=u(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(u);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(u);return`rgb(${r} ${s} ${a}${1===n?"":` / ${u(100*n)}%`})`}toHex(t){const{r:e,g:o,b:n,a:r}=this.toRgb();return 1===r?K(e,o,n,t):O(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 O(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}=F(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}=N(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=u(360*t),e=u(100*e),o=u(100*o),n=u(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=u(360*t),e=u(100*e),o=u(100*o),n=u(100*n);return`hsl(${t}deg ${e}% ${o}%${n<100?` / ${u(n)}%`:""})`}toHwb(){const{r:t,g:e,b:o,a:n}=this,{h:r,w:s,b:a}=T(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=u(360*t),e=u(100*e),o=u(100*o),n=u(100*n);return`hwb(${t}deg ${e}% ${o}%${n<100?` / ${u(n)}%`:""})`}setAlpha(t){return"number"!=typeof t||(this.a=$(t)),this}saturate(t){if("number"!=typeof t)return this;const{h:o,s:n,l:r}=this.toHsl(),{r:s,g:a,b:i}=M(o,S(n+t/100),r);return e(this,{r:s,g:a,b:i}),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:o,s:n,l:r}=this.toHsl(),{r:s,g:a,b:i}=M(o,n,S(r+t/100));return e(this,{r:s,g:a,b:i}),this}darken(t){return"number"==typeof t?this.lighten(-t):this}spin(t){if("number"!=typeof t)return this;const{h:o,s:n,l:r}=this.toHsl(),{r:s,g:a,b:i}=M(S((360*o+t)%360/360),n,r);return e(this,{r:s,g:a,b:i}),this}clone(){return new j(this)}toString(t){const{format:e}=this;return"hex"===e?this.toHexString(t):"hsl"===e?this.toHslString():"hwb"===e?this.toHwbString():this.toRgbString()}}e(j,{ANGLES:"deg|rad|grad|turn",CSS_ANGLE:p,CSS_INTEGER:"[-\\+]?\\d+%?",CSS_NUMBER:"[-\\+]?\\d*\\.\\d+%?",CSS_UNIT:g,CSS_UNIT2:d,PERMISSIVE_MATCH:f,matchers:m,isOnePointZero:v,isPercentage:w,isValidCSSUnit:y,isColorName:x,pad2:E,clamp01:S,bound01:k,boundAlpha:$,getRGBFromName:H,convertHexToDecimal:P,convertDecimalToHex:L,rgbToHsl:N,rgbToHex:K,rgbToHsv:F,rgbToHwb:T,rgbaToHex:O,hslToRgb:M,hsvToRgb:D,hueToRgb:C,hwbToRgb:R,parseIntFromHex:A,stringInputToObject:I,inputToRGB:V,roundPart:u,getElementStyle:a,setElementStyle:i,ObjectAssign:e});const U={};function B(t){const e=this,{type:o,target:n}=t;[...U[o]].forEach(r=>{const[s,a]=r;[n,e].some(t=>s===t)&&[...a].forEach(e=>{const[n,r]=e;n.apply(s,[t]),r&&r.once&&z(s,o,n,r)})})}const _=(t,e,o,n)=>{U[e]||(U[e]=new Map);const r=U[e];r.has(t)||r.set(t,new Map);const s=r.get(t),{size:a}=s;s.set(o,n),a||t.addEventListener(e,B,n)},z=(t,e,o,n)=>{const r=U[e],s=r&&r.get(t),a=s&&s.get(o),{options:i}=void 0!==a?a:{options:n};s&&s.has(o)&&s.delete(o),!r||s&&s.size||r.delete(t),r&&r.size||delete U[e],s&&s.size||t.removeEventListener(e,B,i)},W="ArrowDown",G="ArrowUp",J="ArrowLeft",q="ArrowRight",X="Enter",Y="Space",Z="touchmove",Q="pointermove";function tt(e){return t(e).documentElement}let et=0,ot=0;const nt=new Map;function rt(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 st=[Document,Element,HTMLElement],at=[Element,HTMLElement];function it(e,o){const n=st.some(t=>o instanceof t)?o:t();return at.some(t=>e instanceof t)?e:n.querySelector(e)}function lt(e,o){return(o&&st.some(t=>o instanceof t)?o:t()).getElementsByClassName(e)}const ct=new Map,ut={set:(t,e,o)=>{const n=it(t);if(!n)return;ct.has(e)||ct.set(e,new Map);ct.get(e).set(n,o)},getAllFor:t=>ct.get(t)||null,get:(t,e)=>{const o=it(t),n=ut.getAllFor(e);return o&&n&&n.get(o)||null},remove:(t,e)=>{const o=it(t),n=ct.get(e);n&&o&&(n.delete(o),0===n.size&&ct.delete(e))}};function ht(t){return"true"===t||"false"!==t&&(Number.isNaN(+t)?""===t||"null"===t?null:t:+t)}const pt=t=>Object.keys(t);const gt=t=>t.focus();function dt(t,e){return t.classList.contains(e)}function bt(t,e){t.classList.add(e)}function ft(t,e){t.classList.remove(e)}const mt=(t,e)=>t.removeAttribute(e);class vt{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=u((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)),h=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+h*t];for(let t=1;t<n-i;t+=1)r=[.5-h*t,...r];for(let t=0;t<o;t+=1){const o=(e+t*a)%360/360;r.forEach(t=>{s.push(new j({h:o,s:1,l:t}))})}this.hue=e,this.hueSteps=o,this.lightSteps=n,this.colors=s}}e(vt,{Color:j});const wt={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"},xt=["white","black","grey","red","orange","brown","gold","olive","yellow","lime","green","teal","cyan","blue","violet","magenta","pink"];function yt(t){try{JSON.parse(t)}catch(t){return!1}return!0}const kt=t=>t.toUpperCase();function $t(o,n){if("string"==typeof n)return t().createElementNS(o,n);const{tagName:r}=n,s={...n},a=$t(o,r);return delete s.tagName,e(a,s),a}function St(t,e,s){const{input:a,format:l,componentLabels:c}=t,{defaultsLabel:u,presetsLabel:h}=c,p="color-options"===s,g=e instanceof vt,d=p?h:u,b=g?e.colors:e,f=b.length,{lightSteps:m}=g?e:{lightSteps:null},v=m||[9,10].find(t=>f>=2*t&&!(f%t))||5,w=p&&f>v;let x=2;x=w&&f>2*v?3:x,x=w&&f>3*v?4:x,x=w&&f>4*v?5:x;const y=x-(f<=3*v?1:2),k=w&&f>y*v;let $=s;$+=k?" scrollable":"",$+=w?" multiline":"";const S=w?"1px":"0.25rem";let H=w?1.75:2;H=v>5&&w?1.5:H;const L=y*H+"rem",P=`calc(${x} * ${H}rem + ${x-1} * ${S})`,A=o({tagName:"ul",className:$});var E,N;return n(A,"role","listbox"),n(A,"aria-label",d),k&&(E=A,pt(N={"--grid-item-size":H+"rem","--grid-fit":v,"--grid-gap":S,"--grid-height":L,"--grid-hover-height":P}).forEach(t=>{E.style.setProperty(t,N[t])})),b.forEach(t=>{let[e,s]="string"==typeof t?t.trim().split(":"):[];t instanceof j&&(e=t.toHexString(),s=e);const c=new j(t instanceof j?t:e,l).toString()===r(a,"value"),u=o({tagName:"li",className:"color-option"+(c?" active":""),innerText:""+(s||e)});n(u,"tabindex","0"),n(u,"data-value",""+e),n(u,"role","option"),n(u,"aria-selected",c?"true":"false"),p&&i(u,{backgroundColor:e}),A.append(u)}),A}function Ht(t){const{input:s,parent:a,format:i,id:l,componentLabels:u,colorKeywords:h,colorPresets:p}=t,g=r(s,"value")||"#fff",{toggleLabel:d,pickerLabel:b,formatLabel:f,hexLabel:m}=u,v=c.includes(g)?"#fff":g;t.color=new j(v,i);const w="hex"===i?m:kt(i),x=o({id:"picker-btn-"+l,tagName:"button",className:"picker-toggle btn-appearance"});n(x,"aria-expanded","false"),n(x,"aria-haspopup","true"),x.append(o({tagName:"span",className:"v-hidden",innerText:`${b}. ${f}: ${w}`}));const y=o({tagName:"div",className:"color-dropdown picker"});n(y,"aria-labelledby","picker-btn-"+l),n(y,"role","group");const k=function(t){const{format:e,componentLabels:r}=t,{hueLabel:s,alphaLabel:a,lightnessLabel:i,saturationLabel:l,whitenessLabel:c,blacknessLabel:u}=r,h="hsl"===e?360:100,p="hsl"===e?100:360;let g="hsl"===e?`${s} & ${i}`:`${i} & ${l}`;g="hwb"===e?`${c} & ${u}`:g;const d="hsl"===e?""+l:""+s,b=o({tagName:"div",className:"color-controls "+e});return[{i:1,c:"color-pointer",l:g,min:0,max:h},{i:2,c:"color-slider",l:d,min:0,max:p},{i:3,c:"color-slider",l:a,min:0,max:100}].forEach(t=>{const{i:e,c:r,l:s,min:a,max:i}=t,l=o({tagName:"div",className:"color-control"});n(l,"role","presentation"),l.append(o({tagName:"div",className:"visual-control visual-control"+e}));const c=o({tagName:"div",className:r+" knob",ariaLive:"polite"});n(c,"aria-label",s),n(c,"role","slider"),n(c,"tabindex","0"),n(c,"aria-valuemin",""+a),n(c,"aria-valuemax",""+i),l.append(c),b.append(l)}),b}(t),$=function(t){const{format:r,id:s,componentLabels:a}=t,i=o({tagName:"div",className:"color-form "+r});let l=["hex"];return"rgb"===r?l=["red","green","blue","alpha"]:"hsl"===r?l=["hue","saturation","lightness","alpha"]:"hwb"===r&&(l=["hue","whiteness","blackness","alpha"]),l.forEach(t=>{const[l]="hex"===r?["#"]:kt(t).split(""),c=`color_${r}_${t}_${s}`,u=a[t+"Label"],h=o({tagName:"label"});n(h,"for",c),h.append(o({tagName:"span",ariaHidden:"true",innerText:l+":"}),o({tagName:"span",className:"v-hidden",innerText:u}));const p=o({tagName:"input",id:c,type:"hex"===r?"text":"number",value:"alpha"===t?"100":"0",className:"color-input "+t});n(p,"autocomplete","off"),n(p,"spellcheck","false");let g="100",d="1";"alpha"!==t&&("rgb"===r?(g="255",d="1"):"hue"===t&&(g="360",d="1")),e(p,{min:"0",max:g,step:d}),i.append(h,p)}),i}(t);if(y.append(k,$),s.before(x),a.append(y),h||p){const e=o({tagName:"div",className:"color-dropdown scrollable menu"});p&&e.append(St(t,p,"color-options")),h&&h.length&&e.append(St(t,h,"color-defaults"));const r=o({tagName:"button",className:"menu-toggle btn-appearance"});n(r,"tabindex","-1"),n(r,"aria-expanded","false"),n(r,"aria-haspopup","true");const s=encodeURI("http://www.w3.org/2000/svg"),i=$t(s,{tagName:"svg"});n(i,"xmlns",s),n(i,"viewBox","0 0 512 512"),n(i,"aria-hidden","true");const l=$t(s,{tagName:"path"});n(l,"d","M98,158l157,156L411,158l27,27L255,368L71,185L98,158z"),n(l,"fill","#fff"),i.append(l),r.append(o({tagName:"span",className:"v-hidden",innerText:""+d}),i),a.append(r,e)}h&&c.includes(g)&&(t.value=g),n(s,"tabindex","-1")}const Lt={componentLabels:wt,colorLabels:xt,format:"rgb",colorPresets:!1,colorKeywords:!1};function Pt(t,e){const o=e?_:z,{input:n,pickerToggle:r,menuToggle:s}=t;o(n,"focusin",t.showPicker),o(r,"click",t.togglePicker),s&&o(s,"click",t.toggleMenu)}function At(e,o){const n=o?_:z,{input:r,colorMenu:s,parent:a}=e,i=t(r),l=i.defaultView;n(e.controls,"pointerdown",e.pointerDown),e.controlKnobs.forEach(t=>n(t,"keydown",e.handleKnobs)),n(l,"scroll",e.handleScroll),n(l,"resize",e.update),[r,...e.inputs].forEach(t=>n(t,"change",e.changeHandler)),s&&(n(s,"click",e.menuClickHandler),n(s,"keydown",e.menuKeyHandler)),n(i,Q,e.pointerMove),n(i,"pointerup",e.pointerUp),n(a,"focusout",e.handleFocusOut),n(i,"keyup",e.handleDismiss)}function Et(t){var e,o;e=t.input,o=new CustomEvent("colorpicker.change"),e.dispatchEvent(o)}function Nt(t){t&&["bottom","top"].forEach(e=>ft(t,e))}function Ct(t,e){const{colorPicker:o,colorMenu:r,menuToggle:s,pickerToggle:a,parent:i}=t,l=e===o,c=l?r:o,u=l?s:a,h=l?a:s;dt(i,"open")||bt(i,"open"),c&&(ft(c,"show"),Nt(c)),bt(e,"bottom"),e.offsetHeight,bt(e,"show"),l&&t.update(),t.isOpen||(At(t,!0),t.updateDropdownPosition(),t.isOpen=!0,n(t.input,"tabindex","0"),s&&n(s,"tabindex","0")),n(h,"aria-expanded","true"),u&&n(u,"aria-expanded","false")}class Mt{constructor(t,o){const n=this,s=it(t);if(!s)throw new TypeError(`ColorPicker target "${t}" cannot be found.`);n.input=s;const a=function t(e,o){return e?e.closest(o)||t(e.getRootNode().host,o):null}(s,".color-picker,color-picker");if(!a)throw new TypeError("ColorPicker requires a specific markup to work.");n.parent=a,n.id=function t(e,o){let n=o?et:ot;if(o){const r=t(e),s=nt.get(r)||new Map;nt.has(r)||nt.set(r,s),s.has(o)?n=s.get(o):(s.set(o,n),et+=1)}else{const t=e.id||e;nt.has(t)?n=nt.get(t):(nt.set(t,n),ot+=1)}return n}(s,"color-picker"),n.dragElement=null,n.isOpen=!1,n.controlPositions={c1x:0,c1y:0,c2y:0,c3y:0},n.colorLabels={},n.colorKeywords=void 0,n.colorPresets=void 0;const{format:i,componentLabels:c,colorLabels:u,colorKeywords:h,colorPresets:p}=function(t,e,o,n){const s={...t.dataset},a={},i={};return pt(s).forEach(t=>{const e=n&&t.includes(n)?t.replace(n,"").replace(/[A-Z]/,t=>l(t)):t;i[e]=ht(s[t])}),pt(o).forEach(t=>{o[t]=ht(o[t])}),pt(e).forEach(n=>{a[n]=n in o?o[n]:n in i?i[n]:"title"===n?r(t,"title"):e[n]}),a}(this.isCE?a:s,Lt,o||{});let g=xt;u instanceof Array&&17===u.length?g=u:u&&17===u.split(",").length&&(g=u.split(",")),xt.forEach((t,e)=>{n.colorLabels[t]=g[e].trim()});const d=c&&yt(c)?JSON.parse(c):c;if(n.componentLabels=e({...wt},d),n.color=new j(s.value||"#fff",i),n.format=i,h instanceof Array&&h.length?n.colorKeywords=h:"string"==typeof h&&h.length&&(n.colorKeywords=h.split(",").map(t=>t.trim())),p instanceof Array&&p.length)n.colorPresets=p;else if("string"==typeof p&&p.length)if(yt(p)){const{hue:t,hueSteps:e,lightSteps:o}=JSON.parse(p);n.colorPresets=new vt(t,e,o)}else n.colorPresets=p.split(",").map(t=>t.trim());n.showPicker=n.showPicker.bind(n),n.togglePicker=n.togglePicker.bind(n),n.toggleMenu=n.toggleMenu.bind(n),n.menuClickHandler=n.menuClickHandler.bind(n),n.menuKeyHandler=n.menuKeyHandler.bind(n),n.pointerDown=n.pointerDown.bind(n),n.pointerMove=n.pointerMove.bind(n),n.pointerUp=n.pointerUp.bind(n),n.update=n.update.bind(n),n.handleScroll=n.handleScroll.bind(n),n.handleFocusOut=n.handleFocusOut.bind(n),n.changeHandler=n.changeHandler.bind(n),n.handleDismiss=n.handleDismiss.bind(n),n.handleKnobs=n.handleKnobs.bind(n),Ht(n);const[b,f]=lt("color-dropdown",a);n.pickerToggle=it(".picker-toggle",a),n.menuToggle=it(".menu-toggle",a),n.colorPicker=b,n.colorMenu=f,n.inputs=[...lt("color-input",a)];const[m]=lt("color-controls",a);n.controls=m,n.controlKnobs=[...lt("knob",m)],n.visuals=[...lt("visual-control",m)],n.update(),Pt(n,!0),ut.set(s,"color-picker",n)}get value(){return this.input.value}set value(t){this.input.value=t}get hasNonColor(){return this.colorKeywords instanceof Array&&this.colorKeywords.some(t=>c.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 j(t).isValid}get appearance(){const{colorLabels:t,hsl:e,hsv:o,format:n}=this,r=u(360*e.h),s=u(100*("hsl"===n?e.s:o.s)),a=u(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:l,g:c,b:h}=new j({h:a,s:1,l:.5}).toRgb(),p=1-t.c3y/s,g=u(100*p)/100,d=new j({h:a,s:1,l:.5,a:p}).toRgbString();i(o,{background:`linear-gradient(rgba(0,0,0,0) 0%, rgba(0,0,0,${g}) 100%),\n linear-gradient(to right, rgba(255,255,255,${g}) 0%, ${d} 100%),\n linear-gradient(rgb(255,255,255) 0%, rgb(255,255,255) 100%)`}),i(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%)"}),i(r,{background:`linear-gradient(rgba(${l},${c},${h},1) 0%,rgba(${l},${c},${h},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(e){const{activeElement:o}=t(this.input);this.updateDropdownPosition(),([Q,Z].includes(e.type)&&this.dragElement||o&&this.controlKnobs.includes(o))&&(e.stopPropagation(),e.preventDefault())}menuKeyHandler(t){const{target:e,code:o}=t,{previousElementSibling:n,nextElementSibling:r,parentElement:s}=e,i=s&&dt(s,"color-options"),l=[...s.children],c=i&&a(s,"grid-template-columns").split(" ").length,u=l.indexOf(e),h=u>-1&&c&&l[u-c],p=u>-1&&c&&l[u+c];[W,G,Y].includes(o)&&t.preventDefault(),i?h&&o===G?gt(h):p&&o===W?gt(p):n&&o===J?gt(n):r&&o===q&&gt(r):n&&[J,G].includes(o)?gt(n):r&&[q,W].includes(o)&&gt(r),[X,Y].includes(o)&&this.menuClickHandler({target:e})}menuClickHandler(t){const o=this,{target:s}=t,{colorMenu:a}=o,i=(r(s,"data-value")||"").trim();if(!i.length)return;const l=it("li.active",a);let u=c.includes(i)?"white":i;u="transparent"===i?"rgba(0,0,0,0)":i;const{r:h,g:p,b:g,a:d}=new j(u);e(o.color,{r:h,g:p,b:g,a:d}),o.update(),l!==s&&(l&&(ft(l,"active"),mt(l,"aria-selected")),bt(s,"active"),n(s,"aria-selected","true"),c.includes(i)&&(o.value=i),Et(o))}pointerDown(t){const e=this,{target:o,pageX:n,pageY:r}=t,{colorMenu:s,visuals:a,controlKnobs:i}=e,[l,c,u]=a,[h,p,g]=i,d=i.includes(o)?o.previousElementSibling:o,b=rt(d),f=tt(l),m=n-f.scrollLeft-b.left,v=r-f.scrollTop-b.top;if(o===l||o===h?(e.dragElement=d,e.changeControl1(m,v)):o===c||o===p?(e.dragElement=d,e.changeControl2(v)):o!==u&&o!==g||(e.dragElement=d,e.changeAlpha(v)),s){const t=it("li.active",s);t&&(ft(t,"active"),mt(t,"aria-selected"))}t.preventDefault()}pointerUp({target:e}){const o=this,{parent:n}=o,r=t(n),s=null!==it(".color-picker,color-picker.open",r),a=r.getSelection();o.dragElement||a.toString().length||n.contains(e)||o.hide(s),o.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=rt(o),u=tt(r),h=i-u.scrollLeft-c.left,p=l-u.scrollTop-c.top;o===r&&e.changeControl1(h,p),o===s&&e.changeControl2(p),o===a&&e.changeAlpha(p)}handleKnobs(e){const{target:o,code:n}=e,r=this;if(![G,W,J,q].includes(n))return;e.preventDefault();const{controlKnobs:s,visuals:a}=r,{offsetWidth:i,offsetHeight:l}=a[0],[c,u,h]=s,{activeElement:p}=t(c),g=l/360;if(s.find(t=>t===p)){let t=0,s=0;if(o===c){const e=i/100;[J,q].includes(n)?r.controlPositions.c1x+=n===q?e:-e:[G,W].includes(n)&&(r.controlPositions.c1y+=n===W?g:-g),t=r.controlPositions.c1x,s=r.controlPositions.c1y,r.changeControl1(t,s)}else o===u?(r.controlPositions.c2y+=[W,q].includes(n)?g:-g,s=r.controlPositions.c2y,r.changeControl2(s)):o===h&&(r.controlPositions.c3y+=[W,q].includes(n)?g:-g,s=r.controlPositions.c3y,r.changeAlpha(s));r.handleScroll(e)}}changeHandler(){const o=this;let n;const{inputs:r,format:s,value:a,input:i,controlPositions:l,visuals:u}=o,{activeElement:h}=t(i),{offsetHeight:p}=u[0],[g,,,d]=r,[b,f,m,v]="rgb"===s?r.map(t=>parseFloat(t.value)/(t===d?100:1)):r.map(t=>parseFloat(t.value)/(t!==g?100:360)),w=o.hasNonColor&&c.includes(a),x=d?v:1-l.c3y/p;if(h===i||h&&r.includes(h)){n=h===i?w?"transparent"===a?"rgba(0,0,0,0)":"rgb(0,0,0)":a:"hex"===s?g.value:"hsl"===s?{h:b,s:f,l:m,a:x}:"hwb"===s?{h:b,w:f,b:m,a:x}:{r:b,g:f,b:m,a:x};const{r:t,g:r,b:l,a:c}=new j(n);e(o.color,{r:t,g:r,b:l,a:c}),o.setControlPositions(),o.updateAppearance(),o.updateInputs(),o.updateControls(),o.updateVisuals(),h===i&&w&&(o.value=a)}}changeControl1(t,o){let[n,r]=[0,0];const{controlPositions:s,visuals:a}=this,{offsetHeight:i,offsetWidth:l}=a[0];t>l?n=l:t>=0&&(n=t),o>i?r=i:o>=0&&(r=o);const c=s.c2y/i,u=n/l,h=1-r/i,p=1-s.c3y/i,{r:g,g:d,b:b,a:f}=new j({h:c,s:u,v:h,a:p});e(this.color,{r:g,g:d,b:b,a:f}),this.controlPositions.c1x=n,this.controlPositions.c1y=r,this.updateAppearance(),this.updateInputs(),this.updateControls(),this.updateVisuals()}changeControl2(t){const{controlPositions:o,visuals:n}=this,{offsetHeight:r,offsetWidth:s}=n[0];let a=0;t>r?a=r:t>=0&&(a=t);const i=a/r,l=o.c1x/s,c=1-o.c1y/r,u=1-o.c3y/r,{r:h,g:p,b:g,a:d}=new j({h:i,s:l,v:c,a:u});e(this.color,{r:h,g:p,b:g,a:d}),this.controlPositions.c2y=a,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=rt(t),{top:r,bottom:s}=n,{offsetHeight:a}=t,i=tt(t).clientHeight,l=dt(e,"show")?e:o;if(!l)return;const{offsetHeight:c}=l,u=i-s,h=r,p=r+c+a>i,g=r-c<0;(dt(l,"bottom")||!g)&&u<h&&p?(ft(l,"bottom"),bt(l,"top")):(ft(l,"top"),bt(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:r,hsv:s,hex:a,format:l,controlKnobs:c}=t,{appearanceLabel:h,hexLabel:p,valueLabel:g}=e;let{r:d,g:b,b:f}=o.toRgb();const[m,v,w]=c,x=u(360*s.h),y=o.a,k=u(100*s.s),$=u(100*s.v),S=t.appearance;let H=`${p} ${a.split("").join(" ")}`;if("hwb"===l){const{hwb:e}=t,o=u(100*e.w),r=u(100*e.b);H=`HWB: ${x}°, ${o}%, ${r}%`,n(m,"aria-valuetext",`${o}% & ${r}%`),n(m,"aria-valuenow",""+o),n(v,"aria-description",`${g}: ${H}. ${h}: ${S}.`),n(v,"aria-valuetext",x+"%"),n(v,"aria-valuenow",""+x)}else[d,b,f]=[d,b,f].map(u),H="hsl"===l?`HSL: ${x}°, ${k}%, ${$}%`:H,H="rgb"===l?`RGB: ${d}, ${b}, ${f}`:H,n(m,"aria-valuetext",`${$}% & ${k}%`),n(m,"aria-valuenow",""+$),n(v,"aria-description",`${g}: ${H}. ${h}: ${S}.`),n(v,"aria-valuetext",x+"°"),n(v,"aria-valuenow",""+x);const L=u(100*y);n(w,"aria-valuetext",L+"%"),n(w,"aria-valuenow",""+L);const P=o.toString();i(t.input,{backgroundColor:P}),t.isDark?(dt(r,"txt-light")&&ft(r,"txt-light"),dt(r,"txt-dark")||bt(r,"txt-dark")):(dt(r,"txt-dark")&&ft(r,"txt-dark"),dt(r,"txt-light")||bt(r,"txt-light"))}updateControls(){const{controlKnobs:t,controlPositions:e}=this;let{c1x:o,c1y:n,c2y:r,c3y:s}=e;const[a,l,c]=t;[o,n,r,s]=[o,n,r,s].map(u),i(a,{transform:`translate3d(${o-4}px,${n-4}px,0)`}),i(l,{transform:`translate3d(0,${r-4}px,0)`}),i(c,{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,h]=r,p=u(100*s.a),g=u(360*a.h);let d;if("hex"===n)d=e.color.toHexString(!0),i.value=e.hex;else if("hsl"===n){const t=u(100*a.l),o=u(100*a.s);d=e.color.toHslString(),i.value=""+g,l.value=""+o,c.value=""+t,h.value=""+p}else if("hwb"===n){const{w:t,b:o}=e.hwb,n=u(100*t),r=u(100*o);d=e.color.toHwbString(),i.value=""+g,l.value=""+n,c.value=""+r,h.value=""+p}else if("rgb"===n){let{r:t,g:o,b:n}=e.rgb;[t,o,n]=[t,o,n].map(u),d=e.color.toRgbString(),i.value=""+t,l.value=""+o,c.value=""+n,h.value=""+p}e.value=""+d,t||d===o||Et(e)}togglePicker(t){t&&t.preventDefault();const e=this,{colorPicker:o}=e;e.isOpen&&dt(o,"show")?e.hide(!0):Ct(e,o)}showPicker(){const t=this,{colorPicker:e}=t;["top","bottom"].some(t=>dt(e,t))||Ct(t,e)}toggleMenu(t){t&&t.preventDefault();const e=this,{colorMenu:o}=e;e.isOpen&&dt(o,"show")?e.hide(!0):Ct(e,o)}hide(t){const e=this;if(e.isOpen){const{pickerToggle:o,menuToggle:r,colorPicker:s,colorMenu:i,parent:l,input:c}=e,u=dt(s,"show"),h=u?s:i,p=u?o:r,g=h&&function(t){const e=a(t,"transitionProperty"),o=a(t,"transitionDuration"),n=o.includes("ms")?1:1e3,r=e&&"none"!==e?parseFloat(o)*n:0;return Number.isNaN(r)?0:r}(h);e.value=e.color.toString(!0),h&&(ft(h,"show"),n(p,"aria-expanded","false"),setTimeout(()=>{Nt(h),it(".show",l)||(ft(l,"open"),At(e),e.isOpen=!1)},g)),t||gt(o),n(c,"tabindex","-1"),p===r&&n(r,"tabindex","-1")}}dispose(){const{input:t,parent:e}=this;this.hide(!0),Pt(this),[...e.children].forEach(e=>{e!==t&&e.remove()}),mt(t,"tabindex"),i(t,{backgroundColor:""}),["txt-light","txt-dark"].forEach(t=>ft(e,t)),ut.remove(t,"color-picker")}}function Tt(t,e){if(e){const{input:o,colorPicker:r}=t,{value:s,format:a,colorPresets:i,colorKeywords:l,componentLabels:c,colorLabels:u}=r,{id:h,placeholder:p}=o;if(n(t,"data-id",h),n(t,"data-value",s),n(t,"data-format",a),n(t,"data-placeholder",p),pt(wt).some(t=>wt[t]!==c[t])&&n(t,"data-component-labels",JSON.stringify(c)),xt.every(t=>t===u[t])||n(t,"data-color-labels",xt.map(t=>u[t]).join(",")),i instanceof vt){const{hue:e,hueSteps:o,lightSteps:r}=i;n(t,"data-color-presets",JSON.stringify({hue:e,hueSteps:o,lightSteps:r}))}Array.isArray(i)&&i.length&&n(t,"data-color-presets",i.join(",")),l&&n(t,"data-color-keywords",l.join(",")),setTimeout(e,0)}else mt(t,"data-value"),mt(t,"data-format"),mt(t,"data-placeholder"),mt(t,"data-component-labels"),mt(t,"data-color-labels"),mt(t,"data-color-presets"),mt(t,"data-color-keywords")}e(Mt,{Color:j,ColorPalette:vt,Version:"1.0.0",getInstance:t=>{return e=t,o="color-picker",ut.get(e,o);var e,o},init:t=>new Mt(t),selector:'[data-function="color-picker"]',roundPart:u,setElementStyle:i,setAttribute:n,getBoundingClientRect:rt});let Rt=0;class Ft extends HTMLElement{constructor(){super(),this.attachShadow({mode:"open"})}get value(){return this.input&&this.input.value}connectedCallback(){if(this.input)return;let t=r(this,"data-id");const e=r(this,"data-value")||"#fff",s=r(this,"data-format")||"rgb",a=r(this,"data-placeholder")||"";t||(t=`color-picker-${s}-${Rt}`,Rt+=1);const i=o({tagName:"input",type:"text",className:"color-preview btn-appearance"});n(i,"id",t),n(i,"name",t),n(i,"autocomplete","off"),n(i,"spellcheck","false"),n(i,"value",e),n(i,"placeholder",a),this.append(i),this.input=i,this.colorPicker=new Mt(i),this.shadowRoot.append(o("slot")),Tt(this)}disconnectedCallback(){const t=this,{input:o,colorPicker:n,shadowRoot:r}=t;Tt(t,()=>{o.remove(),n.dispose(),r.innerHTML="",e(t,{colorPicker:void 0,input:void 0})})}}return e(Ft,{Color:j,ColorPicker:Mt,ColorPalette:vt,getInstance:Mt.getInstance,Version:"1.0.0"}),customElements.define("color-picker",Ft),Ft}));
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * ColorPicker v0.0.2 (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
  */
@@ -9,27 +9,26 @@ const EventRegistry = {};
9
9
  /**
10
10
  * The global event listener.
11
11
  *
12
- * @this {Element | HTMLElement | Window | Document}
13
- * @param {Event} e
14
- * @returns {void}
12
+ * @type {EventListener}
13
+ * @this {EventTarget}
15
14
  */
16
15
  function globalListener(e) {
17
16
  const that = this;
18
- const { type } = e;
19
- const oneEvMap = EventRegistry[type] ? [...EventRegistry[type]] : [];
17
+ const { type, target } = e;
20
18
 
21
- oneEvMap.forEach((elementsMap) => {
19
+ [...EventRegistry[type]].forEach((elementsMap) => {
22
20
  const [element, listenersMap] = elementsMap;
23
- [...listenersMap].forEach((listenerMap) => {
24
- if (element === that) {
21
+ /* istanbul ignore else */
22
+ if ([target, that].some((el) => element === el)) {
23
+ [...listenersMap].forEach((listenerMap) => {
25
24
  const [listener, options] = listenerMap;
26
25
  listener.apply(element, [e]);
27
26
 
28
27
  if (options && options.once) {
29
28
  removeListener(element, type, listener, options);
30
29
  }
31
- }
32
- });
30
+ });
31
+ }
33
32
  });
34
33
  }
35
34
 
@@ -37,10 +36,7 @@ function globalListener(e) {
37
36
  * Register a new listener with its options and attach the `globalListener`
38
37
  * to the target if this is the first listener.
39
38
  *
40
- * @param {Element | HTMLElement | Window | Document} element
41
- * @param {string} eventType
42
- * @param {EventListenerObject['handleEvent']} listener
43
- * @param {AddEventListenerOptions=} options
39
+ * @type {Listener.ListenerAction<EventTarget>}
44
40
  */
45
41
  const addListener = (element, eventType, listener, options) => {
46
42
  // get element listeners first
@@ -58,9 +54,7 @@ const addListener = (element, eventType, listener, options) => {
58
54
  const { size } = oneElementMap;
59
55
 
60
56
  // register listener with its options
61
- if (oneElementMap) {
62
- oneElementMap.set(listener, options);
63
- }
57
+ oneElementMap.set(listener, options);
64
58
 
65
59
  // add listener last
66
60
  if (!size) {
@@ -72,10 +66,7 @@ const addListener = (element, eventType, listener, options) => {
72
66
  * Remove a listener from registry and detach the `globalListener`
73
67
  * if no listeners are found in the registry.
74
68
  *
75
- * @param {Element | HTMLElement | Window | Document} element
76
- * @param {string} eventType
77
- * @param {EventListenerObject['handleEvent']} listener
78
- * @param {AddEventListenerOptions=} options
69
+ * @type {Listener.ListenerAction<EventTarget>}
79
70
  */
80
71
  const removeListener = (element, eventType, listener, options) => {
81
72
  // get listener first
@@ -94,6 +85,7 @@ const removeListener = (element, eventType, listener, options) => {
94
85
  if (!oneEventMap || !oneEventMap.size) delete EventRegistry[eventType];
95
86
 
96
87
  // remove listener last
88
+ /* istanbul ignore else */
97
89
  if (!oneElementMap || !oneElementMap.size) {
98
90
  element.removeEventListener(eventType, globalListener, eventOptions);
99
91
  }
@@ -195,12 +187,6 @@ const keydownEvent = 'keydown';
195
187
  */
196
188
  const changeEvent = 'change';
197
189
 
198
- /**
199
- * A global namespace for `touchstart` event.
200
- * @type {string}
201
- */
202
- const touchstartEvent = 'touchstart';
203
-
204
190
  /**
205
191
  * A global namespace for `touchmove` event.
206
192
  * @type {string}
@@ -208,28 +194,22 @@ const touchstartEvent = 'touchstart';
208
194
  const touchmoveEvent = 'touchmove';
209
195
 
210
196
  /**
211
- * A global namespace for `touchend` event.
212
- * @type {string}
213
- */
214
- const touchendEvent = 'touchend';
215
-
216
- /**
217
- * A global namespace for `mousedown` event.
197
+ * A global namespace for `pointerdown` event.
218
198
  * @type {string}
219
199
  */
220
- const mousedownEvent = 'mousedown';
200
+ const pointerdownEvent = 'pointerdown';
221
201
 
222
202
  /**
223
- * A global namespace for `mousemove` event.
203
+ * A global namespace for `pointermove` event.
224
204
  * @type {string}
225
205
  */
226
- const mousemoveEvent = 'mousemove';
206
+ const pointermoveEvent = 'pointermove';
227
207
 
228
208
  /**
229
- * A global namespace for `mouseup` event.
209
+ * A global namespace for `pointerup` event.
230
210
  * @type {string}
231
211
  */
232
- const mouseupEvent = 'mouseup';
212
+ const pointerupEvent = 'pointerup';
233
213
 
234
214
  /**
235
215
  * A global namespace for `scroll` event.
@@ -277,27 +257,6 @@ function getDocumentElement(node) {
277
257
  return getDocument(node).documentElement;
278
258
  }
279
259
 
280
- /**
281
- * Returns the `Window` object of a target node.
282
- * @see https://github.com/floating-ui/floating-ui
283
- *
284
- * @param {(Node | HTMLElement | Element | Window)=} node target node
285
- * @returns {globalThis}
286
- */
287
- function getWindow(node) {
288
- if (node == null) {
289
- return window;
290
- }
291
-
292
- if (!(node instanceof Window)) {
293
- const { ownerDocument } = node;
294
- return ownerDocument ? ownerDocument.defaultView || window : window;
295
- }
296
-
297
- // @ts-ignore
298
- return node;
299
- }
300
-
301
260
  /**
302
261
  * Shortcut for `window.getComputedStyle(element).propertyName`
303
262
  * static method.
@@ -2246,17 +2205,16 @@ function getColorMenu(self, colorsSource, menuClass) {
2246
2205
  const isOptionsMenu = menuClass === 'color-options';
2247
2206
  const isPalette = colorsSource instanceof ColorPalette;
2248
2207
  const menuLabel = isOptionsMenu ? presetsLabel : defaultsLabel;
2249
- let colorsArray = isPalette ? colorsSource.colors : colorsSource;
2250
- colorsArray = colorsArray instanceof Array ? colorsArray : [];
2208
+ const colorsArray = isPalette ? colorsSource.colors : colorsSource;
2251
2209
  const colorsCount = colorsArray.length;
2252
2210
  const { lightSteps } = isPalette ? colorsSource : { lightSteps: null };
2253
- const fit = lightSteps || [9, 10].find((x) => colorsCount > x * 2 && !(colorsCount % x)) || 5;
2211
+ const fit = lightSteps || [9, 10].find((x) => colorsCount >= x * 2 && !(colorsCount % x)) || 5;
2254
2212
  const isMultiLine = isOptionsMenu && colorsCount > fit;
2255
2213
  let rowCountHover = 2;
2256
- rowCountHover = isMultiLine && colorsCount >= fit * 2 ? 3 : rowCountHover;
2257
- rowCountHover = colorsCount >= fit * 3 ? 4 : rowCountHover;
2258
- rowCountHover = colorsCount >= fit * 4 ? 5 : rowCountHover;
2259
- const rowCount = rowCountHover - (colorsCount < fit * 3 ? 1 : 2);
2214
+ rowCountHover = isMultiLine && colorsCount > fit * 2 ? 3 : rowCountHover;
2215
+ rowCountHover = isMultiLine && colorsCount > fit * 3 ? 4 : rowCountHover;
2216
+ rowCountHover = isMultiLine && colorsCount > fit * 4 ? 5 : rowCountHover;
2217
+ const rowCount = rowCountHover - (colorsCount <= fit * 3 ? 1 : 2);
2260
2218
  const isScrollable = isMultiLine && colorsCount > rowCount * fit;
2261
2219
  let finalClass = menuClass;
2262
2220
  finalClass += isScrollable ? ' scrollable' : '';
@@ -2264,7 +2222,7 @@ function getColorMenu(self, colorsSource, menuClass) {
2264
2222
  const gap = isMultiLine ? '1px' : '0.25rem';
2265
2223
  let optionSize = isMultiLine ? 1.75 : 2;
2266
2224
  optionSize = fit > 5 && isMultiLine ? 1.5 : optionSize;
2267
- const menuHeight = `${(rowCount || 1) * optionSize}rem`;
2225
+ const menuHeight = `${rowCount * optionSize}rem`;
2268
2226
  const menuHeightHover = `calc(${rowCountHover} * ${optionSize}rem + ${rowCountHover - 1} * ${gap})`;
2269
2227
  /** @type {HTMLUListElement} */
2270
2228
  // @ts-ignore -- <UL> is an `HTMLElement`
@@ -2371,16 +2329,14 @@ function setMarkup(self) {
2371
2329
  });
2372
2330
 
2373
2331
  // color presets
2374
- if ((colorPresets instanceof Array && colorPresets.length)
2375
- || (colorPresets instanceof ColorPalette && colorPresets.colors)) {
2376
- const presetsMenu = getColorMenu(self, colorPresets, 'color-options');
2377
- presetsDropdown.append(presetsMenu);
2332
+ if (colorPresets) {
2333
+ presetsDropdown.append(getColorMenu(self, colorPresets, 'color-options'));
2378
2334
  }
2379
2335
 
2380
2336
  // explicit defaults [reset, initial, inherit, transparent, currentColor]
2337
+ // also custom defaults [default: #069, complementary: #930]
2381
2338
  if (colorKeywords && colorKeywords.length) {
2382
- const keywordsMenu = getColorMenu(self, colorKeywords, 'color-defaults');
2383
- presetsDropdown.append(keywordsMenu);
2339
+ presetsDropdown.append(getColorMenu(self, colorKeywords, 'color-defaults'));
2384
2340
  }
2385
2341
 
2386
2342
  const presetsBtn = createElement({
@@ -2417,7 +2373,7 @@ function setMarkup(self) {
2417
2373
  setAttribute(input, tabIndex, '-1');
2418
2374
  }
2419
2375
 
2420
- var version = "0.0.2";
2376
+ var version = "1.0.0";
2421
2377
 
2422
2378
  // @ts-ignore
2423
2379
 
@@ -2439,7 +2395,7 @@ const colorPickerDefaults = {
2439
2395
  // ColorPicker Static Methods
2440
2396
  // ==========================
2441
2397
 
2442
- /** @type {CP.GetInstance<ColorPicker>} */
2398
+ /** @type {CP.GetInstance<ColorPicker, HTMLInputElement>} */
2443
2399
  const getColorPickerInstance = (element) => getInstance(element, colorPickerString);
2444
2400
 
2445
2401
  /** @type {CP.InitCallback<ColorPicker>} */
@@ -2474,12 +2430,10 @@ function toggleEventsOnShown(self, action) {
2474
2430
  const fn = action ? addListener : removeListener;
2475
2431
  const { input, colorMenu, parent } = self;
2476
2432
  const doc = getDocument(input);
2477
- const win = getWindow(input);
2478
- const pointerEvents = `on${touchstartEvent}` in doc
2479
- ? { down: touchstartEvent, move: touchmoveEvent, up: touchendEvent }
2480
- : { down: mousedownEvent, move: mousemoveEvent, up: mouseupEvent };
2433
+ // const win = getWindow(input);
2434
+ const win = doc.defaultView;
2481
2435
 
2482
- fn(self.controls, pointerEvents.down, self.pointerDown);
2436
+ fn(self.controls, pointerdownEvent, self.pointerDown);
2483
2437
  self.controlKnobs.forEach((x) => fn(x, keydownEvent, self.handleKnobs));
2484
2438
 
2485
2439
  // @ts-ignore -- this is `Window`
@@ -2494,8 +2448,8 @@ function toggleEventsOnShown(self, action) {
2494
2448
  fn(colorMenu, keydownEvent, self.menuKeyHandler);
2495
2449
  }
2496
2450
 
2497
- fn(doc, pointerEvents.move, self.pointerMove);
2498
- fn(doc, pointerEvents.up, self.pointerUp);
2451
+ fn(doc, pointermoveEvent, self.pointerMove);
2452
+ fn(doc, pointerupEvent, self.pointerUp);
2499
2453
  fn(parent, focusoutEvent, self.handleFocusOut);
2500
2454
  fn(doc, keyupEvent, self.handleDismiss);
2501
2455
  }
@@ -2514,6 +2468,7 @@ function firePickerChange(self) {
2514
2468
  * @returns {void}
2515
2469
  */
2516
2470
  function removePosition(element) {
2471
+ /* istanbul ignore else */
2517
2472
  if (element) {
2518
2473
  ['bottom', 'top'].forEach((x) => removeClass(element, x));
2519
2474
  }
@@ -2616,6 +2571,7 @@ class ColorPicker {
2616
2571
  } = normalizeOptions(this.isCE ? parent : input, colorPickerDefaults, config || {});
2617
2572
 
2618
2573
  let translatedColorLabels = colorNames;
2574
+ /* istanbul ignore else */
2619
2575
  if (colorLabels instanceof Array && colorLabels.length === 17) {
2620
2576
  translatedColorLabels = colorLabels;
2621
2577
  } else if (colorLabels && colorLabels.split(',').length === 17) {
@@ -2632,7 +2588,7 @@ class ColorPicker {
2632
2588
  ? JSON.parse(componentLabels) : componentLabels;
2633
2589
 
2634
2590
  /** @type {Record<string, string>} */
2635
- self.componentLabels = ObjectAssign(colorPickerLabels, tempComponentLabels);
2591
+ self.componentLabels = ObjectAssign({ ...colorPickerLabels }, tempComponentLabels);
2636
2592
 
2637
2593
  /** @type {Color} */
2638
2594
  self.color = new Color(input.value || '#fff', format);
@@ -2641,14 +2597,14 @@ class ColorPicker {
2641
2597
  self.format = format;
2642
2598
 
2643
2599
  // set colour defaults
2644
- if (colorKeywords instanceof Array) {
2600
+ if (colorKeywords instanceof Array && colorKeywords.length) {
2645
2601
  self.colorKeywords = colorKeywords;
2646
2602
  } else if (typeof colorKeywords === 'string' && colorKeywords.length) {
2647
2603
  self.colorKeywords = colorKeywords.split(',').map((x) => x.trim());
2648
2604
  }
2649
2605
 
2650
2606
  // set colour presets
2651
- if (colorPresets instanceof Array) {
2607
+ if (colorPresets instanceof Array && colorPresets.length) {
2652
2608
  self.colorPresets = colorPresets;
2653
2609
  } else if (typeof colorPresets === 'string' && colorPresets.length) {
2654
2610
  if (isValidJSON(colorPresets)) {
@@ -2779,6 +2735,7 @@ class ColorPicker {
2779
2735
  let colorName;
2780
2736
 
2781
2737
  // determine color appearance
2738
+ /* istanbul ignore else */
2782
2739
  if (lightness === 100 && saturation === 0) {
2783
2740
  colorName = colorLabels.white;
2784
2741
  } else if (lightness === 0) {
@@ -2879,13 +2836,14 @@ class ColorPicker {
2879
2836
  const self = this;
2880
2837
  const { activeElement } = getDocument(self.input);
2881
2838
 
2882
- if ((e.type === touchmoveEvent && self.dragElement)
2839
+ self.updateDropdownPosition();
2840
+
2841
+ /* istanbul ignore next */
2842
+ if (([pointermoveEvent, touchmoveEvent].includes(e.type) && self.dragElement)
2883
2843
  || (activeElement && self.controlKnobs.includes(activeElement))) {
2884
2844
  e.stopPropagation();
2885
2845
  e.preventDefault();
2886
2846
  }
2887
-
2888
- self.updateDropdownPosition();
2889
2847
  }
2890
2848
 
2891
2849
  /**
@@ -2959,7 +2917,9 @@ class ColorPicker {
2959
2917
 
2960
2918
  self.update();
2961
2919
 
2920
+ /* istanbul ignore else */
2962
2921
  if (currentActive !== target) {
2922
+ /* istanbul ignore else */
2963
2923
  if (currentActive) {
2964
2924
  removeClass(currentActive, 'active');
2965
2925
  removeAttribute(currentActive, ariaSelected);
@@ -2977,15 +2937,13 @@ class ColorPicker {
2977
2937
 
2978
2938
  /**
2979
2939
  * The `ColorPicker` *touchstart* / *mousedown* events listener for control knobs.
2980
- * @param {TouchEvent} e
2940
+ * @param {PointerEvent} e
2981
2941
  * @this {ColorPicker}
2982
2942
  */
2983
2943
  pointerDown(e) {
2984
2944
  const self = this;
2985
2945
  /** @type {*} */
2986
- const {
2987
- type, target, touches, pageX, pageY,
2988
- } = e;
2946
+ const { target, pageX, pageY } = e;
2989
2947
  const { colorMenu, visuals, controlKnobs } = self;
2990
2948
  const [v1, v2, v3] = visuals;
2991
2949
  const [c1, c2, c3] = controlKnobs;
@@ -2993,11 +2951,10 @@ class ColorPicker {
2993
2951
  const visual = controlKnobs.includes(target) ? target.previousElementSibling : target;
2994
2952
  const visualRect = getBoundingClientRect(visual);
2995
2953
  const html = getDocumentElement(v1);
2996
- const X = type === 'touchstart' ? touches[0].pageX : pageX;
2997
- const Y = type === 'touchstart' ? touches[0].pageY : pageY;
2998
- const offsetX = X - html.scrollLeft - visualRect.left;
2999
- const offsetY = Y - html.scrollTop - visualRect.top;
2954
+ const offsetX = pageX - html.scrollLeft - visualRect.left;
2955
+ const offsetY = pageY - html.scrollTop - visualRect.top;
3000
2956
 
2957
+ /* istanbul ignore else */
3001
2958
  if (target === v1 || target === c1) {
3002
2959
  self.dragElement = visual;
3003
2960
  self.changeControl1(offsetX, offsetY);
@@ -3021,7 +2978,7 @@ class ColorPicker {
3021
2978
 
3022
2979
  /**
3023
2980
  * The `ColorPicker` *touchend* / *mouseup* events listener for control knobs.
3024
- * @param {TouchEvent} e
2981
+ * @param {PointerEvent} e
3025
2982
  * @this {ColorPicker}
3026
2983
  */
3027
2984
  pointerUp({ target }) {
@@ -3030,9 +2987,8 @@ class ColorPicker {
3030
2987
  const doc = getDocument(parent);
3031
2988
  const currentOpen = querySelector(`${colorPickerParentSelector}.open`, doc) !== null;
3032
2989
  const selection = doc.getSelection();
3033
- // @ts-ignore
2990
+
3034
2991
  if (!self.dragElement && !selection.toString().length
3035
- // @ts-ignore
3036
2992
  && !parent.contains(target)) {
3037
2993
  self.hide(currentOpen);
3038
2994
  }
@@ -3042,25 +2998,20 @@ class ColorPicker {
3042
2998
 
3043
2999
  /**
3044
3000
  * The `ColorPicker` *touchmove* / *mousemove* events listener for control knobs.
3045
- * @param {TouchEvent} e
3001
+ * @param {PointerEvent} e
3046
3002
  */
3047
3003
  pointerMove(e) {
3048
3004
  const self = this;
3049
3005
  const { dragElement, visuals } = self;
3050
3006
  const [v1, v2, v3] = visuals;
3051
- const {
3052
- // @ts-ignore
3053
- type, touches, pageX, pageY,
3054
- } = e;
3007
+ const { pageX, pageY } = e;
3055
3008
 
3056
3009
  if (!dragElement) return;
3057
3010
 
3058
3011
  const controlRect = getBoundingClientRect(dragElement);
3059
3012
  const win = getDocumentElement(v1);
3060
- const X = type === touchmoveEvent ? touches[0].pageX : pageX;
3061
- const Y = type === touchmoveEvent ? touches[0].pageY : pageY;
3062
- const offsetX = X - win.scrollLeft - controlRect.left;
3063
- const offsetY = Y - win.scrollTop - controlRect.top;
3013
+ const offsetX = pageX - win.scrollLeft - controlRect.left;
3014
+ const offsetY = pageY - win.scrollTop - controlRect.top;
3064
3015
 
3065
3016
  if (dragElement === v1) {
3066
3017
  self.changeControl1(offsetX, offsetY);
@@ -3094,13 +3045,16 @@ class ColorPicker {
3094
3045
  const currentKnob = controlKnobs.find((x) => x === activeElement);
3095
3046
  const yRatio = offsetHeight / 360;
3096
3047
 
3048
+ /* istanbul ignore else */
3097
3049
  if (currentKnob) {
3098
3050
  let offsetX = 0;
3099
3051
  let offsetY = 0;
3100
3052
 
3053
+ /* istanbul ignore else */
3101
3054
  if (target === c1) {
3102
3055
  const xRatio = offsetWidth / 100;
3103
3056
 
3057
+ /* istanbul ignore else */
3104
3058
  if ([keyArrowLeft, keyArrowRight].includes(code)) {
3105
3059
  self.controlPositions.c1x += code === keyArrowRight ? xRatio : -xRatio;
3106
3060
  } else if ([keyArrowUp, keyArrowDown].includes(code)) {
@@ -3146,6 +3100,7 @@ class ColorPicker {
3146
3100
  const isNonColorValue = self.hasNonColor && nonColors.includes(currentValue);
3147
3101
  const alpha = i4 ? v4 : (1 - controlPositions.c3y / offsetHeight);
3148
3102
 
3103
+ /* istanbul ignore else */
3149
3104
  if (activeElement === input || (activeElement && inputs.includes(activeElement))) {
3150
3105
  if (activeElement === input) {
3151
3106
  if (isNonColorValue) {
@@ -3460,6 +3415,7 @@ class ColorPicker {
3460
3415
  const hue = roundPart(hsl.h * 360);
3461
3416
  let newColor;
3462
3417
 
3418
+ /* istanbul ignore else */
3463
3419
  if (format === 'hex') {
3464
3420
  newColor = self.color.toHexString(true);
3465
3421
  i1.value = self.hex;
@@ -3560,15 +3516,15 @@ class ColorPicker {
3560
3516
  const relatedBtn = openPicker ? pickerToggle : menuToggle;
3561
3517
  const animationDuration = openDropdown && getElementTransitionDuration(openDropdown);
3562
3518
 
3563
- // if (!self.isValid) {
3564
3519
  self.value = self.color.toString(true);
3565
- // }
3566
3520
 
3521
+ /* istanbul ignore else */
3567
3522
  if (openDropdown) {
3568
3523
  removeClass(openDropdown, 'show');
3569
3524
  setAttribute(relatedBtn, ariaExpanded, 'false');
3570
3525
  setTimeout(() => {
3571
3526
  removePosition(openDropdown);
3527
+ /* istanbul ignore else */
3572
3528
  if (!querySelector('.show', parent)) {
3573
3529
  removeClass(parent, 'open');
3574
3530
  toggleEventsOnShown(self);
@@ -3581,7 +3537,7 @@ class ColorPicker {
3581
3537
  focus(pickerToggle);
3582
3538
  }
3583
3539
  setAttribute(input, tabIndex, '-1');
3584
- if (menuToggle) {
3540
+ if (relatedBtn === menuToggle) {
3585
3541
  setAttribute(menuToggle, tabIndex, '-1');
3586
3542
  }
3587
3543
  }