Installation

  npm install @thebespokepixel/es-tinycolor

Usage

import {tinycolor} from '@thebespokepixel/es-tinycolor'

const color = tinycolor("red")

color.toRgbString() // "rgba(255, 0, 0, 1)"

Call tinycolor(input) or new tinycolor(input), and you will have an object with the following properties. See Accepted String Input and Accepted Object Input below for more information about what is accepted.

Accepted String Input

The string parsing is very permissive. It is meant to make typing a color as input as easy as possible. All commas, percentages, parenthesis are optional, and most input allow either 0-1, 0%-100%, or 0-n (where n is either 100, 255, or 360 depending on the value).

HSL and HSV both require either 0%-100% or 0-1 for the S/L/V properties. The H (hue) can have values between 0%-100% or 0-360.

RGB input requires either 0-255 or 0%-100%.

If you call tinycolor.fromRatio, RGB and Hue input can also accept 0-1.

Here are some examples of string input:

Hex, 8-digit (RGBA) Hex

tinycolor("#000");
tinycolor("000");
tinycolor("#369C");
tinycolor("369C");
tinycolor("#f0f0f6");
tinycolor("f0f0f6");
tinycolor("#f0f0f688");
tinycolor("f0f0f688");

RGB, RGBA

tinycolor("rgb (255, 0, 0)");
tinycolor("rgb 255 0 0");
tinycolor("rgba (255, 0, 0, .5)");
tinycolor({ r: 255, g: 0, b: 0 });
tinycolor.fromRatio({ r: 1, g: 0, b: 0 });
tinycolor.fromRatio({ r: .5, g: .5, b: .5 });

HSL, HSLA

tinycolor("hsl(0, 100%, 50%)");
tinycolor("hsla(0, 100%, 50%, .5)");
tinycolor("hsl(0, 100%, 50%)");
tinycolor("hsl 0 1.0 0.5");
tinycolor({ h: 0, s: 1, l: .5 });
tinycolor.fromRatio({ h: 1, s: 0, l: 0 });
tinycolor.fromRatio({ h: .5, s: .5, l: .5 });

HSV, HSVA

tinycolor("hsv(0, 100%, 100%)");
tinycolor("hsva(0, 100%, 100%, .5)");
tinycolor("hsv (0 100% 100%)");
tinycolor("hsv 0 1 1");
tinycolor({ h: 0, s: 100, v: 100 });
tinycolor.fromRatio({ h: 1, s: 0, v: 0 });
tinycolor.fromRatio({ h: .5, s: .5, v: .5 });

Named

tinycolor("RED");
tinycolor("blanchedalmond");
tinycolor("darkblue");

Accepted Object Input

If you are calling this from code, you may want to use object input. Here are some examples of the different types of accepted object inputs:

{ r: 255, g: 0, b: 0 }
{ r: 255, g: 0, b: 0, a: .5 }
{ h: 0, s: 100, l: 50 }
{ h: 0, s: 100, v: 100 }

Methods

getFormat

Returns the format used to create the tinycolor instance

var color = tinycolor("red");
color.getFormat(); // "name"
color = tinycolor({r:255, g:255, b:255});
color.getFormat(); // "rgb"

getOriginalInput

Returns the input passed into the constructer used to create the tinycolor instance

var color = tinycolor("red");
color.getOriginalInput(); // "red"
color = tinycolor({r:255, g:255, b:255});
color.getOriginalInput(); // "{r: 255, g: 255, b: 255}"

isValid

Return a boolean indicating whether the color was successfully parsed. Note: if the color is not valid then it will act like black when being used with other methods.

var color1 = tinycolor("red");
color1.isValid(); // true
color1.toHexString(); // "#ff0000"

var color2 = tinycolor("not a color");
color2.isValid(); // false
color2.toString(); // "#000000"

getBrightness

Returns the perceived brightness of a color, from 0-255, as defined by Web Content Accessibility Guidelines (Version 1.0).

var color1 = tinycolor("#fff");
color1.getBrightness(); // 255

var color2 = tinycolor("#000");
color2.getBrightness(); // 0

isLight

Return a boolean indicating whether the color's perceived brightness is light.

var color1 = tinycolor("#fff");
color1.isLight(); // true

var color2 = tinycolor("#000");
color2.isLight(); // false

isDark

Return a boolean indicating whether the color's perceived brightness is dark.

var color1 = tinycolor("#fff");
color1.isDark(); // false

var color2 = tinycolor("#000");
color2.isDark(); // true

