Skip to content

Instantly share code, notes, and snippets.

@bundle-js
Created October 31, 2023 18:52
Show Gist options
  • Save bundle-js/0b499af7ad3a313cdc9244b74c2a455c to your computer and use it in GitHub Desktop.
Save bundle-js/0b499af7ad3a313cdc9244b74c2a455c to your computer and use it in GitHub Desktop.
function _(t){if(typeof t!="string"&&typeof t!="number")return null;let e;return typeof t=="string"&&t.includes("%")?e=Number.parseFloat(t)/100:e=Number.parseFloat(t.toString()),isNaN(e)&&(e=null),e}var m=_;function j(t,e){switch(t){case"hue":{let r=Number.parseInt(e);if(isNaN(r)||r>360||r<0)throw new Error(`Invalid hue value: ${e})}`);return r}case"saturation":{let r=m(e);if(r===null||r>1||r<0)throw new Error(`Invalid saturation value: ${e}`);return r}case"lightness":{let r=m(e);if(r===null||r>1||r<0)throw new Error(`Invalid lightness value: ${e}`);return r}case"alpha":{let r=m(e);if(r===null&&(r=1),r>1||r<0)throw new Error(`Invalid alpha channel value: ${e})}`);return r=Number(r.toFixed(2)),r}case"rgb":{let r=Number.parseInt(e);if(isNaN(r)||r>255||r<0)throw new Error(`Invalid RGB value: ${e}`);return r}default:throw new Error(`Invalid type for value validation: '${t}'`)}}var i=j;function H(t){if(!t)throw new Error("No color value was provided");if(typeof t!="string")throw new Error("Color value has to be a string");let e=t.replace(/^#/,"");if(!/^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/.test(e))throw new Error(`Invalid HEX color value: ${t}`);(e.length===3||e.length===4)&&(e=e.split("").map(h=>h+h).join(""));let a=i("rgb",parseInt(e.slice(0,2),16)),s=i("rgb",parseInt(e.slice(2,4),16)),n=i("rgb",parseInt(e.slice(4,6),16)),o=i("alpha",parseInt(e.slice(6,8),16)/255);return{r:a,g:s,b:n,a:o}}var A=H;function F(t){let{h:e,s:r,l:a,a:s}=t,n=i("hue",e),o=i("saturation",r),h=i("lightness",a),b=i("alpha",s),f=o*(1-Math.abs(2*h-1)),u=n/60,l=f*(1-Math.abs(Number((u%2).toFixed(10))-1)),w=h-f*.5,d=(I,R,M)=>({r:Math.round((I+w)*255),g:Math.round((R+w)*255),b:Math.round((M+w)*255),a:b});if(u<1)return d(f,l,0);if(u<2)return d(l,f,0);if(u<3)return d(0,f,l);if(u<4)return d(0,l,f);if(u<5)return d(l,0,f);if(u<=6)return d(f,0,l);throw new Error("Invalid HSL calculation: hue value is greater than 360")}var T=F;function g(t){let e=t.toString(16);return e.length===1?`0${e}`:e}function C(t){let{r:e,g:r,b:a,a:s}=t,n=i("rgb",e),o=i("rgb",r),h=i("rgb",a),b=i("alpha",s),f=`#${g(n)}${g(o)}${g(h)}`;return b!==1&&(f+=g(Math.round(b*255))),f}var S=C;function E(t){let{r:e,g:r,b:a}=t,s=.5*(Math.max(e,r,a)+Math.min(e,r,a))/255;return Number(s.toFixed(2))}function O(t){let e=E(t);if(e===0||e===1)return 0;let{r,g:a,b:s}=t,n=(Math.max(r,a,s)-Math.min(r,a,s))/(1-Math.abs(1-2*e))/255;return Number(n.toFixed(2))}function k(t){let{r:e,g:r,b:a}=t,s=Math.min(e,r,a),n=Math.max(e,r,a);if(n===s)return 0;let o=n-s,h=0;switch(n){case e:h=(r-a)/o;break;case r:h=(a-e)/o+2;break;case a:h=(e-r)/o+4;break}let b=h*60;return b<0&&(b+=360),Math.round(b)}function L(t){let{r:e,g:r,b:a,a:s}=t,n={r:i("rgb",e),g:i("rgb",r),b:i("rgb",a),a:i("alpha",s)},o=k(n),h=O(n),b=E(n);return{h:o,s:h,l:b,a:n.a}}var x=L,c=class t{constructor(e,r){switch(e){case"hex":this._rgb=A(r),this._hsl=x(this._rgb);break;case"hsl":{let a=this.#e(r);this._rgb=T(a),this._hsl=a;break}case"rgb":this._rgb=this.#r(r),this._hsl=x(this._rgb);break;default:throw new Error("Invalid color space value")}this._hex=S(this._rgb)}get rgb(){return Object.assign({},this._rgb)}get hsl(){return Object.assign({},this._hsl)}get hex(){return this._hex}get alpha(){return this._rgb.a}get red(){return this._rgb.r}get green(){return this._rgb.g}get blue(){return this._rgb.b}get hue(){return this._hsl.h}get saturation(){return this._hsl.s}get lightness(){return this._hsl.l}#r(e){if(!Array.isArray(e)&&typeof e!="string")throw new Error("RGB color value has to be a string or an Array");let r=Array.isArray(e)?e:e.split(/,\s?|\s/);if(r.length!==3&&r.length!==4)throw new Error(`Invalid RGB values array: ${e}`);let a=i("rgb",r[0]),s=i("rgb",r[1]),n=i("rgb",r[2]),o=i("alpha",r[3]);return{r:a,g:s,b:n,a:o}}#e(e){if(!Array.isArray(e)&&typeof e!="string")throw new Error("HSL color value has to be a string or an Array");let r=Array.isArray(e)?e:e.split(/,\s?|\s/);if(r.length!==3&&r.length!==4)throw new Error(`Invalid HSL values array: ${e}`);let a=i("hue",r[0]),s=i("saturation",r[1]),n=i("lightness",r[2]),o=i("alpha",r[3]);return{h:a,s,l:n,a:o}}static fromHslObj(e){let{h:r,s:a,l:s,a:n}=e;if(typeof r>"u"||typeof a>"u"||typeof s>"u")throw new Error("Invalid HSL object");return new t("hsl",[r,a,s,n])}static fromRgbObj(e){let{r,g:a,b:s,a:n}=e;if(typeof r>"u"||typeof a>"u"||typeof s>"u")throw new Error("Invalid RGB object");return new t("rgb",[r,a,s,n])}};function B(t,e){if(typeof t!="number")throw new Error(`Invalid value: '${t}'. ${e} value has to be a number`);let r=0,a=0;switch(e){case"Hue":r=-360,a=360;break;case"Alpha":r=-1,a=1;break;case"RGB":r=-255,a=255;break;default:throw new Error("Invalid type to check. Allowed values: 'Hue', 'Alpha', 'RGB'")}if(t<r||t>a)throw new Error(`Invalid value: '${t}'. ${e} has to be a number in range [${r}; ${a}]`);return!0}var v=B;function G(t,e,r){for(let a of[t,e,r]){if(typeof a!="number")throw new Error("One or more parameters are not numbers");if(isNaN(a))throw new Error("One or more parameters are NaN")}return Math.max(t,Math.min(e,r))}var $=G;function K(t,e){if(typeof t!="number"||typeof e!="number")throw new TypeError("Arguments must be numbers");if(isNaN(t)||isNaN(e))throw new Error("Invalid argument: one or more arguments is `NaN`");return parseFloat((t+e).toFixed(2))}var p=K;function y(t,e){let r=new RegExp(/^[+-]?\d{1,2}(\.\d+)?%|100(\.0+)?%$/);if(typeof t!="string")throw new Error(`Invalid value: '${t}'. ${e} value has to be a string`);if(!r.test(t))throw new Error(`Invalid value: '${t}'. ${e} value has to be a percent in range [-100%; 100%]`);return!0}function P(t,{hue:e,saturation:r,lightness:a,alpha:s}){if(!(t instanceof c))throw new TypeError("Color value has to be an instance of Spectrum class");let n={...t.hsl};if(e!==void 0&&(v(e,"Hue"),n.h=$(0,360,Math.round(n.h+e))),r!==void 0){y(r,"Saturation");let o=Math.round(parseInt(r))/100;n.s=$(0,1,p(n.s,o))}if(a!==void 0){y(a,"Lightness");let o=Math.round(parseInt(a))/100;n.l=$(0,1,p(n.l,o))}return s!==void 0&&(v(s,"Alpha"),n.a=$(0,1,p(n.a,s))),c.fromHslObj(n)}var rr=P;function W(t,e){if(!(t instanceof c))throw new TypeError("Color value has to be an instance of Spectrum class");let r={...t.rgb},a=["red","green","blue"];for(let[s,n]of Object.entries(e))if(n!==void 0){if(s!=="alpha"&&a.includes(s)){let o=s[0].toLowerCase();v(n,"RGB"),r[o]=$(0,255,Math.round(r[o]+n))}s==="alpha"&&(v(n,"Alpha"),r.a=$(0,1,p(r.a,n)))}return c.fromRgbObj(r)}var er=W;function D(t){if(typeof t!="number")throw new Error(`Invalid weight: ${t}. Weight value has to be a number`);if(isNaN(t))throw new Error("Invalid weight: the 'NaN' value provided");if(t<0||t>1)throw new Error(`Invalid weight: ${t}. Weight value has to be a percent in range [0; 1]`);return!0}var N=D;function X(t,e,r){if(!(t instanceof c)||!(e instanceof c))throw new TypeError("Color values have to be an instance of Spectrum class");N(r);let a=Math.round(t.red*r+e.red*(1-r)),s=Math.round(t.green*r+e.green*(1-r)),n=Math.round(t.blue*r+e.blue*(1-r)),o=Number((t.alpha*r+e.alpha*(1-r)).toFixed(2));return c.fromRgbObj({r:a,g:s,b:n,a:o})}var q=X;function z(t,e){if(!(t instanceof c))throw new TypeError("Color value has to be an instance of Spectrum class");let r={...t.hsl},a=["hue","saturation","lightness","alpha"];for(let[s,n]of Object.entries(e))if(n!==void 0&&a.includes(s)){let o=s[0].toLowerCase();r[o]=i(s,n)}return c.fromHslObj(r)}var J=z;function Q(t){if(!(t instanceof c))throw new TypeError("Color value has to be an instance of Spectrum class");let e={};for(let r=0;r<=100;r++)e[r]=J(t,{lightness:`${r}%`});return e}var tr=Q;function U(t){if(!(t instanceof c))throw new TypeError("Color value has to be an instance of Spectrum class");let{h:e,...r}=t.hsl,a=(e+150)%360,s=(e+210)%360,n=c.fromHslObj({h:a,...r}),o=c.fromHslObj({h:s,...r});return{secondary:n,tertiary:o}}var ar=U;function Y(t,e){if(!(t instanceof c))throw new TypeError("Color values have to be an instance of Spectrum class");N(e);let{r,g:a,b:s}=t.rgb,n=new c("rgb",[255-r,255-a,255-s]);return q(n,t,e)}var nr=Y;function Z(t,e){if(!(t instanceof c))throw new TypeError("Color value has to be an instance of Spectrum class");let r={...t.rgb},a=["red","green","blue"];for(let[s,n]of Object.entries(e)){if(n!==void 0&&s!=="alpha"&&a.includes(s)){let o=s[0].toLowerCase();r[o]=i("rgb",n)}s==="alpha"&&(r.a=i("alpha",n))}return c.fromRgbObj(r)}var sr=Z,V=c;export{rr as adjustHsl,er as adjustRgb,q as colorMix,tr as createPalette,V as default,ar as getSplitComplementary,A as hexToRgb,T as hslToRgb,nr as invert,S as rgbObjToHex,x as rgbObjToHsl,J as setHsl,sr as setRgb};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment