@thi.ng/color
This project is part of the
@thi.ng/umbrella monorepo.
About
Raw, array-based, color operations, conversions and optional type
wrappers. The functions provided by this package are largely using the
same calling convention as those in the
@thi.ng/vectors
package.
Color spaces / modes
Fast color space conversions (any direction) between:
- CSS (string, hex3/hex4/hex6/hex8, rgba(), hsla(), named colors)
- HCYA (float4)
- HSIA (float4)
- HSLA (float4)
- HSVA (float4)
- Int32 (uint32,
0xaarrggbb
) - RGBA (float4)
- XYZA (float4, aka CIE 1931)
- YCbCr (float4)
Apart from CSS
and Int32
colors, all others can be stored as plain
arrays, typed array or custom array-like types of (mostly) normalized
values ([0,1]
interval). Where applicable, the hue too is stored in
that range, NOT in degrees.
Apart from conversions, most other operations provided by this package
are currently only supporting RGBA colors. These can also be converted
to / from sRGB (i.e. linear vs gamma corrected). Additionally, RGBA
colors can be pre-multiplied (and post-multiplied) with their alpha
channel (see Porter-Duff section
below).
Class wrappers
The package provides lightweight class wrappers for each color mode /
space. These wrappers act similarly to the Vec2/3/4
wrappers in
@thi.ng/vectors,
support striding (for mapped memory views), named channel accessor
aliases (in addition to array indexing) and are fully compatible with
all functions (and act as syntax sugar for generic conversion
functions). Wrapper factory functions are provided for convenience.
RGBA transformations
RGBA color matrix
transformations,
including parametric preset transforms:
- brightness
- contrast
- exposure
- saturation (luminance aware)
- hue rotation
- color temperature (warm / cold)
- sepia (w/ fade amount)
- tint (green / purple)
- grayscale (luminance aware)
- subtraction/inversion (also available as non-matrix op)
- luminance to alpha
Transformation matrices can be combined using matrix multiplication /
concatenation (concat()
) for more efficient application.
RGBA Porter-Duff compositing
The package provides all 12 basic
Porter-Duff
compositing / blending operators, both for colors with pre-multiplied
alpha and without.
(Image source)
Cosine gradients
The following presets are bundled (in cosine-gradients.ts
):
| |
---|
| |
rainbow1 | rainbow2 |
| |
rainbow3 | rainbow4 |
| |
yellow-magenta-cyan preset | orange-blue |
| |
green-magenta | green-red |
| |
green-cyan | blue-cyan |
| |
yellow-red | red-blue |
| |
yellow-green-blue | blue-white-red |
| |
cyan-magenta | yellow-purple-magenta |
| |
green-blue-orange | orange-magenta-blue |
| |
blue-magenta-orange | magenta-green |
Multi-stop gradients
The multiCosineGradient()
function returns an iterator of raw RGBA
colors based on given gradient stops. This iterator computes a cosine
gradient between each color stop and yields a sequence of RGBA values.
col.multiCosineGradient(
10,
[0.1, col.RED], [0.5, col.GREEN], [0.9, col.BLUE]
)
.map(col.rgbaCss)
Status
ALPHA - work in progress
Installation
yarn add @thi.ng/color
Dependencies
Usage examples
import * as col from "@thi.ng/color";
const a = col.asRGBA(col.css("#3cf"));
const b = col.parseCss("hsla(30,100%,50%,0.75)", col.ColorMode.RGBA);
const c = col.convert("rgb(0,255,255)", col.ColorMode.HSVA, col.ColorMode.CSS);
col.hslaCss(col.rgbaHsla([], [1, 0.5, 0.5, 1]))
col.luminance(col.css("white"))
col.luminance(0xffffff, col.ColorMode.INT32)
col.transform([], col.saturation(1.25), a)
filter = col.concatMatrices(
col.saturation(0.5),
col.brightness(0.1),
);
col.transform([], filter, col.RED);
Authors
License
© 2018 Karsten Schmidt // Apache Software License 2.0