getLuminance

Returns the perceived luminance of a color, from 0-1 as defined by Web Content Accessibility Guidelines (Version 2.0).

var color1 = tinycolor("#fff");
color1.getLuminance(); // 1

var color2 = tinycolor("#000");
color2.getLuminance(); // 0

getAlpha

Returns the alpha value of a color, from 0-1.

var color1 = tinycolor("rgba(255, 0, 0, .5)");
color1.getAlpha(); // 0.5

var color2 = tinycolor("rgb(255, 0, 0)");
color2.getAlpha(); // 1

var color3 = tinycolor("transparent");
color3.getAlpha(); // 0

setAlpha

Sets the alpha value on a current color. Accepted range is in between 0-1.

var color = tinycolor("red");
color.getAlpha(); // 1
color.setAlpha(.5);
color.getAlpha(); // .5
color.toRgbString(); // "rgba(255, 0, 0, .5)"

String Representations

The following methods will return a property for the alpha value, which can be ignored: toHsv, toHsl, toRgb

toHsv

var color = tinycolor("red");
color.toHsv(); // { h: 0, s: 1, v: 1, a: 1 }

toHsvString

var color = tinycolor("red");
color.toHsvString(); // "hsv(0, 100%, 100%)"
color.setAlpha(0.5);
color.toHsvString(); // "hsva(0, 100%, 100%, 0.5)"

toHsl

var color = tinycolor("red");
color.toHsl(); // { h: 0, s: 1, l: 0.5, a: 1 }

toHslString

var color = tinycolor("red");
color.toHslString(); // "hsl(0, 100%, 50%)"
color.setAlpha(0.5);
color.toHslString(); // "hsla(0, 100%, 50%, 0.5)"

toHex

var color = tinycolor("red");
color.toHex(); // "ff0000"

toHexString

var color = tinycolor("red");
color.toHexString(); // "#ff0000"

toHex8

var color = tinycolor("red");
color.toHex8(); // "ff0000ff"

toHex8String

var color = tinycolor("red");
color.toHex8String(); // "#ff0000ff"

toRgb

var color = tinycolor("red");
color.toRgb(); // { r: 255, g: 0, b: 0, a: 1 }

toRgbString

var color = tinycolor("red");
color.toRgbString(); // "rgb(255, 0, 0)"
color.setAlpha(0.5);
color.toRgbString(); // "rgba(255, 0, 0, 0.5)"

toPercentageRgb

var color = tinycolor("red");
color.toPercentageRgb() // { r: "100%", g: "0%", b: "0%", a: 1 }

toPercentageRgbString

var color = tinycolor("red");
color.toPercentageRgbString(); // "rgb(100%, 0%, 0%)"
color.setAlpha(0.5);
color.toPercentageRgbString(); // "rgba(100%, 0%, 0%, 0.5)"

toName

var color = tinycolor("red");
color.toName(); // "red"

toFilter

var color = tinycolor("red");
color.toFilter(); // "progid:DXImageTransform.Microsoft.gradient(startColorstr=#ffff0000,endColorstr=#ffff0000)"

toString

Print to a string, depending on the input format. You can also override this by passing one of "rgb", "prgb", "hex6", "hex3", "hex8", "name", "hsl", "hsv" into the function.

var color1 = tinycolor("red");
color1.toString(); // "red"
color1.toString("hsv"); // "hsv(0, 100%, 100%)"

var color2 = tinycolor("rgb(255, 0, 0)");
color2.toString(); // "rgb(255, 0, 0)"
color2.setAlpha(.5);
color2.toString(); // "rgba(255, 0, 0, 0.5)"

Color Modification

These methods manipulate the current color, and return it for chaining. For instance:

tinycolor("red").lighten().desaturate().toHexString() // "#f53d3d"

lighten

lighten: function(amount = 10) -> TinyColor. Lighten the color a given amount, from 0 to 100. Providing 100 will always return white.

tinycolor("#f00").lighten().toString(); // "#ff3333"
tinycolor("#f00").lighten(100).toString(); // "#ffffff"

brighten

brighten: function(amount = 10) -> TinyColor. Brighten the color a given amount, from 0 to 100.

tinycolor("#f00").brighten().toString(); // "#ff1919"

darken

darken: function(amount = 10) -> TinyColor. Darken the color a given amount, from 0 to 100. Providing 100 will always return black.

tinycolor("#f00").darken().toString(); // "#cc0000"
tinycolor("#f00").darken(100).toString(); // "#000000"

desaturate

