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

css-color-to-rgb

v1.0.1

Published

A utility library to convert CSS color values into an RGB array and hex code. Supports CSS color names, hex values, RGB, HSL, CSS variables and modern CSS color functions such as color-mix() and color().

Readme

css-color-to-rgb

A utility library to convert CSS color values into an RGB array and hex code. Supports CSS color names, hex values, RGB, HSL, CSS variables and modern CSS color functions such as color-mix() and color().

Motivation

Most color parser libraries implement their own color parsing logic, which often doesn't support the latest CSS color syntax or functions. This library takes a different approach by delegating all color calculations to the browser itself. By using the browser's built-in canvas rendering capabilities, we ensure that:

  • All color formats supported by the browser are automatically supported by this library
  • New CSS color functions like color-mix(), lch(), and color spaces like display-p3 work without requiring library updates
  • The results match exactly what would be rendered in the browser

This approach ensures future compatibility and accurate color representation for all browser-supported color formats.

Notes and Limitations

  • Browser Dependency: The results depend on the CSS features supported by the user's browser. Different browsers or browser versions may return slightly different RGB values for the same color input.

  • Alpha Channel Handling: When converting colors with alpha transparency (like rgba() or hsla()), the alpha component is composited with the canvas background and reflected in the resulting RGB values. This means the output will always be an RGB value without separate alpha information.

Features

  • cssColorToRgb(cssColorValue, options?): Returns an RGB array corresponding to the provided CSS color value.
  • cssColorToHex(cssColorValue, options?): Returns a hex string corresponding to the provided CSS color value.

Installation

Install using pnpm, npm, or yarn.

# pnpm
pnpm add css-color-to-rgb

# npm
npm install css-color-to-rgb

# yarn
yarn add css-color-to-rgb

Usage

// Example using ES Modules
import { cssColorToRgb, cssColorToHex } from "css-color-to-rgb";

// Basic color name
const rgb = cssColorToRgb("red");
console.log(rgb); // e.g., [255, 0, 0]

const hex = cssColorToHex("red");
console.log(hex); // e.g., "#ff0000"

// Hex color values
cssColorToRgb("#0088ff"); // [0, 136, 255]

// RGB color format
cssColorToRgb("rgb(0, 136, 255)"); // [0, 136, 255]

// HSL color format
cssColorToRgb("hsl(210, 100%, 50%)"); // [0, 136, 255]

// With alpha channel (alpha is reflected in the resulting RGB values)
// Since colors are rendered on canvas, transparent colors are composited with the canvas background
cssColorToRgb("rgba(0, 136, 255, 0.5)"); // The resulting RGB values will be affected by alpha compositing

// CSS Variables
// Assuming --primary-color: #0088ff; is defined in your CSS
cssColorToRgb("var(--primary-color)"); // [0, 136, 255]

// CSS Variables can be referenced in multiple ways
// 1. Using var() syntax
cssColorToRgb("var(--primary-color)"); // [0, 136, 255]

// 2. Direct variable name
cssColorToRgb("--primary-color"); // [0, 136, 255]

// 3. Accessing element's CSS properties
// For an element with style="color: blue; background-color: green;"
cssColorToRgb("color", { element: button }); // [0, 0, 255]
cssColorToRgb("backgroundColor", { element: button }); // [0, 128, 0]

// Passing a specific element to resolve CSS variables from
const button = document.querySelector("button");
cssColorToRgb("var(--button-color)", { element: button });
cssColorToRgb("--button-color", { element: button });

// Modern CSS Color functions like color-mix() are supported (in browsers that support them)
cssColorToRgb("color-mix(in srgb, blue 50%, red)"); // Approximately [128, 0, 128]

// Other CSS color functions
cssColorToRgb("color(display-p3 1 0.5 0)"); // Browser-dependent result based on display-p3 color space support
cssColorToRgb("lch(50% 50 120)"); // Browser-dependent result

Note: All color values as described in MDN Web Docs on CSS color values are supported.

Options

The options parameter for the cssColorToRgb function (and consequently for cssColorToHex) is an optional object that can include the following properties:

  • force (optional, boolean):
    When set to true, it bypasses the cached result and forces the function to recompute the color conversion.
  • element (optional, HTMLElement):
    Specifies the DOM element whose computed CSS styles are used to resolve CSS variables.
    Defaults to document.body if not provided.

Build

To build the library:

pnpm build

License

MIT