Skip to content

Color

Module to generate colors.

Overview

For a human-readable color like 'red', use human().

For a hex color like #ff0000 used in HTML/CSS, use rgb(). There are also methods for other color formats such as hsl(), cmyk(), hwb(), lab(), and lch().

cmyk

Returns a CMYK color.

Available since v7.0.0

Parameters

NameTypeDefaultDescription
options?{ ... }{}

Options object.

options.format?ColorFormat'decimal'

Format of generated CMYK color.

Returns: string | number[]

ts
function cmyk(options?: { format?: ColorFormat }): string | number[];

Examples

ts
faker.color.cmyk() // [0.31, 0.52, 0.32, 0.43]
faker.color.cmyk({ format: 'decimal' }) // [0.31, 0.52, 0.32, 0.43]
faker.color.cmyk({ format: 'css' }) // cmyk(100%, 0%, 0%, 0%)
faker.color.cmyk({ format: 'binary' }) // (8-32 bits) x 4

colorByCSSColorSpace

Returns a random color based on CSS color space specified.

Available since v7.0.0

Parameters

NameTypeDefaultDescription
options?{ ... }{}

Options object.

options.format?ColorFormat'decimal'

Format of generated RGB color.

options.space?'sRGB' | 'display-p3' | 'rec2020' | 'a98-rgb' | 'prophoto-rgb''sRGB'

Color space to generate the color for.

Returns: string | number[]

ts
function colorByCSSColorSpace(options?: {
  format?: ColorFormat;
  space?: CssSpaceType;
}): string | number[];

Examples

ts
faker.color.colorByCSSColorSpace() // [0.93, 1, 0.82]
faker.color.colorByCSSColorSpace({ format: 'decimal' }) // [0.12, 0.21, 0.31]
faker.color.colorByCSSColorSpace({ format: 'css', space: 'display-p3' }) // color(display-p3 0.12 1 0.23)
faker.color.colorByCSSColorSpace({ format: 'binary' }) // (8-32 bits x 3)

cssSupportedFunction

Returns a random css supported color function name.

Available since v7.0.0

Returns: 'rgb' | 'rgba' | 'hsl' | 'hsla' | 'hwb' | 'cmyk' | 'lab' | 'lch' | 'color'

ts
function cssSupportedFunction(): CssFunctionType;

Examples

ts
faker.color.cssSupportedFunction() // 'rgb'

cssSupportedSpace

Returns a random css supported color space name.

Available since v7.0.0

Returns: 'sRGB' | 'display-p3' | 'rec2020' | 'a98-rgb' | 'prophoto-rgb'

ts
function cssSupportedSpace(): CssSpaceType;

Examples

ts
faker.color.cssSupportedSpace() // 'display-p3'

hsl

Returns an HSL color.

Available since v7.0.0

Parameters

NameTypeDefaultDescription
options?{ ... }{}

Options object.

options.format?ColorFormat'decimal'

Format of generated HSL color.

options.includeAlpha?booleanfalse

Adds an alpha value to the color (RGBA).

Returns: string | number[]

ts
function hsl(options?: {
  format?: ColorFormat;
  includeAlpha?: boolean;
}): string | number[];

Examples

ts
faker.color.hsl() // [201, 0.23, 0.32]
faker.color.hsl({ format: 'decimal' }) // [300, 0.21, 0.52]
faker.color.hsl({ format: 'decimal', includeAlpha: true }) // [300, 0.21, 0.52, 0.28]
faker.color.hsl({ format: 'css' }) // hsl(0deg, 100%, 80%)
faker.color.hsl({ format: 'css', includeAlpha: true }) // hsl(0deg 100% 50% / 0.5)
faker.color.hsl({ format: 'binary' }) // (8-32 bits) x 3
faker.color.hsl({ format: 'binary', includeAlpha: true }) // (8-32 bits) x 4

human

Returns a random human-readable color name.

Available since v7.0.0

Returns: string

ts
function human(): string;

Examples

ts
faker.color.human() // 'red'

hwb

Returns an HWB color.

Available since v7.0.0

Parameters