desaturate: function(amount = 10) -> TinyColor. Desaturate the color a given amount, from 0 to 100. Providing 100 will is the same as calling greyscale.

tinycolor("#f00").desaturate().toString(); // "#f20d0d"
tinycolor("#f00").desaturate(100).toString(); // "#808080"

saturate

saturate: function(amount = 10) -> TinyColor. Saturate the color a given amount, from 0 to 100.

tinycolor("hsl(0, 10%, 50%)").saturate().toString(); // "hsl(0, 20%, 50%)"

invert

invert: function() -> TinyColor. Inverts the color, in RGB colorspace (as opposed to complement(), which rotates the hue).

tinycolor("#f00").invert().toString(); // "#00ffff"

greyscale

greyscale: function() -> TinyColor. Completely desaturates a color into greyscale. Same as calling desaturate(100).

tinycolor("#f00").greyscale().toString(); // "#808080"

spin

spin: function(amount = 0) -> TinyColor. Spin the hue a given amount, from -360 to 360. Calling with 0, 360, or -360 will do nothing (since it sets the hue back to what it was before).

tinycolor("#f00").spin(180).toString(); // "#00ffff"
tinycolor("#f00").spin(-90).toString(); // "#7f00ff"
tinycolor("#f00").spin(90).toString(); // "#80ff00"

// spin(0) and spin(360) do nothing
tinycolor("#f00").spin(0).toString(); // "#ff0000"
tinycolor("#f00").spin(360).toString(); // "#ff0000"

Color Combinations

Combination functions return an array of TinyColor objects unless otherwise noted.

analogous

analogous: function(, results = 6, slices = 30) -> array<TinyColor>.

var colors = tinycolor("#f00").analogous();

colors.map(function(t) { return t.toHexString(); }); // [ "#ff0000", "#ff0066", "#ff0033", "#ff0000", "#ff3300", "#ff6600" ]

monochromatic

monochromatic: function(, results = 6) -> array<TinyColor>.

var colors = tinycolor("#f00").monochromatic();

colors.map(function(t) { return t.toHexString(); }); // [ "#ff0000", "#2a0000", "#550000", "#800000", "#aa0000", "#d40000" ]

splitcomplement

splitcomplement: function() -> array<TinyColor>.

var colors = tinycolor("#f00").splitcomplement();

colors.map(function(t) { return t.toHexString(); }); // [ "#ff0000", "#ccff00", "#0066ff" ]

triad

triad: function() -> array<TinyColor>.

var colors = tinycolor("#f00").triad();

colors.map(function(t) { return t.toHexString(); }); // [ "#ff0000", "#00ff00", "#0000ff" ]

tetrad

tetrad: function() -> array<TinyColor>.

var colors = tinycolor("#f00").tetrad();

colors.map(function(t) { return t.toHexString(); }); // [ "#ff0000", "#80ff00", "#00ffff", "#7f00ff" ]

complement

complement: function() -> TinyColor.

tinycolor("#f00").complement().toHexString(); // "#00ffff"

Color Utilities

tinycolor.equals(color1, color2)
tinycolor.mix(color1, color2, amount = 50)

random

Returns a random color.

var color = tinycolor.random();
color.toRgb(); // "{r: 145, g: 40, b: 198, a: 1}"

Readability

TinyColor assesses readability based on the Web Content Accessibility Guidelines (Version 2.0).

readability

readability: function(TinyColor, TinyColor) -> Object. Returns the contrast ratio between two colors.

tinycolor.readability("#000", "#000"); // 1
tinycolor.readability("#000", "#111"); // 1.1121078324840545
tinycolor.readability("#000", "#fff"); // 21

Use the values in your own calculations, or use one of the convenience functions below.

isReadable

isReadable: function(TinyColor, TinyColor, Object) -> Boolean. Ensure that foreground and background color combinations meet WCAG guidelines. Object is optional, defaulting to {level: "AA",size: "small"}. level can be "AA" or "AAA" and size can be "small" or "large".

Here are links to read more about the AA and AAA requirements.

tinycolor.isReadable("#000", "#111", {}); // false
tinycolor.isReadable("#ff0088", "#5c1a72",{level:"AA",size:"small"}); //false
tinycolor.isReadable("#ff0088", "#5c1a72",{level:"AA",size:"large"}), //true

mostReadable

mostReadable: function(TinyColor, [TinyColor, Tinycolor ...], Object) -> Boolean. Given a base color and a list of possible foreground or background colors for that base, returns the most readable color. If none of the colors in the list is readable, mostReadable will return the better of black or white if includeFallbackColors:true.

