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

c0lor

v0.1.1

Published

Color space conversions

Downloads

302

Readme

c0lor . . .

A color space conversion library to use in the browser or node.js. It currently contains:

  • RGB, HSV color space
  • XYZ, xyY color space
  • Lab, LCh color space

Browser

To use the library in the browser, you need to include this JavaScript file:

<script src="c0lor.min.js"></script>

It exports the global c0lor object. You can create an alias variable if you like:

var C = c0lor;

The following browsers are tested:

Browser Test Status

node.js

You can install this package with:

npm install c0lor

You can load the global module:

var C = require('c0lor');

You can also require directly on submodules (can e.g. be useful with browserify):

var rgb = require('c0lor/rgb'); // same as: require('c0lor').rgb;

Examples

Color Creation

var cyan_rgb, red_RGB, yellow_RGB, magenta_hsv,
    green_XYZ, orange_xyY, purple_Lab, blue_LCh;

// create RGB colors:
cyan_rgb = C.rgb(0, 1, 1);
red_RGB = C.RGB(255, 0, 0);
yellow_RGB = C.RGB().hex('FFFF00');

// create HSV color:
magenta_hsv = C.hsv(0.8, 1, 1);

// create XYZ and xyY color:
green_XYZ = C.XYZ(0.4, 0.7, 0.2);
orange_xyY = C.xyY(0.5, 0.4, 0.3);

// create Lab and LCh color:
purple_Lab = C.Lab(31, 24, -22);
blue_LCh = C.LCh(30, 56, -1);

Simple Conversions

var cyan_RGB, red_hexStr, yellow_rgb, magenta_rgb, yellow_hsv,
    green_xyY, orange_XYZ, purple_LCh, blue_Lab;

// convert between RGB color spaces:
cyan_RGB = cyan_rgb.RGB();
red_hexStr = red_RGB.hex();
yellow_rgb = yellow_RGB.rgb();

// convert between hsv and rgb color space:
magenta_rgb = magenta_hsv.rgb();
yellow_hsv = yellow_rgb.hsv();

// convert between XYZ and xyY color space:
green_xyY = green_XYZ.xyY();
orange_XYZ = orange_xyY.XYZ();

// convert between Lab and LCh color space:
purple_LCh = purple_Lab.LCh();
blue_Lab = blue_LCh.Lab();

Instead of creating a new color, you can also pass an existing color which will be used to store the result:

// convert hsv to rgb color:
hsvColor.rgb(rgbColor);

rgb ↔ XYZ Conversion

To convert between rgb and XYZ color spaces you need to pick one of the predefined rgb color space definitions or create one by yourself. The following rgb color spaces are included:

Adobe-98, Adobe-RGB, CIE-RGB, ColorMatch, EBU-Monitor, ECI-RGB, HDTV, Kodak-DC, NTSC-53, PAL-SECAM, sRGB, WideGamut

Use predefined rgb color spaces to convert colors:

var sRGB, adobe98, cyan_XYZ;

// create alias to rgb color spaces:
sRGB = C.space.rgb.sRGB;
adobe98 = C.space.rgb['Adobe-98'];

// convert color from sRGB to XYZ color space:
cyan_XYZ = sRGB.XYZ(cyan_rgb);
// convert color from XYZ to Adobe-98 color space:
cyan_rgb = adobe98.rgb(cyan_XYZ);

Instead of creating a new color, you can also pass an existing color which will be used to store the result:

// convert color from XYZ to Adobe-98 color space:
adobe98.rgb(cyan_XYZ, cyan_rgb);

You can create your own rgb color space if you like:

var myRed, myGreen, myBlue, myRgbSpace;

myRed = C.xyY(0.6, 0.3);
myGreen = C.xyY(0.2, 0.7);
myBlue = C.xyY(0.1, 0.1);

myRgbSpace = C.space.rgb(myRed, myGreen, myBlue, C.white.D65, 2.1);

Instead of giving a gamma value you can also give a gamma function and its inverse function:

myRgbSpace = C.space.rgb(myRed, myGreen, myBlue, C.white.D65, function (x) {
    return Math.pow(x, 2.1);
}, function (x) {
    return Math.pow(x, 1 / 2.1);
});

The following white points (in XYZ color space) are included:

A, B, C, D50, D55, D65, D75, E, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12

Lab ↔ XYZ Conversion

Convert between Lab and XYZ color space:

var lab50, purple_XYZ, green_Lab;

// create Lab color space with D50 white point
labD50 = C.space.lab(C.white.D50);

// convert color from Lab to XYZ color space:
purple_XYZ = labD50.XYZ(purple_Lab);
// convert color from XYZ to Lab color space:
green_Lab = labD50.Lab(green_XYZ);

Instead of creating a new color, you can also pass an existing color which will be used to store the result:

// convert color from XYZ to Lab color space:
labD50.Lab(green_XYZ, green_Lab);