UNPKG

colvert

Version:

A comprehensive color conversion and manipulation package for JavaScript/TypeScript

2 lines (1 loc) 14.1 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});let t=class extends Error{constructor(t){super(t),this.name="ColorConversionError"}};const e=(e,r,o)=>{if(!((t,e)=>{switch(e){case"rgb":return!(!Array.isArray(t)||3!==t.length)&&t.every((t=>"number"==typeof t&&t>=0&&t<=255));case"rgba":return!(!Array.isArray(t)||4!==t.length)&&t.slice(0,3).every((t=>"number"==typeof t&&t>=0&&t<=255))&&"number"==typeof t[3]&&t[3]>=0&&t[3]<=1;case"hsl":case"hsv":return!(!Array.isArray(t)||3!==t.length)&&"number"==typeof t[0]&&t[0]>=0&&t[0]<=360&&"number"==typeof t[1]&&t[1]>=0&&t[1]<=100&&"number"==typeof t[2]&&t[2]>=0&&t[2]<=100;case"cmyk":return!(!Array.isArray(t)||4!==t.length)&&t.every((t=>"number"==typeof t&&t>=0&&t<=100));case"hex":return"string"==typeof t&&/^#([0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8})$/i.test(t);default:return!1}})(e,r))throw new t(o);return e},r=(t,e,r)=>Math.min(Math.max(t,e),r),o=(t,e,o)=>{if(void 0===t)return Math.floor(Math.random()*(o-e+1))+e;if(Array.isArray(t)){const a=r(t[0],e,o),n=r(t[1],e,o);return Math.floor(Math.random()*(n-a+1))+a}return r(t,e,o)},a=t=>{let e=Math.round(r(t,0,255)).toString(16);return e.length<2&&(e="0"+e),e},n=r=>{try{const t=e(r,"rgb","Invalid RGB value. Expected an array of 3 numbers between 0-255."),o=a(t[0]),n=a(t[1]);return`#${o}${n}${a(t[2])}`}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert RGB to Hex: ${o.message}`)}},s=r=>{try{const t=e(r,"rgb","Invalid RGB value. Expected an array of 3 numbers between 0-255."),o=t[0]/255,a=t[1]/255,n=t[2]/255,s=Math.min(o,a,n),h=Math.max(o,a,n),c=h-s;let l=0,i=0,u=(s+h)/2;return h!==s&&(i=u<=.5?c/(h+s):c/(2-h-s),o===h?l=(a-n)/c+(a<n?6:0):a===h?l=(n-o)/c+2:n===h&&(l=(o-a)/c+4),l*=60),[Math.round(l),Math.round(100*i),Math.round(100*u)]}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert RGB to HSL: ${o.message}`)}},h=r=>{try{const t=e(r,"rgb","Invalid RGB value. Expected an array of 3 numbers between 0-255."),o=t[0]/255,a=t[1]/255,n=t[2]/255,s=Math.max(o,a,n),h=s-Math.min(o,a,n);let c=0,l=0===s?0:h/s,i=s;return 0!==h&&(c=s===o?(a-n)/h%6:s===a?(n-o)/h+2:(o-a)/h+4,c*=60,c<0&&(c+=360)),[Math.round(c),Math.round(100*l),Math.round(100*i)]}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert RGB to HSV: ${o.message}`)}},c=r=>{try{const t=e(r,"rgb","Invalid RGB value. Expected an array of 3 numbers between 0-255."),o=t[0]/255,a=t[1]/255,n=t[2]/255;if(0===o&&0===a&&0===n)return[0,0,0,100];const s=1-Math.max(o,a,n),h=(1-o-s)/(1-s),c=(1-a-s)/(1-s),l=(1-n-s)/(1-s);return[Math.round(100*h),Math.round(100*c),Math.round(100*l),Math.round(100*s)]}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert RGB to CMYK: ${o.message}`)}},l=r=>{try{const t=e(r,"rgba","Invalid RGBA value. Expected an array of 4 numbers, RGB between 0-255 and alpha between 0-1."),o=a(t[0]),n=a(t[1]),s=a(t[2]);return`#${o}${n}${s}${a(Math.round(255*t[3]))}`}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert RGBA to Hex: ${o.message}`)}},i=r=>{try{const t=e(r,"rgb","Invalid RGB value. Expected an array of 3 numbers between 0-255."),o=u(t),a=95.047,n=100,s=108.883;let h=o[0]/a,c=o[1]/n,l=o[2]/s;h=h>.008856?Math.pow(h,1/3):7.787*h+16/116,c=c>.008856?Math.pow(c,1/3):7.787*c+16/116,l=l>.008856?Math.pow(l,1/3):7.787*l+16/116;const i=116*c-16,d=500*(h-c),g=200*(c-l);return[Math.round(i),Math.round(d),Math.round(g)]}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert RGB to LAB: ${o.message}`)}},u=r=>{try{const t=e(r,"rgb","Invalid RGB value. Expected an array of 3 numbers between 0-255.");let o=t[0]/255,a=t[1]/255,n=t[2]/255;o=o>.04045?Math.pow((o+.055)/1.055,2.4):o/12.92,a=a>.04045?Math.pow((a+.055)/1.055,2.4):a/12.92,n=n>.04045?Math.pow((n+.055)/1.055,2.4):n/12.92,o*=100,a*=100,n*=100;return[.4124*o+.3576*a+.1805*n,.2126*o+.7152*a+.0722*n,.0193*o+.1192*a+.9505*n]}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert RGB to XYZ: ${o.message}`)}},d=r=>{try{const t=e(r,"rgb","Invalid RGB value. Expected an array of 3 numbers between 0-255."),o=i(t),a=o[0],n=Math.sqrt(Math.pow(o[1],2)+Math.pow(o[2],2));let s=Math.atan2(o[2],o[1])*(180/Math.PI);return s<0&&(s+=360),[a,Math.round(n),Math.round(s)]}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert RGB to LCH: ${o.message}`)}},g=r=>{try{let t=e(r,"hex","Invalid HEX value. Expected a string in format #RGB, #RGBA, #RRGGBB, or #RRGGBBAA.").replace(/^#/,"");3===t.length&&(t=t.split("").map((t=>t+t)).join("")),8===t.length?t=t.substring(0,6):4===t.length&&(t=t.substring(0,3),t=t.split("").map((t=>t+t)).join(""));const o=parseInt(t,16);return[o>>16&255,o>>8&255,255&o]}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert HEX to RGB: ${o.message}`)}},f=r=>{try{const t=e(r,"hex","Invalid HEX value. Expected a string in format #RGB, #RGBA, #RRGGBB, or #RRGGBBAA."),o=g(t);return s(o)}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert HEX to HSL: ${o.message}`)}},m=r=>{try{const t=e(r,"hex","Invalid HEX value. Expected a string in format #RGB, #RGBA, #RRGGBB, or #RRGGBBAA."),o=g(t);return h(o)}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert HEX to HSV: ${o.message}`)}},w=r=>{try{const t=e(r,"hex","Invalid HEX value. Expected a string in format #RGB, #RGBA, #RRGGBB, or #RRGGBBAA."),o=g(t);return c(o)}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert HEX to CMYK: ${o.message}`)}},x=r=>{try{let t=e(r,"hex","Invalid HEX value. Expected a string in format #RGB, #RGBA, #RRGGBB, or #RRGGBBAA.").replace(/^#/,"");3===t.length?(t=t.split("").map((t=>t+t)).join(""),t+="FF"):4===t.length?t=t.split("").map((t=>t+t)).join(""):6===t.length&&(t+="FF");const o=parseInt(t,16);return[o>>24&255,o>>16&255,o>>8&255,parseFloat(((255&o)/255).toFixed(2))]}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert HEX to RGBA: ${o.message}`)}},p=r=>{try{const t=e(r,"hsl","Invalid HSL value. Expected an array with h(0-360), s(0-100), l(0-100)."),o=t[0]/360,a=t[1]/100,n=t[2]/100;let s,h,c;if(0===a)s=h=c=n;else{const t=(t,e,r)=>(r<0&&(r+=1),r>1&&(r-=1),r<1/6?t+6*(e-t)*r:r<.5?e:r<2/3?t+(e-t)*(2/3-r)*6:t),e=n<.5?n*(1+a):n+a-n*a,r=2*n-e;s=t(r,e,o+1/3),h=t(r,e,o),c=t(r,e,o-1/3)}return[Math.round(255*s),Math.round(255*h),Math.round(255*c)]}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert HSL to RGB: ${o.message}`)}},y=r=>{try{const t=e(r,"hsl","Invalid HSL value. Expected an array with h(0-360), s(0-100), l(0-100)."),o=p(t);return n(o)}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert HSL to HEX: ${o.message}`)}},v=r=>{try{const t=e(r,"hsl","Invalid HSL value. Expected an array with h(0-360), s(0-100), l(0-100)."),o=t[0],a=t[1]/100,n=t[2]/100;let s=n+a*Math.min(n,1-n),h=0===s?0:2*(1-n/s);return[o,Math.round(100*h),Math.round(100*s)]}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert HSL to HSV: ${o.message}`)}},b=r=>{try{const t=e(r,"cymk","Invalid CMYK value. Expected an array with c,m,y,k values between 0-100."),o=t[0]/100,a=t[1]/100,n=t[2]/100,s=t[3]/100,h=Math.round(255*(1-o)*(1-s)),c=Math.round(255*(1-a)*(1-s));return[h,c,Math.round(255*(1-n)*(1-s))]}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert CMYK to RGB: ${o.message}`)}},M=r=>{try{const t=e(r,"cymk","Invalid CMYK value. Expected an array with c,m,y,k values between 0-100."),o=b(t);return n(o)}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert CMYK to HEX: ${o.message}`)}},T=r=>{try{const t=e(r,"hsl","Invalid CMYK value. Expected an array with c,m,y,k values between 0-100."),o=b(t);return s(o)}catch(o){if(o instanceof t)throw o;throw new t(`Failed to convert CMYK to HSL: ${o.message}`)}},B=Object.freeze({__proto__:null,cmykToHex:M,cmykToHsl:T,cmykToRgb:b,cmykTohex:M,cmykTohsl:T,cmykTorgb:b,hexToCmyk:w,hexToHsl:f,hexToHsv:m,hexToRgb:g,hexToRgba:x,hexTocmyk:w,hexTohsl:f,hexTohsv:m,hexTorgb:g,hslToHex:y,hslToHsv:v,hslToRgb:p,hslTohex:y,hslTohsv:v,hslTorgb:p,rgbToCmyk:c,rgbToHex:n,rgbToHsl:s,rgbToHsv:h,rgbToLab:i,rgbToLch:d,rgbToXyz:u,rgbTocmyk:c,rgbTohex:n,rgbTohsl:s,rgbTohsv:h,rgbaToHex:l}),R=e=>{try{const t=(null==e?void 0:e.format)||"hex",r=o(null==e?void 0:e.hue,0,359),a=o(null==e?void 0:e.saturation,0,100),n=[r,a,o(null==e?void 0:e.lightness,0,100)];switch(t){case"hex":return y(n);case"rgb":return p(n);default:return n}}catch(r){throw new t(`Failed to generate random color: ${r.message}`)}},G=e=>{try{let t;t="string"==typeof e?f(e):s(e);const r=t[0],o=t[1],a=t[2],h="string"==typeof e?e:n(e),c=[y([(r+30)%360,o,a]),y([(r+330)%360,o,a])],l=[y([(r+180)%360,o,a])],i=[y([(r+120)%360,o,a]),y([(r+240)%360,o,a])],u=[y([(r+90)%360,o,a]),y([(r+180)%360,o,a]),y([(r+270)%360,o,a])],d=[y([r,Math.max(0,o-20),a]),y([r,Math.min(100,o+20),a]),y([r,o,Math.max(0,a-20)]),y([r,o,Math.min(100,a+20)])];return{base:h,analogous:c,complementary:l,triadic:i,tetradic:u,monochromatic:d,splitComplementary:[y([(r+150)%360,o,a]),y([(r+210)%360,o,a])]}}catch(r){if(r instanceof t)throw r;throw new t(`Failed to generate color scheme: ${r.message}`)}},H=e=>{try{const t=("string"==typeof e?g(e):e).map((t=>(t/=255)<=.03928?t/12.92:Math.pow((t+.055)/1.055,2.4)));return.2126*t[0]+.7152*t[1]+.0722*t[2]}catch(r){if(r instanceof t)throw r;throw new t(`Failed to calculate luminance: ${r.message}`)}},A=(e,r)=>{try{const t=H(e),o=H(r);return(Math.max(t,o)+.05)/(Math.min(t,o)+.05)}catch(o){if(o instanceof t)throw o;throw new t(`Failed to calculate contrast ratio: ${o.message}`)}},E=(e,r,o=!1)=>{try{const t=A(e,r);return o?t>=3:t>=4.5}catch(a){if(a instanceof t)throw a;throw new t(`Failed to check WCAG AA compliance: ${a.message}`)}},F=(e,r,o=!1)=>{try{const t=A(e,r);return o?t>=4.5:t>=7}catch(a){if(a instanceof t)throw a;throw new t(`Failed to check WCAG AAA compliance: ${a.message}`)}},$=(o,a)=>{try{const n=r(a,0,100)/100;if("string"==typeof o){const t=f(o),e=Math.min(100,t[2]+Math.round(100*n));return y([t[0],t[1],e])}if(3===o.length)if(Boolean(e(o,"rgb","Invalid RGB value. Expected an array of 3 numbers between 0-255."))){const t=s(o),r=Math.min(100,t[2]+Math.round(100*n));if(Boolean(e([t[0],t[1],r],"hsl","Invalid HSL value. Expected an array with h(0-360), s(0-100), l(0-100).")))return p([t[0],t[1],r])}else if(Boolean(e(o,"hsl","Invalid HSL value. Expected an array with h(0-360), s(0-100), l(0-100)."))){const t=o,e=Math.min(100,t[2]+Math.round(100*n));return[t[0],t[1],e]}throw new t("Unsupported color format for lighten function")}catch(n){if(n instanceof t)throw n;throw new t(`Failed to lighten color: ${n.message}`)}},C=(o,a)=>{try{const n=r(a,0,100)/100;if("string"==typeof o){const t=f(o),e=Math.max(0,t[2]-Math.round(100*n));return y([t[0],t[1],e])}if(3===o.length){if(Boolean(e(o,"rgb","Invalid RGB value. Expected an array of 3 numbers between 0-255."))){const t=s(o),e=Math.max(0,t[2]-Math.round(100*n));return p([t[0],t[1],e])}if(Boolean(e(o,"hsl","Invalid HSL value. Expected an array with h(0-360), s(0-100), l(0-100)."))){const t=o,e=Math.max(0,t[2]-Math.round(100*n));return[t[0],t[1],e]}}throw new t("Unsupported color format for darken function")}catch(n){if(n instanceof t)throw n;throw new t(`Failed to darken color: ${n.message}`)}},k=(o,a)=>{try{const n=r(a,0,100)/100;if("string"==typeof o){const t=f(o),e=Math.min(100,t[1]+Math.round(100*n));return y([t[0],e,t[2]])}if(3===o.length){if(Boolean(e(o,"rgb","Invalid RGB value. Expected an array of 3 numbers between 0-255."))){const t=s(o),e=Math.min(100,t[1]+Math.round(100*n));return p([t[0],e,t[2]])}if(Boolean(e(o,"hsl","Invalid HSL value. Expected an array with h(0-360), s(0-100), l(0-100)."))){const t=o,e=Math.min(100,t[1]+Math.round(100*n));return[t[0],e,t[2]]}}throw new t("Unsupported color format for saturate function")}catch(n){if(n instanceof t)throw n;throw new t(`Failed to saturate color: ${n.message}`)}},I=(o,a)=>{try{const n=r(a,0,100)/100;if("string"==typeof o){const t=f(o),e=Math.max(0,t[1]-Math.round(100*n));return y([t[0],e,t[2]])}if(3===o.length){if(Boolean(e(o,"rgb","Invalid RGB value. Expected an array of 3 numbers between 0-255."))){const t=s(o),e=Math.max(0,t[1]-Math.round(100*n));return p([t[0],e,t[2]])}if(Boolean(e(o,"hsl","Invalid HSL value. Expected an array with h(0-360), s(0-100), l(0-100)."))){const t=o,e=Math.max(0,t[1]-Math.round(100*n));return[t[0],e,t[2]]}}throw new t("Unsupported color format for desaturate function")}catch(n){if(n instanceof t)throw n;throw new t(`Failed to desaturate color: ${n.message}`)}},L=e=>{try{return I(e,100)}catch(r){if(r instanceof t)throw r;throw new t(`Failed to convert color to grayscale: ${r.message}`)}},S=e=>{try{return H(e)>.5?"#000000":"#ffffff"}catch(r){if(r instanceof t)throw r;throw new t(`Failed to suggest text color: ${r.message}`)}},X=Object.freeze({__proto__:null,darken:C,darkenColor:C,desaturate:I,desaturateColor:I,generateColorScheme:G,getContrastRatio:A,getLuminance:H,getSuggestedTextColor:S,grayscale:L,grayscaleColor:L,isWcagAA:E,isWcagAAA:F,lighten:$,lightenColor:$,randomColor:R,randomcolor:R,saturate:k,saturateColor:k});class _ extends Error{constructor(t){super(t),this.name="ColorConversionError"}}const Y={...B,...X};exports.ColorConversionError=_,exports.cmykToHex=M,exports.cmykToHsl=T,exports.cmykToRgb=b,exports.cmykTohex=M,exports.cmykTohsl=T,exports.cmykTorgb=b,exports.darken=C,exports.default=Y,exports.desaturate=I,exports.generateColorScheme=G,exports.getContrastRatio=A,exports.getLuminance=H,exports.getSuggestedTextColor=S,exports.grayscale=L,exports.hexToCmyk=w,exports.hexToHsl=f,exports.hexToHsv=m,exports.hexToRgb=g,exports.hexToRgba=x,exports.hexTocmyk=w,exports.hexTohsl=f,exports.hexTohsv=m,exports.hexTorgb=g,exports.hslToHex=y,exports.hslToHsv=v,exports.hslToRgb=p,exports.hslTohex=y,exports.hslTohsv=v,exports.hslTorgb=p,exports.isWcagAA=E,exports.isWcagAAA=F,exports.lighten=$,exports.randomColor=R,exports.randomcolor=R,exports.rgbToCmyk=c,exports.rgbToHex=n,exports.rgbToHsl=s,exports.rgbToHsv=h,exports.rgbToLab=i,exports.rgbToLch=d,exports.rgbToXyz=u,exports.rgbTocmyk=c,exports.rgbTohex=n,exports.rgbTohsl=s,exports.rgbTohsv=h,exports.rgbaToHex=l,exports.saturate=k;