RgbColor
import { RgbColor } from "https://raw.githubusercontent.com/i-xi-dev/color.es/5.2.10/mod.ts";
A color represented by red, green, and blue channels
§Properties
Gets the color space.
Always returns "srgb"
in the current version.
Gets the red component value.
const color = RgbColor.fromHexString("#ff0000");
const r = color.red;
// r
// → 1
const color = RgbColor.fromHexString("#88ffff");
const r = color.red;
// r
// → 0.533333
Gets the green component value.
const color = RgbColor.fromHexString("#00ff00");
const g = color.green;
// g
// → 1
const color = RgbColor.fromHexString("#ff88ff");
const g = color.green;
// g
// → 0.533333
Gets the blue component value.
const color = RgbColor.fromHexString("#0000ff");
const b = color.blue;
// b
// → 1
const color = RgbColor.fromHexString("#ffff88");
const b = color.blue;
// b
// → 0.533333
Gets the alpha component value.
const color = RgbColor.fromHexString("#000000");
const a = color.alpha;
// a
// → 1
const color = RgbColor.fromHexString("#00000000");
const a = color.alpha;
// a
// → 0
const color = RgbColor.fromHexString("#ffffff88");
const a = color.alpha;
// a
// → 0.533333
Get the HSL saturation value.
const color = RgbColor.fromHexString("#598910");
const saturation = color.saturation;
// saturation
// → 0.790850
Get the HSL lightness value.
const color = RgbColor.fromHexString("#598910");
const lightness = color.lightness;
// lightness
// → 0.300000
Get the HWB whiteness value.
const color = RgbColor.fromHexString("#598910");
const whiteness = color.whiteness;
// whiteness
// → 0.062745
Get the HWB blackness value.
const color = RgbColor.fromHexString("#598910");
const blackness = color.blackness;
// blackness
// → 0.462745
§Methods
Returns the RGB expressed as a string contains hexadecimal formatted bytes.
- An options object.
A RGB(A) byte sequence or the ARGB byte sequence.
const color = RgbColor.fromHexString("#112233");
const hex = color.toHexString();
// hex
// → "#112233FF" // RGBA
const color = RgbColor.fromHexString("#112233");
const hex = color.toHexString({ discardAlpha: true });
// hex
// → "#112233" // RGB
const color = RgbColor.fromHexString("#112233");
const hex = color.toHexString({ order: "argb" });
// hex
// → "#FF112233" // ARGB
const color = RgbColor.fromHexString("#112233");
const hex = color.toHexString({ order: "argb", discardAlpha: true });
// hex
// → "#FF112233" // ARGB
// If the `order` is "argb", the `discardAlpha` and `omitAlphaIfOpaque` are ignored.
Returns the triplet of h
(hue), s
(saturation), and l
(lightness).
- An options object.
A triplet of h
, s
, and l
.
const color = RgbColor.fromHexString("#ff0000");
const hsl = color.toHsl();
// hsl
// → {
// h: 0,
// s: 1,
// l: 0.5,
// a: 1,
// }
const color = RgbColor.fromHexString("#ff0000");
const hsl = color.toHsl({ omitAlphaIfOpaque: true });
// hsl
// → {
// h: 0,
// s: 1,
// l: 0.5,
// }
const color = RgbColor.fromHexString("#ff000088");
const hsl = color.toHsl({ discardAlpha: true });
// hsl
// → {
// h: 0,
// s: 1,
// l: 0.5,
// }
Returns the triplet of h
(hue), w
(whiteness), and b
(blackness).
- An options object.
A triplet of h
, w
, and b
.
const color = RgbColor.fromHexString("#ff8888");
const hwb = color.toHwb();
// hwb
// → {
// h: 0,
// w: 0.533333,
// b: 0,
// a: 1,
// }
const color = RgbColor.fromHexString("#ff8888");
const hwb = color.toHwb({ omitAlphaIfOpaque: true });
// hwb
// → {
// h: 0,
// w: 0.533333,
// b: 0,
// }
const color = RgbColor.fromHexString("#ff888888");
const hwb = color.toHwb({ discardAlpha: true });
// hwb
// → {
// h: 0,
// w: 0.533333,
// b: 0,
// }
Returns the RGB expressed as a byte sequence.
- An options object.
A RGB(A) byte sequence or the ARGB byte sequence.
const color = RgbColor.fromHexString("#112233");
const bytes = color.toUint8Array();
// bytes
// → Uint8Array[ 0x11, 0x22, 0x33, 0xFF ] // RGBA
const color = RgbColor.fromHexString("#112233");
const bytes = color.toUint8Array({ discardAlpha: true });
// bytes
// → Uint8Array[ 0x11, 0x22, 0x33 ] // RGB
const color = RgbColor.fromHexString("#112233");
const bytes = color.toUint8Array({ order: "argb" });
// bytes
// → Uint8Array[ 0xFF, 0x11, 0x22, 0x33 ] // ARGB
const color = RgbColor.fromHexString("#112233");
const bytes = color.toUint8Array({ order: "argb", discardAlpha: true });
// bytes
// → Uint8Array[ 0xFF, 0x11, 0x22, 0x33 ] // ARGB
// If the `order` is "argb", the `discardAlpha` and `omitAlphaIfOpaque` are ignored.
Returns the RGB expressed as a byte sequence.
- An options object.
A RGB(A) byte sequence or the ARGB byte sequence.
const color = RgbColor.fromHexString("#112233");
const bytes = color.toUint8ClampedArray();
// bytes
// → Uint8ClampedArray[ 0x11, 0x22, 0x33, 0xFF ] // RGBA