Skip to content

Commit

Permalink
feat: provide enums for color values (#1910)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Mar 14, 2023
1 parent b584038 commit a001090
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 41 deletions.
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ export * as allLocales from './locales';
export { Aircraft, AircraftType } from './modules/airline';
export type { AirlineModule } from './modules/airline';
export type { AnimalModule } from './modules/animal';
export { CssFunction, CssSpace } from './modules/color';
export type {
Casing,
ColorFormat,
ColorModule,
CSSFunction,
CSSSpace,
/** @deprecated Use CssFunctionType instead */
CssFunctionType as CSSFunction,
CssFunctionType,
/** @deprecated Use CssSpaceType instead */
CssSpaceType as CSSSpace,
CssSpaceType,
NumberColorFormat,
StringColorFormat,
} from './modules/color';
Expand Down
73 changes: 40 additions & 33 deletions src/modules/color/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -893,7 +900,7 @@ export class ColorModule {
*
* @default 'sRGB'
*/
space?: CSSSpace;
space?: CssSpaceType;
}): string;
/**
* Returns a random color based on CSS color space specified.
Expand All @@ -920,7 +927,7 @@ export class ColorModule {
*
* @default 'sRGB'
*/
space?: CSSSpace;
space?: CssSpaceType;
}): number[];
/**
* Returns a random color based on CSS color space specified.
Expand Down Expand Up @@ -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' };
Expand Down
4 changes: 2 additions & 2 deletions test/__snapshots__/color.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exports[`color > 42 > colorByCSSColorSpace 1`] = `

exports[`color > 42 > cssSupportedFunction 1`] = `"hsla"`;

exports[`color > 42 > cssSupportedSpace 1`] = `"rec2020"`;
exports[`color > 42 > cssSupportedSpace 1`] = `"display-p3"`;

exports[`color > 42 > hsl 1`] = `
[
Expand Down Expand Up @@ -78,7 +78,7 @@ exports[`color > 1211 > colorByCSSColorSpace 1`] = `

exports[`color > 1211 > cssSupportedFunction 1`] = `"color"`;

exports[`color > 1211 > cssSupportedSpace 1`] = `"rec2020"`;
exports[`color > 1211 > cssSupportedSpace 1`] = `"prophoto-rgb"`;

exports[`color > 1211 > hsl 1`] = `
[
Expand Down
7 changes: 3 additions & 4 deletions test/color.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it } from 'vitest';
import { faker } from '../src';
import { CSS_FUNCTIONS, CSS_SPACES } from '../src/modules/color';
import { CssFunction, CssSpace, faker } from '../src';
import { seededTests } from './support/seededRuns';

const NON_SEEDED_BASED_RUN = 5;
Expand Down Expand Up @@ -44,14 +43,14 @@ describe('color', () => {
describe(`cssSupportedFunction()`, () => {
it('should return random css supported color function from css functions array', () => {
const func = faker.color.cssSupportedFunction();
expect(CSS_FUNCTIONS).toContain(func);
expect(Object.values(CssFunction)).toContain(func);
});
});

describe(`cssSupportedSpace()`, () => {
it('should return random css supported color space from css spaces array', () => {
const space = faker.color.cssSupportedSpace();
expect(CSS_SPACES).toContain(space);
expect(Object.values(CssSpace)).toContain(space);
});
});

Expand Down

0 comments on commit a001090

Please sign in to comment.