Chromatism 🌈
A simple set of utility functions for colours.
Awesome resources for colour stuff.
Table of Contents
- Chromatism
- Table of Contents
- Contributing
- Installation
- Functions
- Colour Transformations
- Convert colour types
- Generate a complementary colour
- Generate an array of triad colours
- Generate an array of tetrad colours
- Find the mid point between two colours
- Invert a colour
- Invert a grey colour
- Blend two colours (Multiply)
- Generate an array of adjacent hue-shifted colours (rainbow effect)
- Generate an array of the fade between two colours
- Generate a new shade of a colour
- Generate a new saturation of a colour
- Change colour's brightness
- Shift the hue of a colour
- Shift the contrast of a colour
- Greyscale version of the colour
- Sepia version of the colour
- Determine accessible colour for foreground text
- Chromatic Adaptation (White point)
- Colour Metering Functions
- Colour Transformations
- Constants
- Scales + Colour Spaces
- Colour Modes
Contributing
First off, thanks so much for helping out! Colour modes + functions contributing info will be added soon.
Note: The type definitions file (/index.d.ts) must be updated as part of your pull request. (If you're not familiar with typescript, I can update it for ya.) It contains definitions for colour modes AND colour functions. (Special thanks to @bdoss on GitHub for adding the inital TS definitions!)
Installation
$ npm install --save chromatism
CommonJS
var chromatism = require("chromatism");
Browser without bundling
<script type="text/javascript" src="path-to-files/dist/chromatism.min.js"></script>
Functions
All functions can take any colour type. You can even mix colour types if a function takes more than one. A list of colour types are available here. Return values are also not bound by the function. Any functions that return colours, return a colour object, which contains getters for each colour type. If you take the return value of one of these functions and get returnValue.hex
for example, you will get the hexadecimal equivalent value.
You can also chain these functions. The last parameter will be piped in from the output of the parent function.
💱 Colour Transformations
The following functions return a Colour Object, which contains getters to get a return value of any colour mode. See [Colour Mode](#Colour Modes) table for a list of all the available colour modes.
Convert colour types
var newColour = chromatism.convert( colour ).hex;
Value can be any colour in any of the supported colour modes. (See chart at bottom of README) This is an identity operation, as it just returns an object containing all of the available colour modes of the result.
All colour modes supported can be converted to any other. This does however mean that some conversions will have intermediate values, which can cause small inconsistencies, especially when changing colour spaces. The path for each conversion is optimised as much as possible to avoid loss of colour information.
Generate a complementary colour
var newColour = chromatism.complementary( colour ).rgb;
Generate an array of triad colours
var newColour = chromatism.triad( colour ).hsl;
Generate an array of tetrad colours
var newColour = chromatism.tetrad( colour ).cmyk;
Find the mid point between two colours
var newColour = chromatism.mid( colourOne, colourTwo ).cssrgb;
Invert a colour
var newColour = chromatism.invert( colour ).hex;
Invert a grey colour
var newColour = chromatism.invertLightness( colour ).hsl;
Blend two colours (Multiply)
var newColour = chromatism.multiply( colourOne, colourTwo ).hsv;
Generate an array of adjacent hue-shifted colours (rainbow effect)
var newColour = chromatism.adjacent( degrees, sections, colour ).cmyk;
Shift should be in degrees. It can be either positive and negative.
Generate an array of the fade between two colours
var newColour = chromatism.fade( amount, colourFrom, colourTo ).hsl;
Generate a new shade of a colour
var newColour = chromatism.shade( percent, colour ).csshsl;
Percent should be a number between -100 and 100.
Generate a new saturation of a colour
var newColour = chromatism.saturation( percent, colour ).hex;
Percent should be a number between -100 and 100.
Change colour's brightness
var newColour = chromatism.brightness( percent, colour ).hsl;
This essentially acts as a sum of saturation and shade, and thus does not adjust luminosity. Brightness works for 99% of most scenarios though.
Percent should be a number between -100 and 100.
Shift the hue of a colour
var newColour = chromatism.hue( degrees, colour ).hex;
Hue shift is measured in degrees, using the HSL colour model.
Shift the contrast of a colour
var newColour = chromatism.contrast( contrastCoeff, colour ).hsl;
Contrast coefficient is supplied in decimal form! You'll normally use a value between 0 and 4.
Imagine increasing the contrast (shift > 1) as making lighter colours lighter, and darker colours darker. Decreasing (shift < 1) makes all colours more similar.
Greyscale version of the colour
var newColour = chromatism.greyscale( colour ).cmyk;
Sepia version of the colour
var newColour = chromatism.sepia( colour ).hsv;
Determine accessible colour for foreground text
var newColour = chromatism.contrastRatio( colour ).rgb;
Use this function to determine the colour of text needed create a high contrast between text and a solid background of the supplied colour. Made according to the W3C Standard on Web Accessibility
Chromatic Adaptation (White point)
var newColour = chromatism.adapt( colour, illuminantColour, [source illuminant] ).XYZ;
Shifts the Illuminant (white-point) on the supplied colour. Supply a value from the ILLUMINANTS constant to use a standard white-point. (Most colours in Chromatism are assumed to be illuminated by D65, so you can leave off the source illuminant
property normally, it defaults to CIE 2° D65
in XYZ format.)
🔢 Colour Metering Functions
These functions do not return a colour, but instead return some aspect or measure of the colour(s).
Colour Difference
var diff = chromatism.difference( colourOne, colourTwo, [luminance weight], [chroma weight] );
Returns a measure of how different the two supplied colours are. Luminance and Chroma weight are equal to l and c in the CMC l:c Delta-E equation. By default they are both set to 1. (Thus testing imperceptibility)
Constants
Chromatism has some useful constants built in, you can access them using the imported chromatism object.
Reference | Values | Description |
---|---|---|
chromatism.ILLUMINANTS |
.A , .B , .C , .D50 , .D55 , .D65 , .D75 , .E , .F2 , .F7 , .F11 |
Standard CIE illuminants in XYZ format |
Scales + Colour Spaces
Mode | Scale | Colour Space |
---|---|---|
.hex |
#000000 - #FFFFFF | sRGB |
.rgb |
(r, g, b) 0 - 255 | sRGB |
.cssrgb |
(r, g, b) 0 - 255 | sRGB |
.hsl |
(h) 0 - 365, (s, l) 0 - 100 | sRGB |
.csshsl |
(h) 0 - 365, (s, l) 0 - 100 | sRGB |
.hsv |
(h) 0 - 365, (s, v) 0 - 100 | sRGB |
.cmyk |
(c, m, y, k) 0 - 1 | CMYK |
.yiq |
(y, i, q) 0 - 1 | YUV |
.XYZ |
(Y) 0 - 100, (X, Z) derived | XYZ |
.xyY |
(Y) 0 - 100, (x, y) 0 - 1 | XYZ |
.lms |
(⍴, γ, β) 0 - 1 | XYZ |
.cielab (L*a*b*) |
(L) 0 - 100, (a, b) -128 - 128 | CIE |
Colour Modes
Mode | Example Syntax |
---|---|
.hex |
"#FFC837" |
.rgb |
{ r:255, g: 200, b: 55 } |
.cssrgb |
"rgb(255,200,55)" |
.hsl |
{ h: 44, s: 100, l: 61 } |
.csshsl |
"hsl(44,100,61)" |
.hsv |
{h: 44, s: 78, v: 100} |
.cmyk |
{c: 0.5, m: 1, y: 0.2, k: 0.45} |
.yiq |
{ y: 0.132, i: 0.0222, q: 0.195 } |
.XYZ |
{ X: 41.24, Y: 21.26, Z: 1.93 } |
.xyY |
{ x: 0.64, y: 0.33, Y: 21.26 } |
.lms |
{ rho: 42.266, gamma: 5.561, beta: 2.135 } |
.cielab (L*a*b*) |
{ L: 53.23, a: 80.11, b: 67.22 } |
All functions return an object containing all modes of the result. (In getters, so don't worry, Chromatism doesn't calculate all the versions of the result when you use a function!)
For example, if you need a string containing the hex code for the colour result, simply use .hex
:
var newColour = chromatism.invert("#5300FF").hex