2.1.0 • Published 3 months ago

pex-color v2.1.0

Weekly downloads
24
License
MIT
Repository
github
Last release
3 months ago

pex-color

npm version stability-stable npm minzipped size dependencies types Conventional Commits styled with prettier linted with eslint license

Color utilities (css, rgb, hex, hsl, hsv, hwb, lab, xyz, okhsl, okhsv, oklab, hpluv, hsluv, lchuv, bytes) for PEX.

Installation

npm install pex-color

Usage

import color from "pex-color";

// Hexadecimal
color.toHex([1, 0, 0]);
// => "#FF0000"

color.toHex([1, 0, 0, 0.5]);
// => "#FF000080"

color.toHex([1, 0, 0, 0.5], false);
// => "#FF0000"

color.fromHex(color.create(), "#FF0000");
// => [1, 0, 0, 1]

color.fromHex(new Array(3), "#FF0000");
// => [1, 0, 0]

// CSS
color.toCSSRGB([1, 0, 0, 1]);
// => "rgba(255, 0, 0, 1)"

color.toCSSLab([1, 0, 0, 1]);
// => "lab(53.23711% 78.27048 62.14609 / 1)"

// Various color spaces
color.toOklab([1, 0, 0, 1]);
// => [0.62796, 0.22486, 0.12585, 1]

// Utils
color.utils.linearToSrgb(0.5);
// => 0.7353569830524495

API

The "color" primitive is an array of 3 (RGB) or 4 (A) values in the range 0 < x < 1.

API naming follows the following rules:

  • fromType(color, ...values) = set a color primitive from Type values
  • toType(color, out) = convert a color primitive to an array of Type and optionally set it to out

Modules

Functions

Typedefs

utils

utils.linearToSrgb ⇒ number

Convert component from linear value

Kind: static constant of utils

ParamType
cnumber

utils.srgbToLinear ⇒ number

Convert component to linear value

Kind: static constant of utils

ParamType
cnumber

fromBytes(color, bytes) ⇒ color

Updates a color based on byte values.

Kind: global function

ParamType
colorcolor
bytesbytes

toBytes(color, out) ⇒ bytes

Get RGBA color components as bytes array.

Kind: global function

ParamType
colorcolor
outArray

create(r, g, b, a) ⇒ color

Creates a new color from linear values.

Kind: global function

ParamTypeDefault
rnumber0
gnumber0
bnumber0
anumber

copy(color) ⇒ color

Returns a copy of a color.

Kind: global function

ParamType
colorcolor

set(color, color2) ⇒ color

Sets a color to another color.

Kind: global function

ParamType
colorcolor
color2color

fromValues(color, r, g, b, a) ⇒ color

Updates a color based on r, g, b, a values.

Kind: global function

ParamType
colorcolor
rnumber
gnumber
bnumber
anumber

toCSSRGB(color, precision) ⇒ css

Returns a rgb CSS string representation of a given color.

Kind: global function

ParamTypeDefault
colorcolor
precisionnumber5

toCSSHSL(color, precision) ⇒ css

Returns a hsl CSS string representation of a given color.

Kind: global function

ParamTypeDefault
colorcolor
precisionnumber5

toCSSLab(color, precision) ⇒ css

Returns a lab CSS string representation of a given color.

Kind: global function

ParamTypeDefault
colorcolor
precisionnumber5

toCSSLCH(color, precision) ⇒ css

Returns a lch CSS string representation of a given color.

Kind: global function

ParamTypeDefault
colorcolor
precisionnumber5

toCSSHWB(color, precision) ⇒ css

Returns a hwb CSS string representation of a given color.

Kind: global function

ParamTypeDefault
colorcolor
precisionnumber5

fromHex(color, hex) ⇒ color

Updates a color based on a hexadecimal string.

Kind: global function

ParamTypeDescription
colorcolor
hexhexLeading '#' is optional.

toHex(color, alpha) ⇒ hex

Returns a hexadecimal string representation of a given color.

