-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolor-picker.min.js
26 lines (26 loc) · 8.77 KB
/
color-picker.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*!
*
* The MIT License (MIT)
*
* Copyright © 2021 Taufik Nurrohman <https://github.com/taufik-nurrohman>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(n="undefined"!=typeof globalThis?globalThis:n||self).CP=t()}(this,(function(){"use strict";var n=function(n,t){return-1!==t.indexOf(n)},t=function(n){return Array.isArray(n)},e=function(n){return"function"==typeof n},r=function(n,t){return n&&u(t)&&n instanceof t},i=function(n){return"number"==typeof n},o=function(n,t){return void 0===t&&(t=!0),"object"==typeof n&&(!t||r(n,Object))},u=function(n){return function(n){return void 0!==n}(n)&&!function(n){return null===n}(n)},c=function(n){return"string"==typeof n},f=function(n){return n.toUpperCase()},s=function(n){return n.length},a=function(n,t){return u(t[0])&&n<t[0]?t[0]:u(t[1])&&n>t[1]?t[1]:n},l=function(n,t){return void 0===t&&(t=10),t?parseInt(n,t):parseFloat(n)},d=function(n){return Object.keys(n)},v=function(n){return i(n)?Math.round(n):null},p=function(n,t){return void 0===t&&(t=10),i(n)?n.toString(t):""+n},h=function e(){for(var r=arguments.length,i=Array(r),c=0;c<r;c++)i[c]=arguments[c];for(var f=i.shift(),a=0,l=s(i);a<l;++a)for(var d in i[a])if(u(f[d]))if(t(f[d])&&t(i[a][d])){f[d]=[].concat(f[d]);for(var v=0,p=s(i[a][d]);v<p;++v)n(i[a][d][v],f[d])||f[d].push(i[a][d][v])}else o(f[d])&&o(i[a][d])?f[d]=e({},f[d],i[a][d]):f[d]=i[a][d];else f[d]=i[a][d];return f},g=function n(e){if(t(e))return e.map((function(t){return n(e)}));if(o(e)){for(var r in e)e[r]=n(e[r]);return e}return!1===e?"false":null===e?"null":!0===e?"true":""+e},b=document,m=window,y=b.body,k=b.documentElement,E=function(n){return n.parentNode||null},w=function(n,t){return n.removeAttribute(t),n},C=function(n,t,e){return!0===e&&(e=t),n.setAttribute(t,g(e)),n},x=function(n,t){return n.append(t),t},L=function(n,t,e){return n=c(n)?b.createElement(n):n,o(t)&&(e=t,t=!1),c(t)&&O(n,t),o(e)&&function(n,t){var e;for(var r in t)(e=t[r])||""===e||0===e?C(n,r,e):w(n,r)}(n,e),n},O=function(n,t,e){if(void 0===e&&(e=!0),null===t)return n;var r="innerHTML";return function(n,t){return t in n}(n,r)&&(n[r]=e?t.trim():t),n},P=function(n,t,e){return i(e)&&(e+="px"),n.style[(r=t,r.replace(/[-_.](\w)/g,(function(n,t){return f(t)})))]=g(e),n;var r},T=function(n){return n&&n.preventDefault()},A=function(n,t,e){n.forEach((function(n){return function(n,t,e){t.removeEventListener(n,e)}(n,t,e)}))},H=function(n,t,e,r){void 0===r&&(r=!1),n.forEach((function(n){return function(n,t,e,r){void 0===r&&(r=!1),t.addEventListener(n,e,r)}(n,t,e,r)}))};var M=function(n,t){var e=n.touches,r=e?e[0].clientX:n.clientX,i=e?e[0].clientY:n.clientY;if(t){var o=X(t);return[r-o[0],i-o[1],o[0],o[1]]}return[r,i]},X=function(n){var t,e,r,i,o,u,c;return!function(n){return n===m}(n)?(i=(e=n.getBoundingClientRect()).left,o=e.top,r=e.width,t=e.height,u=e.right,c=e.bottom):(i=n.pageXOffset||k.scrollLeft||y.scrollLeft,o=n.pageYOffset||k.scrollTop||y.scrollTop,r=n.innerWidth,t=n.innerHeight),[i,o,r,t,u,c]};const Y="HEX",_=["touchstart","mousedown"],$=["touchmove","mousemove"],z=["orientationchange","resize"],B=["touchend","mouseup"];let D=m.setTimeout;function F(n,t){if(n===t)return n;for(;(n=n.parentElement)&&n!==t;);return n}function I(n){let t,e,r,i,o,c,f,s,a=+n[0],l=+n[1],d=+n[2];switch(i=Math.floor(6*a),o=6*a-i,c=d*(1-l),f=d*(1-o*l),s=d*(1-(1-o)*l),i=i||0,f=f||0,s=s||0,i%6){case 0:t=d,e=s,r=c;break;case 1:t=f,e=d,r=c;break;case 2:t=c,e=d,r=s;break;case 3:t=c,e=f,r=d;break;case 4:t=s,e=c,r=d;break;case 5:t=d,e=c,r=f}return[v(255*t),v(255*e),v(255*r),u(n[3])?+n[3]:1]}function N(n){let t,e,r=+n[0]/255,i=+n[1]/255,o=+n[2]/255,c=Math.max(r,i,o),f=Math.min(r,i,o),s=c,a=c-f;if(e=0===c?0:a/c,c===f)t=0;else{switch(c){case r:t=(i-o)/a+(i<o?6:0);break;case i:t=(o-r)/a+2;break;case o:t=(r-i)/a+4}t/=6}return[t,e,s,u(n[3])?+n[3]:1]}function R(n,t={}){if(!n)return;if(n.CP)return n.CP;const i=this;if(!r(i,R))return new R(n,t);var f;i.state=t=h(R.state,c(t)?{color:t}:t||{}),R.instances[n.id||n.name||(f=R.instances,s(d(f)))]=i,n.CP=i;let{fire:l,hooks:v}=function(n){var t={};return n.hooks=t,n.fire=function(e,r){return u(t[e])?(t[e].forEach((function(t){return t.apply(n,r)})),n):n},n.off=function(e,r){if(!u(e))return t={},n;if(u(t[e]))if(u(r)){for(var i=0,o=t[e].length;i<o;++i)if(r===t[e][i]){t[e].splice(i,1);break}0===j&&delete t[e]}else delete t[e];return n},n.on=function(e,r){return u(t[e])||(t[e]=[]),u(r)&&t[e].push(r),n},n}(i);function p(){return n.value?R[e(R[t.color])?t.color:Y](n.value||""):[0,0,0,1]}let g,w,C,O,S=t.class,U=()=>n.disabled,W=()=>n.readOnly,q=p(),G=N(q),J=L("div",{class:S}),K=L("div"),Q=L("div",{class:S+":sv"}),V=L("div",{class:S+":h"}),Z=L("div",{class:S+":a"}),nn=L("div"),tn=L("div"),en=L("div"),rn=L("i"),on=L("div"),un=L("i"),cn=L("div"),fn=L("div"),sn=L("i"),an=0,ln=0,dn=0,vn=0,pn=0,hn=0;function gn(t){if(v.focus)l("focus",q);else{let e=t.target;n===F(e,n)?!E(J)&&g():w()}}return x(J,K),x(K,Q),x(K,V),x(K,Z),x(Q,nn),x(Q,tn),x(Q,en),x(Q,rn),x(V,on),x(V,un),x(Z,cn),x(Z,fn),x(Z,sn),function r(c,f){function s(n){i.current=Z,dn=hn=1,L(n),T(n)}function d(n){i.current=V,ln=pn=1,L(n),T(n)}function h(n){i.current=Q,an=vn=1,L(n),T(n)}function L(n){vn&&function(n){let t=M(n,Q),e=a(t[0],[0,En]),r=a(t[1],[0,yn]);G[1]=1-(En-e)/En,G[2]=(yn-r)/yn,j()}(n),pn&&function(n){G[0]=(Cn-a(M(n,V)[1],[0,Cn]))/Cn,j()}(n),hn&&function(n){G[3]=(Ln-a(M(n,Z)[1],[0,Ln]))/Ln,j()}(n),q=I(G),(vn||pn||hn)&&(l(an||ln||dn?"start":"drag",q),l("change",q)),an=ln=dn=0}function j(){!function(n){u(n[1])&&P(rn,"right",En-wn/2-En*+n[1]),u(n[2])&&P(rn,"top",yn-kn/2-yn*+n[2]),u(n[0])&&P(un,"top",Cn-xn/2-Cn*+n[0]),u(n[3])&&P(sn,"top",Ln-On/2-Ln*+n[3])}(G);let n=I(G),t=I([G[0],1,1]);P(nn,"background-color","rgb("+t[0]+","+t[1]+","+t[2]+")"),P(cn,"background-image","linear-gradient(rgb("+n[0]+","+n[1]+","+n[2]+"),transparent)")}function K(t){q=I(G);let e=t.target,r=n===F(e,n),o=J===F(e,J);i.current=null,r||o?o&&(vn||pn||hn)&&l("stop",q):v.blur?l("blur",q):vn||pn||hn||E(J)&&w(),vn=pn=hn=0}G=N(q=p()),c||(x(f||y,J),i.visible=!0),g=n=>(U()||W()||(r(0,n),l("enter",q)),i),w=()=>{return U()||W()?i:(E(J)&&(E(n=J),n.remove(),i.current=null,i.visible=!1),A(_,Q,h),A(_,V,d),A(_,Z,s),A($,b,L),A(B,b,K),A(z,m,O),l("exit",q));var n},C=t=>{let e=X(k),r=X(n),i=X(m),c=i[3]-k.clientHeight,f=i[2]-e[2],s=X(J),d=s[3],v=s[2],p=r[0]+i[0],h=r[1]+i[1]+r[3];if(o(t))u(t[0])&&(p=t[0]),u(t[1])&&(h=t[1]);else{let n=i[0],t=i[1],e=i[0]+i[2]-v-f,r=i[1]+i[3]-d-c;p=a(p,[n,e])>>0,h=a(h,[t,r])>>0}return P(J,"left",p),P(J,"top",h),l("fit",q)},O=()=>C();let tn=X(Q),en=X(rn),on=X(V),fn=X(un),bn=X(Z),mn=X(sn),yn=tn[3],kn=en[3],En=tn[2],wn=en[2],Cn=on[3],xn=fn[3],Ln=bn[3],On=mn[3];var Pn;c?(H(_,n,gn),D((()=>{l("change",q)}),1)):(H(_,Q,h),H(_,V,d),H(_,Z,s),H($,b,L),H(B,b,K),H(z,m,O),C()),j(),i.color=(n,r,i,o)=>R[e(R[t.color])?t.color:Y]([n,r,i,o]),i.current=null,i.enter=g,i.exit=w,i.fit=C,i.get=()=>p(),i.pop=()=>{return n.CP?(delete n.CP,t=S+"-source",n.classList.remove(t),A(_,n,gn),w(),l("pop",q)):i;var t},i.set=(n,t,e,r)=>(i._set(n,t,e,r),l("change",[n,t,e,r])),i.self=J,i._set=(n,t,e,r)=>(G=N([n,t,e,r]),j(),i),Pn=S+"-source",n.classList.add(Pn)}(1),i.source=n,i.visible=!1,i}return R.HEX=n=>{if(c(n)){let t=s(n=n.trim());if(4!==t&&7!==t||"#"!==n[0]){if((5===t||9===t)&&"#"===n[0]&&/^#([a-f\d]{3,4}){1,2}$/i.test(n))return 5===t?[l(n[1]+n[1],16),l(n[2]+n[2],16),l(n[3]+n[3],16),l(n[4]+n[4],16)/255]:[l(n[1]+n[2],16),l(n[3]+n[4],16),l(n[5]+n[6],16),l(n[7]+n[8],16)/255]}else if(/^#([a-f\d]{3}){1,2}$/i.test(n))return 4===t?[l(n[1]+n[1],16),l(n[2]+n[2],16),l(n[3]+n[3],16),1]:[l(n[1]+n[2],16),l(n[3]+n[4],16),l(n[5]+n[6],16),1];return[0,0,0,1]}return"#"+("000000"+p(+n[2]|+n[1]<<8|+n[0]<<16,16)).slice(-6)+(u(n[3])&&n[3]<1?p(v(255*n[3])+65536,16).substr(-2):"")},R.instances={},R.state={class:"color-picker",color:Y},R.version="2.3.2",R}));