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?'binary' | 'css' | 'decimal''decimal'

Format of generated CMYK color.

Returns: number[] | string

ts
faker.color.cmyk(options?: {
  format: 'binary' | 'css' | 'decimal'
}): string | number[]
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?'binary' | 'css' | 'decimal''decimal'

Format of generated RGB color.

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

Color space to generate the color for.

Returns: number[] | string

ts
faker.color.colorByCSSColorSpace(options?: {
  format: 'binary' | 'css' | 'decimal',
  space: 'a98-rgb' | 'display-p3' | 'prophoto-rgb' | 'rec2020' | 'sRGB'
}): string | number[]
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: 'cmyk' | 'color' | 'hsl' | 'hsla' | 'hwb' | 'lab' | 'lch' | 'rgb' | 'rgba'

ts
faker.color.cssSupportedFunction(): "rgb" | "color" | "lab" | "rgba" | "hsl" | "hsla" | "hwb" | "cmyk" | "lch"
faker.color.cssSupportedFunction() // 'rgb'

cssSupportedSpace

Returns a random css supported color space name.

Available since v7.0.0

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

ts
faker.color.cssSupportedSpace(): "display-p3" | "rec2020" | "sRGB" | "a98-rgb" | "prophoto-rgb"
faker.color.cssSupportedSpace() // 'display-p3'

hsl

Returns an HSL color.

Available since v7.0.0

Parameters

NameTypeDefaultDescription
options?{ ... }

Options object.

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

Format of generated HSL color.

options.includeAlpha?booleanfalse

Adds an alpha value to the color (RGBA).

Returns: number[] | string

ts
faker.color.hsl(options?: {
  format: 'binary' | 'css' | 'decimal',
  includeAlpha: boolean
}): string | number[]
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
faker.color.human(): string
faker.color.human() // 'red'

hwb

Returns an HWB color.

Available since v7.0.0

Parameters

NameTypeDefaultDescription
options?{ ... }

Options object.

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

Format of generated RGB color.

Returns: number[] | string

ts
faker.color.hwb(options?: {
  format: 'binary' | 'css' | 'decimal'
}): string | number[]
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?'binary' | 'css' | 'decimal''decimal'

Format of generated RGB color.

Returns: number[] | string

ts
faker.color.lab(options?: {
  format: 'binary' | 'css' | 'decimal'
}): string | number[]
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?'binary' | 'css' | 'decimal''decimal'

Format of generated RGB color.

Returns: number[] | string

ts
faker.color.lch(options?: {
  format: 'binary' | 'css' | 'decimal'
}): string | number[]
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?'lower' | 'mixed' | 'upper''lower'

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

options.format?'binary' | 'css' | '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: number[] | string

ts
faker.color.rgb(options?: {
  casing: 'lower' | 'mixed' | 'upper',
  format: 'binary' | 'css' | 'decimal' | 'hex',
  includeAlpha: boolean,
  prefix: string
} = {}): string | number[]
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
faker.color.space(): string
faker.color.space() // 'sRGB'

Released under the MIT License.