UNPKG

fast-color-generator

Version:

Super fast and efficient pseudo-random color generator, powered by a Linear Congruential Generator (LCG).

2 lines 1.31 kB
var f=Object.defineProperty;var b=Object.getOwnPropertyDescriptor;var h=Object.getOwnPropertyNames;var o=Object.prototype.hasOwnProperty;var c=(n,t)=>{for(var e in t)f(n,e,{get:t[e],enumerable:!0})},g=(n,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of h(t))!o.call(n,s)&&s!==e&&f(n,s,{get:()=>t[s],enumerable:!(r=b(t,s))||r.enumerable});return n};var x=n=>g(f({},"__esModule",{value:!0}),n);var l={};c(l,{default:()=>a});module.exports=x(l);var i="0123456789abcdef",a=class{state;constructor(t=Date.now()){this.state=t>>>0}next(){this.state=this.state*1664525+1013904223>>>0}get hex(){let{r:t,g:e,b:r}=this.rgb;return`#${i[t>>4]}${i[t&15]}${i[e>>4]}${i[e&15]}${i[r>>4]}${i[r&15]}`}set hex(t){if(!/^#[0-9a-fA-F]{6}$/.test(t))throw new Error("Invalid hex color format. Expected format: #rrggbb.");let e=parseInt(t.slice(1,3),16),r=parseInt(t.slice(3,5),16),s=parseInt(t.slice(5,7),16);this.rgb={r:e,g:r,b:s}}get rgb(){return{r:this.state>>16&255,g:this.state>>8&255,b:this.state&255}}set rgb(t){let{r:e,g:r,b:s}=t;if(!this.isValidChannel(e)||!this.isValidChannel(r)||!this.isValidChannel(s))throw new Error("RGB values must be integers between 0 and 255.");this.state=(e&255)<<16|(r&255)<<8|s&255}isValidChannel(t){return Number.isInteger(t)&&t>=0&&t<=255}}; //# sourceMappingURL=index.cjs.map