1.1.3 • Published 5 years ago

@znemz/color-math v1.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

color-math

ColorMath is an expression parser and evaluator dealing with color representations. Using special grammar it supports various color models, mixing, blending, channels manipulation, scaling, bezier interpolation and more. It also supports transpiling most of the expressions to less.js.

Install

yarn add color-math

or

npm i color-math -S

Usage Example

import * as ColorMath from "color-math"

// will return color which is result of mixing red and green colors
const result = ColorMath.evaluate("red | green")
// prints "#804000" ('result.result' is a chroma.js instance; see link below)
console.log(result.result.hex())

// set color's alpha channel to 30%
result = ColorMath.evaluate("red @a 30%")
console.log(result.result.css()) // prints "rgba(255, 0, 0, 0.3)"

// arithmetic operations with colors and numbers
result = ColorMath.evaluate("(#222 + #444) / 2")
console.log(result.resultStr) // prints "#333333"

// transpile to Less
result = ColorMath.evaluate("rgb 165 42 42 >> .2", { evaluator: new ColorMath.Evaluators.LessEvaluator() })
console.log(result.result) // prints "saturate(rgb(165, 42, 42), 20%, relative)"

chroma.js color library is used internally to manipulate colors and wrap results.

Expressions Cheatsheet

Click on expression to visualize result in ColorMath online parser.

Use parenthesis to control evaluation order and build complex expressions.

Ways to Define Color

ExpressionDescription
#ffcc00hexadecimal color representation
ffcc00hexadecimal color representation without hash symbol
#fc0short hexadecimal color representation
fc0short hexadecimal color representation without hash symbol
skybluecolor literals from W3C/X11 specification
randgenerate random color
num 33023color from number
temp 3500color by temperature in Kelvin
wl 560color from wavelength value

Color Models

ExpressionDescription
rgb 127 255 212RGB color model
rgba 135 206 235 75%RGB color model with alpha channel
argb .7 255 99 71RGB color model with alpha channel (first)
cmyk .43 .12 0 .8CMYK color model
cmyka 0 .61 .72 0 60%CMYK color model with alpha channel
cmy .5 0 .17CMY color model
cmya 0 .61 .72 .65CMY color model with alpha channel
hsl 159.8 100% 75%HSL color model
hsla 197 .71 .73 55%HSL color model with alpha channel
hsv 160 .5 1HSV (also known as HSB) color model
hsb 197 .43 .92HSB color model (alias for HSV)
hsva 9 .72 1 50%HSV color model with alpha channel
hsi 161 .36 .78HSI color model
hsia 196 .30 .75 45%HSI color model with alpha channel
lab 92 (-46) 9.7LAB color model
laba 79 (-14.8) (-21) 40%LAB color model with alpha channel
lch 92 46.5 168LCH color model
lcha 79 26 235 35%LCH color model with alpha channel
hcl 168 46.5 92HCL color model (reversed LCH)
hcla 235 26 79 35%HCL color model with alpha channel

Color Operations

ExpressionDescription
#444 * 2arithmetic operations with numbers
skyblue - 0xfarithmetic operations with numbers
~redinvert color
red \| greenmix colors
red \| {25%} greenmix colors in variable proportion
red \| {25% hsl} greenmix colors in variable proportion and specific color model
red \| {hsl} greenmix colors in specific color model
hotpink << 50%desaturate color
rgb 165 42 42 >> .2saturate color
temp 4000 <<< 30%darken color
#fc0 >>> 70%lighten color
pink %% hotpinkcompute WCAG contrast ratio between two colors

Color Blending

ExpressionDescription
#222 + #444add
#ccc - #111subtract
#ff6600 * #cccmultiply
#222 / #444divide
skyblue <<< tomatodarken
skyblue >>> tomatolighten
#ff6600 !* #00ff00screen
#ff6600 ** #999overlay
olive <* pinkhard light
olive *> pinksoft light
ffcc00 ^* cccdifference
ffcc00 ^^ cccexclusion
ffcc00 !^ cccnegate
indigo << redburn
indigo >> reddodge

Color Channels Manipulation

ExpressionDescription
brown @redget channel's value using its name or synonym
#ffcc00 @gget channel's value using its name or synonym
t 5000 @cmyk.yget channel's value using its name or synonym
aquamarine @a = .3set channel's absolute value
rgb 5 7 9 @hsl.h 90set channel's absolute value
#000 @lightness 50%set channel's absolute value
red @a /= 2set relative channel's value
ffcc00 @rgb.r -= 50set relative channel's value
tomato @s *= 30%set relative channel's value
olive @nget color's numeric value
fff @n 0set color's absolute numeric value
#0080ff @n /= 2set color's relative numeric value
#ffe3cd @tget color's temperature in Kelvin
red @t 3500set color's absolute temperature
ffe3cd @t += 500set color's relative temperature

Color Scales

ExpressionDescription
scale (red 0f0 blue)scale list of colors (make gradient)
scale (yellow 008ae5) -> 20grab n equi-distant colors from a color scale
scale (t 2000 t 6000)scale list of colors
bezier (ff0 red #000)perform bezier interpolate of list of colors
bezier (red 0f0)perform bezier interpolate of list of colors
scale (red:.2 0f0:50%)set position of each color (inline)
scale (red 0f0) @domain (.2 .5)set position of each color (as a parameter)
scale (red 0f0) @pad .25cut off a fraction of the gradient on both sides
bezier (red 0f0) @pad (.1 .3)cut off a fraction of the gradient on both sides
cubehelixDave Green's cubehelix scaled color scheme
cubehelix @pad (0 .2)cut off a fraction of the gradient on both sides
cubehelix @start 200start for hue rotation
cubehelix @rot (-.5)number of rotations
cubehelix @hue .5controls how saturated colors of all hues are
cubehelix @hue (1 0)controls how saturated colors of all hues are
cubehelix @gamma .7emphasize low or high intensity values
cubehelix @lt (.3 .8)adjust lightness (black to white)
cubehelix @start 200 @rot .5parameters can be chained
+scale (black red yellow)auto-correct lightness of a scale
+bezier (yellow 0f0 blue) -> 10auto-correct lightness and grab n equi-distant colors

Numbers

ExpressionDescription
0b01101001binary
0o151octal
105decinal
0x69hexadecimal
55%percent value
5 + 10add numbers
-360 * 0.5 + (100 - 40)arithmetic operations
0xf / 0b1010division
4 ^ (2 / 4)take expression to a specified power

Lists

ExpressionDescription
red 0f0 bluedefine list of three colors
(pink >> .5) golddefine list of two colors

Brewer Constants

OrRd, PuBu, BuPu, Oranges, BuGn, YlOrBr, YlGn, Reds, RdPu, Greens, YlGnBu, Purples, GnBu, Greys, YlOrRd, PuRd, Blues, PuBuGn, Spectral, RdYlGn, RdBu, PiYG, PRGn, RdYlBu, BrBG, RdGy, PuOr, Set2, Accent, Set1, Set3, Dark2, Paired, Pastel2, Pastel1

Variables and Statements

ExpressionDescription
$col = rgb 255 204 0assign color value to a variable
$num = 2^8 - 1assign number value to a variable
$lst = #444 #888assign list value to a variable
$my = yellow black; bezier $myseparate statements with semicolon

A read–eval–print loop (REPL)

npm start

Interpreter will start. Now you can type your expressions.

Demo and documentation

Please go to http://colormath.net/ to test your expressions, visualize results and read about all available features with examples.