uuid-color package on npm: https://www.npmjs.com/package/uuid-color
A lightweight package to deterministically generate unique and uniformly sampled colors from UUIDs.
The RGB color space is represented by hexadecimal numbers in the 0x000000 to 0xffffff range. We use a simple modulo hash function (% (0xffffff + 0x000001)
i.e. % 0x100000
) to produce numbers in that range from the decimal representation of the UUID (a base 10 39-digit integer). We then extract the red, green, and blue components from the resulting hash (which represents a specific color in hexadecimal notation) using bit masks.
Since the color space is only 256^3 whereas the UUID space is much larger (approximtely 5.3 x 10^36 possible UUIDs), it is impossible to create a complete bijection between the spaces, and so collisions in the generated color space can occur. This package does however guarantee that the color space is maximally utilized, assuming a uniform distribution of the input UUIDs within the UUID space.
npm i uuid-color
import { v4 as uuidv4 } from "uuid";
import { colorFromUuid } from "uuid-color";
const myUuid = uuidv4();
// returns a hex color code as a string "#rrggbb"
const hexColor = colorFromUuid(myUuid);
▸ colorFromUuid(uuid
, options?
): string
Returns the generated color associated with the given uuid.
throws
Error
This exception is thrown if the input uuid string is not a valid UUID.
Name | Type | Description |
---|---|---|
uuid |
string |
The uuid for which to generate a color |
options |
Options |
An optional object to configure the color generation, and attach callbacks that directly receive the generated color code or components in various formats |
string
The generated color as a CSS <color>
notation string