tinycolor.mostReadable("#000", ["#f00", "#0f0", "#00f"]).toHexString(); // "#00ff00"
tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"],{includeFallbackColors:false}).toHexString(); // "#112255"
tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"],{includeFallbackColors:true}).toHexString();  // "#ffffff"
tinycolor.mostReadable("#ff0088", ["#2e0c3a"],{includeFallbackColors:true,level:"AAA",size:"large"}).toHexString()   // "#2e0c3a",
tinycolor.mostReadable("#ff0088", ["#2e0c3a"],{includeFallbackColors:true,level:"AAA",size:"small"}).toHexString()   // "#000000",

See index.html in the project for a demo.

Common operations

clone

clone: function() -> TinyColor. Instantiate a new TinyColor object with the same color. Any changes to the new one won't affect the old one.

var color1 = tinycolor("#F00");
var color2 = color1.clone();
color2.setAlpha(.5);

color1.toString(); // "#ff0000"
color2.toString(); // "rgba(255, 0, 0, 0.5)"

API

tinycolor.equals boolean

src/index.js

Are two TinyColor colours equivalent?

tinycolor.equals(color1: TinyColor, color2: TinyColor) → boolean
Parameters
color1 (TinyColor) The first color
color2 (TinyColor) The second color
Returns
boolean: Equivalent or not?

tinycolor.registerFormat → TinyColorExtension

src/index.js

Register a TinyColor extension

tinycolor.registerFormat(id: string, options: object) → TinyColorExtension
Parameters
id (string) The plugin identifier
options (object = {}) Plugin options
Name Description
options.alphaFormat string rgb|hex
options.shortHex boolean Short hex codes #ABC, if possible
options.upperCaseHex boolean User UPPER case hex
Returns
TinyColorExtension: The TinyColor extension

tinycolor.fromRatio → TinyColor

src/index.js

Create a new TinyColor from values from 0..1

tinycolor.fromRatio(color: object, options: object) → TinyColor
Parameters
color (object) The color values
options (object) Options to pass to TinyColor constructor
Returns
TinyColor: A TinyColor instance

tinycolor.mix → TinyColor

src/index.js

Mix a second colour into the first

tinycolor.mix(color1: TinyColor, color2: TinyColor, amount: number) → TinyColor
Parameters
color1 (TinyColor) The first color
color2 (TinyColor) The second color
amount (number) The mix amount of the second color
Returns
TinyColor: A new, mixed TinyColor instance

tinycolor.readability number

src/index.js

How readable is the first color over the second color

tinycolor.readability(color1: TinyColor, color2: TinyColor) → number
Parameters
color1 (TinyColor) The first color
color2 (TinyColor) The second color
Returns
number: The color contrast defined by (WCAG Version 2)

tinycolor.isReadable → (boolean | number)

src/index.js

Ensure that foreground and background color combinations meet WCAG2 guidelines.

tinycolor.isReadable(color1: TinyColor, color2: TinyColor, wcag2: object) → (boolean | number)
Parameters
color1 (TinyColor) The first color
color2 (TinyColor) The second color
wcag2 (object) The WCAG2 properties to test
Name Description
wcag2.level object The level to test "AA" or "AAA" (default "AA")
wcag2.size object The content size to test "large" or "small" (default "small")
Returns
(boolean | number): True if readable, False otherwise.
Example
tinycolor.isReadable("#000", "#111") → false
tinycolor.isReadable("#000", "#111", {level:"AA",size:"large"}) → false

tinycolor.mostReadable → TinyColor

src/index.js

Given a base color and a list of possible foreground or background colors for that base, returns the most readable color.

Optionally returns Black or White if the most readable color is unreadable.

tinycolor.mostReadable(baseColor: TinyColor, colorList: [TinyColor], args: object) → TinyColor
Parameters
baseColor (TinyColor) The base color
colorList ([TinyColor]) An array of TinyColors
args (object = {}) The arguments
Name Description
args.includeFallbackColors boolean Include fallback colors?
args.level object The level to test "AA" or "AAA" (default "AA")
args.size object The content size to test "large" or "small" (default "small")
Returns
TinyColor: A TinyColor instance of the msot readable color.
Example
tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"],{includeFallbackColors:false}).toHexString(); // "#112255"
tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"],{includeFallbackColors:true}).toHexString();  // "#ffffff"
tinycolor.mostReadable("#a8015a", ["#faf3f3"], {includeFallbackColors:true,level:"AAA",size:"large"}).toHexString(); // "#faf3f3"
tinycolor.mostReadable("#a8015a", ["#faf3f3"], {includeFallbackColors:true,level:"AAA",size:"small"}).toHexString(); // "#ffffff"

