UNPKG

fast-color-generator

Version:

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

1 lines 3.85 kB
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["// Lookup table for hex digits\r\nconst hexDigits = \"0123456789abcdef\";\r\n\r\nexport interface RGB {\r\n r: number;\r\n g: number;\r\n b: number;\r\n}\r\n\r\nexport default class FastColorGenerator {\r\n private state: number;\r\n\r\n /**\r\n * Creates an instance of FastColorGenerator.\r\n * @param seed - The seed value for the generator. Defaults to the current timestamp.\r\n */\r\n constructor(seed: number = Date.now()) {\r\n this.state = seed >>> 0; // Ensure the seed is a 32-bit unsigned integer\r\n }\r\n\r\n /**\r\n * Generates the next pseudo-random color state.\r\n */\r\n next(): void {\r\n const multiplier = 1664525;\r\n const increment = 1013904223;\r\n\r\n // Linear Congruential Generator (LCG) for pseudo-random numbers\r\n this.state = (this.state * multiplier + increment) >>> 0;\r\n }\r\n\r\n /**\r\n * Gets the current color as a hex string (e.g., #rrggbb).\r\n */\r\n get hex(): string {\r\n const { r, g, b } = this.rgb;\r\n return `#${hexDigits[r >> 4]}${hexDigits[r & 0xf]}${hexDigits[g >> 4]}${\r\n hexDigits[g & 0xf]\r\n }${hexDigits[b >> 4]}${hexDigits[b & 0xf]}`;\r\n }\r\n\r\n /**\r\n * Sets the color using a hex string (e.g., #rrggbb).\r\n * @param value - The hex color string to set.\r\n * @throws {Error} - If invalid hex color format was passed.\r\n */\r\n set hex(value: string) {\r\n if (!/^#[0-9a-fA-F]{6}$/.test(value)) {\r\n throw new Error(\"Invalid hex color format. Expected format: #rrggbb.\");\r\n }\r\n\r\n const r = parseInt(value.slice(1, 3), 16);\r\n const g = parseInt(value.slice(3, 5), 16);\r\n const b = parseInt(value.slice(5, 7), 16);\r\n\r\n this.rgb = { r, g, b };\r\n }\r\n\r\n /**\r\n * Gets the current color as an RGB object.\r\n */\r\n get rgb(): RGB {\r\n return {\r\n r: (this.state >> 16) & 0xff,\r\n g: (this.state >> 8) & 0xff,\r\n b: this.state & 0xff,\r\n };\r\n }\r\n\r\n /**\r\n * Sets the color using an RGB object.\r\n * @param value - The RGB object to set.\r\n */\r\n set rgb(value: RGB) {\r\n const { r, g, b } = value;\r\n if (\r\n !this.isValidChannel(r) ||\r\n !this.isValidChannel(g) ||\r\n !this.isValidChannel(b)\r\n ) {\r\n throw new Error(\"RGB values must be integers between 0 and 255.\");\r\n }\r\n\r\n this.state = ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);\r\n }\r\n\r\n /**\r\n * Validates if a color channel value is between 0 and 255.\r\n * @param value - The channel value to validate.\r\n * @returns True if the value is valid, false otherwise.\r\n */\r\n private isValidChannel(value: number): boolean {\r\n return Number.isInteger(value) && value >= 0 && value <= 255;\r\n }\r\n}\r\n"],"mappings":"AACA,MAAM,EAAY,mBAQlB,IAAqB,EAArB,KAAwC,CACtC,MAMA,YAAY,EAAe,KAAK,IAAI,EAAG,CACrC,KAAK,MAAQ,IAAS,CACxB,CAKA,MAAa,CAKX,KAAK,MAAS,KAAK,MAAQ,QAAa,aAAe,CACzD,CAKA,IAAI,KAAc,CAChB,GAAM,CAAE,IAAG,IAAG,KAAM,KAAK,IACzB,MAAO,IAAI,EAAU,GAAK,KAAK,EAAU,EAAI,MAAO,EAAU,GAAK,KACjE,EAAU,EAAI,MACb,EAAU,GAAK,KAAK,EAAU,EAAI,KACvC,CAOA,IAAI,IAAI,EAAe,CACrB,GAAI,CAAC,oBAAoB,KAAK,CAAK,EACjC,MAAU,MAAM,qDAAqD,EAGvE,IAAM,EAAI,SAAS,EAAM,MAAM,EAAG,CAAC,EAAG,EAAE,EAClC,EAAI,SAAS,EAAM,MAAM,EAAG,CAAC,EAAG,EAAE,EAClC,EAAI,SAAS,EAAM,MAAM,EAAG,CAAC,EAAG,EAAE,EAExC,KAAK,IAAM,CAAE,IAAG,IAAG,GAAE,CACvB,CAKA,IAAI,KAAW,CACb,MAAO,CACL,EAAI,KAAK,OAAS,GAAM,IACxB,EAAI,KAAK,OAAS,EAAK,IACvB,EAAG,KAAK,MAAQ,GAClB,CACF,CAMA,IAAI,IAAI,EAAY,CAClB,GAAM,CAAE,IAAG,IAAG,KAAM,EACpB,GACE,CAAC,KAAK,eAAe,CAAC,GACtB,CAAC,KAAK,eAAe,CAAC,GACtB,CAAC,KAAK,eAAe,CAAC,EAEtB,MAAU,MAAM,gDAAgD,EAGlE,KAAK,OAAU,EAAI,MAAS,IAAQ,EAAI,MAAS,EAAM,EAAI,GAC7D,CAOA,eAAuB,EAAwB,CAC7C,OAAO,OAAO,UAAU,CAAK,GAAK,GAAS,GAAK,GAAS,GAC3D,CACF"}