ES module
import { stringToColor } from "@rtcoder/string-to-color";
const color = stringToColor("Ada Lovelace");
Deterministic color utility
Convert any string into a stable HEX or HSL color. Useful for avatars, badges, labels, charts, tags, and lightweight visual identity without storing color values.
npm i @rtcoder/string-to-color
const color = stringToColor("Lorem ipsum dolor sit amet");
Usage
The same input always returns the same color. Keep the default HEX output for backward-compatible values, or use HSL when you want more predictable saturation and lightness in interfaces.
import { stringToColor } from "@rtcoder/string-to-color";
const color = stringToColor("Ada Lovelace");
const { stringToColor } = require("@rtcoder/string-to-color");
const color = stringToColor("Ada Lovelace");
const color = stringToColor("Ada Lovelace", {
format: "hsl",
saturation: 65,
lightness: 55,
});
interface StringToColorOptions {
format?: "hex" | "hsl";
saturation?: number;
lightness?: number;
}
API reference
#de2db0. This is the default and preserves the
original library behavior.
"hex" for hexadecimal output or
"hsl" for CSS HSL output.
0 to 100.
Values outside the range are clamped.
0 to 100.
Use higher values for softer badges and lower values for
stronger foregrounds.
Examples