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 🙏

© 2024 – Pkg Stats / Ryan Hefner

lab-color

v1.0.1

Published

Color utility library to convert lab color space with high accuracy.

Downloads

7

Readme

Lab-color

This npm module provides a robust method for converting colors from Lab (CIELAB) space to RGB space with maximum accuracy.

Commonly the lab color space is converted from other modules in a fast but inaccurate manner. This module provides a robust method for converting colors from Lab (CIELAB) space to RGB space with maximum accuracy. With this module you can decide the white point of the color space you want to convert to, or the RGB color space you want to convert from.

Lab color space is a color-opponent space with dimensions for lightness and the color-opponent dimensions a and b. RGB, on the other hand, is an additive color model where red, green, and blue light are added together in various ways to reproduce a broad array of colors.

All the formulas are taken from the studies of Bruce Justin Lindbloom. You can find more information about the CIE color space here: CIE Color Space

Installation

You can install this module via npm:

npm install lab-color --save-dev

Usage

import { rgbToLab } from 'lab-color';

// Convert a Lab color to RGB
const labColor = { l: 50, a: 20, b: -10 };
const options = {
	refWhite: WHITE.D50,
	rgbModel: MODEL.sRGB,
	gamma: GAMMA.sRGB,
	adaptationMethod: ADAPTATION.Bradford
};
const rgbColor = labToRgb(labColor, options);
console.log(rgbColor); // { r: 120, g: 180, b: 200 }

API

rgbToLab

Converts an RGB color to a Lab color representation.

Arguments:

  • rgb (RGB): An object representing the RGB color with properties R, G, and B, each ranging from 0 to 255.
  • options (OPTIONS, optional): An optional object containing additional conversion options.

Returns:

  • LAB: An object representing the Lab color with properties L, a, and b.

rgbToXyz

Converts an RGB color to an XYZ color representation.

Arguments:

  • rgb (RGB): An object representing the RGB color with properties R, G, and B, each ranging from 0 to 255.
  • options (OPTIONS, optional): An optional object containing additional conversion options.

Returns:

  • XYZ: An object representing the XYZ color with properties X, Y, and Z.

labToRgb

Converts a Lab color to an RGB color representation.

Arguments:

  • lab (LAB): An object representing the Lab color with properties L, a, and b.
  • options (OPTIONS, optional): An optional object containing additional conversion options.

Returns:

  • RGB: An object representing the RGB color with properties R, G, and B, each ranging from 0 to 255.

labToXyz

Converts a Lab color to an XYZ color representation.

Arguments:

  • lab (LAB): An object representing the Lab color with properties L, a, and b.
  • options (OPTIONS, optional): An optional object containing additional conversion options.

Returns:

  • XYZ: An object representing the XYZ color with properties X, Y, and Z.

Options

  • refWhite: The reference white used to convert the color, and is used in the white interpretation of the CIE color system. When converting to color temperature, there are many different colors that map onto the same color temperature. Robertson's method has been implemented to perform this conversion, and if the color temperature field is blank after doing a calculation, that means the color was out of the range that Robertson's method supports. When converting from color temperature, the standard CIE method used is only valid for color temperatures in the range [4000K, 25000K]

A, B, C, D50, D55, D65, D75, E, F2, F7, F11

  • MODEL: The color model used to change the colorimetric interpretation of the RGB color values. Included in this list are all the RGB working spaces that ship with Adobe Photoshop, as well as several others that have emerged as a result of research efforts from various individuals (details here).

AdobeRGB, AppleRGB, BestRGB, BetaRGB, BruceRGB, CI, ColorMatch, DonRGB, ECI_RGB_v2, EktaSpacePS5, NTSC , PAL_SECAM , ProPhoto , SMPTE_C , sRGB , WideGamut

  • ADAPTATION: The color adaptation method used to convert the color. The differences among the three methods lie in the definition of the cone response domains

Bradford, von_Kries, XYZ_Scaling, None

  • GAMMA: The gamma correction used to convert the color allowing the native RGB companding function to be overridden. This is useful for computing linear RGB values. G1_0, G1_8, G2_2 stands for gamma 1.0, 1.8, 2.2 respectively while sRGB and L stands for sRGB and linear respectively

G1_0, G1_8, G2_2, sRGB, L

Examples

LAB to RGB conversion:


import { labToRgb } from 'lab-color';

const labColor = { l: 50, a: 20, b: -10 };
const rgbColor = labToRgb(labColor, {
	refWhite: WHITE.D50,
	rgbModel: MODEL.sRGB,
	gamma: GAMMA.sRGB,
	adaptationMethod: ADAPTATION.Bradford
});
console.log(rgbColor); // { r: 120, g: 180, b: 200 }

RGB to LAB conversion:


import { rgbToLab } from 'lab-color';

const rgbColor = { r: 120, g: 180, b: 200 };
const labColor = rgbToLab(rgbColor, {
	refWhite: WHITE.D50,
	rgbModel: MODEL.sRGB,
	gamma: GAMMA.sRGB,
	adaptationMethod: ADAPTATION.Bradford
});
console.log(labColor); // { l: 50, a: 20, b: -10 }

References and inspirations