diff options
| author | Shinigami <[email protected]> | 2023-03-14 18:37:05 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-03-14 17:37:05 +0000 |
| commit | a0010900205ed8b1d1ba2adefe222c1cb41c1a70 (patch) | |
| tree | ca92e04bda028b7ab2d192287505404550b0c393 /src/modules | |
| parent | b584038985a8e7cf6fcacb7ce94a38454c23a95a (diff) | |
| download | faker-a0010900205ed8b1d1ba2adefe222c1cb41c1a70.tar.xz faker-a0010900205ed8b1d1ba2adefe222c1cb41c1a70.zip | |
feat: provide enums for color values (#1910)
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/color/index.ts | 73 |
1 files changed, 40 insertions, 33 deletions
diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index 67fa9b9a..5100776a 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -3,32 +3,39 @@ import type { Faker } from '../../faker'; /** * Color space names supported by CSS. */ -export const CSS_SPACES = [ - 'sRGB', - 'display-p3', - 'rec2020', - 'a98-rgb', - 'prophoto-rgb', - 'rec2020', -] as const; +export enum CssSpace { + SRGB = 'sRGB', + DisplayP3 = 'display-p3', + REC2020 = 'rec2020', + A98RGB = 'a98-rgb', + ProphotoRGB = 'prophoto-rgb', +} + +/** + * Color space names supported by CSS. + */ +export type CssSpaceType = `${CssSpace}`; /** * Functions supported by CSS to produce color. */ -export const CSS_FUNCTIONS = [ - 'rgb', - 'rgba', - 'hsl', - 'hsla', - 'hwb', - 'cmyk', - 'lab', - 'lch', - 'color', -] as const; - -export type CSSFunction = (typeof CSS_FUNCTIONS)[number]; -export type CSSSpace = (typeof CSS_SPACES)[number]; +export enum CssFunction { + RGB = 'rgb', + RGBA = 'rgba', + HSL = 'hsl', + HSLA = 'hsla', + HWB = 'hwb', + CMYK = 'cmyk', + LAB = 'lab', + LCH = 'lch', + COLOR = 'color', +} + +/** + * Functions supported by CSS to produce color. + */ +export type CssFunctionType = `${CssFunction}`; + export type StringColorFormat = 'css' | 'binary'; export type NumberColorFormat = 'decimal'; export type ColorFormat = StringColorFormat | NumberColorFormat; @@ -95,8 +102,8 @@ function toBinary(values: number[]): string { */ function toCSS( values: number[], - cssFunction: CSSFunction = 'rgb', - space: CSSSpace = 'sRGB' + cssFunction: CssFunctionType = 'rgb', + space: CssSpaceType = 'sRGB' ): string { const percentage = (value: number) => Math.round(value * 100); switch (cssFunction) { @@ -141,8 +148,8 @@ function toCSS( function toColorFormat( values: number[], format: ColorFormat, - cssFunction: CSSFunction = 'rgb', - space: CSSSpace = 'sRGB' + cssFunction: CssFunctionType = 'rgb', + space: CssSpaceType = 'sRGB' ): string | number[] { switch (format) { case 'css': @@ -205,7 +212,7 @@ export class ColorModule { * @since 7.0.0 */ cssSupportedFunction(): string { - return this.faker.helpers.arrayElement(CSS_FUNCTIONS); + return this.faker.helpers.objectValue(CssFunction); } /** @@ -217,7 +224,7 @@ export class ColorModule { * @since 7.0.0 */ cssSupportedSpace(): string { - return this.faker.helpers.arrayElement(CSS_SPACES); + return this.faker.helpers.objectValue(CssSpace); } /** @@ -367,7 +374,7 @@ export class ColorModule { } = options || {}; options = { format, includeAlpha, prefix, casing }; let color: string | number[]; - let cssFunction: CSSFunction = 'rgb'; + let cssFunction: CssFunctionType = 'rgb'; if (format === 'hex') { color = this.faker.string.hexadecimal({ length: includeAlpha ? 8 : 6, @@ -893,7 +900,7 @@ export class ColorModule { * * @default 'sRGB' */ - space?: CSSSpace; + space?: CssSpaceType; }): string; /** * Returns a random color based on CSS color space specified. @@ -920,7 +927,7 @@ export class ColorModule { * * @default 'sRGB' */ - space?: CSSSpace; + space?: CssSpaceType; }): number[]; /** * Returns a random color based on CSS color space specified. @@ -949,11 +956,11 @@ export class ColorModule { * * @default 'sRGB' */ - space?: CSSSpace; + space?: CssSpaceType; }): string | number[]; colorByCSSColorSpace(options?: { format?: ColorFormat; - space?: CSSSpace; + space?: CssSpaceType; }): string | number[] { if (options?.format === 'css' && !options?.space) { options = { ...options, space: 'sRGB' }; |
