nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.
38 lines (37 loc) • 1.5 kB
JavaScript
import { convertColorCode } from './convert.js';
import { _generateRandomHSL, _isSimilarToLast } from './helpers.js';
/** Track previously generated colors. */
const generatedColors = new Set();
/** Array of recently generated colors */
const recentColors = [];
/**
* * Utility to generate a unique random HSL color.
*
* @param maxColors - The maximum number of recent colors to store in memory. Default is `16`.
* @returns Generated unique random color in `HSL` format.
*/
export const generateRandomHSLColor = (maxColors = 16) => {
let color;
// Keep generating until a unique color is found that is also different from the last one
do {
color = _generateRandomHSL();
} while (generatedColors.has(color) ||
_isSimilarToLast(recentColors, color));
// Add the newly generated color to the set and recent colors
generatedColors.add(color);
recentColors.push(color);
// Limit the recent colors to the last `maxColors` to avoid excessive memory usage
if (recentColors?.length > maxColors) {
recentColors?.shift();
}
return color;
};
/**
* * Utility to generate a unique random color in Hex and RGB format.
*
* @param maxColors - The maximum number of recent colors to store in memory. Default is `16`.
* @returns An object of generated unique random color in both `Hex` and `RGB` formats.
*/
export const generateRandomColorInHexRGB = (maxColors = 16) => {
return convertColorCode(generateRandomHSLColor(maxColors));
};