@thednp/color-picker 0.0.1-alpha1 → 0.0.1-alpha2
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE +1 -1
- package/README.md +40 -19
- package/dist/css/color-picker.css +481 -337
- package/dist/css/color-picker.min.css +2 -0
- package/dist/css/color-picker.rtl.css +506 -0
- package/dist/css/color-picker.rtl.min.css +2 -0
- package/dist/js/color-picker-element-esm.js +3810 -2
- package/dist/js/color-picker-element-esm.min.js +2 -0
- package/dist/js/color-picker-element.js +2009 -1242
- package/dist/js/color-picker-element.min.js +2 -2
- package/dist/js/color-picker-esm.js +3704 -0
- package/dist/js/color-picker-esm.min.js +2 -0
- package/dist/js/color-picker.js +1962 -1256
- package/dist/js/color-picker.min.js +2 -2
- package/package.json +18 -9
- package/src/js/color-palette.js +62 -0
- package/src/js/color-picker-element.js +55 -13
- package/src/js/color-picker.js +686 -595
- package/src/js/color.js +615 -349
- package/src/js/index.js +0 -9
- package/src/js/util/colorNames.js +2 -152
- package/src/js/util/colorPickerLabels.js +22 -0
- package/src/js/util/getColorControls.js +103 -0
- package/src/js/util/getColorForm.js +27 -19
- package/src/js/util/getColorMenu.js +95 -0
- package/src/js/util/isValidJSON.js +13 -0
- package/src/js/util/nonColors.js +5 -0
- package/src/js/util/templates.js +1 -0
- package/src/scss/color-picker.rtl.scss +23 -0
- package/src/scss/color-picker.scss +430 -0
- package/types/cp.d.ts +263 -160
- package/types/index.d.ts +9 -2
- package/types/source/source.ts +2 -1
- package/types/source/types.d.ts +28 -5
- package/dist/js/color-picker.esm.js +0 -2998
- package/dist/js/color-picker.esm.min.js +0 -2
- package/src/js/util/getColorControl.js +0 -49
- package/src/js/util/init.js +0 -14
@@ -1,2 +0,0 @@
|
|
1
|
-
// ColorPicker v0.0.1alpha1 | thednp © 2022 | MIT-License
|
2
|
-
function e(e){return e instanceof HTMLElement?e.ownerDocument:e instanceof Window?e.document:window.document}const t=[Document,Element,HTMLElement];function o(o,n){return(n&&t.some(e=>n instanceof e)?n:e()).querySelectorAll(o)}const n={};function r(e){const t=this,{type:o}=e;(n[o]?[...n[o]]:[]).forEach(n=>{const[r,a]=n;[...a].forEach(n=>{if(r===t){const[t,a]=n;t.apply(r,[e]),a&&a.once&&s(r,o,t,a)}})})}const a=(e,t,o,a)=>{n[t]||(n[t]=new Map);const s=n[t];s.has(e)||s.set(e,new Map);const i=s.get(e),{size:l}=i;i&&i.set(o,a),l||e.addEventListener(t,r,a)},s=(e,t,o,a)=>{const s=n[t],i=s&&s.get(e),l=i&&i.get(o),{options:c}=void 0!==l?l:{options:a};i&&i.has(o)&&i.delete(o),!s||i&&i.size||s.delete(e),s&&s.size||delete n[t],i&&i.size||e.removeEventListener(t,r,c)},i="ArrowDown",l="ArrowUp",c="ArrowLeft",h="ArrowRight",u="Enter",d="Space",{userAgentData:g}=navigator,p=g,{userAgent:m}=navigator,f=m,b=/iPhone|iPad|iPod|Android/i;let w=!1;w=p?p.brands.some(e=>b.test(e.brand)):b.test(f);const v=w;let x=1;const k=new Map;function y(e,t){const{width:o,height:n,top:r,right:a,bottom:s,left:i}=e.getBoundingClientRect();let l=1,c=1;if(t&&e instanceof HTMLElement){const{offsetWidth:t,offsetHeight:r}=e;l=t>0&&Math.round(o)/t||1,c=r>0&&Math.round(n)/r||1}return{width:o/l,height:n/c,top:r/c,right:a/l,bottom:s/c,left:i/l,x:i/l,y:r/c}}const S=[Element,HTMLElement];function M(o,n){const r=t.some(e=>n instanceof e)?n:e();return S.some(e=>o instanceof e)?o:r.querySelector(o)}const $=(e,t)=>Object.assign(e,t);function C(t){if("string"==typeof t)return e().createElement(t);const{tagName:o}=t,n={...t},r=C(o);return delete n.tagName,$(r,n),r}function L(t,o){if("string"==typeof o)return e().createElementNS(t,o);const{tagName:n}=o,r={...o},a=L(t,n);return delete r.tagName,$(a,r),a}const N=new Map,H={set:(e,t,o)=>{const n=M(e);if(!n)return;N.has(t)||N.set(t,new Map);N.get(t).set(n,o)},getAllFor:e=>N.get(e)||null,get:(e,t)=>{const o=M(e),n=H.getAllFor(t);return o&&n&&n.get(o)||null},remove:(e,t)=>{const o=M(e),n=N.get(t);n&&o&&(n.delete(o),0===n.size&&N.delete(t))}};function P(e,t){return e.classList.contains(t)}function E(e,t){e.classList.add(t)}function A(e,t){e.classList.remove(t)}const T=(e,t,o)=>e.setAttribute(t,o),R=(e,t)=>e.getAttribute(t),F="v-hidden";function D(e,t,o,n,r){const a=`appearance${e}_${t}`,s=1===e?"color-pointer":"color-slider",i=C({tagName:"div",className:"color-control"});T(i,"role","presentation"),i.append(C({id:a,tagName:"label",className:"color-label v-hidden",ariaLive:"polite"}),C({tagName:"canvas",className:"visual-control"+e,ariaHidden:"true",width:""+o,height:""+n}));const l=C({tagName:"div",className:s+" knob"});return T(l,"aria-labelledby",r||a),T(l,"tabindex","0"),i.append(l),i}const I=(e,t)=>{$(e.style,t)},O=["aliceblue","antiquewhite","aqua","aquamarine","azure","beige","bisque","black","blanchedalmond","blue","blueviolet","brown","burlywood","cadetblue","chartreuse","chocolate","coral","cornflowerblue","cornsilk","crimson","cyan","darkblue","darkcyan","darkgoldenrod","darkgray","darkgreen","darkgrey","darkkhaki","darkmagenta","darkolivegreen","darkorange","darkorchid","darkred","darksalmon","darkseagreen","darkslateblue","darkslategray","darkslategrey","darkturquoise","darkviolet","deeppink","deepskyblue","dimgray","dimgrey","dodgerblue","firebrick","floralwhite","forestgreen","fuchsia","gainsboro","ghostwhite","goldenrod","gold","gray","green","greenyellow","grey","honeydew","hotpink","indianred","indigo","ivory","khaki","lavenderblush","lavender","lawngreen","lemonchiffon","lightblue","lightcoral","lightcyan","lightgoldenrodyellow","lightgray","lightgreen","lightgrey","lightpink","lightsalmon","lightseagreen","lightskyblue","lightslategray","lightslategrey","lightsteelblue","lightyellow","lime","limegreen","linen","magenta","maroon","mediumaquamarine","mediumblue","mediumorchid","mediumpurple","mediumseagreen","mediumslateblue","mediumspringgreen","mediumturquoise","mediumvioletred","midnightblue","mintcream","mistyrose","moccasin","navajowhite","navy","oldlace","olive","olivedrab","orange","orangered","orchid","palegoldenrod","palegreen","paleturquoise","palevioletred","papayawhip","peachpuff","peru","pink","plum","powderblue","purple","rebeccapurple","red","rosybrown","royalblue","saddlebrown","salmon","sandybrown","seagreen","seashell","sienna","silver","skyblue","slateblue","slategray","slategrey","snow","springgreen","steelblue","tan","teal","thistle","tomato","turquoise","violet","wheat","white","whitesmoke","yellow","yellowgreen"],q="(?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?)",Y=`[\\s|\\(]+(${q})[,|\\s]+(${q})[,|\\s]+(${q})\\s*\\)?`,_=`[\\s|\\(]+(${q})[,|\\s]+(${q})[,|\\s]+(${q})[,|\\s]+(${q})\\s*\\)?`,V={CSS_UNIT:new RegExp(q),rgb:new RegExp("rgb"+Y),rgba:new RegExp("rgba"+_),hsl:new RegExp("hsl"+Y),hsla:new RegExp("hsla"+_),hsv:new RegExp("hsv"+Y),hsva:new RegExp("hsva"+_),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 K(e){return"string"==typeof e&&e.includes(".")&&1===parseFloat(e)}function G(e){return"string"==typeof e&&e.includes("%")}function U(e){return Boolean(V.CSS_UNIT.exec(String(e)))}function X(e,t){let o=e;return K(e)&&(o="100%"),o=360===t?o:Math.min(t,Math.max(0,parseFloat(o))),G(o)&&(o=parseInt(String(o*t),10)/100),Math.abs(o-t)<1e-6?1:(o=360===t?(o<0?o%t+t:o%t)/parseFloat(String(t)):o%t/parseFloat(String(t)),o)}function j(e){let t=parseFloat(e);return(Number.isNaN(t)||t<0||t>1)&&(t=1),t}function B(e){return Math.min(1,Math.max(0,e))}function z(t){const o=e(n).head;var n;I(o,{color:t});const r=function(e,t){const o=getComputedStyle(e);return t in o?o[t]:""}(o,"color");return I(o,{color:""}),r}function W(e){return e<=1?100*Number(e)+"%":e}function J(e){return 1===e.length?"0"+e:String(e)}function Z(e,t,o){return{r:255*X(e,255),g:255*X(t,255),b:255*X(o,255)}}function Q(e,t,o){const n=X(e,255),r=X(t,255),a=X(o,255),s=Math.max(n,r,a),i=Math.min(n,r,a);let l=0,c=0;const h=(s+i)/2;if(s===i)c=0,l=0;else{const e=s-i;switch(c=h>.5?e/(2-s-i):e/(s+i),s){case n:l=(r-a)/e+(r<a?6:0);break;case r:l=(a-n)/e+2;break;case a:l=(n-r)/e+4}l/=6}return{h:l,s:c,l:h}}function ee(e,t,o){let n=o;return n<0&&(n+=1),n>1&&(n-=1),n<1/6?e+6*n*(t-e):n<.5?t:n<2/3?e+(t-e)*(2/3-n)*6:e}function te(e,t,o){let n=0,r=0,a=0;const s=X(e,360),i=X(t,100),l=X(o,100);if(0===i)r=l,a=l,n=l;else{const e=l<.5?l*(1+i):l+i-l*i,t=2*l-e;n=ee(t,e,s+1/3),r=ee(t,e,s),a=ee(t,e,s-1/3)}return{r:255*n,g:255*r,b:255*a}}function oe(e,t,o){const n=X(e,255),r=X(t,255),a=X(o,255),s=Math.max(n,r,a),i=Math.min(n,r,a);let l=0;const c=s,h=s-i,u=0===s?0:h/s;if(s===i)l=0;else{switch(s){case n:l=(r-a)/h+(r<a?6:0);break;case r:l=(a-n)/h+2;break;case a:l=(n-r)/h+4}l/=6}return{h:l,s:u,v:c}}function ne(e,t,o){const n=6*X(e,360),r=X(t,100),a=X(o,100),s=Math.floor(n),i=n-s,l=a*(1-r),c=a*(1-i*r),h=a*(1-(1-i)*r),u=s%6;return{r:255*[a,c,l,l,h,a][u],g:255*[h,a,a,c,l,l][u],b:255*[l,l,h,a,a,c][u]}}function re(e,t,o){return[J(Math.round(e).toString(16)),J(Math.round(t).toString(16)),J(Math.round(o).toString(16))].join("")}function ae(e){return se(e)/255}function se(e){return parseInt(e,16)}function ie(e){return{r:e>>16,g:(65280&e)>>8,b:255&e}}function le(e){let t=e.trim().toLowerCase();if(0===t.length)return{r:0,g:0,b:0,a:0};let o=!1;if(O.includes(t))t=z(t),o=!0;else if("transparent"===t)return{r:0,g:0,b:0,a:0,format:"name"};let n=V.rgb.exec(t);return n?{r:n[1],g:n[2],b:n[3]}:(n=V.rgba.exec(t),n?{r:n[1],g:n[2],b:n[3],a:n[4]}:(n=V.hsl.exec(t),n?{h:n[1],s:n[2],l:n[3]}:(n=V.hsla.exec(t),n?{h:n[1],s:n[2],l:n[3],a:n[4]}:(n=V.hsv.exec(t),n?{h:n[1],s:n[2],v:n[3]}:(n=V.hsva.exec(t),n?{h:n[1],s:n[2],v:n[3],a:n[4]}:(n=V.hex8.exec(t),n?{r:se(n[1]),g:se(n[2]),b:se(n[3]),a:ae(n[4]),format:o?"name":"hex8"}:(n=V.hex6.exec(t),n?{r:se(n[1]),g:se(n[2]),b:se(n[3]),format:o?"name":"hex"}:(n=V.hex4.exec(t),n?{r:se(n[1]+n[1]),g:se(n[2]+n[2]),b:se(n[3]+n[3]),a:ae(n[4]+n[4]),format:o?"name":"hex8"}:(n=V.hex3.exec(t),!!n&&{r:se(n[1]+n[1]),g:se(n[2]+n[2]),b:se(n[3]+n[3]),format:o?"name":"hex"})))))))))}function ce(e){let t,o={r:0,g:0,b:0},n=e,r=null,a=null,s=null,i=!1,l="hex";return"string"==typeof e&&(n=le(e),n&&(i=!0)),"object"==typeof n&&(U(n.r)&&U(n.g)&&U(n.b)?(o=Z(n.r,n.g,n.b),i=!0,l="%"===(""+n.r).slice(-1)?"prgb":"rgb"):U(n.h)&&U(n.s)&&U(n.v)?(r=W(n.s),a=W(n.v),o=ne(n.h,r,a),i=!0,l="hsv"):U(n.h)&&U(n.s)&&U(n.l)&&(r=W(n.s),s=W(n.l),o=te(n.h,r,s),i=!0,l="hsl"),"a"in n&&(t=n.a)),{ok:i,format:n.format||l,r:Math.min(255,Math.max(o.r,0)),g:Math.min(255,Math.max(o.g,0)),b:Math.min(255,Math.max(o.b,0)),a:j(t)}}const he={format:"hex"};class ue{constructor(e,t){let o=e;const n="object"==typeof t?$(he,t):$({},he);o instanceof ue&&(o=ce(o)),"number"==typeof o&&(o=ie(o));const{r:r,g:a,b:s,a:i,ok:l,format:c}=ce(o);this.originalInput=o,this.r=r,this.g=a,this.b=s,this.a=i,this.ok=l,this.roundA=Math.round(100*this.a)/100,this.format=n.format||c,this.r<1&&(this.r=Math.round(this.r)),this.g<1&&(this.g=Math.round(this.g)),this.b<1&&(this.b=Math.round(this.b))}get isValid(){return this.ok}get isDark(){return this.brightness<128}get luminance(){const{r:e,g:t,b:o}=this;let n=0,r=0,a=0;const s=e/255,i=t/255,l=o/255;return n=s<=.03928?s/12.92:((s+.055)/1.055)**2.4,r=i<=.03928?i/12.92:((i+.055)/1.055)**2.4,a=l<=.03928?l/12.92:((l+.055)/1.055)**2.4,.2126*n+.7152*r+.0722*a}get brightness(){const{r:e,g:t,b:o}=this;return(299*e+587*t+114*o)/1e3}toRgb(){return{r:Math.round(this.r),g:Math.round(this.g),b:Math.round(this.b),a:this.a}}toRgbString(){const e=Math.round(this.r),t=Math.round(this.g),o=Math.round(this.b);return 1===this.a?`rgb(${e},${t},${o})`:`rgba(${e},${t},${o},${this.roundA})`}toHex(){return re(this.r,this.g,this.b)}toHexString(){return"#"+this.toHex()}toHsv(){const{h:e,s:t,v:o}=oe(this.r,this.g,this.b);return{h:360*e,s:t,v:o,a:this.a}}toHsl(){const{h:e,s:t,l:o}=Q(this.r,this.g,this.b);return{h:360*e,s:t,l:o,a:this.a}}toHslString(){const e=this.toHsl(),t=Math.round(e.h),o=Math.round(100*e.s),n=Math.round(100*e.l);return 1===this.a?`hsl(${t},${o}%,${n}%)`:`hsla(${t},${o}%,${n}%,${this.roundA})`}setAlpha(e){return this.a=j(e),this.roundA=Math.round(100*this.a)/100,this}saturate(e){if(!e)return this;const t=this.toHsl();return t.s+=e/100,t.s=B(t.s),new ue(t)}desaturate(e){return e?this.saturate(-e):this}greyscale(){return this.desaturate(100)}clone(){return new ue(this)}toString(){const{format:e}=this;return"rgb"===e?this.toRgbString():"hsl"===e?this.toHslString():this.toHexString()}}$(ue,{colorNames:O,CSS_INTEGER:"[-\\+]?\\d+%?",CSS_NUMBER:"[-\\+]?\\d*\\.\\d+%?",CSS_UNIT:q,PERMISSIVE_MATCH3:Y,PERMISSIVE_MATCH4:_,matchers:V,isOnePointZero:K,isPercentage:G,isValidCSSUnit:U,bound01:X,boundAlpha:j,clamp01:B,getHexFromColorName:z,convertToPercentage:W,convertHexToDecimal:ae,pad2:J,rgbToRgb:Z,rgbToHsl:Q,rgbToHex:re,rgbToHsv:oe,hslToRgb:te,hsvToRgb:ne,hue2rgb:ee,parseIntFromHex:se,numberInputToObject:ie,stringInputToObject:le,inputToRGB:ce});const de=["transparent","currentColor","inherit","initial"],ge=["white","black","grey","red","orange","brown","gold","olive","yellow","lime","green","teal","cyan","blue","violet","magenta","pink"],pe={pickerLabel:"Colour Picker",toggleLabel:"Select colour",menuLabel:"Select colour preset",requiredLabel:"Required",formatLabel:"Colour Format",formatHEX:"Hexadecimal Format",formatRGB:"RGB Format",formatHSL:"HSL Format",alphaLabel:"Alpha",appearanceLabel:"Colour Appearance",hexLabel:"Hexadecimal",hueLabel:"Hue",saturationLabel:"Saturation",lightnessLabel:"Lightness",redLabel:"Red",greenLabel:"Green",blueLabel:"Blue"};function me(e,t){const o=t?a:s,{input:n,pickerToggle:r,menuToggle:i}=e;o(n,"focusin",e.showPicker),o(r,"click",e.togglePicker),o(n,"keydown",e.keyHandler),i&&o(i,"click",e.toggleMenu)}function fe(e){const{input:t,parent:o,format:n,id:r,componentLabels:a,keywords:s}=e,i=R(t,"value")||"#fff",{toggleLabel:l,menuLabel:c,formatLabel:h,pickerLabel:u,appearanceLabel:d}=a,g=de.includes(i)?"#fff":i;e.color=new ue(g,{format:n});const p=v?150:230,m=v?150:230,f=v?" mobile":"",b="hsl"===n?`appearance_${r} appearance1_${r}`:"appearance1_"+r,w="hsl"===n?"appearance2_"+r:`appearance_${r} appearance2_${r}`,x=C({tagName:"button",className:"picker-toggle button-appearance",ariaExpanded:"false",ariaHasPopup:"true",ariaLive:"polite"});T(x,"tabindex","-1"),x.append(C({tagName:"span",className:F,innerText:"Open Color Picker"}));const k=C({tagName:"div",className:"color-dropdown picker"+f});T(k,"aria-labelledby",`picker-label-${r} format-label-${r}`),T(k,"role","group"),k.append(C({tagName:"label",className:F,ariaHidden:"true",id:"picker-label-"+r,innerText:""+u}),C({tagName:"label",className:F,ariaHidden:"true",id:"format-label-"+r,innerText:""+h}),C({tagName:"label",className:"color-appearance v-hidden",ariaHidden:"true",ariaLive:"polite",id:"appearance_"+r,innerText:""+d}));const y=C({tagName:"div",className:"color-controls "+n});y.append(D(1,r,p,m,b),D(2,r,21,m,w)),"hex"!==n&&y.append(D(3,r,21,m));const S=function(e){const{format:t,id:o}=e,n=C({tagName:"div",className:"color-form "+t});let r=["hex"];return"rgb"===t?r=["red","green","blue","alpha"]:"hsl"===t&&(r=["hue","saturation","lightness","alpha"]),r.forEach(e=>{const[r]="hex"===t?["#"]:(a=e,a.toUpperCase()).split("");var a;const s=`color_${t}_${e}_${o}`,i=C({tagName:"label"});T(i,"for",s),i.append(C({tagName:"span",ariaHidden:"true",innerText:r+":"}),C({tagName:"span",className:F,innerText:""+e}));const l=C({tagName:"input",id:s,type:"hex"===t?"text":"number",value:"alpha"===e?"1":"0",className:"color-input "+e,autocomplete:"off",spellcheck:"false"});if("hex"!==t){let o="1",n="0.01";"alpha"!==e&&("rgb"===t?(o="255",n="1"):"hue"===e?(o="360",n="1"):(o="100",n="1")),$(l,{min:"0",max:o,step:n})}n.append(i,l)}),n}(e);if(k.append(y,S),o.append(x,k),s){const e=s,r=C({tagName:"div",className:"color-dropdown menu"+f}),a=C({tagName:"ul",ariaLabel:""+c,className:"color-menu"});T(a,"role","listbox"),r.append(a),e.forEach(e=>{const o=e.trim(),r=new ue(o,{format:n}).toString()===R(t,"value"),s=C({tagName:"li",className:"color-option"+(r?" active":""),ariaSelected:r?"true":"false",innerText:""+e});T(s,"role","option"),T(s,"tabindex","0"),T(s,"data-value",""+o),a.append(s)});const i=C({tagName:"button",className:"menu-toggle button-appearance",ariaExpanded:"false",ariaHasPopup:"true"}),h=encodeURI("http://www.w3.org/2000/svg"),u=L(h,{tagName:"svg"});T(u,"xmlns",h),T(u,"aria-hidden","true"),T(u,"viewBox","0 0 512 512");const d=L(h,{tagName:"path"});T(d,"d","M98,158l157,156L411,158l27,27L255,368L71,185L98,158z"),T(d,"fill","#fff"),u.append(d),i.append(C({tagName:"span",className:F,innerText:""+l}),u),o.append(i,r)}s&&de.includes(i)&&(e.value=i)}function be(e,t){const o=t?a:s,n="ontouchstart"in document?{down:"touchstart",move:"touchmove",up:"touchend"}:{down:"mousedown",move:"mousemove",up:"mouseup"};o(e.controls,n.down,e.pointerDown),e.controlKnobs.forEach(t=>o(t,"keydown",e.handleKnobs)),o(window,"scroll",e.handleScroll),[e.input,...e.inputs].forEach(t=>o(t,"change",e.changeHandler)),e.colorMenu&&(o(e.colorMenu,"click",e.menuClickHandler),o(e.colorMenu,"keydown",e.menuKeyHandler)),o(document,n.move,e.pointerMove),o(document,n.up,e.pointerUp),o(window,"keyup",e.handleDismiss),o(e.parent,"focusout",e.handleFocusOut)}function we(e){var t,o;t=e.input,o=new CustomEvent("colorpicker.change"),t.dispatchEvent(o)}function ve(e,t){const o=t?P:A;return!!e&&["show","show-top"][t?"some":"forEach"](t=>o(e,t))}class xe{constructor(e){const t=this;if(t.input=M(e),!t.input)throw new TypeError(`ColorPicker target ${e} cannot be found.`);const{input:n}=t;if(t.parent=function e(t,o){return t?t.closest(o)||e(t.getRootNode().host,o):null}(n,".color-picker,color-picker"),!t.parent)throw new TypeError("ColorPicker requires a specific markup to work.");t.id=function(e,t){x+=1;let o=k.get(e),n=x;if(t&&t.length)if(o){const e=o.get(t);Number.isNaN(e)?o.set(t,n):n=e}else k.set(e,new Map),o=k.get(e),o.set(t,n);else Number.isNaN(o)?k.set(e,n):n=o;return n}(n,"color-picker"),t.dragElement=null,t.isOpen=!1,t.controlPositions={c1x:0,c1y:0,c2y:0,c3y:0},t.colorLabels={},t.keywords=!1,t.color=new ue("white",{format:t.format}),t.componentLabels=$({},pe);const{componentLabels:r,colorLabels:a,keywords:s}=n.dataset,i=r?JSON.parse(r):{};t.componentLabels=$(t.componentLabels,i);const l=a&&17===a.split(",").length?a.split(","):ge;ge.forEach((e,o)=>{t.colorLabels[e]=l[o]}),"false"!==s&&(t.keywords=s?s.split(","):de),t.showPicker=t.showPicker.bind(t),t.togglePicker=t.togglePicker.bind(t),t.toggleMenu=t.toggleMenu.bind(t),t.menuClickHandler=t.menuClickHandler.bind(t),t.menuKeyHandler=t.menuKeyHandler.bind(t),t.pointerDown=t.pointerDown.bind(t),t.pointerMove=t.pointerMove.bind(t),t.pointerUp=t.pointerUp.bind(t),t.handleScroll=t.handleScroll.bind(t),t.handleFocusOut=t.handleFocusOut.bind(t),t.changeHandler=t.changeHandler.bind(t),t.handleDismiss=t.handleDismiss.bind(t),t.keyHandler=t.keyHandler.bind(t),t.handleKnobs=t.handleKnobs.bind(t),fe(t);const{parent:c}=t;t.pickerToggle=M(".picker-toggle",c),t.menuToggle=M(".menu-toggle",c),t.colorMenu=M(".color-dropdown.menu",c),t.colorPicker=M(".color-dropdown.picker",c),t.controls=M(".color-controls",c),t.inputs=[...o(".color-input",c)],t.controlKnobs=[...o(".knob",c)],t.visuals=[...o("canvas",t.controls)],t.knobLabels=[...o(".color-label",c)],t.appearance=M(".color-appearance",c);const[h,u,d]=t.visuals;t.width1=h.width,t.height1=h.height,t.width2=u.width,t.height2=u.height,t.ctx1=h.getContext("2d"),t.ctx2=u.getContext("2d"),t.ctx1.rect(0,0,t.width1,t.height1),t.ctx2.rect(0,0,t.width2,t.height2),t.width3=0,t.height3=0,"hex"!==t.format&&(t.width3=d.width,t.height3=d.height,this.ctx3=d.getContext("2d"),t.ctx3.rect(0,0,t.width3,t.height3)),this.setControlPositions(),this.setColorAppearence(),this.updateInputs(!0),this.updateControls(),this.updateVisuals(),me(t,!0),H.set(n,"color-picker",t)}get value(){return this.input.value}set value(e){this.input.value=e}get required(){return e=this.input,t="required",e.hasAttribute(t);var e,t}get format(){return R(this.input,"format")||"hex"}get name(){return R(this.input,"name")}get label(){return M(`[for="${this.input.id}"]`)}get includeNonColor(){return this.keywords instanceof Array&&this.keywords.some(e=>de.includes(e))}get hex(){return this.color.toHex()}get hsv(){return this.color.toHsv()}get hsl(){return this.color.toHsl()}get rgb(){return this.color.toRgb()}get brightness(){return this.color.brightness}get luminance(){return this.color.luminance}get isDark(){const{rgb:e,brightness:t}=this;return t<120&&e.a>.33}get isValid(){const e=this.input.value;return""!==e&&new ue(e).isValid}updateVisuals(){const{color:e,format:t,controlPositions:o,width1:n,width2:r,width3:a,height1:s,height2:i,height3:l,ctx1:c,ctx2:h,ctx3:u}=this,{r:d,g:g,b:p}=e;if("hsl"!==t){const e=Math.round(o.c2y/i*360);c.fillStyle=new ue(`hsl(${e},100%,50%})`).toRgbString(),c.fillRect(0,0,n,s);const t=h.createLinearGradient(0,0,n,0);t.addColorStop(0,"rgba(255,255,255,1)"),t.addColorStop(1,"rgba(255,255,255,0)"),c.fillStyle=t,c.fillRect(0,0,n,s);const a=h.createLinearGradient(0,0,0,s);a.addColorStop(0,"rgba(0,0,0,0)"),a.addColorStop(1,"rgba(0,0,0,1)"),c.fillStyle=a,c.fillRect(0,0,n,s);const l=h.createLinearGradient(0,0,0,s);l.addColorStop(0,"rgba(255,0,0,1)"),l.addColorStop(.17,"rgba(255,255,0,1)"),l.addColorStop(.34,"rgba(0,255,0,1)"),l.addColorStop(.51,"rgba(0,255,255,1)"),l.addColorStop(.68,"rgba(0,0,255,1)"),l.addColorStop(.85,"rgba(255,0,255,1)"),l.addColorStop(1,"rgba(255,0,0,1)"),h.fillStyle=l,h.fillRect(0,0,r,i)}else{const t=c.createLinearGradient(0,0,n,0),r=Math.round(100*(1-o.c2y/i));t.addColorStop(0,new ue("rgba(255,0,0,1)").desaturate(100-r).toRgbString()),t.addColorStop(.17,new ue("rgba(255,255,0,1)").desaturate(100-r).toRgbString()),t.addColorStop(.34,new ue("rgba(0,255,0,1)").desaturate(100-r).toRgbString()),t.addColorStop(.51,new ue("rgba(0,255,255,1)").desaturate(100-r).toRgbString()),t.addColorStop(.68,new ue("rgba(0,0,255,1)").desaturate(100-r).toRgbString()),t.addColorStop(.85,new ue("rgba(255,0,255,1)").desaturate(100-r).toRgbString()),t.addColorStop(1,new ue("rgba(255,0,0,1)").desaturate(100-r).toRgbString()),c.fillStyle=t,c.fillRect(0,0,n,s);const u=c.createLinearGradient(0,0,0,s);u.addColorStop(0,"rgba(255,255,255,1)"),u.addColorStop(.5,"rgba(255,255,255,0)"),c.fillStyle=u,c.fillRect(0,0,n,s);const m=c.createLinearGradient(0,0,0,s);m.addColorStop(.5,"rgba(0,0,0,0)"),m.addColorStop(1,"rgba(0,0,0,1)"),c.fillStyle=m,c.fillRect(0,0,n,s);const f=h.createLinearGradient(0,0,0,i),b=e.clone().greyscale().toRgb();f.addColorStop(0,`rgba(${d},${g},${p},1)`),f.addColorStop(1,`rgba(${b.r},${b.g},${b.b},1)`),h.fillStyle=f,h.fillRect(0,0,a,l)}if("hex"!==t){u.clearRect(0,0,a,l);const e=u.createLinearGradient(0,0,0,l);e.addColorStop(0,`rgba(${d},${g},${p},1)`),e.addColorStop(1,`rgba(${d},${g},${p},0)`),u.fillStyle=e,u.fillRect(0,0,a,l)}}handleFocusOut({relatedTarget:e}){e&&!this.parent.contains(e)&&this.hide(!0)}handleDismiss({code:e}){const t=this;t.isOpen&&"Escape"===e&&t.hide()}handleScroll(e){const{activeElement:t}=document;(v&&this.dragElement||t&&this.controlKnobs.includes(t))&&(e.stopPropagation(),e.preventDefault()),this.updateDropdownPosition()}menuKeyHandler(e){const{target:t,code:o}=e;[i,l].includes(o)?e.preventDefault():[u,d].includes(o)&&this.menuClickHandler({target:t})}menuClickHandler(e){const t=this,{target:o}=e,{format:n}=t,r=(R(o,"data-value")||"").trim(),a=t.colorMenu.querySelector("li.active"),s=de.includes(r)?"white":r;var i;t.color=new ue(s,{format:n}),t.setControlPositions(),t.setColorAppearence(),t.updateInputs(!0),t.updateControls(),t.updateVisuals(),a&&(A(a,"active"),i="aria-selected",a.removeAttribute(i)),a!==o&&(E(o,"active"),T(o,"aria-selected","true"),de.includes(r)&&(t.value=r,we(t)))}pointerDown(e){const t=this,{type:o,target:n,touches:r,pageX:a,pageY:s}=e,{visuals:i,controlKnobs:l,format:c}=t,[h,u,d]=i,[g,p,m]=l,f="canvas"===n.tagName?n:M("canvas",n.parentElement),b=y(f),w="touchstart"===o?r[0].pageX:a,v="touchstart"===o?r[0].pageY:s,x=w-window.pageXOffset-b.left,k=v-window.pageYOffset-b.top;n===h||n===g?(t.dragElement=f,t.changeControl1({offsetX:x,offsetY:k})):n===u||n===p?(t.dragElement=f,t.changeControl2({offsetY:k})):"hex"===c||n!==d&&n!==m||(t.dragElement=f,t.changeAlpha({offsetY:k})),e.preventDefault()}pointerUp({target:e}){const t=this,o=document.getSelection();t.dragElement||o.toString().length||t.parent.contains(e)||t.hide(),t.dragElement=null}pointerMove(e){const t=this,{dragElement:o,visuals:n,format:r}=t,[a,s,i]=n,{type:l,touches:c,pageX:h,pageY:u}=e;if(!o)return;const d=y(o),g="touchmove"===l?c[0].pageX:h,p="touchmove"===l?c[0].pageY:u,m=g-window.pageXOffset-d.left,f=p-window.pageYOffset-d.top;o===a&&t.changeControl1({offsetX:m,offsetY:f}),o===s&&t.changeControl2({offsetY:f}),o===i&&"hex"!==r&&t.changeAlpha({offsetY:f})}handleKnobs(e){const{target:t,code:o}=e,n=this;if(![l,i,c,h].includes(o))return;e.preventDefault();const{activeElement:r}=document,{controlKnobs:a}=n,s=a.find(e=>e===r),[u,d,g]=a;if(s){let r=0,a=0;t===u?([c,h].includes(o)?n.controlPositions.c1x+=o===h?1:-1:[l,i].includes(o)&&(n.controlPositions.c1y+=o===i?1:-1),r=n.controlPositions.c1x,a=n.controlPositions.c1y,n.changeControl1({offsetX:r,offsetY:a})):t===d?(n.controlPositions.c2y+=[i,h].includes(o)?1:-1,a=n.controlPositions.c2y,n.changeControl2({offsetY:a})):t===g&&(n.controlPositions.c3y+=[i,h].includes(o)?1:-1,a=n.controlPositions.c3y,n.changeAlpha({offsetY:a})),n.setColorAppearence(),n.updateInputs(),n.updateControls(),n.updateVisuals(),n.handleScroll(e)}}changeHandler(){const e=this;let t;const{activeElement:o}=document,{inputs:n,format:r,value:a,input:s}=e,[i,l,c,h]=n,u=e.includeNonColor&&de.includes(a);(o===s||o&&n.includes(o))&&(t=o===s?u?"white":a:"hex"===r?i.value:"hsl"===r?`hsla(${i.value},${l.value}%,${c.value}%,${h.value})`:`rgba(${n.map(e=>e.value).join(",")})`,e.color=new ue(t,{format:r}),e.setControlPositions(),e.setColorAppearence(),e.updateInputs(),e.updateControls(),e.updateVisuals(),o===s&&u&&(e.value=a))}changeControl1(e){let[t,o]=[0,0];const{offsetX:n,offsetY:r}=e,{format:a,controlPositions:s,height1:i,height2:l,height3:c,width1:h}=this;n>h?t=h:n>=0&&(t=n),r>i?o=i:r>=0&&(o=r);const u="hsl"!==a?Math.round(s.c2y/l*360):Math.round(t/h*360),d="hsl"!==a?Math.round(t/h*100):Math.round(100*(1-s.c2y/l)),g=Math.round(100*(1-o/i)),p="hex"!==a?Math.round(100*(1-s.c3y/c))/100:1,m="hsl"!==a?"hsva":"hsla";this.color=new ue(`${m}(${u},${d}%,${g}%,${p})`,{format:a}),this.controlPositions.c1x=t,this.controlPositions.c1y=o,this.setColorAppearence(),this.updateInputs(),this.updateControls(),this.updateVisuals()}changeControl2(e){const{offsetY:t}=e,{format:o,width1:n,height1:r,height2:a,height3:s,controlPositions:i}=this;let l=0;t>a?l=a:t>=0&&(l=t);const c="hsl"!==o?Math.round(l/a*360):Math.round(i.c1x/n*360),h="hsl"!==o?Math.round(i.c1x/n*100):Math.round(100*(1-l/a)),u=Math.round(100*(1-i.c1y/r)),d="hex"!==o?Math.round(100*(1-i.c3y/s))/100:1,g="hsl"!==o?"hsva":"hsla";this.color=new ue(`${g}(${c},${h}%,${u}%,${d})`,{format:o}),this.controlPositions.c2y=l,this.setColorAppearence(),this.updateInputs(),this.updateControls(),this.updateVisuals()}changeAlpha(e){const{height3:t}=this,{offsetY:o}=e;let n=0;o>t?n=t:o>=0&&(n=o);const r=Math.round(100*(1-n/t));this.color.setAlpha(r/100),this.controlPositions.c3y=n,this.updateInputs(),this.updateControls(),this.updateVisuals()}updateDropdownPosition(){const{input:e,colorPicker:t,colorMenu:o}=this,n=y(e),{offsetHeight:r}=e,a=document.documentElement.clientHeight,s=ve(t,!0)?t:o,{offsetHeight:i}=s,l=a-n.bottom,c=n.top,h=n.top+i+r>a,u=n.top-i<0;P(s,"show")&&l<c&&h&&(A(s,"show"),E(s,"show-top")),P(s,"show-top")&&l>c&&u&&(A(s,"show-top"),E(s,"show"))}setControlPositions(){const e=this,{hsv:t,hsl:o,format:n,height1:r,height2:a,height3:s,width1:i}=e,l=o.h,c="hsl"!==n?t.s:o.s,h="hsl"!==n?t.v:o.l,u=t.a;e.controlPositions.c1x="hsl"!==n?c*i:l/360*i,e.controlPositions.c1y=(1-h)*r,e.controlPositions.c2y="hsl"!==n?l/360*a:(1-c)*a,"hex"!==n&&(e.controlPositions.c3y=(1-u)*s)}setColorAppearence(){const e=this,{componentLabels:t,colorLabels:o,hsl:n,hsv:r,hex:a,format:s,knobLabels:i}=e,{lightnessLabel:l,saturationLabel:c,hueLabel:h,alphaLabel:u,appearanceLabel:d,hexLabel:g}=t;let{requiredLabel:p}=t;const[m,f,b]=i,w=Math.round(n.h),v=r.a,x="hsl"===s?n.s:r.s,k=Math.round(100*x),y=Math.round(100*n.l),S=100*r.v;let M;if(100===y&&0===k)M=o.white;else if(0===y)M=o.black;else if(0===k)M=o.grey;else if(w<15||w>=345)M=o.red;else if(w>=15&&w<45)M=S>80&&k>80?o.orange:o.brown;else if(w>=45&&w<75){const e=w>=54&&w<75&&S<80;M=w>46&&w<54&&S<80&&k>90?o.gold:o.yellow,M=e?o.olive:M}else w>=75&&w<155?M=S<68?o.green:o.lime:w>=155&&w<175?M=o.teal:w>=175&&w<195?M=o.cyan:w>=195&&w<255?M=o.blue:w>=255&&w<270?M=o.violet:w>=270&&w<295?M=o.magenta:w>=295&&w<345&&(M=o.pink);if("hsl"===s?(m.innerText=`${h}: ${w}°. ${l}: ${y}%`,f.innerText=`${c}: ${k}%`):(m.innerText=`${l}: ${y}%. ${c}: ${k}%`,f.innerText=`${h}: ${w}°`),"hex"!==s){const e=Math.round(100*v);b.innerText=`${u}: ${e}%`}e.appearance.innerText=`${d}: ${M}.`;const $="hex"===s?`${g} ${a.split("").join(" ")}.`:e.value.toUpperCase();if(e.label){const t=e.label.innerText.replace("*","").trim(),[o]=e.pickerToggle.children;p=e.required?" "+p:"",o.innerText=`${t}: ${$}${p}`}}updateControls(){const{format:e,controlKnobs:t,controlPositions:o}=this,[n,r,a]=t;n.style.transform=`translate3d(${o.c1x-3}px,${o.c1y-3}px,0)`,r.style.transform=`translate3d(0,${o.c2y-3}px,0)`,"hex"!==e&&(a.style.transform=`translate3d(0,${o.c3y-3}px,0)`)}updateInputs(e){const t=this,{value:o,rgb:n,hsl:r,hsv:a,format:s,parent:i,input:l,inputs:c}=t,[h,u,d,g]=c,p=r.a,m=Math.round(r.h),f=Math.round(100*r.s),b="hsl"===s?r.l:a.v,w=Math.round(100*b);let v;"hex"===s?(v=t.color.toHexString(),h.value=t.hex):"hsl"===s?(v=t.color.toHslString(),h.value=""+m,u.value=""+f,d.value=""+w,g.value=""+p):"rgb"===s&&(v=t.color.toRgbString(),h.value=""+n.r,u.value=""+n.g,d.value=""+n.b,g.value=""+p),t.value=""+v,$(l.style,{backgroundColor:v}),t.isDark?(P(i,"light")&&A(i,"light"),P(i,"dark")||E(i,"dark")):(P(i,"dark")&&A(i,"dark"),P(i,"light")||E(i,"light")),e||v===o||we(t)}keyHandler(e){const t=this,{menuToggle:o}=t,{activeElement:n}=document,{code:r}=e;[u,d].includes(r)&&(o&&n===o||!n)&&(e.preventDefault(),n?t.toggleMenu():t.togglePicker(e))}togglePicker(e){e.preventDefault();const t=this,o=ve(t.colorPicker,!0);t.isOpen&&o?t.hide(!0):t.showPicker()}showPicker(){ve(this.colorMenu),E(this.colorPicker,"show"),this.input.focus(),this.show(),T(this.pickerToggle,"aria-expanded","true")}toggleMenu(){const e=this,t=ve(e.colorMenu,!0);e.isOpen&&t?e.hide(!0):function(e){ve(e.colorPicker),E(e.colorMenu,"show"),e.show(),T(e.menuToggle,"aria-expanded","true")}(e)}show(){const e=this;e.isOpen||(E(e.parent,"open"),be(e,!0),e.updateDropdownPosition(),e.isOpen=!0)}hide(e){const t=this;if(t.isOpen){const{pickerToggle:o,colorMenu:n}=t;be(t),A(t.parent,"open"),ve(t.colorPicker),T(o,"aria-expanded","false"),n&&(ve(n),T(t.menuToggle,"aria-expanded","false")),t.isValid||(t.value=t.color.toString()),t.isOpen=!1,e||o.focus()}}dispose(){const{input:e,parent:t}=this;this.hide(!0),me(this),[...t.children].forEach(t=>{t!==e&&t.remove()}),H.remove(e,"color-picker")}}function ke(){const{init:e,selector:t}=xe;[...o(t)].forEach(e)}$(xe,{Color:ue,getInstance:e=>{return t=e,o="color-picker",H.get(t,o);var t,o},init:e=>new xe(e),selector:'[data-function="color-picker"]'}),document.body?ke():document.addEventListener("DOMContentLoaded",ke,{once:!0});export default xe;
|
@@ -1,49 +0,0 @@
|
|
1
|
-
import createElement from 'shorter-js/src/misc/createElement';
|
2
|
-
import setAttribute from 'shorter-js/src/attr/setAttribute';
|
3
|
-
import ariaLabelledBy from 'shorter-js/src/strings/ariaLabelledBy';
|
4
|
-
|
5
|
-
import vHidden from './vHidden';
|
6
|
-
|
7
|
-
/**
|
8
|
-
* Returns a new color control `HTMLElement`.
|
9
|
-
* @param {number} iteration
|
10
|
-
* @param {number} id
|
11
|
-
* @param {number} width
|
12
|
-
* @param {number} height
|
13
|
-
* @param {string=} labelledby
|
14
|
-
* @returns {HTMLElement | Element}
|
15
|
-
*/
|
16
|
-
export default function getColorControl(iteration, id, width, height, labelledby) {
|
17
|
-
const labelID = `appearance${iteration}_${id}`;
|
18
|
-
const knobClass = iteration === 1 ? 'color-pointer' : 'color-slider';
|
19
|
-
const control = createElement({
|
20
|
-
tagName: 'div',
|
21
|
-
className: 'color-control',
|
22
|
-
});
|
23
|
-
setAttribute(control, 'role', 'presentation');
|
24
|
-
|
25
|
-
control.append(
|
26
|
-
createElement({
|
27
|
-
id: labelID,
|
28
|
-
tagName: 'label',
|
29
|
-
className: `color-label ${vHidden}`,
|
30
|
-
ariaLive: 'polite',
|
31
|
-
}),
|
32
|
-
createElement({
|
33
|
-
tagName: 'canvas',
|
34
|
-
className: `visual-control${iteration}`,
|
35
|
-
ariaHidden: 'true',
|
36
|
-
width: `${width}`,
|
37
|
-
height: `${height}`,
|
38
|
-
}),
|
39
|
-
);
|
40
|
-
|
41
|
-
const knob = createElement({
|
42
|
-
tagName: 'div',
|
43
|
-
className: `${knobClass} knob`,
|
44
|
-
});
|
45
|
-
setAttribute(knob, ariaLabelledBy, labelledby || labelID);
|
46
|
-
setAttribute(knob, 'tabindex', '0');
|
47
|
-
control.append(knob);
|
48
|
-
return control;
|
49
|
-
}
|
package/src/js/util/init.js
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
import querySelectorAll from 'shorter-js/src/selectors/querySelectorAll';
|
2
|
-
import getDocument from 'shorter-js/src/get/getDocument';
|
3
|
-
import getDocumentBody from 'shorter-js/src/get/getDocumentBody';
|
4
|
-
|
5
|
-
import { addListener } from 'event-listener.js';
|
6
|
-
import ColorPicker from '../color-picker';
|
7
|
-
|
8
|
-
function initCallBack() {
|
9
|
-
const { init, selector } = ColorPicker;
|
10
|
-
[...querySelectorAll(selector)].forEach(init);
|
11
|
-
}
|
12
|
-
|
13
|
-
if (getDocumentBody()) initCallBack();
|
14
|
-
else addListener(getDocument(), 'DOMContentLoaded', initCallBack, { once: true });
|