Kind: global function

ParamTypeDescription
colorcolor
alphabooleanHandle alpha

fromHPLuv(color, h, s, l, a) ⇒ color

Updates a color based on HPLuv values and alpha.

Kind: global function

ParamType
colorcolor
hnumber
snumber
lnumber
anumber

toHPLuv(color, out) ⇒ hpluv

Returns a HPLuv representation of a given color.

Kind: global function

ParamType
colorcolor
outArray

fromHSL(color, h, s, l, a) ⇒ color

Updates a color based on HSL values and alpha.

Kind: global function

ParamType
colorcolor
hnumber
snumber
lnumber
anumber

toHSL(color, out) ⇒ hsl

Returns a HSL representation of a given color.

Kind: global function

ParamType
colorcolor
outArray

fromHSLuv(color, h, s, l, a) ⇒ color

Updates a color based on HSLuv values and alpha.

Kind: global function

ParamType
colorcolor
hnumber
snumber
lnumber
anumber

toHSLuv(color, out) ⇒ hsluv

Returns a HSLuv representation of a given color.

Kind: global function

ParamType
colorcolor
outArray

fromHSV(color, h, s, v, a) ⇒ color

Updates a color based on HSV values and alpha.

Kind: global function

ParamType
colorcolor
hnumber
snumber
vnumber
anumber

toHSV(color, out) ⇒ hsv

Returns a HSV representation of a given color.

Kind: global function

ParamType
colorcolor
outArray

fromHWB(color, h, w, b, a) ⇒ color

Updates a color based on HWB values and alpha.

Kind: global function

ParamType
colorcolor
hnumber
wnumber
bnumber
anumber

toHWB(color, out) ⇒ hwb

Returns a HWB representation of a given color.

Kind: global function

ParamType
colorcolor
outArray

fromLab(color, l, a, b, α, illuminant) ⇒ color

Updates a color based on Lab values and alpha.

Kind: global function

ParamType
colorcolor
lnumber
anumber
bnumber
αnumber
illuminantArray

toLab(color, out, illuminant) ⇒ lab

Returns a Lab representation of a given color.

Kind: global function

ParamType
colorcolor
outArray
illuminantArray

fromLCHuv(color, l, c, h, a) ⇒ color

Updates a color based on LCHuv values and alpha.

Kind: global function

ParamType
colorcolor
lnumber
cnumber
hnumber
anumber

toLCHuv(color, out) ⇒ lchuv

Returns a LCHuv representation of a given color.

Kind: global function

ParamType
colorcolor
outArray

fromLinear(color, r, g, b, a) ⇒ color

Updates a color based on linear values.

Kind: global function

ParamType
colorcolor
rnumber
gnumber
bnumber
anumber

toLinear(color, out) ⇒ linear

Returns a linear color representation of a given color.

Kind: global function

ParamType
colorcolor
outArray

fromOkhsl(color, h, s, l, α) ⇒ color

Updates a color based on Okhsl values and alpha.

Kind: global function

ParamType
colorcolor
hnumber
snumber
lnumber
αnumber

toOkhsl(color, out) ⇒ okhsl

Returns an Okhsl representation of a given color.

Kind: global function

ParamType
colorcolor
outArray

fromOkhsv(color, h, s, v, α) ⇒ color

Updates a color based on Okhsv values and alpha.

Kind: global function

ParamType
colorcolor
hnumber
snumber
vnumber
αnumber

toOkhsv(color, out) ⇒ okhsv

Returns an Okhsv representation of a given color.

Kind: global function

ParamType
colorcolor
outArray

fromOklab(color, L, a, b, α) ⇒ color

Updates a color based on Oklab values and alpha.

Kind: global function

ParamType
colorcolor
Lnumber
anumber
bnumber
αnumber

toOklab(color, out) ⇒ oklab

Returns an Oklab representation of a given color.

Kind: global function

ParamType
colorcolor
outArray

fromXYZ(color, x, y, z, a) ⇒ color

