frostcolor v1.0.4
FrostColor
FrostColor is a free, open-source color manipulation library for JavaScript.
It is a lightweight (~4kb gzipped) and modern library, and features full support for RGB, HSL, HSV, CMY and CMYK color-spaces.
Table Of Contents
- Installation
- Basic Usage
- Color Creation
- Color Formatting
- Color Attributes
- Color Manipulation
- Color Schemes
- Color Palettes
- Static Methods
Installation
In Browser
<script type="text/javascript" src="/path/to/frost-color.min.js"></script>Using NPM
npm i frostcolorIn Node.js:
const { Color, ColorImmutable } = require('frostcolor');Basic Usage
From RGB
redis a number between 0 and 255.greenis a number between 0 and 255.blueis a number between 0 and 255.alphais a number between 0 and 1, and will default to 1.
const color = new Color(red, green, blue, alpha);From Brightness
brightnessis a number between 0 and 100.alphais a number between 0 and 1, and will default to 1.
const color = new Color(brightness, alpha);Immutable Colors
By default, Color objects are mutable, but if you wish to create an immutable reference you can use the following syntax.
Immutable Color objects return a new ColorImmutable whenever they are modified.
const color = new ColorImmutable(red, green, blue, alpha);Color Creation
From String
Create a new Color from a HTML color string.
colorStringis a string containing a color value in either hexadecimal, RGB, RGBA, HSL, HSLA or a standard HTML color name.
const color = Color.fromString(colorString);From CMY
Create a new Color from CMY values.
cyanis a number between 0 and 100.magentais a number between 0 and 100.yellowis a number between 0 and 100.alphais a number between 0 and 1, and will default to 1.
const color = Color.fromCMY(cyan, magenta, yellow, alpha);From CMYK
Create a new Color from CMYK values.
cyanis a number between 0 and 100.magentais a number between 0 and 100.yellowis a number between 0 and 100.keyis a number between 0 and 100.alphais a number between 0 and 1, and will default to 1.
const color = Color.fromCMYK(cyan, magenta, yellow, key, alpha);From HSL
Create a new Color from HSL values.
hueis a number between 0 and 360.saturationis a number between 0 and 100.lightnessis a number between 0 and 100.alphais a number between 0 and 1, and will default to 1.
const color = Color.fromHSL(hue, saturation, lightness, alpha);From HSV
Create a new Color from HSV values.
hueis a number between 0 and 360.saturationis a number between 0 and 100.valueis a number between 0 and 100.alphais a number between 0 and 1, and will default to 1.
const color = Color.fromHSV(hue, saturation, value, alpha);Clone
Create a new Color from an existing Color.
const clone = color.clone();Color Formatting
To String
Return a HTML string representation of the color.
The colorString returned will be a string containing either a HTML color name (if one exists), a hexadecimal string (if alpha is 1) or an RGBA string.
const colorString = color.toString();To Hex String
Return a hexadecimal string representation of the color.
const hexString = color.toHexString();To RGB String
Return a RGB/RGBA string representation of the color.
const rgbString = color.toRGBString();To HSL String
Return a HSL/HSLA string representation of the color.
const hslString = color.toHSLString();Label
Get the closest color name for the color.
const label = color.label();Color Attributes
Get Alpha
Get the alpha value of the color (between 0 and 1).
const alpha = color.getAlpha();Get Brightness
Get the brightness value of the color (between 0 and 100).
const brightness = color.getBrightness();Get Hue
Get the hue value of the color (between 0 and 360).
const hue = color.getHue();Get Saturation
Get the saturation value of the color (between 0 and 100).
const saturation = color.getSaturation();Luma
Get the relative luminance value of the color (between 0 and 1).
const luma = color.luma();Set Alpha
Set the alpha value of the color.
alphais a number between 0 and 1.
color.setAlpha(alpha);Set Brightness
Set the brightness value of the color.
brightnessis a number between 0 and 100.
color.setBrightness(brightness);Set Hue
Set the hue value of the color.
hueis a number between 0 and 360.
color.setHue(hue);Set Saturation
Set the saturation value of the color.
saturationis a number between 0 and 100.
color.setSaturation(saturation);Color Manipulation
Darken
Darken the color by a specified amount.
amountis a number between 0 and 1.
color.darken(amount);Invert
Invert the color.
color.invert();Lighten
Lighten the color by a specified amount.
amountis a number between 0 and 1.
color.lighten(amount);Shade
Shade the color by a specified amount.
amountis a number between 0 and 1.
color.shade(amount);Tint
Tint the color by a specified amount.
amountis a number between 0 and 1.
color.tint(amount);Tone
Tone the color by a specified amount.
amountis a number between 0 and 1.
color.tone(amount);Color Schemes
Complementary
Create a complementary color variation.
const complementary = color.complementary();Split
Create an array with 2 split color variations.
const [secondary, accent] = color.split();Analogous
Create an array with 2 analogous color variations.
const [secondary, accent] = color.analogous();Triadic
Create an array with 2 triadic color variations.
const [secondary, accent] = color.triadic();Tetradic
Create an array with 3 tetradic color variations.
const [secondary, alternate, accent] = color.tetradic();Color Palettes
Create a palette of colors from a Color object you have created using the following methods.
Shades
Create an array with a specified number of shade variations.
shadesis a number indicating how many shades you wish to generate, and will default to 10.
const colorShades = color.shades(shades);Tints
Create an array with a specified number of tint variations.
tintsis a number indicating how many tints you wish to generate, and will default to 10.
const colorTints = color.tints(tints);Tones
Create an array with a specified number of tone variations.
tonesis a number indicating how many tones you wish to generate, and will default to 10.
const colorTones = color.tones(tones);Palette
Create a palette object with a specified number of shades, tints and tone variations.
shadesis a number indicating how many shades you wish to generate, and will default to 10.tintsis a number indicating how many tints you wish to generate, and will default to 10.tonesis a number indicating how many tones you wish to generate, and will default to 10.
const colorPalette = color.palette(shades, tints, tones);Static Methods
Contrast
Calculate the contrast between two colors (between 1 and 21).
color1is a Color object.color2is a Color object.
const contrast = Color.contrast(color1, color2);Distance
Calculate the distance between two colors.
color1is a Color object.color2is a Color object.
const distance = Color.dist(color1, color2);Find Contrast
Find an optimally contrasting color for another color.
color1is a Color object.color2is a Color object, and will default to null.minContrastis a number between 1 and 21 indicating the minimum valid contrast, and will default to 4.5.stepSizeis a number between 0 and 1 indicating the amount to darken/lighten the color on each iteration, and will default to 0.01.
const contrastColor = Color.findContrast(color1, color2, minContrast, stepSize);If color2 value is null, a clone of color1 will be used instead.
This method will tint/shade color2 until it meets a minimum contrast threshold with color1, then the new color will be returned. If no valid contrast value can be found, this method will return null instead.
Mix
Create a new Color by mixing two colors together by a specified amount.
color1is a Color object.color2is a Color object.amountis a number between 0 and 1.
const mixed = Color.mix(color1, color2, amount);Multiply
Create a new Color by multiplying two colors together by a specified amount.
color1is a Color object.color2is a Color object.amountis a number between 0 and 1.
const multiplied = Color.multiply(color1, color2, amount);3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago