Color
@ripl/core
Color parsing, serialization, and utility functions.
Overview
Classes: ColorParseError
Interfaces: ColorParser
Type Aliases: ColorRGBA · ColorHSL · ColorHSLA · ColorHSV · ColorHSVA · ColorSpace
Functions: getColorParser · parseColor · parseHEX · parseRGB · parseRGBA · parseHSL · parseHSLA · parseHSV · parseHSVA · rgbChannelToHEX · serialiseHEX · serialiseRGB · serialiseRGBA · serialiseHSL · serialiseHSLA · serialiseHSV · serialiseHSVA · setColorAlpha · rgbaToHSL · hslToRGBA · rgbaToHSV · hsvToRGBA
ColorParseError class
Error thrown when a color string cannot be parsed in the expected format.
class ColorParseError extends ErrorConstructor:
Parameters:
| Name | Type | Description |
|---|---|---|
value | string | |
type | ColorSpace |
Properties:
| Property | Type | Description |
|---|---|---|
name | string | |
message | string | |
stack | string | undefined | |
cause | unknown |
ColorParser interface
A color parser that can test, parse, and serialise a specific color format.
interface ColorParser {
pattern: RegExp;
parse(value: string): ColorRGBA;
serialise(...args: ColorRGBA): string;
}Properties:
| Property | Type | Description |
|---|---|---|
pattern | RegExp | |
parse | (value: string) => ColorRGBA | |
serialise | (red: number, green: number, blue: number, alpha: number) => string |
ColorRGBA type
An RGBA color represented as a four-element tuple of channel values.
type ColorRGBA = [red: number, green: number, blue: number, alpha: number];ColorHSL type
An HSL color represented as a three-element tuple.
type ColorHSL = [hue: number, saturation: number, lightness: number];ColorHSLA type
An HSLA color represented as a four-element tuple.
type ColorHSLA = [hue: number, saturation: number, lightness: number, alpha: number];ColorHSV type
An HSV color represented as a three-element tuple.
type ColorHSV = [hue: number, saturation: number, value: number];ColorHSVA type
An HSVA color represented as a four-element tuple.
type ColorHSVA = [hue: number, saturation: number, value: number, alpha: number];ColorSpace type
Supported color space identifiers.
type ColorSpace = 'hex'
| 'rgb'
| 'rgba'
| 'hsl'
| 'hsla'
| 'hsv'
| 'hsva';getColorParser function
Finds the first color parser whose pattern matches the given color string.
function getColorParser(value: string): ColorParser | undefinedParameters:
| Name | Type | Description |
|---|---|---|
value | string |
Returns: ColorParser \| undefined
parseColor function
Parses any supported color string into an RGBA tuple, or returns undefined if no parser matches.
function parseColor(value: string): ColorRGBA | undefinedParameters:
| Name | Type | Description |
|---|---|---|
value | string |
Returns: ColorRGBA \| undefined
parseHEX function
Parses a hexadecimal color string (e.g. #ff0000 or #ff000080) into an RGBA tuple.
function parseHEX(value: string): ColorRGBAParameters:
| Name | Type | Description |
|---|---|---|
value | string |
Returns: ColorRGBA
parseRGB function
Parses an rgb() color string into an RGBA tuple with alpha set to 1.
function parseRGB(value: string): ColorRGBAParameters:
| Name | Type | Description |
|---|---|---|
value | string |
Returns: ColorRGBA
parseRGBA function
Parses an rgba() color string into an RGBA tuple.
function parseRGBA(value: string): ColorRGBAParameters:
| Name | Type | Description |
|---|---|---|
value | string |
Returns: ColorRGBA
parseHSL function
Parses an hsl() color string into an RGBA tuple.
function parseHSL(value: string): ColorRGBAParameters:
| Name | Type | Description |
|---|---|---|
value | string |
Returns: ColorRGBA
parseHSLA function
Parses an hsla() color string into an RGBA tuple.
function parseHSLA(value: string): ColorRGBAParameters:
| Name | Type | Description |
|---|---|---|
value | string |
Returns: ColorRGBA
parseHSV function
Parses an hsv() color string into an RGBA tuple.
function parseHSV(value: string): ColorRGBAParameters:
| Name | Type | Description |
|---|---|---|
value | string |
Returns: ColorRGBA
parseHSVA function
Parses an hsva() color string into an RGBA tuple.
function parseHSVA(value: string): ColorRGBAParameters:
| Name | Type | Description |
|---|---|---|
value | string |
Returns: ColorRGBA
rgbChannelToHEX function
Converts a single RGB channel value (0–255) to a two-character hexadecimal string.
function rgbChannelToHEX(channel: number): stringParameters:
| Name | Type | Description |
|---|---|---|
channel | number |
Returns: string
serialiseHEX function
Serialises RGBA channel values into a hexadecimal color string (e.g. #ff0000).
function serialiseHEX(...args: ColorRGBA): stringParameters:
| Name | Type | Description |
|---|---|---|
args | ColorRGBA |
Returns: string
serialiseRGB function
Serialises RGBA channel values into an rgb() color string.
function serialiseRGB(...args: ColorRGBA): stringParameters:
| Name | Type | Description |
|---|---|---|
args | ColorRGBA |
Returns: string
serialiseRGBA function
Serialises RGBA channel values into an rgba() color string.
function serialiseRGBA(...args: ColorRGBA): stringParameters:
| Name | Type | Description |
|---|---|---|
args | ColorRGBA |
Returns: string
serialiseHSL function
Serialises RGBA channel values into an hsl() color string.
function serialiseHSL(...args: ColorRGBA): stringParameters:
| Name | Type | Description |
|---|---|---|
args | ColorRGBA |
Returns: string
serialiseHSLA function
Serialises RGBA channel values into an hsla() color string.
function serialiseHSLA(...args: ColorRGBA): stringParameters:
| Name | Type | Description |
|---|---|---|
args | ColorRGBA |
Returns: string
serialiseHSV function
Serialises RGBA channel values into an hsv() color string.
function serialiseHSV(...args: ColorRGBA): stringParameters:
| Name | Type | Description |
|---|---|---|
args | ColorRGBA |
Returns: string
serialiseHSVA function
Serialises RGBA channel values into an hsva() color string.
function serialiseHSVA(...args: ColorRGBA): stringParameters:
| Name | Type | Description |
|---|---|---|
args | ColorRGBA |
Returns: string
setColorAlpha function
Returns a new color string with the alpha channel replaced by the given value.
function setColorAlpha(color: string, alpha: number)Parameters:
| Name | Type | Description |
|---|---|---|
color | string | |
alpha | number |
Returns: string
rgbaToHSL function
Converts RGBA channel values to an HSLA tuple.
function rgbaToHSL(red: number, green: number, blue: number, alpha: number = 1): ColorHSLAParameters:
| Name | Type | Description |
|---|---|---|
red | number | |
green | number | |
blue | number | |
alpha | number |
Returns: ColorHSLA
hslToRGBA function
Converts HSLA values to an RGBA tuple.
function hslToRGBA(hue: number, saturation: number, lightness: number, alpha: number = 1): ColorRGBAParameters:
| Name | Type | Description |
|---|---|---|
hue | number | |
saturation | number | |
lightness | number | |
alpha | number |
Returns: ColorRGBA
rgbaToHSV function
Converts RGBA channel values to an HSVA tuple.
function rgbaToHSV(red: number, green: number, blue: number, alpha: number = 1): ColorHSVAParameters:
| Name | Type | Description |
|---|---|---|
red | number | |
green | number | |
blue | number | |
alpha | number |
Returns: ColorHSVA
hsvToRGBA function
Converts HSVA values to an RGBA tuple.
function hsvToRGBA(hue: number, saturation: number, value: number, alpha: number = 1): ColorRGBAParameters:
| Name | Type | Description |
|---|---|---|
hue | number | |
saturation | number | |
value | number | |
alpha | number |
Returns: ColorRGBA