@rtcoder/string-to-color

Deterministic color utility

String to Color

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
0 runtime dependencies
ESM + CJS package exports
Typed TypeScript declarations
Generated color #de2db0
Output format
65%
55%
const color = stringToColor("Lorem ipsum dolor sit amet");

Usage

Import it wherever colors need to be stable.

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.

ES module

import { stringToColor } from "@rtcoder/string-to-color";

const color = stringToColor("Ada Lovelace");

CommonJS

const { stringToColor } = require("@rtcoder/string-to-color");

const color = stringToColor("Ada Lovelace");

Readable HSL

const color = stringToColor("Ada Lovelace", {
  format: "hsl",
  saturation: 65,
  lightness: 55,
});

TypeScript

interface StringToColorOptions {
  format?: "hex" | "hsl";
  saturation?: number;
  lightness?: number;
}

API reference

One function, small surface area.

stringToColor(str)
Returns a deterministic hexadecimal color, for example #de2db0. This is the default and preserves the original library behavior.
format
Optional. Use "hex" for hexadecimal output or "hsl" for CSS HSL output.
saturation
Optional HSL percentage from 0 to 100. Values outside the range are clamped.
lightness
Optional HSL percentage from 0 to 100. Use higher values for softer badges and lower values for stronger foregrounds.

Examples

A few stable outputs.