some color generation functions

Version
  • 1.0
Author

Methods

(static) HSBtoRGB(hue, sat, bri) → {Array}

Cover HSB color (0-1 range) to RGB color (0-255), this is based on the function in java.awt.Color

Parameters:
NameTypeDescription
huenumber

0-1

satnumber

0-1

brinumber

0-1

Returns:

int array representing the corresponding RGB color

Type: 
Array
Example
colorgen.HSBtoRGB(0.5,1,1);
// return [0, 255, 255];

(static) RGBtoHSB(red, green, blue) → {Array}

convert RGB (0-255) to HSB (0-1), base on the function in java.awt.Color

Parameters:
NameTypeDescription
rednumber

between 0 - 255

greennumber

between 0 - 255

bluenumber

between 0 - 255

Returns:

a float array representing the HSB color

Type: 
Array
Example
colorgen.RGBtoHSB(0, 255, 255);
//return [0.5, 1, 1]

(static) analogousColor(n, range, ref, sat, bri, offset1, offset2) → {Array}

Generate a bunch of color according to the analogous color scheme

Parameters:
NameTypeDescription
nnumber

number of colors

rangenumber

range of color

refnumber

optional, reference angle, default is random

satnumber

optional, float, 0 - 1, default to 1

brinumber

optional, float, 0 - 1, default to 1

offset1number

optional, float, offset amount 1, default 0, must between 0 - 1

offset2number

optional, float, offset amount 1, default 0, must between 0 - 1

See
  • standerHarmonyColor
Returns:

an array of colors in RGB format

Type: 
Array

(static) colorDistance(color1, color2) → {number}

caculate the weighted Euclidean distance of two RGB colors

Parameters:
NameTypeDescription
color1*

a color, @seeparseColor for more info

color2*

the other color, @seeparseColor for more info

Tutorials
Returns:

the distance

Type: 
number

(static) colorDistanceHSB(color1, color2) → {number}

caculate the unweighted Euclidean distance of two HSB colors in the HSB cone

Parameters:
NameTypeDescription
color1array

float array representing the first color, [H, S, B] range from 0 - 1;

color2array

float array representing the second color, [H, S, B] range from 0 - 1;

Returns:

float, the distance between two color

Type: 
number

(static) colorDistanceUnweightedRGB(color1, color2) → {number}

caculate the unweighted Euclidean distance of two RGB colors

Parameters:
NameTypeDescription
color1*

first color

color2*

second color

See
  • parseColor
Returns:

float, the distance between two color

Type: 
number

(static) complementaryColor(n, range1, range2, ref, sat, bri, offset2) → {Array}

Generate a bunch of color according to the complementary color scheme

Parameters:
NameTypeDescription
nnumber

number of colors

range1number
range2number
refnumber

optional, reference angle, default is random

satnumber

optional, float, 0 - 1, default to 1

brinumber

optional, float, 0 - 1, default to 1

offset2number

optional, float, offset amount 2, default 0, must between 0 - 1

See
  • standerHarmonyColor
Returns:

an array of colors in RGB format

Type: 
Array

(static) gradientRGB(n, from, to, type, sat, bri) → {Array}

generate a bunch of color in gradient, take different type

Parameters:
NameTypeDescription
nnumber

the number of color return

fromnumber

the starting point of the gradient (0 - 1)

tonumber

the end point of the gradient (0 - 1)

typestring

how the colors are picked. "UR": Uniform Random, randomly select values between 0 and 1, and map this to the gradient to select colours. "G": Grid, uniform sections, no two colours will be closer to each other(along the gradient) than 1/n. "JG": Jittered Grid, grid with random offset. "GR" Golden Ratio, using golden ratio.

satnumber

optional, satuation value between 0 - 1, defalut to 1

brinumber

optional, brightness value between 0 - 1, defalut to 1

Tutorials
Returns:

an array of rbg colors

Type: 
Array

(static) parseColor(color) → {Array}

parse color input and output it as an array

Parameters:
NameTypeDescription
color*

an array ([r,g,b]) or an object (o.r,o.g,o.b or o.R,o.G,o.B) or a (A)RGB color (only RGB is used in this library and only RGB is output from this function)

Returns:

an array representing the color [r,g,b];

Type: 
Array
Example
colorgen.parseColor({r:24, g:25, b:144});
//return [24,25,144]