Named Colours (as per CSS color names)

names

constructor → TinyColor

src/lib/classes/tinycolor.js

Create a new TinyColor instance

constructor(color: (string | array | object), options: object) → TinyColor
Parameters
color ((string | array | object)) Notation describing a color
options (object = {}) Options object (see below)
Returns
TinyColor: An instance representing the color

Determines if dark.

isDark() → boolean
Returns
boolean: True if dark, False otherwise.

Determines if light.

isLight() → boolean
Returns
boolean: True if light, False otherwise.

Determines if valid.

isValid() → boolean
Returns
boolean: True if valid, False otherwise.

Gets the original input.

getOriginalInput() → (string | object)
Returns
(string | object): The original input.

Gets the format.

getFormat() → string
Returns
string: The format.

Gets the alpha.

getAlpha() → number
Returns
number: The alpha.

Gets the brightness.

getBrightness() → number
Returns
number: The brightness.

Gets the luminance.

getLuminance() → number
Returns
number: The luminance.

Return the current color as a string.

toString(format: string) → string
Parameters
format (string) The color format
Returns
string: The current color, as a string.

Returns a name representation of the object.

toName() → string
Returns
string: The name of the colour.

Returns a rgb representation of the object.

toRgb() → object
Returns
object: Rgb representation of the object.

Returns a rgb string representation of the object.

toRgbString() → string
Returns
string: Rgb string representation of the object.

Returns a rgb array representation of the object.

toRgbArray() → [number]
Returns
[number]: Rgb array representation of the object.

Returns a percentage rgb representation of the object.

toPercentageRgb() → object
Returns
object: Percentage rgb representation of the object.

toPercentageRgbString string

src/lib/classes/tinycolor.js

Returns a percentage rgb string representation of the object.

toPercentageRgbString() → string
Returns
string: Percentage rgb string representation of the object.

Return the hex string of a color, as pure hexadecimal.

toHex(allow3Char: boolean) → string
Parameters
allow3Char (boolean) Allow 3 digit RGB strings
Returns
string: The Hex string of the color.

Return the hex string of a color, with a leading #

toHexString(allow3Char: boolean) → string
Parameters
allow3Char (boolean) Allow 3 digit RGB strings
Returns
string: The Hex string of the color.

Return the hex string of a color with aplha, as pure hexadecimal.

toHex8(allow4Char: boolean) → string
Parameters
allow4Char (boolean) Allow 4 digit RGBA strings
Returns
string: The Hex string of the color.

Return the hex string of a color with aplha, with a leading #

toHex8String(allow4Char: any, allow3Char: boolean) → string
Parameters
allow4Char (any)
allow3Char (boolean) Allow 4 digit RGBA strings
Returns
string: The Hex string of the color.

Returns a HSV object representation of the object.

toHsv() → object
Returns
object: HSV(A) representation of the color.

Returns a HSV string representation of the object.

toHsvString() → string
Returns
string: hsv(h, s, v[, a]) representation of the color.

Returns a HSL object representation of the object.

toHsl() → object
Returns
object: HSL(A) representation of the color.

Returns a HSL string representation of the object.

toHslString() → string
Returns
string: hsl(h, s, l[, a]) representation of the color.

setAlpha → TinyColor

src/lib/classes/tinycolor.js

Sets the alpha.

setAlpha(value: number) → TinyColor
Parameters
value (number) The alpha value (0 - 1.0)
Returns
TinyColor: The current colour with the set alpha.

clone → TinyColor

src/lib/classes/tinycolor.js

Creates a new instance of the object with same properties than original.

clone() → TinyColor
Returns
TinyColor: Copy of this object.

Create a new ID

newId() → number
Returns
number: Incremented ID counter

registerFormat → TinyColorExtension

src/lib/classes/tinycolor.js

Register a TinyColor extension

registerFormat(id: string, options: object) → TinyColorExtension
Parameters
id (string) The plugin identifier
options (object = {}) Plugin options
Name Description
options.alphaFormat string rgb|hex
options.shortHex boolean Short hex codes #ABC, if possible
options.upperCaseHex boolean User UPPER case hex
Returns
TinyColorExtension: The TinyColor extension

Are two TinyColor colours equivalent?

