Skip to content

Channel Read/Write ​

All channel methods act as a getter when called without arguments (returning the current value) and as a setter when called with an argument (setting the value and returning this).

RGB Channels ​

red(value?) ​

Read or write the red channel. Range 0–255.

Alias: r()

ts
xcolor('#3b82f6').red()     // 59
xcolor('#3b82f6').red(200)  // set red to 200, returns this

green(value?) ​

Read or write the green channel. Range 0–255.

Alias: g()

ts
xcolor('#3b82f6').green()   // 130

blue(value?) ​

Read or write the blue channel. Range 0–255.

Alias: b()

ts
xcolor('#3b82f6').blue()    // 246

alpha(value?) ​

Read or write the opacity. Range 0–1.

Alias: a(), opacity()

ts
xcolor('#ff0000').alpha()      // 1
xcolor('#ff0000').alpha(0.5)   // set to semi-transparent

HSL Channels ​

hue(value?) ​

Read or write the hue. Range 0–360.

Alias: h()

ts
xcolor('#ff0000').hue()     // 0
xcolor('#ff0000').hue(120)  // becomes green

saturation(value?) ​

Read or write HSL saturation. Range 0–100.

Alias: s()

ts
xcolor('#ff0000').saturation()    // 100
xcolor('#ff0000').saturation(50)  // reduce saturation

lightness(value?) ​

Read or write HSL lightness. Range 0–100.

Alias: l()

ts
xcolor('#ff0000').lightness()    // 50
xcolor('#ff0000').lightness(80)  // lighten

Compound Read/Write ​

rgb(r?, g?, b?) ​

Without arguments, returns a { r, g, b, a } object. With arguments, sets all three RGB channels at once.

ts
xcolor('#ff0000').rgb()             // { r: 255, g: 0, b: 0, a: 1 }
xcolor('#ff0000').rgb(0, 128, 255)  // set three channels

hsl(h?, s?, l?) ​

Without arguments, returns a { h, s, l, a } object. With arguments, sets all three HSL channels at once.

ts
xcolor('#ff0000').hsl()              // { h: 0, s: 100, l: 50, a: 1 }
xcolor('#ff0000').hsl(210, 80, 60)   // set to a blue tone

hex(value?) ​

Without arguments, returns a hex string: 6 digits when fully opaque, 8 digits when alpha < 1. With an argument, sets the color from a hex string.

The setter accepts strings recognized by the active color parsers. A successful call marks the instance valid. A parse failure throws and marks it invalid while retaining its channels.

ts
xcolor('#ff0000').hex()          // '#ff0000'
xcolor('#ff0000').hex('#00ff00') // becomes green

Released under the MIT License.