Skip to main content

@rtorcato/js-common / colors

colors

Functions

randomColor()

randomColor(): string

Defined in: colors/index.ts:12

Generates a random hex color string.

Example

randomColor() // '#a1b2c3'
randomColor() // '#0f9e21'

Returns

string

A string representing a random color in hexadecimal format (e.g., #a1b2c3).


matchingTextColor()

matchingTextColor(color): "#000" | "#fff"

Defined in: colors/index.ts:33

Picks the readable text colour (#000 or #fff) for a hex background.

Example

matchingTextColor('#ffffff') // '#000'
matchingTextColor('#000000') // '#fff'
matchingTextColor('#1e90ff') // '#fff'

Parameters

color

string

Returns

"#000" | "#fff"

Export


hexToRgb()

hexToRgb(hex): { r: number; g: number; b: number; } | null

Defined in: colors/index.ts:56

Converts a hex color string to an RGB object.

Example

hexToRgb('#ff8800') // { r: 255, g: 136, b: 0 }
hexToRgb('ff8800') // { r: 255, g: 136, b: 0 }
hexToRgb('nope') // null

Parameters

hex

string

Returns

{ r: number; g: number; b: number; } | null

{({ r: number; g: number; b: number } | null)}

Export


rgbToHex()

rgbToHex(r, g, b): string

Defined in: colors/index.ts:76

Converts RGB values to a hex color string.

Parameters

r

number

g

number

b

number

Returns

string

{string}

Export


isValidHex()

isValidHex(hex): boolean

Defined in: colors/index.ts:92

Checks whether a string is a valid 3- or 6-digit hex colour.

Parameters

hex

string

Returns

boolean

{boolean}

Export


darken()

darken(hex, percent): string

Defined in: colors/index.ts:104

Darkens a hex color by a given percentage.

Parameters

hex

string

percent

number

Returns

string

{string}

Export


lighten()

lighten(hex, percent): string

Defined in: colors/index.ts:121

Lightens a hex color by a given percentage.

Parameters

hex

string

percent

number

Returns

string

{string}

Export