Skip to content

Commit 0d73928

Browse files
author
Ibrahim Odev
committed
[feat] update methods fixing
1 parent 7803278 commit 0d73928

File tree

4 files changed

+116
-67
lines changed

4 files changed

+116
-67
lines changed

src/colorResult.ts

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,57 @@
11
import chroma from 'chroma-js';
22
import type { IColorResultOptions } from './types';
33

4-
const darkenValue = (shade: number, mainShade: number) => {
4+
/**
5+
* Calculates the darkening value based on the provided shade and mainShade.
6+
* @param shade - The shade value to darken.
7+
* @param mainShade - The main shade value.
8+
* @returns The darkening value.
9+
*/
10+
const calculateDarkenValue = (shade: number, mainShade: number): number => {
511
return (shade - mainShade) / 100 / 2;
612
};
7-
const shadeColor = (primaryColor: string, mainShade: number, shade: number) => {
8-
return chroma(primaryColor).darken(darkenValue(shade, mainShade)).hex();
13+
14+
/**
15+
* Shades the primary color based on the provided shade and mainShade.
16+
* @param primaryColor - The primary color.
17+
* @param mainShade - The main shade value.
18+
* @param shade - The shade value.
19+
* @returns The shaded color in hexadecimal format.
20+
*/
21+
const shadeColor = (primaryColor: string, mainShade: number, shade: number): string => {
22+
return chroma(primaryColor).darken(calculateDarkenValue(shade, mainShade)).hex();
923
};
10-
const shadeColorResult = (fn: any, options: IColorResultOptions) => {
11-
return options.shades.reduce((acc: any, shade: number) => {
12-
acc[shade] = fn(options.primaryColor, options.mainShade, shade);
24+
25+
/**
26+
* Generates the color palette based on the provided options.
27+
* @param fn - The function to apply shade to the primary color.
28+
* @param options - The color result options.
29+
* @returns The generated color palette.
30+
*/
31+
const generateColorPalette = (
32+
fn: (primaryColor: string, mainShade: number, shade: number) => string,
33+
options: IColorResultOptions,
34+
): Record<string, string> => {
35+
return options.shades.reduce((acc: Record<string, string>, shade: number) => {
36+
acc[String(shade)] = fn(options.primaryColor, options.mainShade, shade);
1337
return acc;
1438
}, {});
1539
};
16-
const colorResult = (options: IColorResultOptions) => {
17-
const palette = shadeColorResult(shadeColor, options);
18-
const colorPalette = {
19-
...palette,
20-
DEFAULT: options.primaryColor,
21-
};
22-
return Object.freeze(colorPalette) ?? {};
40+
41+
/**
42+
* Generates the final color result based on the provided options.
43+
* @param options - The color result options.
44+
* @returns The final color result.
45+
*/
46+
const colorResult = (options: IColorResultOptions): Record<string, string> => {
47+
try {
48+
const palette = generateColorPalette(shadeColor, options);
49+
palette.DEFAULT = options.primaryColor;
50+
return Object.freeze(palette);
51+
} catch (error) {
52+
console.error('Error generating color result:', error);
53+
return Object.create(null); // Return an empty object in case of error.
54+
}
2355
};
2456

2557
export default colorResult;

src/helpers.ts

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
11
import type { IColorResultOptions, Palette } from './types';
2+
import { isColor } from './utils';
23

34
export const initalOptions: IColorResultOptions = {
45
mainShade: 500,
56
primaryColor: '#FFBD00',
67
shades: [50, 100, 200, 300, 400, 500, 600, 700, 800, 900],
78
};
89

9-
export const isColor = (color: string) => {
10-
const reg = /^#([\da-f]{3}){1,2}$|^#([\da-f]{6}){1,2}$|(rgb|hsl)a?\((\s*-?\d+%?\s*,){2}(\s*-?\d+%?\s*,?\s*\)?)(,\s*(0?\.\d+)?|1)?\)/gim;
11-
if (typeof color === 'string' && reg.test(color)) {
12-
return true;
13-
} else {
14-
return false;
10+
export const checkParam = (palette: Palette): boolean => {
11+
const { color, name, shade, shades } = palette;
12+
13+
if (!color || typeof color !== 'string' || !isColor(color)) {
14+
throw new Error(`Invalid color: '${color}'. Please provide a valid color.`);
15+
}
16+
17+
if (!name || typeof name !== 'string') {
18+
throw new Error(`Invalid name: '${name}'. Please provide a valid name.`);
19+
}
20+
21+
if (shade && typeof shade !== 'number') {
22+
throw new Error(`Invalid shade value: '${shade}'. Shade must be a number.`);
1523
}
16-
};
1724

18-
export const checkParam = (palette: Palette) => {
19-
if (palette.color && typeof palette.color === 'string' && palette.name && typeof palette.name == 'string') {
20-
if (!isColor(palette.color)) {
21-
throw new Error(
22-
`'${palette.color}' The value you entered is not a color. e.g #ffbd00 or #ffb or rgba(255, 189, 0, 1) or rgb(255, 189, 0) or hsl(44, 100%, 50%)`,
23-
);
24-
} else if (!palette.shade && palette.shades) {
25-
throw new Error(`If you want to specify the shades, you have to specify the main shade.`);
26-
} else if (palette.shade && typeof palette.shade !== 'number') {
27-
throw new Error(`'${palette.shade}' - type: ${typeof palette.shade} It must be of type number.`);
28-
} else if (palette.shades && !Array.isArray(palette.shades)) {
29-
throw new Error(`Shades are not array.`);
30-
} else if (palette.shades && palette.shades.length <= 2) {
31-
throw new Error(`Shades can consist of at least 3 elements.`);
32-
} else if (palette.shade && palette.shades && !palette.shades.includes(palette.shade)) {
33-
throw new Error(`'${palette.shade}' mainShade are not included in the your shades.`);
34-
} else if (!palette.shades && palette.shade && !initalOptions.shades.includes(palette.shade)) {
35-
throw new Error(`'${palette.shade}' mainShade can only be 50, 100, 200, 300, 400, 500, 600, 700, 800 or 900.`);
36-
} else {
37-
return true;
25+
if (shades) {
26+
if (!Array.isArray(shades)) {
27+
throw new Error(`Invalid shades: '${shades}'. Shades must be an array.`);
28+
}
29+
30+
if (shades.length < 3) {
31+
throw new Error('Shades array must contain at least 3 elements.');
32+
}
33+
34+
if (shade && !shades.includes(shade)) {
35+
throw new Error(`Main shade '${shade}' is not included in the shades array.`);
3836
}
3937
} else {
40-
throw new Error('Make sure the required data is included.');
38+
if (shade && ![50, 100, 200, 300, 400, 500, 600, 700, 800, 900].includes(shade)) {
39+
throw new Error(`Main shade '${shade}' must be one of: 50, 100, 200, 300, 400, 500, 600, 700, 800, 900.`);
40+
}
4141
}
42+
43+
return true;
4244
};

src/main.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,48 @@ import colorResult from './colorResult';
33
import { checkParam, initalOptions } from './helpers';
44
import type { IColorResultOptions, Palette } from './types';
55

6-
const getPalette = (params: Palette[] | Palette | string): any => {
7-
const palette: any = {};
6+
const getPalette = (params: Palette[] | Palette | string): Record<string, Record<string, string>> => {
7+
const palette: Record<string, Record<string, string>> = {};
8+
9+
const generateColorPalette = (options: IColorResultOptions): Record<string, string> => {
10+
return colorResult(options);
11+
};
12+
13+
const getColorOptions = (colorPalette: Palette): IColorResultOptions => {
14+
const { color, shade, shades } = colorPalette;
15+
16+
return {
17+
mainShade: shade ?? initalOptions.mainShade,
18+
primaryColor: chroma(color).hex() ?? initalOptions.primaryColor,
19+
shades: shades ?? initalOptions.shades,
20+
};
21+
};
22+
23+
const addPalette = (name: string, options: IColorResultOptions) => {
24+
palette[name] = generateColorPalette(options);
25+
};
26+
827
if (Array.isArray(params)) {
9-
for (let i = 0; i < params.length; i++) {
10-
const colorPalette = params[i];
28+
for (const colorPalette of params) {
1129
if (checkParam(colorPalette)) {
12-
const options: IColorResultOptions = {
13-
mainShade: colorPalette.shade ?? initalOptions.mainShade,
14-
primaryColor: chroma(colorPalette.color).hex() ?? initalOptions.primaryColor,
15-
shades: colorPalette.shades ?? initalOptions.shades,
16-
};
17-
18-
palette[colorPalette.name] = colorResult(options);
30+
const options = getColorOptions(colorPalette);
31+
addPalette(colorPalette.name, options);
1932
}
2033
}
2134
} else if (typeof params !== 'string' && !Array.isArray(params)) {
2235
if (checkParam(params)) {
23-
const options: IColorResultOptions = {
24-
mainShade: params.shade ?? initalOptions.mainShade,
25-
primaryColor: chroma(params.color).hex() ?? initalOptions.primaryColor,
26-
shades: params.shades ?? initalOptions.shades,
27-
};
28-
29-
palette[params.name] = colorResult(options);
36+
const options = getColorOptions(params);
37+
addPalette(params.name, options);
3038
}
3139
} else if (typeof params === 'string') {
32-
let options: IColorResultOptions = {
40+
const options: IColorResultOptions = {
3341
mainShade: initalOptions.mainShade,
34-
primaryColor: initalOptions.primaryColor,
42+
primaryColor: chroma(params).hex(),
3543
shades: initalOptions.shades,
3644
};
37-
38-
options = Object.assign(initalOptions, {
39-
primaryColor: chroma(params).hex(),
40-
});
41-
42-
palette['primary'] = colorResult(options);
45+
addPalette('primary', options);
4346
}
47+
4448
return palette;
4549
};
4650

src/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const isColor = (color: string) => {
2+
const reg = new RegExp(
3+
/^#([\da-f]{3}){1,2}$|^#([\da-f]{6}){1,2}$|(rgb|hsl)a?\((\s*-?\d+%?\s*,){2}(\s*-?\d+%?\s*,?\s*\)?)(,\s*(0?\.\d+)?|1)?\)/,
4+
'gim',
5+
);
6+
if (typeof color === 'string' && reg.test(color)) {
7+
return true;
8+
}
9+
10+
return false;
11+
};

0 commit comments

Comments
 (0)