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
Name | Type | Default | Description |
---|---|---|---|
options? | { ... } | {} | Options object. |
options.format? | ColorFormat | 'decimal' | Format of generated CMYK color. |
Returns: string | number[]
function cmyk(options?: { format?: ColorFormat }): string | number[];
Examples
faker.color.cmyk() // [ 0.55, 0.72, 0.6, 0.55 ]
faker.color.cmyk({ format: 'decimal' }) // [ 0.42, 0.65, 0.44, 0.9 ]
faker.color.cmyk({ format: 'css' }) // 'cmyk(97%, 38%, 79%, 53%)'
faker.color.cmyk({ format: 'binary' }) // '00111111000100011110101110000101 00111111011011100001010001111011 00111101100011110101110000101001 00111101101000111101011100001010'
Source
colorByCSSColorSpace
Returns a random color based on CSS color space specified.
Available since v7.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
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[]
function colorByCSSColorSpace(options?: {
format?: ColorFormat;
space?: CssSpaceType;
}): string | number[];
Examples
faker.color.colorByCSSColorSpace() // [ 0.5488, 0.7152, 0.6028 ]
faker.color.colorByCSSColorSpace({ format: 'decimal' }) // [ 0.5449, 0.4236, 0.6459 ]
faker.color.colorByCSSColorSpace({ format: 'css', space: 'display-p3' }) // 'color(display-p3 0.4376 0.8918 0.9637)'
faker.color.colorByCSSColorSpace({ format: 'binary' }) // '00111110110001000100110100000001 00111111010010101011001101101000 00111111000001110110010111111110'
Source
cssSupportedFunction
Returns a random css supported color function name.
Available since v7.0.0
Returns: 'rgb' | 'rgba' | 'hsl' | 'hsla' | 'hwb' | 'cmyk' | 'lab' | 'lch' | 'color'
function cssSupportedFunction(): CssFunctionType;
Examples
faker.color.cssSupportedFunction() // 'hwb'
Source
cssSupportedSpace
Returns a random css supported color space name.
Available since v7.0.0
Returns: 'sRGB' | 'display-p3' | 'rec2020' | 'a98-rgb' | 'prophoto-rgb'
function cssSupportedSpace(): CssSpaceType;
Examples
faker.color.cssSupportedSpace() // 'rec2020'
Source
hsl
Returns an HSL color.
Available since v7.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options? | { ... } | {} | Options object. |
options.format? | ColorFormat | 'decimal' | Format of generated HSL color. |
options.includeAlpha? | boolean | false | Adds an alpha value to the color (RGBA). |
Returns: string | number[]
function hsl(options?: {
format?: ColorFormat;
includeAlpha?: boolean;
}): string | number[];
Examples
faker.color.hsl() // [ 198, 0.72, 0.6 ]
faker.color.hsl({ format: 'decimal' }) // [ 196, 0.42, 0.65 ]
faker.color.hsl({ format: 'decimal', includeAlpha: true }) // [ 157, 0.9, 0.97, 0.38 ]
faker.color.hsl({ format: 'css' }) // 'hsl(285deg 53% 57%)'
faker.color.hsl({ format: 'css', includeAlpha: true }) // 'hsl(334deg 7% 8% / 2)'
faker.color.hsl({ format: 'binary' }) // '100101100 00111111010001111010111000010100 00111111010111101011100001010010'
faker.color.hsl({ format: 'binary', includeAlpha: true }) // '101100001 00111111010011001100110011001101 00111110111010111000010100011111 00111111010001111010111000010100'
Source
human
Returns a random human-readable color name.
Available since v7.0.0
Returns: string
function human(): string;
Examples
faker.color.human() // 'orchid'
Source
hwb
Returns an HWB color.
Available since v7.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options? | { ... } | {} | Options object. |
options.format? | ColorFormat | 'decimal' | Format of generated RGB color. |
Returns: string | number[]
function hwb(options?: { format?: ColorFormat }): string | number[];
Examples
faker.color.hwb() // [ 198, 0.72, 0.6 ]
faker.color.hwb({ format: 'decimal' }) // [ 196, 0.42, 0.65 ]
faker.color.hwb({ format: 'css' }) // 'hwb(157 90% 97%)'
faker.color.hwb({ format: 'binary' }) // '10001010 00111111010010100011110101110001 00111111000001111010111000010100'
Source
lab
Returns a LAB (CIELAB) color.
Available since v7.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options? | { ... } | {} | Options object. |
options.format? | ColorFormat | 'decimal' | Format of generated RGB color. |
Returns: string | number[]
function lab(options?: { format?: ColorFormat }): string | number[];
Examples
faker.color.lab() // [ 0.548814, 43.0379, 20.5527 ]
faker.color.lab({ format: 'decimal' }) // [ 0.544883, -15.269, 29.1788 ]
faker.color.lab({ format: 'css' }) // 'lab(44% 78.3546 92.7326)'
faker.color.lab({ format: 'binary' }) // '00111110110001000101001001100001 01000010011010010110000101001000 01000000101110001110110110010001'
Source
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
Name | Type | Default | Description |
---|---|---|---|
options? | { ... } | {} | Options object. |
options.format? | ColorFormat | 'decimal' | Format of generated RGB color. |
Returns: string | number[]
function lch(options?: { format?: ColorFormat }): string | number[];
Examples
faker.color.lch() // [ 0.548814, 164.5, 138.6 ]
faker.color.lch({ format: 'decimal' }) // [ 0.544883, 97.4, 148.6 ]
faker.color.lch({ format: 'css' }) // 'lch(44% 205.1 221.7)'
faker.color.lch({ format: 'binary' }) // '00111110110001000101001001100001 01000011001101100001100110011010 01000010111100110011001100110011'
Source
rgb
Returns an RGB color.
Available since v7.0.0
Parameters
Name | Type | Default | Description |
---|---|---|---|
options? | { ... } | {} | Options object. |
options.casing? | Casing | 'lower' | Letter type case of the generated hex color. Only applied when |
options.format? | 'css' | 'binary' | 'decimal' | 'hex' | 'hex' | Format of generated RGB color. |
options.includeAlpha? | boolean | false | Adds an alpha value to the color (RGBA). |
options.prefix? | string | '#' | Prefix of the generated hex color. Only applied when |
Returns: string | number[]
function rgb(options?: {
prefix?: string;
casing?: Casing;
format?: 'hex' | ColorFormat;
includeAlpha?: boolean;
}): string | number[];
Examples
faker.color.rgb() // '#cfdb9e'
faker.color.rgb({ prefix: '0x' }) // '0x9df8bb'
faker.color.rgb({ casing: 'upper' }) // '#CE110C'
faker.color.rgb({ casing: 'lower' }) // '#bdfbab'
faker.color.rgb({ prefix: '#', casing: 'lower' }) // '#2e3eb9'
faker.color.rgb({ format: 'hex', casing: 'lower' }) // '#5bac0d'
faker.color.rgb({ format: 'decimal' }) // [ 156, 157, 241 ]
faker.color.rgb({ format: 'css' }) // 'rgb(174, 92, 111)'
faker.color.rgb({ format: 'binary' }) // '10110010 00001111 10101010'
faker.color.rgb({ includeAlpha: true }) // '#e4268c9f'
faker.color.rgb({ format: 'css', includeAlpha: true }) // 'rgba(26, 53, 41, 0.65)'
faker.color.rgb({ format: 'decimal', includeAlpha: true }) // [ 64, 119, 62, 0.16 ]
Source
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
function space(): string;
Examples
faker.color.space() // 'Natural Color System (NSC)'