(static) randomOffset(baseColor, offset) → {Array}

generate random offset color based on a chosen color

Parameters:
NameTypeDescription
baseColor*

a color, @seeparseColor for more info

offsetnumber

the max offset

Tutorials
Returns:

an RGB array (range from 0 - 255);

Type: 
Array

(static) randomRGB() → {Array}

get a random RGB color

Tutorials
Returns:

an array of int [R, G, B] in range of 0 - 255

Type: 
Array

(static) splitcomplementaryColor(n, range1, range2, range3, vari, ref, sat, bri) → {Array}

Generate a bunch of color according to the split complementary color scheme

Parameters:
NameTypeDescription
nnumber

number of colors

range1number
range2number

range2, must smaller than 2*var

range3number

range3, must smaller than 2*var

varinumber

variation from 180 for the offset values

refnumber

optional, reference angle, default is random

satnumber

optional, float, 0 - 1, default to 1

brinumber

optional, float, 0 - 1, default to 1

See
  • standerHarmonyColor
Returns:

an array of colors in RGB format

Type: 
Array

(static) standerHarmonyColor(n, range1, range2, range3, reference, sat, bri, offset1, offset2) → {Array}

Generate a bunch of color according to the stander harmony color scheme, see http://doi.acm.org/10.1145/1179352.1141933

Parameters:
NameTypeDescription
nnumber

number of the colors

range1number

float, range angle 1 in the hue ring

range2number

float, range angle 2 in the hue ring

range3number

float, range angle 3 in the hue ring

referencenumber

optional, float, as the reference hue for generation, default is random

satnumber

optional, float (0 - 1), sat value, default to 1

brinumber

optional, float (0 - 1), bri value, default to 1

offset1number

optional, float, offset amount 1, default 0, must between 0 - 1

offset2number

optional, float, offset amount 2, default 0, must between 0 - 1

See
  • analogousColor complementaryColor splitcomplementaryColor triadColor
Returns:

an array of color in RGB format

Type: 
Array

(static) to24BitRGB(color) → {color}

cast the color to 24 bit int RGB color

Parameters:
NameTypeDescription
colorany

the color to cast,

See
  • parseColor for input options
Returns:

a 24bit int color

Type: 
color
Example
colorgen.to24BitRGB([255, 255, 255]).toString(16);
//return "ffffff"

(static) to32BitARGB(color, alpha, rgba) → {color}

cast the color array to 32 bit int ARGB color

Parameters:
NameTypeDescription
color*

the color to cast,

alphanumber

alpha value of the color output, 0 - 255

rgbaboolean

optional, if true, output in RRGGBBAA format

See
  • parseColor for input options
  • to24BitRGB
Returns:

a 32bit int color

Type: 
color

(static) toCSSString(color, alpha, forceHex) → {string}

cast the color to css string

Parameters:
NameTypeDescription
color*

input color

alphanumber

optional, alpha value between 0-1

forceHexboolean

optional, if true and alpha is used, return color in #rrggbbaa, this notation may not be supported on old browsers

Tutorials
Returns:

the result css string

Type: 
string

(static) triadColor(n, range1, range2, range3, ref, sat, bri) → {Array}

Generate a bunch of color according to the traid color scheme

Parameters:
NameTypeDescription
nnumber

number of colors

range1number
range2number
range3number
refnumber

optional, reference angle, default is random

satnumber

optional, float, 0 - 1, default to 1

brinumber

optional, float, 0 - 1, default to 1

Tutorials
See
  • standerHarmonyColor
Returns:

an array of colors in RGB format

Type: 
Array

(static) triadMixing(color1, color2, color3, greyControl) → {Array}

mix 3 color to get a new one, use the greyControl to control the grey value (0:low -1:high)

Parameters:
NameTypeDescription
color1*

first color to mix, see parseColor

color2*

second color to mix, see parseColor

color3*

third color to mix, see parseColor

greyControlnumber

0-1, how much grey get mix in

Tutorials
See
  • parseColor
Returns:

a color array in RGB format

Type: 
Array

(static) yiq(color) → {boolean}

YIQ color contrast calculation, returning black or white determination of optimal foreground color

Parameters:
NameTypeDescription
color*

the foreground color,

Tutorials
See
  • parseColor
Returns:

true for black(dark color) and false for white(bright color)

Type: 
boolean