This is the README for a package I recently published on NPM. It's not really meant to be a blog post. I was mostly curious what it would look like with the styles I currently have set for my site. But if you're reading this and are curious about it, give it a try and let me know what you think. Ok... now back to the README.
A lightweight library for color validation, manipulation, conversion, theme and palette generation
Installation
Color Tools is available on Npm. You can install it using the package manager of your choice.
# NPMnpm install @mattbrannon/color-tools# Yarnyarn add @mattbrannon/color-tools# Pnpmpnpm add @mattbrannon/color-tools
Usage
This package uses named exports and supports both CommonJS and ESModules.
ES Modules
import { Color, Theme, convert } from '@mattbrannon/color-tools'
Common JS
const { Color, Theme, convert } = require('@mattbrannon/color-tools')
Color
The Color
class utility is versatile. It is able to convert between hexadecimal, rgb, and hsl color spaces. Alpha
channels are supported as well. It accepts any valid (and quite a few invalid) css color strings. In most cases
operators like deg
and %
are not necessary. For hsl
colors, deg
and %
will be assumed unless another operator
is supplied.
const deepSkyBlue = new Color('deepskyblue')const blue = new Color('rgb(0 0 255)')const magenta = new Color('hsl(300 100 50 1)') // look ma! no commasconst brown = new Color('654321') // look ma! no # sign
Objects are also acceptable input. Objects should be in the form of
// the alpha `a` channel is optionalnew Color({ r, g, b, a })new Color({ h, s, l, a })
The values for each color channel can be raw numbers or percentage strings
new Color({ r: '25%', g: 200, b: 100, a: '90%' })new Color({ h: 270, s: 100, l: '50%', a: 0.75 })
You can use an array for input as well with one caveat. When supplying an array of values as input, you must also supply a configuration object as a second argument. Otherwise, it's impossible to know which color space the values belong to.
Config
// using an array we need to specify the initial color space// we can still set the preferred data type to whatever we chooseconst green = new Color([0, 128, 0], {colorSpace: 'rgb',dataType: {},})green.value() // { r:0, g:128, b:0 }// here we don't need to set a color space to be able to parse the input// instead we're setting a color space to use as the default for this colorconst deepPink = new Color('deeppink', {colorSpace: 'hsl',dataType: [],})deepPink.value() // [328, 100, 54]
Color Spaces
The available color spaces currently include rgb
, hsl
, hex
Each color space has the following methods for specifying the output datatype
array()
object()
css()
The syntax for each color space method is
color.<color-space>.<method>
For example:
const color = new Color('deepskyblue')// HSL colorscolor.hsl.css() // hsl(195deg, 100%, 50%)color.hsl.object() // {h:195, s:100, l:50}color.hsl.array() // [195, 100, 50]// RGB colorscolor.rgb.css() // rgb(0, 191, 255)color.rgb.object() // {r:0, g:191, b:255}color.rgb.array() // [0, 191, 255]// HEX colorscolor.hex.css() // '#00bfff'color.hex.object() // {r:'00', g:'bf', b:'ff'};color.hex.array() // ['00', 'bf', 'ff']
Color Methods
The methods listed below are globally available per instance of Color
. There's no need to target a specific color
space.
value()
A convenience method that returns the current instance's color value based on the preferred color space and data typescontrast()
Returns the contrast ratio between the current color instance and another color. accepts the same input parameters asColor
minus the configluminance()
Returns a floating point number between 0 and 1 representing the color's luminance valuered()
set the redrgb
channelgreen()
set the greenrgb
channelblue()
set the bluergb
channelhue()
set thehsl
hue valuesaturation()
set thehsl
saturation valuelightness()
set thehsl
lightness value
Value
The simplest way to obtain the color output is with the value
method. The output of this method will vary depending on
the input and configuration provided.
For named colors, without a configuration, the default output will be in hexadecimal format. For all other inputs
without a config, the output of value
will directly match the input.
// named colors default to hex formatconst blue = new Color('blue')blue.value() // '#0000FF'const red = new Color('rgb(255 0 0)')red.value() // 'rgb(255, 0, 0)'const green = new Color({ h: 120, s: 100, l: 25 })green.value() // {h:120, s:100, l:25}
Contrast
Get the contrast ratio between the current color and another. Accepts the same input formats as Color
const orange = new Color('orange')const ratio = orange.contrast('red') // 2.02
Luminance
Get the relative luminance value of the current color Returns a value between 0 - 1 with 0 being total darkness and 1 being all white
new Color('#8faba2').luminance() // 0.3757409455965292
RGB Methods
Set the value for red, green or blue channels. Values will be converted to rgb
to make the adjustment and then
converted back to the current color space (if necessary).
const black = new Color('#000')const pureRed = black.red(255)const magenta = pureRed.blue(255)const purple = magenta.red(-127).blue(-127)const grey = magenta.red(-127).blue(-127).green(127)
HSL Methods
Set the value for hue, saturation or lightness. Values will be converted to hsl
to make the adjustment and then
converted back to the current color space (if necessary).
const black = new Color('#000')const maroon = black.saturation(100).lightness(25)const purple = maroon.hue(300)
Static Methods
Color.random()
- returns a random hexadecimal color valueColor.contrast()
- Takes 2 colors as input and returns the contrast ratio between them.
const randomColor = Color.random() // #0d7ae8const ratio = Color.contrast('orange', 'red') // 2.02
Theme
Theme
is an extension of Color
and has some useful methods for generating color palettes.
The output will be an array of strings based on the input color space.
The color palettes provided will be uniform in their levels of saturation
and lightness
. More than likely you'll
want to tweak these a bit when deciding which colors to use.
Theme Methods
Complementary
A complementary color palette is one which consists of two colors opposite each other on the color wheel.
new Theme('hsl(0 100 50)').complementary()/*['hsl(0deg, 100%, 50%)','hsl(180deg, 100%, 50%)']*/
Compound
Similar to complementary but generates 3 colors. The base color provided as input and then the 2 colors on either side of the base complementary color. This is also known as splitComplementary
new Theme('rgb(255 0 0)').compound()/*['rgb(0, 127, 255)','rgb(255, 0, 0)','rgb(0, 255, 128)']*/
Analagous
An analagous color palette is one in which three colors are positioned next to each other on the color wheel.
new Theme('hsl(240 100 50)').analagous()/*['hsl(210deg, 100%, 50%)','hsl(240deg, 100%, 50%)','hsl(270deg, 100%, 50%)']*/
Triadic
A triadic color palette is one which consists of three colors evenly spaced on the color wheel.
new Theme('rgb(255 0 0)').triadic()/*['rgb(0, 0, 255)','rgb(255, 0, 0)','rgb(0, 255, 0)']*/
Rectangle
A rectangle color palette is one which consists of four colors grouped in pairs of complementary colors. Drawing a line through to each color on the color wheel forms the shape of a rectangle. This is also known as a tetradic color palette
new Theme('rgb(255 0 0)').rectangle()/*['rgb(255, 0, 0)','rgb(0, 255, 0)','rgb(0, 255, 255)','rgb(255, 0, 255)']*/
Square
A square color palette is one in which all four colors are at an equal distance on the color wheel
new Theme('hsl(0 100 50)').square()/*['hsl(0deg, 100%, 50%)','hsl(90deg, 100%, 50%)','hsl(180deg, 100%, 50%)','hsl(270deg, 100%, 50%)']*/