polychrome v4.1.2
polychrome
A small
~2kB (gzipped)library for parsing and manipulating colors

Installation
feel free to replace
yarn addwithnpm install$> yarn add polychrome
Usage
// using ES6 modules
import polychrome from "polychrome";
// using CommonJS modules
const polychrome = require("polychrome");// Make a polychrome color from hex, rgb(a), and hsl(a) strings
const red = polychrome("#F00");
// Get a string representation of a polychrome color in other formats
red.rgb() // "rgb(255,0,0)"
// Manipulate polychrome colors
const darkerRed = red.darken(20); // (pass in an integer percentage)
darkerRed.hsl() // "hsl(0,100%,40%)"
// Chain polychrome methods together before outputting
polychrome("#F00").darken(20).fadeOut(60).rgb() // "rgba(204,0,0,0.4)"API Reference
Polychrome Object
polychrome(colorString)
colorStringcan be a hex (3 or 6 digit), rgb(a), or hsl(a) string. Returns apolychromeobject.
A polychrome object consists of the following properties:
rHex- 2 character hex string representation of the red color channelgHex- 2 character hex string representation of the green color channelbHex- 2 character hex string representation of the blue color channelr- value of the red color channel 0 - 255g- value of the green color channel 0 - 255b- value of the blue color channel 0 - 255h- hue of the color 0 - 360s- saturation of the color 0 - 100l- lightness of the color 0 - 100luma- luma value of the color 0 - 255
In addition to the above properties, the following methods are available to a polychrome for outputting CSS color strings:
.hex()- returns a 6-digit hexadecimal CSS compatible color stringpolychrome("rgb(0, 0, 0)").hex() // "#000000".rgb()- returns an rgb(a) CSS compatible color string// rgba will be used if an alpha value exists polychrome("#000").rgb() // "rgb(0,0,0)" polychrome("#000").fadeOut(60).rgb() // "rgba(0,0,0,.4)".hsl()- returns an hsl(a) CSS compatible color string// hsla will be used if an alpha value exists polychrome("#000").hsl() // "hsl(0,0%,0%)" polychrome("#000").fadeOut(60).hsl() // "hsla(0,0%,0%,.4)"
Alpha
.setAlpha(value)Returns a
polychromewith analphavalue absolutely set tovalue. No change occurs if value is omitted..fadeIn(percentage)Returns a
polychromefaded in bypercentage. Default50if no percentage is passed in..fadeOut(percentage)Returns a
polychromefaded out bypercentage. Default50if no percentage is passed in.
Contrast
.contrast(dark, light)Checks
lumavalue ofpolychromeand returnsdarkorlightpolychromedepending on the contrast level. If a contrast ratio of at least4.5:1is not met, thedark/lightcolors will be darkened / lightened until a valid ratio is met.polychrome("#000").contrast().rgb() // "rgb(255,255,255)" polychrome("#DDD").contrast("#333", "#EEE").hex() // "#333333"darkandlightcan be aStringorpolychrome. They default toblack (#000)andwhite (#FFF)if params are not passed in.
Hue
.setHue(value)Returns a
polychromewith ahuevalue absolutely set tovalue. No change occurs if value is omitted..spin(degrees)Returns a
polychromewith ahuevalue rotateddegrees. Can be a positive or negative degree value. When bounds of[0 - 360]are reached,huewill continue in a circular fashion until it has been spun the full amount..complimentary()Returns a
polychromewith ahuevalue rotated180 degrees. Shorthand for.spin(180).
Lightness
.setLightness(value)Returns a
polychromewith alightnessvalue absolutely set tovalue. No change occurs if value is omitted..lighten(percentage)Returns a
polychromelightened bypercentage. Default10if no percentage is passed in.
.darken(percentage)Returns a
polychromedarkened bypercentage. Default10if no percentage is passed in.
Mix
.mix(otherColor)Returns a
polychromemixed withotherColor.otherColorcan be anotherpolychromeor a color string that will be parsed..tint()Returns a
polychromemixed withwhite (#FFFFFF). Shorthand for.mix("#FFFFFF")..shade()Returns a
polychromemixed withblack (#000000). Shorthand for.mix("#000000").
Saturation
.setSaturation(value)Returns a
polychromewith asaturationvalue absolutely set tovalue. No change occurs if value is omitted..saturate(percentage)Returns a
polychromesaturated bypercentage. Default10if no percentage is passed in..desaturate(percentage)Returns a
polychromedesaturated bypercentage. Default10if no percentage is passed in..grayscale()Returns a
polychromewithsaturationset to0. Shorthand for.setSaturation(0).
License
MIT License 2017 © Chad Donohue