equals(color1: TinyColor, color2: TinyColor) → boolean
Parameters
color1 (TinyColor) The first color
color2 (TinyColor) The second color
Returns
boolean: Equivalent or not?

fromRatio → TinyColor

src/lib/classes/tinycolor.js

Create a new TinyColor from values from 0..1

fromRatio(color: object, options: object) → TinyColor
Parameters
color (object) The color
options (object) The options
Returns
TinyColor: The tiny color.

Analyze the 2 colors and returns the color contrast defined by (WCAG Version 2)

readability(color1: TinyColor, color2: TinyColor) → number
Parameters
color1 (TinyColor) The first color
color2 (TinyColor) The second color
Returns
number: The color contrast defined by (WCAG Version 2)

Ensure that foreground and background color combinations meet WCAG2 guidelines.

isReadable(color1: TinyColor, color2: TinyColor, wcag2: object) → (boolean | number)
Parameters
color1 (TinyColor) The first color
color2 (TinyColor) The second color
wcag2 (object) The WCAG2 properties to test
Name Description
wcag2.level object The level to test "AA" or "AAA" (default "AA")
wcag2.size object The content size to test "large" or "small" (default "small")
Returns
(boolean | number): True if readable, False otherwise.
Example
Tinycolor.isReadable("#000", "#111") → false
Tinycolor.isReadable("#000", "#111", {level:"AA",size:"large"}) → false

mostReadable → TinyColor

src/lib/classes/tinycolor.js

Given a base color and a list of possible foreground or background colors for that base, returns the most readable color.

Optionally returns Black or White if the most readable color is unreadable.

mostReadable(baseColor: TinyColor, colorList: [TinyColor], args: object) → TinyColor
Parameters
baseColor (TinyColor) The base color
colorList ([TinyColor]) An array of TinyColors
args (object = {}) The arguments
Name Description
args.includeFallbackColors boolean Include fallback colors?
args.level object The level to test "AA" or "AAA" (default "AA")
args.size object The content size to test "large" or "small" (default "small")
Returns
TinyColor: A TinyColor instance of the msot readable color.
Example
Tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"], {includeFallbackColors:false}).toHexString(); // "#112255"
Tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"], {includeFallbackColors:true}).toHexString();  // "#ffffff"
Tinycolor.mostReadable("#a8015a", ["#faf3f3"], {includeFallbackColors:true, level:"AAA", size:"large"}).toHexString(); // "#faf3f3"
Tinycolor.mostReadable("#a8015a", ["#faf3f3"], {includeFallbackColors:true, level:"AAA", size:"small"}).toHexString(); // "#ffffff"

Mix a second colour into the first

mix(color1: TinyColor, color2: TinyColor, amount: number) → TinyColor
Parameters
color1 (TinyColor) The first color
color2 (TinyColor) The second color
amount (number) The mix amount of the second color
Returns
TinyColor: A new, mixed TinyColor instance

converters.convertDecimalToHex string

src/lib/converters.js

Converts a decimal to a hex value

converters.convertDecimalToHex
Parameters
d (number) Decimal input value
Returns
string: Hexadecimal string

converters.convertHexToInt number

src/lib/converters.js

Converts a base-16 hex value into a base-10 integer

converters.convertHexToInt
Parameters
value (any)
val (string) Hexadecimal input value
Returns
number: Integer value

converters.convertHexToDecimal number

src/lib/converters.js

Converts a hex value to a decimal

converters.convertHexToDecimal
Parameters
h (string) Hexadecimal input value
Returns
number: Decimal value

converters.convertToPercentage string

src/lib/converters.js

Replace a decimal with it's percentage value

converters.convertToPercentage
Parameters
n (number) Decimal input value
Returns
string: Percentage string

converters.rawToRgba object

src/lib/converters.js

Handle conversion of internal precise values to exportable values. Should be able to accept a tinycolour instance 'this' value.

converters.rawToRgba
Parameters
raw (object) { _r, _g, _b, _a } with _r, _g, _b in [0.0, 255.0] and _a in [0, 1]
Returns
object: { r, g, b } in [0, 255]

converters.rawToDeepRgba object

src/lib/converters.js

Handle conversion of internal precise values to exportable values, maintaining deep precision. Should be able to accept a tinycolour instance 'this' value.

converters.rawToDeepRgba
Parameters
raw (object) { _r, _g, _b, _a } with _r, _g, _b in [0.0, 255.0] and _a in [0, 1]
Returns
object: { r, g, b, a } in [0.0, 255.0]

