@@ -3,32 +3,39 @@ import type { Faker } from '../../faker';
3
3
/**
4
4
* Color space names supported by CSS.
5
5
*/
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 } `;
14
18
15
19
/**
16
20
* Functions supported by CSS to produce color.
17
21
*/
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
+
32
39
export type StringColorFormat = 'css' | 'binary' ;
33
40
export type NumberColorFormat = 'decimal' ;
34
41
export type ColorFormat = StringColorFormat | NumberColorFormat ;
@@ -95,8 +102,8 @@ function toBinary(values: number[]): string {
95
102
*/
96
103
function toCSS (
97
104
values : number [ ] ,
98
- cssFunction : CSSFunction = 'rgb' ,
99
- space : CSSSpace = 'sRGB'
105
+ cssFunction : CssFunctionType = 'rgb' ,
106
+ space : CssSpaceType = 'sRGB'
100
107
) : string {
101
108
const percentage = ( value : number ) => Math . round ( value * 100 ) ;
102
109
switch ( cssFunction ) {
@@ -141,8 +148,8 @@ function toCSS(
141
148
function toColorFormat (
142
149
values : number [ ] ,
143
150
format : ColorFormat ,
144
- cssFunction : CSSFunction = 'rgb' ,
145
- space : CSSSpace = 'sRGB'
151
+ cssFunction : CssFunctionType = 'rgb' ,
152
+ space : CssSpaceType = 'sRGB'
146
153
) : string | number [ ] {
147
154
switch ( format ) {
148
155
case 'css' :
@@ -205,7 +212,7 @@ export class ColorModule {
205
212
* @since 7.0.0
206
213
*/
207
214
cssSupportedFunction ( ) : string {
208
- return this . faker . helpers . arrayElement ( CSS_FUNCTIONS ) ;
215
+ return this . faker . helpers . objectValue ( CssFunction ) ;
209
216
}
210
217
211
218
/**
@@ -217,7 +224,7 @@ export class ColorModule {
217
224
* @since 7.0.0
218
225
*/
219
226
cssSupportedSpace ( ) : string {
220
- return this . faker . helpers . arrayElement ( CSS_SPACES ) ;
227
+ return this . faker . helpers . objectValue ( CssSpace ) ;
221
228
}
222
229
223
230
/**
@@ -367,7 +374,7 @@ export class ColorModule {
367
374
} = options || { } ;
368
375
options = { format, includeAlpha, prefix, casing } ;
369
376
let color : string | number [ ] ;
370
- let cssFunction : CSSFunction = 'rgb' ;
377
+ let cssFunction : CssFunctionType = 'rgb' ;
371
378
if ( format === 'hex' ) {
372
379
color = this . faker . string . hexadecimal ( {
373
380
length : includeAlpha ? 8 : 6 ,
@@ -893,7 +900,7 @@ export class ColorModule {
893
900
*
894
901
* @default 'sRGB'
895
902
*/
896
- space ?: CSSSpace ;
903
+ space ?: CssSpaceType ;
897
904
} ) : string ;
898
905
/**
899
906
* Returns a random color based on CSS color space specified.
@@ -920,7 +927,7 @@ export class ColorModule {
920
927
*
921
928
* @default 'sRGB'
922
929
*/
923
- space ?: CSSSpace ;
930
+ space ?: CssSpaceType ;
924
931
} ) : number [ ] ;
925
932
/**
926
933
* Returns a random color based on CSS color space specified.
@@ -949,11 +956,11 @@ export class ColorModule {
949
956
*
950
957
* @default 'sRGB'
951
958
*/
952
- space ?: CSSSpace ;
959
+ space ?: CssSpaceType ;
953
960
} ) : string | number [ ] ;
954
961
colorByCSSColorSpace ( options ?: {
955
962
format ?: ColorFormat ;
956
- space ?: CSSSpace ;
963
+ space ?: CssSpaceType ;
957
964
} ) : string | number [ ] {
958
965
if ( options ?. format === 'css' && ! options ?. space ) {
959
966
options = { ...options , space : 'sRGB' } ;
0 commit comments