@thednp/color-picker 0.0.1 → 0.0.2-alpha1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ // ColorPalette v0.0.2alpha1 | thednp © 2022 | MIT-License
2
+ const t=(t,r)=>Object.assign(t,r);function r(t){const r=Math.floor(t);return t-r<.5?r:Math.round(t)}const{head:e}=document;function n(t,r){const e=getComputedStyle(t);return r in e?e[r]:""}const o=(r,e)=>{t(r.style,e)},s=["transparent","currentColor","inherit","revert","initial"],a=["rgb","hex","hsl","hsv","hwb"],h="[-\\+]?\\d*\\.?\\d+(?:deg|rad|grad|turn)?",i="(?:[-\\+]?\\d*\\.\\d+%?)|(?:[-\\+]?\\d+%?)",u=`(?:${i})|(?:${h})`,c="(?:[,|\\s]+)",l=`(?:[\\s|\\(\\s|\\s\\(\\s]+)?(${u})${c}(${i})${c}(${i})(?:[,|\\/\\s]*)?(${i})?(?:[\\s|\\)\\s]+)?`,g={CSS_UNIT:new RegExp(u),hwb:new RegExp("hwb"+l),rgb:new RegExp("rgb(?:a)?"+l),hsl:new RegExp("hsl(?:a)?"+l),hsv:new RegExp("hsv(?:a)?"+l),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 b(t){return(""+t).includes(".")&&1===parseFloat(t)}function f(t){return(""+t).includes("%")}function m(t){return!s.includes(t)&&!["#",...a].some(r=>t.includes(r))&&["rgb(255, 255, 255)","rgb(0, 0, 0)"].every(r=>{o(e,{color:t});const s=n(e,"color");return o(e,{color:""}),s!==r})}function p(t){return Boolean(g.CSS_UNIT.exec(String(t)))}function x(t,r){let e=t;b(t)&&(e="100%");const n=f(e);return e=360===r?parseFloat(e):Math.min(r,Math.max(0,parseFloat(e))),n&&(e=e*r/100),Math.abs(e-r)<1e-6?1:(e=360===r?(e<0?e%r+r:e%r)/r:e%r/r,e)}function S(t){let r=parseFloat(""+t);return(Number.isNaN(r)||r<0||r>1)&&(r=1),r}function d(t){return Math.min(1,Math.max(0,t))}function $(t){o(e,{color:t});const r=n(e,"color");return o(e,{color:""}),r}function A(t){return r(255*t).toString(16)}function w(t){return H(t)/255}function H(t){return parseInt(t,16)}function y(t){return 1===t.length?"0"+t:String(t)}function v(t,r,e){const n=t/255,o=r/255,s=e/255,a=Math.max(n,o,s),h=Math.min(n,o,s);let i=0,u=0;const c=(a+h)/2;if(a===h)u=0,i=0;else{const t=a-h;switch(u=c>.5?t/(2-a-h):t/(a+h),a){case n:i=(o-s)/t+(o<s?6:0);break;case o:i=(s-n)/t+2;break;case s:i=(n-o)/t+4}i/=6}return{h:i,s:u,l:c}}function R(t,r,e){let n=e;return n<0&&(n+=1),n>1&&(n-=1),n<1/6?t+6*n*(r-t):n<.5?r:n<2/3?t+(r-t)*(2/3-n)*6:t}function M(t,r,e){let n=0,o=0,s=0;if(0===r)o=e,s=e,n=e;else{const a=e<.5?e*(1+r):e+r-e*r,h=2*e-a;n=R(h,a,t+1/3),o=R(h,a,t),s=R(h,a,t-1/3)}return[n,o,s]=[n,o,s].map(t=>255*t),{r:n,g:o,b:s}}function F(t,r,e){const n=t/255,o=r/255,s=e/255;let a=0,h=0;const i=Math.min(n,o,s),u=Math.max(n,o,s),c=1-u;if(u===i)return{h:0,w:i,b:c};n===i?(a=o-s,h=3):(a=o===i?s-n:n-o,h=o===i?5:1);const l=(h-a/(u-i))/6;return{h:1===l?0:l,w:i,b:c}}function T(t,r,e){if(r+e>=1){const t=r/(r+e)*255;return{r:t,g:t,b:t}}let{r:n,g:o,b:s}=M(t,1,.5);return[n,o,s]=[n,o,s].map(t=>t/255*(1-r-e)+r).map(t=>255*t),{r:n,g:o,b:s}}function C(t,r,e){const n=t/255,o=r/255,s=e/255,a=Math.max(n,o,s),h=Math.min(n,o,s);let i=0;const u=a,c=a-h,l=0===a?0:c/a;if(a===h)i=0;else{switch(a){case n:i=(o-s)/c+(o<s?6:0);break;case o:i=(s-n)/c+2;break;case s:i=(n-o)/c+4}i/=6}return{h:i,s:l,v:u}}function E(t,r,e){const n=6*t,o=r,s=e,a=Math.floor(n),h=n-a,i=s*(1-o),u=s*(1-h*o),c=s*(1-(1-h)*o),l=a%6;let g=[s,u,i,i,c,s][l],b=[c,s,s,u,i,i][l],f=[i,i,c,s,s,u][l];return[g,b,f]=[g,b,f].map(t=>255*t),{r:g,g:b,b:f}}function N(t,e,n,o){const s=[y(r(t).toString(16)),y(r(e).toString(16)),y(r(n).toString(16))];return o&&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[0].charAt(0)+s[1].charAt(0)+s[2].charAt(0):s.join("")}function k(t,e,n,o,s){const a=[y(r(t).toString(16)),y(r(e).toString(16)),y(r(n).toString(16)),y(A(o))];return s&&a[0].charAt(0)===a[0].charAt(1)&&a[1].charAt(0)===a[1].charAt(1)&&a[2].charAt(0)===a[2].charAt(1)&&a[3].charAt(0)===a[3].charAt(1)?a[0].charAt(0)+a[1].charAt(0)+a[2].charAt(0)+a[3].charAt(0):a.join("")}function I(t){let r=t.trim().toLowerCase();if(0===r.length)return{r:0,g:0,b:0,a:1};let e=!1;if(m(r))r=$(r),e=!0;else if(s.includes(r)){return{r:0,g:0,b:0,a:"transparent"===r?0:1,format:"rgb",ok:!0}}let[,n,o,a,h]=g.rgb.exec(r)||[];return n&&o&&a?{r:n,g:o,b:a,a:void 0!==h?h:1,format:"rgb"}:([,n,o,a,h]=g.hsl.exec(r)||[],n&&o&&a?{h:n,s:o,l:a,a:void 0!==h?h:1,format:"hsl"}:([,n,o,a,h]=g.hsv.exec(r)||[],n&&o&&a?{h:n,s:o,v:a,a:void 0!==h?h:1,format:"hsv"}:([,n,o,a,h]=g.hwb.exec(r)||[],n&&o&&a?{h:n,w:o,b:a,a:void 0!==h?h:1,format:"hwb"}:([,n,o,a,h]=g.hex8.exec(r)||[],n&&o&&a&&h?{r:H(n),g:H(o),b:H(a),a:w(h),format:e?"rgb":"hex"}:([,n,o,a]=g.hex6.exec(r)||[],n&&o&&a?{r:H(n),g:H(o),b:H(a),format:e?"rgb":"hex"}:([,n,o,a,h]=g.hex4.exec(r)||[],n&&o&&a&&h?{r:H(n+n),g:H(o+o),b:H(a+a),a:w(h+h),format:e?"rgb":"hex"}:([,n,o,a]=g.hex3.exec(r)||[],!!(n&&o&&a)&&{r:H(n+n),g:H(o+o),b:H(a+a),format:e?"rgb":"hex"})))))))}function _(t){let r={r:0,g:0,b:0},e=t,n=1,o=null,s=null,h=null,i=null,u=null,c=null,l=null,g=null,b=!1;const m="object"==typeof e&&e.format;let d=m&&a.includes(m)?m:"rgb";return"string"==typeof t&&(e=I(t),e&&(b=!0)),"object"==typeof e&&(p(e.r)&&p(e.g)&&p(e.b)?(({r:l,g:g,b:u}=e),[l,g,u]=[l,g,u].map(t=>255*x(t,f(t)?100:255)),r={r:l,g:g,b:u},b=!0,d="rgb"):p(e.h)&&p(e.s)&&p(e.v)?(({h:c,s:o,v:s}=e),c="number"==typeof c?c:x(c,360),o="number"==typeof o?o:x(o,100),s="number"==typeof s?s:x(s,100),r=E(c,o,s),b=!0,d="hsv"):p(e.h)&&p(e.s)&&p(e.l)?(({h:c,s:o,l:h}=e),c="number"==typeof c?c:x(c,360),o="number"==typeof o?o:x(o,100),h="number"==typeof h?h:x(h,100),r=M(c,o,h),b=!0,d="hsl"):p(e.h)&&p(e.w)&&p(e.b)&&(({h:c,w:i,b:u}=e),c="number"==typeof c?c:x(c,360),i="number"==typeof i?i:x(i,100),u="number"==typeof u?u:x(u,100),r=T(c,i,u),b=!0,d="hwb"),p(e.a)&&(n=e.a,n=f(""+n)||parseFloat(n)>1?x(n,100):n)),void 0===e&&(b=!0),{ok:b,format:d,r:Math.min(255,Math.max(r.r,0)),g:Math.min(255,Math.max(r.g,0)),b:Math.min(255,Math.max(r.b,0)),a:S(n)}}class j{constructor(t,r){let e=t;const n=r&&a.includes(r)?r:"rgb";if(e instanceof j&&(e=_(e)),"number"==typeof e){e=`#${2===(""+e).length?"0":"00"}${e}`}const{r:o,g:s,b:h,a:i,ok:u,format:c}=_(e);this.originalInput=t,this.r=o,this.g=s,this.b=h,this.a=i,this.ok=u,this.format=n||c}get isValid(){return this.ok}get isDark(){return this.brightness<120}get luminance(){const{r:t,g:r,b:e}=this;let n=0,o=0,s=0;const a=t/255,h=r/255,i=e/255;return n=a<=.03928?a/12.92:((a+.055)/1.055)**2.4,o=h<=.03928?h/12.92:((h+.055)/1.055)**2.4,s=i<=.03928?i/12.92:((i+.055)/1.055)**2.4,.2126*n+.7152*o+.0722*s}get brightness(){const{r:t,g:r,b:e}=this;return(299*t+587*r+114*e)/1e3}toRgb(){const{r:t,g:e,b:n,a:o}=this;return{r:t,g:e,b:n,a:r(100*o)/100}}toRgbString(){const{r:t,g:e,b:n,a:o}=this.toRgb(),[s,a,h]=[t,e,n].map(r);return 1===o?`rgb(${s}, ${a}, ${h})`:`rgba(${s}, ${a}, ${h}, ${o})`}toRgbCSS4String(){const{r:t,g:e,b:n,a:o}=this.toRgb(),[s,a,h]=[t,e,n].map(r);return`rgb(${s} ${a} ${h}${1===o?"":` / ${r(100*o)}%`})`}toHex(t){const{r:r,g:e,b:n,a:o}=this.toRgb();return 1===o?N(r,e,n,t):k(r,e,n,o,t)}toHexString(t){return"#"+this.toHex(t)}toHex8(t){const{r:r,g:e,b:n,a:o}=this.toRgb();return k(r,e,n,o,t)}toHex8String(t){return"#"+this.toHex8(t)}toHsv(){const{r:t,g:r,b:e,a:n}=this.toRgb(),{h:o,s:s,v:a}=C(t,r,e);return{h:o,s:s,v:a,a:n}}toHsl(){const{r:t,g:r,b:e,a:n}=this.toRgb(),{h:o,s:s,l:a}=v(t,r,e);return{h:o,s:s,l:a,a:n}}toHslString(){let{h:t,s:e,l:n,a:o}=this.toHsl();return t=r(360*t),e=r(100*e),n=r(100*n),o=r(100*o)/100,1===o?`hsl(${t}, ${e}%, ${n}%)`:`hsla(${t}, ${e}%, ${n}%, ${o})`}toHslCSS4String(){let{h:t,s:e,l:n,a:o}=this.toHsl();t=r(360*t),e=r(100*e),n=r(100*n),o=r(100*o);return`hsl(${t}deg ${e}% ${n}%${o<100?` / ${r(o)}%`:""})`}toHwb(){const{r:t,g:r,b:e,a:n}=this,{h:o,w:s,b:a}=F(t,r,e);return{h:o,w:s,b:a,a:n}}toHwbString(){let{h:t,w:e,b:n,a:o}=this.toHwb();t=r(360*t),e=r(100*e),n=r(100*n),o=r(100*o);return`hwb(${t}deg ${e}% ${n}%${o<100?` / ${r(o)}%`:""})`}setAlpha(t){return this.a=S(t),this}saturate(r){if("number"!=typeof r)return this;const{h:e,s:n,l:o}=this.toHsl(),{r:s,g:a,b:h}=M(e,d(n+r/100),o);return t(this,{r:s,g:a,b:h}),this}desaturate(t){return"number"==typeof t?this.saturate(-t):this}greyscale(){return this.saturate(-100)}lighten(r){if("number"!=typeof r)return this;const{h:e,s:n,l:o}=this.toHsl(),{r:s,g:a,b:h}=M(e,n,d(o+r/100));return t(this,{r:s,g:a,b:h}),this}darken(t){return"number"==typeof t?this.lighten(-t):this}spin(r){if("number"!=typeof r)return this;const{h:e,s:n,l:o}=this.toHsl(),{r:s,g:a,b:h}=M(d((360*e+r)%360/360),n,o);return t(this,{r:s,g:a,b:h}),this}clone(){return new j(this)}toString(t){const{format:r}=this;return"hex"===r?this.toHexString(t):"hsl"===r?this.toHslString():"hwb"===r?this.toHwbString():this.toRgbString()}}t(j,{ANGLES:"deg|rad|grad|turn",CSS_ANGLE:h,CSS_INTEGER:"[-\\+]?\\d+%?",CSS_NUMBER:"[-\\+]?\\d*\\.\\d+%?",CSS_UNIT:i,CSS_UNIT2:u,PERMISSIVE_MATCH:l,matchers:g,isOnePointZero:b,isPercentage:f,isValidCSSUnit:p,isColorName:m,pad2:y,clamp01:d,bound01:x,boundAlpha:S,getRGBFromName:$,convertHexToDecimal:w,convertDecimalToHex:A,rgbToHsl:v,rgbToHex:N,rgbToHsv:C,rgbToHwb:F,rgbaToHex:k,hslToRgb:M,hsvToRgb:E,hueToRgb:R,hwbToRgb:T,parseIntFromHex:H,stringInputToObject:I,inputToRGB:_,roundPart:r,getElementStyle:n,setElementStyle:o,ObjectAssign:t});class P{constructor(...t){let e=0,n=12,o=10,s=[.5];if(3===t.length)[e,n,o]=t;else{if(2!==t.length)throw TypeError("ColorPalette requires minimum 2 arguments");if([n,o]=t,[n,o].some(t=>t<1))throw TypeError("ColorPalette: when 2 arguments used, both must be larger than 0.")}const a=[],h=360/n,i=r((o-(o%2?1:0))/2),u=100/(o+(o%2?0:1))/100;let c=.25;c=[4,5].includes(o)?.2:c,c=[6,7].includes(o)?.15:c,c=[8,9].includes(o)?.11:c,c=[10,11].includes(o)?.09:c,c=[12,13].includes(o)?.075:c,c=o>13?u:c;for(let t=1;t<i+1;t+=1)s=[...s,.5+c*t];for(let t=1;t<o-i;t+=1)s=[.5-c*t,...s];for(let t=0;t<n;t+=1){const r=(e+t*h)%360/360;s.forEach(t=>{a.push(new j({h:r,s:1,l:t}))})}this.hue=e,this.hueSteps=n,this.lightSteps=o,this.colors=a}}t(P,{Color:j});export{P as default};