npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

colorati

v1.1.0

Published

Transform values into consistent colors

Readme

colorati

Transform values into consistent colors

import { colorati } from 'colorati';
import { useMemo } from 'react';

function LabelComponent({ text }: { text: string }) {
    const style = useMemo(() => {
        const color = colorati(text);

        return {
            backgroundColor: color.hsl.css,
            color: color.harmonies.complement[1].hsl.css,
        };
    }, [text]);

    return <div className="label" style={style}>{text}</div>;
}

How it works

colorati uses hash-it to create a consistent hash code for any object value, and then derives a unique color based on that hash code.

API

Color types

The following color formats are supported and available with each colorati instance:

  • ansi16 ANSI (16-bit terminal)
  • ansi256 ANSI (256-bit terminal)
  • hex => hexadecimal
  • hsl => hue saturation light / alpha
  • hwb => hue whiteness blackness / alpha
  • lab => lightness a* (green to red axis) b* (blue to yellow axis) / alpha (CIELAB)
  • lch => lightness chroma hue / alpha (CIELAB)
  • oklab => lightness a* (green to red axis) b* (blue to yellow axis) / alpha (CIELAB with uniformity)
  • oklch => lightness chroma hue / alpha (CIELAB with uniformity)
  • rgb => red green blue / alpha

Output values

Each color type is represented as a tuple of the color values with additional properties:

  • alpha => the computed alpha value based on the hashed value (will be 1 when alpha option is false)
  • channels => the non-alpha numeric values representing each channel of the color
  • css => the string value expected by CSS
  • value => the channels plus the alpha

Example:

const { hsl } = colorati({ foo: 'bar' }, { alpha: true });

console.log(hsl.alpha); // 0.29411764705882354
console.log(hsl.channels); // [166.80851063829786, 62.66666666666668, 55.88235294117647]
console.log(hsl.css); // "hsl(167 62.67% 55.88% / 0.29)"
console.log(hsl.value); // [166.80851063829786, 62.66666666666668, 55.88235294117647, 0.29411764705882354]

In addition, you can destructure the channels / values:

const [h, s, l] = hsl;

And when stringified, the output will produce the CSS value for easy composition:

boxShadow: `0 0 10px ${hsl}`,

If you stringify the colorati instance itself, it will give a metadata representation of the color using the default RGB channels:

const color = colorati({ foo: bar });

console.log(JSON.stringify(color)); // "Colorati ({ red: 72, green: 213, blue: 182, alpha: 1 })"

Color utilities

In addition to providing various formats for the color, colorati will provide supporting utilities for the color.

harmonies

Color harmonies based on the computed color. This can be useful to determine supporting colors (or even entire color schemes) based on the computed color.

  • analogous
  • clash
  • complement
  • neutral
  • splitComplement
  • tetradic
  • triadic

Each harmony is represented as an array of colors (themselves colorati instances), where the first color is the computed color for the object value and the colors following are the harmonies.

const color = colorati({ foo: 'bar' });

console.log(
  JSON.stringify({
    main: color.rgb,
    complement: color.harmonies.analogous,
  }),
);
// {
//   "main": "rgb(72 213 182 / 1)",
//   "complement": [
//     "rgb(72 213 182 / 1)",
//     "rgb(72 174 213 / 1)",
//     "rgb(72 103 213 / 1)",
//     "rgb(111 72 213 / 1)",
//     "rgb(182 72 213 / 1)",
//     "rgb(213 72 173 / 1)"
//   ]
// }

getContrastRatio

Get the contrast ratio to the color provided.

const color = colorati({ foo: 'bar' });
const black = colorati.from([0, 0, 0, 1]);

console.log(color.getContrastRatio(black)); // 11.468723206193813

This is useful for determining contrast for accessibility purposes, such as if text foreground is readable against a colored background.

hasDarkContrast

DEPRECATED: Use getContrastRatio instead.

Whether the contrasting color for the computed color is considered dark by W3C standards. This can be useful when pairing colored backgrounds with foreground text to ensure accessibility standards are met.

luminance

The relative luminance value of the color, based on W3C standards.

Options

When creating a new colorati instance, you can provide options that will drive how the computed colors are represented.

alpha

defaults to false

If a boolean is provided, it determines whether an alpha channel is applied to the computed color (also consistent, driven by the hash). If false is provided, then an alpha of 1 (fully opaque) is used.

If a number is provided, then it overrides the computed alpha value and uses the provided value directly.

alphaPrecision

defaults to 2

How many decimal places the alpha value should be rounded to when represented in CSS. Only applies when alpha option is not false.

channelPrecision

defaults to 2

How many decimal places the channel values should be rounded to when represented in CSS.

Instance from RGBA

You can create a colorati instance from existing RGB channels (and optional alpha value), which is useful for comparison purposes via getContrastRatio.

const black = colorati.from([0, 0, 0]);
const semiOpaqueWhite = colorati.from([255, 255, 255, 0.5], { alpha: true });