converters.conformRgba object

src/lib/converters.js

Handle bounds / percentage checking to conform to CSS color spec

converters.conformRgba
Parameters
rgba (object) { r, g, b, a } in [0, 255] or [0, 1]
Returns
object: { r, g, b } in [0, 255]

converters.rgbaToHex string

src/lib/converters.js

Convert RGBA to hexadecimal

Converts an RGBA color plus alpha transparency to hex Assumes r, g, b are contained in the set [0, 255] and a in [0, 1]. Returns a 4 or 8 character rgba hex

converters.rgbaToHex
Parameters
rgba (object) The rgba object.
allowShort (boolean) Allow short hex output
Returns
string: The hex output.

converters.rgbToHex string

src/lib/converters.js

Convert RGB to hexadecimal

Converts an RGBA color plus alpha transparency to hex Assumes r, g, b are contained in the set [0, 255]. Returns a 3 or 6 character rgba hex

converters.rgbToHex
Parameters
rgba (any)
allowShort (boolean) Allow short hex output
rgb (object) The rgb object.
Returns
string: The hex output.

calculations.calcBrightness number

src/lib/calculations.js

Calculates the brightness. http://www.w3.org/TR/AERT#color-contrast

calculations.calcBrightness
Parameters
rgb (object) The rgb
Returns
number: The brightness.

calculations.calcLuminance number

src/lib/calculations.js

Calculates the luminance. http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef

calculations.calcLuminance(rgb: TinyColor) → number
Parameters
rgb (TinyColor) The rgb color
Returns
number: The luminance.

calculations.calcMix → TinyColor

src/lib/calculations.js

Calculates the mix of two colors.

calculations.calcMix(color1: TinyColor, color2: TinyColor, amount: number) → TinyColor
Parameters
color1 (TinyColor) The first color
color2 (TinyColor) The second color
amount (number) The amount to mix
Returns
TinyColor: The mixed color.

readability.validateWCAG2Parms object

src/lib/readability.js

Return valid WCAG2 parameters for isReadable.

readability.validateWCAG2Parms(parms: object) → object
Parameters
parms (object) The parameters
Name Description
parms.level object The level to test "AA" or "AAA" (default "AA")
parms.size object The content size to test "large" or "small" (default "small")
Returns
object: sanitized parameters

Analyze the 2 colors and returns the color contrast defined by (WCAG Version 2)

readability(color1: TinyColor, color2: TinyColor) → number
Parameters
color1 (TinyColor) The first color
color2 (TinyColor) The second color
Returns
number: The color contrast defined by (WCAG Version 2)

Ensure that foreground and background color combinations meet WCAG2 guidelines.

isReadable(color1: TinyColor, color2: TinyColor, wcag2: object) → (boolean | number)
Parameters
color1 (TinyColor) The first color
color2 (TinyColor) The second color
wcag2 (object) The WCAG2 properties to test
Name Description
wcag2.level object The level to test "AA" or "AAA" (default "AA")
wcag2.size object The content size to test "large" or "small" (default "small")
Returns
(boolean | number): True if readable, False otherwise.
Example
Tinycolor.isReadable("#000", "#111") → false
Tinycolor.isReadable("#000", "#111", {level:"AA",size:"large"}) → false

mostReadable → TinyColor

src/lib/readability.js

Given a base color and a list of possible foreground or background colors for that base, returns the most readable color.

Optionally returns Black or White if the most readable color is unreadable.

