UNPKG

fast-color-generator

Version:

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

2 lines 870 B
var s="0123456789abcdef",i=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`#${s[t>>4]}${s[t&15]}${s[e>>4]}${s[e&15]}${s[r>>4]}${s[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),n=parseInt(t.slice(5,7),16);this.rgb={r:e,g:r,b:n}}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:n}=t;if(!this.isValidChannel(e)||!this.isValidChannel(r)||!this.isValidChannel(n))throw new Error("RGB values must be integers between 0 and 255.");this.state=(e&255)<<16|(r&255)<<8|n&255}isValidChannel(t){return Number.isInteger(t)&&t>=0&&t<=255}};export{i as default}; //# sourceMappingURL=index.js.map