NameTypeDefaultDescription
options?{ ... }{}

Options object.

options.format?ColorFormat'decimal'

Format of generated RGB color.

Returns: string | number[]

ts
function hwb(options?: { format?: ColorFormat }): string | number[];

Examples

ts
faker.color.hwb() // [201, 0.21, 0.31]
faker.color.hwb({ format: 'decimal' }) // [201, 0.21, 0.31]
faker.color.hwb({ format: 'css' }) // hwb(194 0% 0%)
faker.color.hwb({ format: 'binary' }) // (8-32 bits x 3)

lab

Returns a LAB (CIELAB) color.

Available since v7.0.0

Parameters

NameTypeDefaultDescription
options?{ ... }{}

Options object.

options.format?ColorFormat'decimal'

Format of generated RGB color.

Returns: string | number[]

ts
function lab(options?: { format?: ColorFormat }): string | number[];

Examples

ts
faker.color.lab() // [0.832133, -80.3245, 100.1234]
faker.color.lab({ format: 'decimal' }) // [0.856773, -80.2345, 100.2341]
faker.color.lab({ format: 'css' }) // lab(29.2345% 39.3825 20.0664)
faker.color.lab({ format: 'binary' }) // (8-32 bits x 3)

lch

Returns an LCH color. Even though upper bound of chroma in LCH color space is theoretically unbounded, it is bounded to 230 as anything above will not make a noticeable difference in the browser.

Available since v7.0.0

Parameters

NameTypeDefaultDescription
options?{ ... }{}

Options object.

options.format?ColorFormat'decimal'

Format of generated RGB color.

Returns: string | number[]

ts
function lch(options?: { format?: ColorFormat }): string | number[];

Examples

ts
faker.color.lch() // [0.522345, 72.2, 56.2]
faker.color.lch({ format: 'decimal' }) // [0.522345, 72.2, 56.2]
faker.color.lch({ format: 'css' }) // lch(52.2345% 72.2 56.2)
faker.color.lch({ format: 'binary' }) // (8-32 bits x 3)

rgb

Returns an RGB color.

Available since v7.0.0

Parameters

NameTypeDefaultDescription
options?{ ... }{}

Options object.

options.casing?Casing'lower'

Letter type case of the generated hex color. Only applied when 'hex' format is used.

options.format?'css' | 'binary' | 'decimal' | 'hex''hex'

Format of generated RGB color.

options.includeAlpha?booleanfalse

Adds an alpha value to the color (RGBA).

options.prefix?string'#'

Prefix of the generated hex color. Only applied when 'hex' format is used.

Returns: string | number[]

ts
function rgb(options?: {
  prefix?: string;
  casing?: Casing;
  format?: 'hex' | ColorFormat;
  includeAlpha?: boolean;
}): string | number[];

Examples

ts
faker.color.rgb() // '#0d7f26'
faker.color.rgb({ prefix: '0x' }) // '0x9ddc8b'
faker.color.rgb({ casing: 'upper' }) // '#B8A51E'
faker.color.rgb({ casing: 'lower' }) // '#b12f8b'
faker.color.rgb({ prefix: '#', casing: 'lower' }) // '#eb0c16'
faker.color.rgb({ format: 'hex', casing: 'lower' }) // '#bb9d17'
faker.color.rgb({ format: 'decimal' }) // [64, 192,174]
faker.color.rgb({ format: 'css' }) // 'rgb(216, 17, 192)'
faker.color.rgb({ format: 'binary' }) // '00110010 00001000 01110110'
faker.color.rgb({ includeAlpha: true }) // '#f96efb5e'
faker.color.rgb({ format: 'css', includeAlpha: true }) // 'rgba(180, 158, 24, 0.75)'
faker.color.rgb({ format: 'decimal', includeAlpha: true }) // [52, 250, 209, 0.21]

space

Returns a random color space name from the worldwide accepted color spaces. Source: https://en.wikipedia.org/wiki/List_of_color_spaces_and_their_uses

Available since v7.0.0

Returns: string

ts
function space(): string;

Examples

ts
faker.color.space() // 'sRGB'

Released under the MIT License.