mostReadable(baseColor: TinyColor, colorList: [TinyColor], args: object) → TinyColor
Parameters
baseColor (TinyColor) The base color
colorList ([TinyColor]) An array of TinyColors
args (object = {}) The arguments
Name Description
args.includeFallbackColors boolean Include fallback colors?
args.level object The level to test "AA" or "AAA" (default "AA")
args.size object The content size to test "large" or "small" (default "small")
Returns
TinyColor: A TinyColor instance of the msot readable color.
Example
Tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"], {includeFallbackColors:false}).toHexString(); // "#112255"
Tinycolor.mostReadable(tinycolor.mostReadable("#123", ["#124", "#125"], {includeFallbackColors:true}).toHexString();  // "#ffffff"
Tinycolor.mostReadable("#a8015a", ["#faf3f3"], {includeFallbackColors:true, level:"AAA", size:"large"}).toHexString(); // "#faf3f3"
Tinycolor.mostReadable("#a8015a", ["#faf3f3"], {includeFallbackColors:true, level:"AAA", size:"small"}).toHexString(); // "#ffffff"

complement → TinyColor

src/lib/combiners.js

Find the complementary color.

complement(color: TinyColor) → TinyColor
Parameters
color (TinyColor) The color
Returns
TinyColor: The new complementary Tinycolor.

triad → [TinyColor]

src/lib/combiners.js

Find the color triad colors.

triad(color: TinyColor) → [TinyColor]
Parameters
color (TinyColor) The color
Returns
[TinyColor]: An array of 3 triad TinyColors.

tetrad → [TinyColor]

src/lib/combiners.js

Find the color tetrad colors.

tetrad(color: TinyColor) → [TinyColor]
Parameters
color (TinyColor) The color
Returns
[TinyColor]: An array of 4 tetrad TinyColors.

splitcomplement → [TinyColor]

src/lib/combiners.js

Find the split complementary colors.

splitcomplement(color: TinyColor) → [TinyColor]
Parameters
color (TinyColor) The color
Returns
[TinyColor]: An array of 3 split complementary TinyColors.

analogous → [TinyColor]

src/lib/combiners.js

Find the analogous colors.

analogous(color: TinyColor, results: any, slices: any) → [TinyColor]
Parameters
color (TinyColor) The color
results (any = 6)
slices (any = 30)
Returns
[TinyColor]: The new analogous Tinycolors.

monochromatic → TinyColor

src/lib/combiners.js

Find the monochromatic color.

monochromatic(color: TinyColor, results: any) → TinyColor
Parameters
color (TinyColor) The color
results (any = 6)
Returns
TinyColor: The new monochromatic Tinycolor.

modify → TinyColor

src/lib/modifiers.js

Apply a modification conditionally

modify(action: Function, args: arguments) → TinyColor
Parameters
action (Function) The modification function to apply
args (arguments) Arguments passed to specified function
Returns
TinyColor: The modified color

invert → TinyColor

src/lib/modifiers.js

Invert Color

invert(color: TinyColor) → TinyColor
Parameters
color (TinyColor) The color to invert
Returns
TinyColor: The inverted color

desaturate → TinyColor

src/lib/modifiers.js

Desaturate Color

desaturate(color: TinyColor, amount: Number) → TinyColor
Parameters
color (TinyColor) The color to modify
amount (Number) The amount to desaturate <= 100
Returns
TinyColor: The modified color

saturate → TinyColor

src/lib/modifiers.js

Saturate color

saturate(color: TinyColor, amount: Number) → TinyColor
Parameters
color (TinyColor) The color to modify
amount (Number) The amount to saturate <= 100
Returns
TinyColor: The modified color

greyscale → TinyColor

src/lib/modifiers.js

Remove all chroma, leaving luminence

greyscale(color: TinyColor) → TinyColor
Parameters
color (TinyColor) The color to modify
Returns
TinyColor: The modified color

lighten → TinyColor

src/lib/modifiers.js

Lighten a color

lighten(color: TinyColor, amount: Number) → TinyColor
Parameters
color (TinyColor) The color to modify
amount (Number) The amount to ligten by <= 100
Returns
TinyColor: The modified color

brighten → TinyColor

src/lib/modifiers.js

Brighten a color

brighten(color: TinyColor, amount: Number) → TinyColor
Parameters
color (TinyColor) The color to modify
amount (Number) The amount to brighten by <= 100
Returns
TinyColor: The modified color

darken → TinyColor

src/lib/modifiers.js

Darken a color

darken(color: TinyColor, amount: Number) → TinyColor
Parameters
color (TinyColor) The color to modify
amount (Number) The amount to brighten by <= 100
Returns
TinyColor: The modified color

spin → TinyColor

src/lib/modifiers.js

Spin takes a positive or negative amount within [-360, 360] indicating the change of hue. Values outside of this range will be wrapped into this range.

spin(color: TinyColor, amount: Number) → TinyColor
Parameters
color (TinyColor) The color to modify
amount (Number) Degrees to rotate hue by
Returns
TinyColor: The modified color

rgbStringToObject object

src/lib/parser.js

Permissive string parsing. Take in a number of formats, and output an object based on detected format.

Try to match string input using regular expressions. Keep most of the number bounding out of this function - don't worry about [0,1] or [0,100] or [0,360] Just return an object and let the conversion functions handle that. This way the result will be the same whether the tinycolor is initialized with string or object.

rgbStringToObject(color: string) → object
Parameters
color (string) The color
Returns
object: Returns { r, g, b } or { r, g, b, a }