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

color-js

v1.0.5

Published

A color management API for JavaScript

Downloads

88,202

Readme

Readme

Color.js provides an API to do simple color management in javascript. Color objects provide methods to do a number of common, useful operations independent of the underlying color model they need.

The library supports RGB, HSV and HSL color models, along with alpha channel on all of them. CSS string representations of colors are supported for convenience. If you need colorspace management that correlates to human perception rather than display technology, colorspace.js looks like an excellent library.

Color objects are always immutable; all operations on them return new objects.

This API is designed for correctness and clarity first, but operates at the expense of some performance. If you need raw performance look for a library that forces you to do the cloning and conversion yourself, rather than implicitly as this one does.

Note: In 2017 it's quite possible that you don't need this library, particularly for client side tasks; many use-cases can simply be achieved using CSS RGB, HSV, and HSL color syntaxes.

Using the library

Once you have included the library in your page, you can access the public namespace net.brehaut (reverse DNS scheme). The only export from color.js is the Color factory function. You may wish to import this into your local scope, eg:

var Color = net.brehaut.Color;

You can use the library in a CommonJS environment, too:

var Color = require('./color').Color;

Color will create a new object for you from an object, string, or array. Note that this is a factory function, not a constructor. eg:

var Green = Color("#00FF00");
var Red = Color({hue: 0, saturation: 1, value: 1});
var Blue = Color("rgb(0,0,255)");
var Cyan = Color({hue: 180, saturation: 1, lightness: 0.5});
var Magenta = Color("hsl(300, 100%, 50%)");
var Yellow = Color([255,255,0]);

Each method on a color either returns a new color (or set of colors) or returns a value. You can chain manipulation methods together as much as you need. Eg

var C1 = Red.shiftHue(45).darkenByRatio(0.5).desaturateByAmount(0.1);

A common use case for this code is to set page elements colors to calculated values, the method toCSS provides a string for you to use. eg:

document.getElementById('myElement').style.backgroundColor = C1.toCSS();

Methods

There are three sorts of operations available on color objects; accessor methods (eg getHue/setHue), methods that manipulate the color and return a new object (eh shiftHue) or methods that return arrays of colors (eg, splitComplementaryScheme), and methods that do conversion and construction (eg toCSS).

All values are a float from 0 to 1, with the exception of hue which is a degree (from 0 to 360).

Notes on terminology

  • A ratio multiplies the existing property by the ratio.
  • An amount is just added directly to the existing value.

If you have a saturation of 0.7 and you apply a ratio of 0.5 the new color has a saturation of 0.35. If you then add an amount of 0.15 to that color’s saturation the final color will have a saturation of 0.5.

Accessor Methods

These methods are automatically generated by the API and are used to access the properties of the object. It is advisable to use these methods than try to access the property directly as the API makes no guarantees about the specific model members available.

  • getRed() : Number
  • setRed( newRed ) : Color
  • getGreen(): Number
  • setGreen( newGreen ) : Color
  • getBlue() : Number
  • setBlue( newBlue ): Color
  • getHue() : Number (degrees)
  • setHue( newHue ) : color
  • getSaturation() : Number
  • setSaturation( newSaturation ) : Color
  • getValue() : Number
  • setValue( newValue ) : Color
  • getLightness() : Number
  • setLightness( newValue ) : Color
  • getLuminance() : Number --- returns a number between 0 and 1 that represents how bright this color appears on a conventional monitor.
  • getAlpha() : Number
  • setAlpha( newAlpha ) : Number

Color Methods

The following methods all return a new color.

  • shiftHue( degrees ) : Color
  • darkenByAmount( amount ) : Color
  • darkenByRatio( ratio ) : Color
  • lightenByAmount( amount ) : Color
  • lightenByRatio( amount ) : Color
  • devalueByAmount( amount ) : Color
  • devalueByRatio( ratio ) : Color
  • valueByAmount( amount ) : Color
  • valueByRatio( amount ) : Color
  • desaturateByAmount( amount ) : Color
  • desaturateByRatio( ratio ) : Color
  • saturateByAmount( amount ) : Color
  • saturateByRatio( ratio ) : Color
  • blend( color , alpha ) : Color --- returns a new color that is self blended with alpha of color. eg black.blend ( white , 0 ) is black, black.blend ( white , 0.5 ) is grey and black.blend ( white , 1 ) is white.

The following methods return a list of colors

  • schemeFromDegrees( listOfdegrees ) : List of Colors
  • complementaryScheme( ) : List of Colors
  • splitComplementaryScheme( ) : List of Colors
  • splitComplementaryCWScheme( ) : List of Colors
  • splitComplementaryCCWScheme( ) : List of Colors
  • triadicScheme( ) : List of Colors
  • clashScheme( ) : List of Colors
  • tetradicScheme( ) : List of Colors
  • fourToneCWScheme( ) : List of Colors
  • fourToneCCWScheme( ) : List of Colors
  • fiveToneAScheme( ) : List of Colors
  • fiveToneBScheme( ) : List of Colors
  • fiveToneCScheme( ) : List of Colors
  • fiveToneDScheme( ) : List of Colors
  • fiveToneEScheme( ) : List of Colors
  • sixToneCWScheme( ) : List of Colors
  • sixToneCCWScheme( ) : List of Colors
  • neutralScheme( ) : List of Colors
  • analogousScheme( ) : List of Colors

Conversion and Construction

  • fromObject( o ) : Color --- o is an object with values from a color model, a css string, or an RGB array.
  • toCSS([bytesPerChannel]) : String --- css hexdecimal representation. bytesPerChannel defaults to 2
  • toString() : String --- returns CSS representation
  • toHSV() : Color
  • toRGB() : Color
  • toHSL() : Color

Type definitions

Users of TypeScript can find type definitions in color.d.ts.

Library design notes

This library has been designed for correctness and with ease of use in mind. With regard to ease of use, Color objects allow you to chain operations by returning a new object each time. These operations involve common modification to the values in the object by a value or by a ratio where suitable, and automatically convert to the appropriate color model as required.

The models chosen have been picked with regard to what makes most sense to a web developer, and ease of implementation, hence RGB, HSV and HSL.

Internally theres a bit of metaprogramming soup to make it easier to define color models correctly, and ensure that colors remain values.

Demo

Changes