uniqolor
Version:
Generate unique and beautiful colors from any texts or numbers
10 lines (9 loc) • 4.18 kB
JavaScript
/**
* Generate unique and beautiful colors from any texts or numbers
* @version v1.1.1
* @link https://github.com/dastoori/uniqolor#README
* @author Rasool Dastoori
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(t="undefined"!=typeof globalThis?globalThis:t||self).uniqolor=n()}(this,(function(){"use strict";function t(t,n){return function(t){if(Array.isArray(t))return t}(t)||function(t,n){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var o,e,a,i,u=[],c=!0,l=!1;try{if(a=(r=r.call(t)).next,0===n){if(Object(r)!==r)return;c=!1}else for(;!(c=(o=a.call(r)).done)&&(u.push(o.value),u.length!==n);c=!0);}catch(t){l=!0,e=t}finally{try{if(!c&&null!=r.return&&(i=r.return(),Object(i)!==i))return}finally{if(l)throw e}}return u}}(t,n)||r(t,n)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function n(t){return function(t){if(Array.isArray(t))return o(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||r(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function r(t,n){if(t){if("string"==typeof t)return o(t,n);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?o(t,n):void 0}}function o(t,n){(null==n||n>t.length)&&(n=t.length);for(var r=0,o=new Array(n);r<n;r++)o[r]=t[r];return o}var e=[0,100],a=[0,100],i=function(t){return"".concat(1===t.length?"0":"").concat(t)},u=function(t,n,r){return Math.max(Math.min(t,r),n)},c=function(t,n){return Math.floor(Math.random()*(n-t+1))+t},l=function t(n,r,o){for(var e=c(n,r),a=0;a<(null==o?void 0:o.length);a++){var i=o[a];if(2===(null==i?void 0:i.length)&&e>=i[0]&&e<=i[1])return t(n,r,o)}return e},f=function(t,n){return"number"==typeof n?n:t%Math.abs(n[1]-n[0])+n[0]},d=function(t,r){return"number"==typeof t?u.apply(void 0,[Math.abs(t)].concat(n(r))):1===t.length||t[0]===t[1]?u.apply(void 0,[Math.abs(t[0])].concat(n(r))):[Math.abs(u.apply(void 0,[t[0]].concat(n(r)))),u.apply(void 0,[Math.abs(t[1])].concat(n(r)))]},h=function(t,n,r){return r<0?r+=1:r>1&&(r-=1),r<1/6?t+6*(n-t)*r:r<.5?n:r<2/3?t+(n-t)*(2/3-r)*6:t},s=function(t,n,r){var o,e,a;if(t/=360,r/=100,0===(n/=100))o=e=a=r;else{var i=r<.5?r*(1+n):r+n-r*n,u=2*r-i;o=h(u,i,t+1/3),e=h(u,i,t),a=h(u,i,t-1/3)}return[Math.round(255*o),Math.round(255*e),Math.round(255*a)]},y=function(t,n,r,o){return(299*t+587*n+114*r)/1e3>=o},v=function(t,n,r){return"hsl(".concat(t,", ").concat(n,"%, ").concat(r,"%)")},p=function(t,n,r,o){return"rgb"===o?"rgb(".concat(t,", ").concat(n,", ").concat(r,")"):"#".concat(i(t.toString(16))).concat(i(n.toString(16))).concat(i(r.toString(16)))},b=function(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},o=r.format,i=void 0===o?"hex":o,u=r.saturation,c=void 0===u?[50,55]:u,l=r.lightness,h=void 0===l?[50,60]:l,b=r.differencePoint,m=void 0===b?130:b,g=Math.abs(function(t){for(var n=t.length,r=0,o=0;o<n;o++)r=(r<<5)-r+t.charCodeAt(o),r&=r;return r}(String(n))),M=f(g,[0,360]),S=f(g,d(c,e)),A=f(g,d(h,a)),j=t(s(M,S,A),3),x=j[0],w=j[1],I=j[2];return{color:"hsl"===i?v(M,S,A):p(x,w,I,i),isLight:y(x,w,I,m)}};return b.random=function(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},o=r.format,i=void 0===o?"hex":o,u=r.saturation,f=void 0===u?[50,55]:u,h=r.lightness,b=void 0===h?[50,60]:h,m=r.differencePoint,g=void 0===m?130:m,M=r.excludeHue;f=d(f,e),b=d(b,a);var S=M?l(0,359,M):c(0,359),A="number"==typeof f?f:c.apply(void 0,n(f)),j="number"==typeof b?b:c.apply(void 0,n(b)),x=t(s(S,A,j),3),w=x[0],I=x[1],O=x[2];return{color:"hsl"===i?v(S,A,j):p(w,I,O,i),isLight:y(w,I,O,g)}},b}));
//# sourceMappingURL=uniqolor.min.js.map