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

hex-color-to-color-name

v2.0.1

Published

Converts Hex Codes to Color Names

Readme

🎨 HexColorToColorName

npm version npm provenance License: MIT Test Status

A lightweight, zero-dependency TypeScript library that maps hex codes to the closest human-readable color names. Perfect for accessibility, UI/UX tools, and design systems.

✨ Features

  • Accurate Matching: Uses mathematical distance in RGB and HSL color spaces to find the closest color name if an exact match isn't found.
  • Flexible Input: Supports 3, 4 (RGBA - alpha ignored), and 6-digit hex codes, with or without the # prefix.
  • TypeScript Ready: Full type definitions included out of the box, ensuring robust and predictable usage.
  • Lightweight: Zero external runtime dependencies.
  • Fast: Optimized lookup with pre-processed color data for high-performance applications.

🚀 Installation

Install via npm:

npm install hex-color-to-color-name

Or via yarn:

yarn add hex-color-to-color-name

🛠 Usage

GetColorName(code: string): Basic Color Name Lookup

Pass a hex string (with or without the # symbol, including 4-digit RGBA) to get the most accurate human-readable name. If no exact match is found, it returns the closest known color name.

import { GetColorName } from "hex-color-to-color-name";

// Exact matches
const color1 = GetColorName("#000000"); // returns "Black"
const color2 = GetColorName("ff0000"); // returns "Red"
const color3 = GetColorName("#f5f5dc"); // returns "Beige"

// Closest matches for non-exact hex codes
const approximateColor1 = GetColorName("#1a1a1b"); // returns "Black"
const approximateColor2 = GetColorName("1E90F0"); // returns "Dodger Blue"

// 4-digit hex codes (alpha ignored)
const rgbaColor = GetColorName("#F00A"); // returns "Red" (alpha channel 'A' is ignored)
const rgbaColorNoHash = GetColorName("000F"); // returns "Black" (alpha channel 'F' is ignored)

GetColorPacket(code: string): Detailed Color Information

Use GetColorPacket to retrieve a comprehensive ColorPacket object, which includes the resolved colorName (exact or closest match), the normalized hexCode, and its rgb and hsl components. Even for invalid inputs, it returns a ColorPacket with an "Invalid Color" name and default rgb/hsl values.

import { GetColorPacket, ColorPacket } from "hex-color-to-color-name";

// For an exact match
const blackPacket: ColorPacket = GetColorPacket("#000000");
/*
{
  colorName: "Black",
  hexCode: "000000",
  rgb: { r: 0, g: 0, b: 0 },
  hsl: { h: 0, s: 0, l: 0 }
}
*/

// For a closest match
const approximatePacket: ColorPacket = GetColorPacket("#1a1a1b");
/*
{
  colorName: "Black",
  hexCode: "1A1A1B",
  rgb: { r: 26, g: 26, b: 27 },
  hsl: { h: 240, s: 2, l: 10 }
}
*/

// For a 4-digit hex code
const rgbaPacket: ColorPacket = GetColorPacket("#F00A");
/*
{
  colorName: "Red",
  hexCode: "FF0000",
  rgb: { r: 255, g: 0, b: 0 },
  hsl: { h: 0, s: 100, l: 50 }
}
*/

// For an invalid hex code
const invalidPacket: ColorPacket = GetColorPacket("not-a-color");
/*
{
  colorName: "Invalid Color: not-a-color",
  hexCode: "not-a-color", // Original invalid input is preserved
  rgb: { r: 0, g: 0, b: 0 },
  hsl: { h: 0, s: 0, l: 0 }
}
*/

🤝 Contributing

Pull requests are welcome. Please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

☕ Support

If you find this library useful, consider supporting the development by buying me a Ko-Fi! ☕❤️

📄 License

MIT

Maintained by Jefferin Joseph