Skip to content

Color Manipulation ​

All manipulation methods modify the instance in place and return this, supporting method chaining. If you need to preserve the original color, call .clone() first.

Compare adjustments ​

Source
rgb(51, 102, 153)
hsl(210, 50%, 40%)
lighten
rgb(71, 133, 194)
hsl(210, 50%, 52%)
brighten
rgb(92, 133, 173)
hsl(210, 33%, 52%)
tint
rgb(92, 133, 173)
hsl(210, 33%, 52%)
import xcolor from '@xpyjs/color'
const original = xcolor('#336699').alpha(1)
original.clone().lighten(20).toRgbString()
original.clone().brighten(20).toRgbString()
original.clone().tint(20).toRgbString()

Brightness ​

lighten(amount?) ​

Move HSL lightness toward white across the remaining range. amount is the percentage of the distance from the current lightness to 100% (0–100), default 10; 50 moves halfway and 100 always produces white.

ts
xcolor('#ff0000').lighten(20).toHex()  // '#ff3333'

darken(amount?) ​

Move HSL lightness toward black across the current range. amount is the percentage of the distance from the current lightness to 0% (0–100), default 10; 50 moves halfway and 100 always produces black. Equivalent to lighten(-amount).

ts
xcolor('#ff0000').darken(20).toHex()  // '#cc0000'

brighten(amount?) ​

Push each channel toward 255 in RGB space. Default 10, range 0–100.

ts
xcolor('#ff0000').brighten(50).toHex()  // '#ff8080'

Saturation ​

saturate(amount?) ​

Increase HSL saturation. Default 10, range 0–100.

ts
xcolor('#bf4040').saturate(40).toHex()  // '#f20d0d'

desaturate(amount?) ​

Decrease HSL saturation. Default 10.

ts
xcolor('#ff0000').desaturate(50).toHex()  // '#bf4040'

grayscale() ​

Fully desaturate, equivalent to desaturate(100).

Alias: greyscale()

ts
xcolor('#ff0000').grayscale().toHex()  // '#808080'

Hue ​

spin(degrees) ​

Rotate the hue on the HSL color wheel. Supports positive and negative values, automatically wraps at 360°.

Alias: rotate()

ts
xcolor('#ff0000').spin(120).toHex()   // '#00ff00'
xcolor('#ff0000').spin(-120).toHex()  // '#0000ff'

Inversion ​

negate() ​

Invert each RGB channel (255 - channel), alpha remains unchanged.

Alias: invert()

ts
xcolor('#ff0000').negate().toHex()  // '#00ffff'

Opacity ​

fade(amount?) ​

Decrease opacity. Alpha is reduced by amount / 100, default 10.

Alias: fadeOut()

ts
xcolor('#ff0000').fade(50).alpha()  // 0.5

opaquer(amount?) ​

Increase opacity. Alpha is increased by amount / 100, default 10.

Alias: fadeIn()

ts
xcolor('rgba(255,0,0,0.5)').opaquer(30).alpha()  // 0.8

Mixing ​

mix(color, amount?) ​

Linearly mix with another color in RGB space. amount is the weight of the target color (0–100), default 50.

ts
xcolor('#ff0000').mix('#0000ff', 50).toHex()  // '#800080'

tint(amount?) ​

Mix with white. amount is the weight of white (0–100), default 50.

ts
xcolor('#ff0000').tint(50).toHex()  // '#ff8080'

shade(amount?) ​

Mix with black. amount is the weight of black (0–100), default 50.

ts
xcolor('#ff0000').shade(50).toHex()  // '#800000'

Method Signature Summary ​

MethodParamDefaultReturnsAlias
lighten(amount?)0–10010this—
darken(amount?)0–10010this—
brighten(amount?)0–10010this—
saturate(amount?)0–10010this—
desaturate(amount?)0–10010this—
grayscale()——thisgreyscale()
spin(degrees)any number—thisrotate()
negate()——thisinvert()
fade(amount?)0–10010thisfadeOut()
opaquer(amount?)0–10010thisfadeIn()
mix(color, amount?)0–10050this—
tint(amount?)0–10050this—
shade(amount?)0–10050this—

Released under the MIT License.