@@ -3,32 +3,39 @@ import type { Faker } from '../../faker';
33/**
44 * Color space names supported by CSS.
55 */
6- export const CSS_SPACES = [
7- 'sRGB' ,
8- 'display-p3' ,
9- 'rec2020' ,
10- 'a98-rgb' ,
11- 'prophoto-rgb' ,
12- 'rec2020' ,
13- ] as const ;
6+ export enum CssSpace {
7+ SRGB = 'sRGB' ,
8+ DisplayP3 = 'display-p3' ,
9+ REC2020 = 'rec2020' ,
10+ A98RGB = 'a98-rgb' ,
11+ ProphotoRGB = 'prophoto-rgb' ,
12+ }
13+
14+ /**
15+ * Color space names supported by CSS.
16+ */
17+ export type CssSpaceType = `${CssSpace } `;
1418
1519/**
1620 * Functions supported by CSS to produce color.
1721 */
18- export const CSS_FUNCTIONS = [
19- 'rgb' ,
20- 'rgba' ,
21- 'hsl' ,
22- 'hsla' ,
23- 'hwb' ,
24- 'cmyk' ,
25- 'lab' ,
26- 'lch' ,
27- 'color' ,
28- ] as const ;
29-
30- export type CSSFunction = ( typeof CSS_FUNCTIONS ) [ number ] ;
31- export type CSSSpace = ( typeof CSS_SPACES ) [ number ] ;
22+ export enum CssFunction {
23+ RGB = 'rgb' ,
24+ RGBA = 'rgba' ,
25+ HSL = 'hsl' ,
26+ HSLA = 'hsla' ,
27+ HWB = 'hwb' ,
28+ CMYK = 'cmyk' ,
29+ LAB = 'lab' ,
30+ LCH = 'lch' ,
31+ COLOR = 'color' ,
32+ }
33+
34+ /**
35+ * Functions supported by CSS to produce color.
36+ */
37+ export type CssFunctionType = `${CssFunction } `;
38+
3239export type StringColorFormat = 'css' | 'binary' ;
3340export type NumberColorFormat = 'decimal' ;
3441export type ColorFormat = StringColorFormat | NumberColorFormat ;
@@ -95,8 +102,8 @@ function toBinary(values: number[]): string {
95102 */
96103function toCSS (
97104 values : number [ ] ,
98- cssFunction : CSSFunction = 'rgb' ,
99- space : CSSSpace = 'sRGB'
105+ cssFunction : CssFunctionType = 'rgb' ,
106+ space : CssSpaceType = 'sRGB'
100107) : string {
101108 const percentage = ( value : number ) => Math . round ( value * 100 ) ;
102109 switch ( cssFunction ) {
@@ -141,8 +148,8 @@ function toCSS(
141148function toColorFormat (
142149 values : number [ ] ,
143150 format : ColorFormat ,
144- cssFunction : CSSFunction = 'rgb' ,
145- space : CSSSpace = 'sRGB'
151+ cssFunction : CssFunctionType = 'rgb' ,
152+ space : CssSpaceType = 'sRGB'
146153) : string | number [ ] {
147154 switch ( format ) {
148155 case 'css' :
@@ -205,7 +212,7 @@ export class ColorModule {
205212 * @since 7.0.0
206213 */
207214 cssSupportedFunction ( ) : string {
208- return this . faker . helpers . arrayElement ( CSS_FUNCTIONS ) ;
215+ return this . faker . helpers . objectValue ( CssFunction ) ;
209216 }
210217
211218 /**
@@ -217,7 +224,7 @@ export class ColorModule {
217224 * @since 7.0.0
218225 */
219226 cssSupportedSpace ( ) : string {
220- return this . faker . helpers . arrayElement ( CSS_SPACES ) ;
227+ return this . faker . helpers . objectValue ( CssSpace ) ;
221228 }
222229
223230 /**
@@ -367,7 +374,7 @@ export class ColorModule {
367374 } = options || { } ;
368375 options = { format, includeAlpha, prefix, casing } ;
369376 let color : string | number [ ] ;
370- let cssFunction : CSSFunction = 'rgb' ;
377+ let cssFunction : CssFunctionType = 'rgb' ;
371378 if ( format === 'hex' ) {
372379 color = this . faker . string . hexadecimal ( {
373380 length : includeAlpha ? 8 : 6 ,
@@ -893,7 +900,7 @@ export class ColorModule {
893900 *
894901 * @default 'sRGB'
895902 */
896- space ?: CSSSpace ;
903+ space ?: CssSpaceType ;
897904 } ) : string ;
898905 /**
899906 * Returns a random color based on CSS color space specified.
@@ -920,7 +927,7 @@ export class ColorModule {
920927 *
921928 * @default 'sRGB'
922929 */
923- space ?: CSSSpace ;
930+ space ?: CssSpaceType ;
924931 } ) : number [ ] ;
925932 /**
926933 * Returns a random color based on CSS color space specified.
@@ -949,11 +956,11 @@ export class ColorModule {
949956 *
950957 * @default 'sRGB'
951958 */
952- space ?: CSSSpace ;
959+ space ?: CssSpaceType ;
953960 } ) : string | number [ ] ;
954961 colorByCSSColorSpace ( options ?: {
955962 format ?: ColorFormat ;
956- space ?: CSSSpace ;
963+ space ?: CssSpaceType ;
957964 } ) : string | number [ ] {
958965 if ( options ?. format === 'css' && ! options ?. space ) {
959966 options = { ...options , space : 'sRGB' } ;
0 commit comments