A (mini) colour manipulation library
npm install --save kandinsky-js
and import as needed.
- Written in TypeScript, usable in JavaScript
- Immutable, composable functions
- Deals with hex, rgb and hsl colours
- Programmatic generation for linear and non-linear gradients
type RGB = [number, number, number];
type HSL = [number, number, number];
// When either RGB or HSL colours can be used
type XYZ = RGB | HSL;
// `t` is expected to be in range [0, 1], and the function should return a value
// in the range [0, 1]
type EaseFn = (t: number) => number;
returns a hsl array
rgb2hsl: ([r, g, b]: RGB) => HSL;
returns an rgb array
hsl2rgb: ([h, s, l]: HSL) => RGB;
returns an rgb array
hex2rgb: (hex: string) => RGB;
returns a hex string
rgb2hex: (rgb: RGB) => string;
returns a hsl array
hex2hsl: (hex: string) => HSL;
returns a hex string
hsl2hex: (hsl: HSL) => string;
returns a darkened rgb array.
amount
is a value in the range[0, 1]
darkenRgb: (amount: number, rgb: RGB) => RGB;
returns a lightened rgb array.
amount
is a value in the range[0, 1]
lightenRgb: (amount: number, rgb: RGB) => RGB;
returns a darkened hsl array.
amount
is a value in the range[0, 1]
darkenHsl: (amount: number, [h, s, l]: HSL) => HSL;
returns a lightened hsl array.
amount
is a value in the range[0, 1]
lightenHsl: (amount: number, [h, s, l]: HSL) => HSL;
returns a lightened hex string.
amount
is a value in the range[0, 1]
lightenHex: (amount: number, hex: string) => string;
returns a darkened hex string.
amount
is a value in the range[0, 1]
darkenHex: (amount: number, hex: string) => string;
returns a Vector3 colour somewhere between
c1
andc2
.t
is the "time" value in the range[0, 1]
lerp3: (t: number, [a1, b1, c1]: XYZ, [a2, b2, c2]: XYZ) => XYZ;
returns an length
n
array of Vector3 colours. colours are evenly spaced betweenc1
andc2
.
linearGradient: (n: number, c1: XYZ, c2: XYZ) => XYZ[];
returns an length
n
array of Vector3 colours. colours are betweenc1
andc2
, and are spaced according to the easing functioneaseFn
.
gradient: (ease: EaseFn, n: number, c1: XYZ, c2: XYZ) => XYZ[];
returns a length
n
array of Vector3 colours. colours are the ones formed from thelinearGradient(n/(numColours-1), col1, col2)
for all colourscol1, col2, ..., colN
multiGradient: (n: number, colors: XYZ[]) => XYZ[];
returns a rounded, length
n
array of Vector3 colours. colours are evenly spaced betweenc1
andc2
.
rLinearGradient: (n: number, c1: XYZ, c2: XYZ) => XYZ[];
returns a rounded, length
n
array of Vector3 colours. colours are betweenc1
andc2
, and are spaced according to the easing functioneaseFn
.
rGradient: (ease: EaseFn, n: number, c1: XYZ, c2: XYZ) => XYZ[];
returns a rounded, length
n
array of Vector3 colours. colours are the ones formed from thelinearGradient(n/(numColours-1), col1, col2)
for all colourscol1, col2, ..., colN
rMultiGradient: (n: number, colors: XYZ[]) => XYZ[];
returns an length
n
array of hex strings. The 0th color is the same as the inputhexString
, while the others are colours corresponding to an eve turn around the colour wheel. Ifn
is 3 for example, the two other colours would represent a 1/3 and 2/3 rotation of the colour wheel.
complimentHex: (n: number, hex: string) => string[];
returns an length
n
array of hsl Vector3. The 0th color is the same as the inputhsl
, while the others are colours corresponding to an eve turn around the colour wheel. Ifn
is 3 for example, the two other colours would represent a 1/3 and 2/3 rotation of the colour wheel.
complimentHsl: (n: number, [h, s, l]: HSL) => HSL[];
returns an length
n
array of rgb Vector3. The 0th color is the same as the inputrgb
, while the others are colours corresponding to an eve turn around the colour wheel. Ifn
is 3 for example, the two other colours would represent a 1/3 and 2/3 rotation of the colour wheel.
complimentRgb: (n: number, rgb: RGB) => RGB[];
returns an rgba css string like
rgba(255, 255, 255, 1)
from the rgb color and alpha value
rgb2css: (alpha: number, rgb: RGB) => string;
returns an hsl css string like
hsl(222, 50%, 75%, 0.6)
from the hsl color and alpha value
hsl2css: (alpha: number, [h, s, l]: HSL) => string;