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 🙏

© 2025 – Pkg Stats / Ryan Hefner

color-calculus

v0.2.1

Published

A zero-dependency library of color conversion functions

Downloads

43

Readme

color-calculus

A zero-dependency library of color conversion functions.

Supported Color Spaces

  • General Color Spaces

    • lab (CIELAB with D65 Illuminant)
    • hcl (lab in polar coordinates)
    • xyz (CIEXYZ)
  • Commonly Used RGB Color Spaces

    • sRGB
    • AdobeRGB
    • DisplayP3
    • AppleRGB
  • CIECAM02 Perceptual Color Spaces

    • JCh (CIECAM02 with D65 Illuminant)
    • Jab (CIECAM02 with D65 Illuminant)

Usage

Install

npm install color-calculus

Example

Recommended method: import the library with ES module syntax. With this method, you can take advantage of the tree shaking features of a modern packaging tool such as rollup or webpack.

// Import the desired conversion function
import { xyz_to_lab } from "color-calculus";

// Convert xyz (10, 20, 30) to lab
const [l, a, b] = xyz_to_lab(10, 20, 30);

// Input can be supplied as an array
const lab = xyz_to_lab([10, 20, 30]);

Import with CommonJS syntax: useful when targeting Node.js:

// Import the desired conversion function
const { xyz_to_lab } = require("color-calculus");

Load via a script tag: useful when writing a simple HTML file:

<script
  src="node_modules/color-calculus/dist/color-calculus.umd.min.js"
  type="text/javascript"
></script>

<script type="text/javascript">
  const xyz_to_lab = ColorCalculus.xyz_to_lab;
</script>

The color-calculus.umd.min.js has zero dependency, you can copy the file from the node_modules folder to a different location and specify the src attribute accordingly.

Conversion Functions

All color conversions are named in the following convention:

/**
 * Convert from "source" colorspace to "destination" colorspace
 *
 * The parameter can be either three components, or an array of three components.
 * No default value is provided.
 */
source_to_destination(x1: number, x2: number, x3: number): [number, number, number ];
source_to_destination(x: [number, number, number]): [number, number, number];

source and destination can be any of the supported colorspaces, here is a list:

  • General Color Spaces

    • lab (CIELAB with D65 Illuminant)
    • hcl (Polar coordinates of lab)
    • xyz (CIEXYZ)
  • Commonly Used RGB Color Spaces

    • sRGB
    • AdobeRGB
    • DisplayP3
    • AppleRGB
  • CIECAM02 Perceptual Color Spaces

    • JCh (CIECAM02 with D65 Illuminant)

For example, to convert from sRGB to DisplayP3, you can use sRGB_to_DisplayP3.

Advanced CIECAM02 Functions

The CIECAM02 model captures color consistency. You can use this to convert colors between different viewing conditions.

import { CIECAM02 } from "color-calculus";

// TODO: Example

Derivatives of Conversion Functions

color-calculus allows you to compute the derivative (Jacobian) of some of the conversion functions. The derivatives would be useful if you want to do gradient-based optimizations for colors.

A gradient function follows the convention below:

/**
 * Convert from "source" colorspace to "destination" colorspace
 *
 * The parameter can be either three components, or an array of three components.
 * No default value is provided.
 */
source_to_destination_derivative(x1: number, x2: number, x3: number): [
  [number, number, number],
  [number, number, number],
  [number, number, number]
]
source_to_destination_derivative(x: [number, number, number]): [
  [number, number, number],
  [number, number, number],
  [number, number, number]
]

Color conversion functions are multivariate functions. The derivative of a multivariate function is called a Jacobian. color-calculus's derivative functions return Jacobian matrices in the following format:

For a color conversion that takes [x1, x2, x3] and outputs [y1, y2, y3],
the Jacobian is returned in this format:

[
  [ dy1/dx1, dy1/dx2, dy1/dx3 ],
  [ dy2/dx1, dy2/dx2, dy2/dx3 ],
  [ dy3/dx1, dy3/dx2, dy3/dx3 ]
]

High-level API

TODO.

Development

First, install dependencies with yarn:

yarn

Build the project:

yarn build

Test the project:

yarn test

License

MIT.