Updates a color based on XYZ values and alpha.

Kind: global function

ParamType
colorcolor
xnumber
ynumber
znumber
anumber

toXYZ(color, out) ⇒ xyz

Returns a XYZ representation of a given color.

Kind: global function

ParamType
colorcolor
outArray

bytes : Array.<number>

An array of 3 (RGB) or 4 (A) values in bytes.

All components in the range 0 <= x <= 255

Kind: global typedef

color : Array.<number>

An array of 3 (RGB) or 4 (A) values.

All components in the range 0 <= x <= 1

Kind: global typedef

css : string

CSS string representation.

Kind: global typedef See: https://www.w3.org/TR/css-color-4/

hex : string

hexadecimal string (RGBA or RRGGBBAA).

Kind: global typedef

hpluv : Array.<number>

CIELUV hue, saturation, lightness.

All components in the range 0 <= x <= 1.

Kind: global typedef

hsl : Array.<number>

hue, saturation, lightness.

All components in the range 0 <= x <= 1

Kind: global typedef See: https://en.wikipedia.org/wiki/HSL_and_HSV

hsluv : Array.<number>

CIELUV hue, saturation, lightness.

All components in the range 0 <= x <= 1

Kind: global typedef See: https://www.hsluv.org/

hsv : Array.<number>

hue, saturation, value.

All components in the range 0 <= x <= 1

Kind: global typedef See: https://en.wikipedia.org/wiki/HSL_and_HSV

hwb : Array.<number>

hue, whiteness, blackness.

All components in the range 0 <= x <= 1

Kind: global typedef See: https://en.wikipedia.org/wiki/HWB_color_model

lab : Array.<number>

CIELAB with D65 standard illuminant as default.

Components range (D65): 0 <= l <= 1; -0.86183 <= a <= 0.98234; -1.0786 <= b <= 0.94478;

Components range (D50): 0 <= l <= 1; -0.79287 <= a <= 0.9355; -1.12029 <= b <= 0.93388;

Kind: global typedef See: https://en.wikipedia.org/wiki/CIELAB_color_space

lchuv : Array.<number>

CIELChuv Luminance Chroma Hue.

All components in the range 0 <= x <= 1

Kind: global typedef See: https://en.wikipedia.org/wiki/CIELUV

linear : Array.<number>

r g b linear values.

All components in the range 0 <= x <= 1

Kind: global typedef See: https://en.wikipedia.org/wiki/SRGB

okhsl : Array.<number>

All components in the range 0 <= x <= 1

Kind: global typedef See: https://bottosson.github.io/posts/colorpicker/#hsv-2

okhsv : Array.<number>

All components in the range 0 <= x <= 1

Kind: global typedef See: https://bottosson.github.io/posts/colorpicker/#hsv-2

oklab : Array.<number>

Cartesian form using D65 standard illuminant.

Components range: 0 <= l <= 1; -0.233 <= a <= 0.276; -0.311 <= b <= 0.198;

Kind: global typedef See: https://bottosson.github.io/posts/oklab/#converting-from-linear-srgb-to-oklab

xyz : Array.<number>

CIE XYZ using D65 standard illuminant.

Components range: 0 <= x <= 0.95; 0 <= y <= 1; 0 <= z <= 1.08;

Kind: global typedef See: https://en.wikipedia.org/wiki/CIE_1931_color_space

License

MIT. See license file.

2.1.0

3 months ago

2.0.3

9 months ago

2.0.2

11 months ago

2.0.4

9 months ago

2.0.1

11 months ago

2.0.0

11 months ago

2.0.0-alpha.2

2 years ago

2.0.0-alpha.0

2 years ago

2.0.0-alpha.1

2 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago

0.4.0

9 years ago

0.3.2

9 years ago

0.3.1

9 years ago

0.2.1

9 years ago

0.2.0

10 years ago

0.1.0

10 years ago

0.0.5

10 years ago

0.0.4

10 years ago