|
1 | 1 | import chroma from "chroma-js"; |
| 2 | +import type { IColorResultOptions, Palette } from "./types"; |
| 3 | +import colorResult from "./colorResult"; |
| 4 | +import { checkParam, initalOptions } from "./helpers"; |
2 | 5 |
|
3 | | -export interface Palette { |
4 | | - name: string; |
5 | | - color: string; |
6 | | - shade?: number; |
7 | | - shades?: number[]; |
8 | | -} |
9 | | - |
10 | | -const main = { |
11 | | - state: { |
12 | | - initialParams: { |
13 | | - color: "#FFBD00", |
14 | | - name: "primary", |
15 | | - shade: 500, |
16 | | - shades: [50, 100, 200, 300, 400, 500, 600, 700, 800, 900] |
17 | | - }, |
18 | | - primaryColor: "#FFBD00", |
19 | | - shades: [50, 100, 200, 300, 400, 500, 600, 700, 800, 900], |
20 | | - mainShade: 500 |
21 | | - }, |
22 | | - getters: { |
23 | | - darkenValue: (shade: number) => { |
24 | | - return (shade - main.state.mainShade) / 100 / 2; |
25 | | - }, |
26 | | - shadeColor: (shade: number) => { |
27 | | - return chroma(main.state.primaryColor) |
28 | | - .darken(main.getters.darkenValue(shade)) |
29 | | - .hex(); |
30 | | - }, |
31 | | - shadeColorResult: (fn: any) => { |
32 | | - return main.state.shades.reduce((acc: any, shade: number) => { |
33 | | - acc[shade] = fn(shade); |
34 | | - return acc; |
35 | | - }, {}); |
36 | | - }, |
37 | | - colorResult: () => { |
38 | | - const palette = main.getters.shadeColorResult(main.getters.shadeColor); |
39 | | - const DEFAULT = main.getters.shadeColor(main.state.mainShade); |
40 | | - const colorPalette = { |
41 | | - ...palette, |
42 | | - DEFAULT: DEFAULT |
43 | | - }; |
44 | | - return Object.freeze(colorPalette) ?? {}; |
45 | | - } |
46 | | - }, |
47 | | - checks: { |
48 | | - checkParam: (palette: Palette) => { |
49 | | - if ( |
50 | | - palette.color && |
51 | | - typeof palette.color === "string" && |
52 | | - palette.name && |
53 | | - typeof palette.name == "string" |
54 | | - ) { |
55 | | - if (!main.checks.checkColor(palette.color)) { |
56 | | - throw new Error( |
57 | | - `'${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%)` |
58 | | - ); |
59 | | - } else if (!palette.shade && palette.shades) { |
60 | | - throw new Error( |
61 | | - `If you want to specify the shades, you have to specify the main shade.` |
62 | | - ); |
63 | | - } else if (palette.shade && typeof palette.shade !== "number") { |
64 | | - throw new Error( |
65 | | - `'${palette.shade}' - type: ${typeof palette.shade} It must be of type number.` |
66 | | - ); |
67 | | - } else if ( |
68 | | - palette.shades && |
69 | | - !Array.isArray(palette.shades) |
70 | | - ) { |
71 | | - throw new Error(`Shades are not array.`); |
72 | | - } else if (palette.shades && palette.shades.length <= 2) { |
73 | | - throw new Error(`Shades can consist of at least 3 elements.`); |
74 | | - } else if ( |
75 | | - palette.shade && |
76 | | - palette.shades && |
77 | | - !palette.shades.includes(palette.shade) |
78 | | - ) { |
79 | | - throw new Error( |
80 | | - `'${palette.shade}' mainShade are not included in the your shades.` |
81 | | - ); |
82 | | - } else if ( |
83 | | - !palette.shades && |
84 | | - palette.shade && |
85 | | - !main.state.initialParams.shades.includes(palette.shade) |
86 | | - ) { |
87 | | - throw new Error( |
88 | | - `'${palette.shade}' mainShade can only be 50, 100, 200, 300, 400, 500, 600, 700, 800 or 900.` |
89 | | - ); |
90 | | - } else { |
91 | | - return true; |
92 | | - } |
93 | | - } else { |
94 | | - throw new Error("Make sure the required data is included."); |
95 | | - } |
96 | | - }, |
97 | | - checkColor: (color: string) => { |
98 | | - var 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; |
99 | | - if (typeof color === "string" && reg.test(color)) { |
100 | | - return true; |
101 | | - } else { |
102 | | - return false; |
103 | | - } |
104 | | - } |
105 | | - } |
106 | | -}; |
107 | | - |
108 | | -const getPalette = ( |
109 | | - params: Palette[] | Palette | string = main.state.initialParams |
110 | | -): any => { |
| 6 | +const getPalette = (params: Palette[] | Palette | string): any => { |
111 | 7 | let palette: any = {}; |
112 | | - main.state.primaryColor = "#FFBD00"; |
113 | | - main.state.mainShade = 500; |
114 | | - main.state.shades = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900]; |
115 | 8 | if (Array.isArray(params)) { |
116 | | - for (let index = 0; index < params.length; index++) { |
117 | | - const colorPalette = params[index]; |
118 | | - if (main.checks.checkParam(colorPalette)) { |
119 | | - main.state.primaryColor = chroma(colorPalette.color).hex(); |
120 | | - main.state.mainShade = colorPalette.shade || main.state.mainShade; |
121 | | - main.state.shades = colorPalette.shades || main.state.shades; |
122 | | - let result = main.getters.colorResult(); |
123 | | - |
124 | | - palette[colorPalette.name] = result; |
| 9 | + for (let i = 0; i < params.length; i++) { |
| 10 | + const colorPalette = params[i]; |
| 11 | + 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); |
125 | 19 | } |
126 | 20 | } |
127 | | - return palette; |
128 | 21 | } else if (typeof params !== "string" && !Array.isArray(params)) { |
129 | | - if (main.checks.checkParam(params)) { |
130 | | - main.state.primaryColor = chroma(params.color).hex(); |
131 | | - main.state.mainShade = params.shade || main.state.mainShade; |
132 | | - main.state.shades = params.shades || main.state.shades; |
133 | | - let result = main.getters.colorResult(); |
134 | | - |
135 | | - palette[params.name] = result; |
| 22 | + 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 | + }; |
136 | 28 |
|
137 | | - return palette; |
| 29 | + palette[params.name] = colorResult(options); |
138 | 30 | } |
139 | 31 | } else if (typeof params === "string") { |
140 | | - main.state.primaryColor = chroma(params.toString()).hex(); |
141 | | - let result = main.getters.colorResult(); |
142 | | - |
143 | | - palette[main.state.initialParams.name] = result; |
| 32 | + const options: IColorResultOptions = Object.assign(initalOptions, { |
| 33 | + primaryColor: chroma(params).hex() |
| 34 | + }); |
144 | 35 |
|
145 | | - return palette; |
| 36 | + palette["primary"] = colorResult(options); |
146 | 37 | } |
| 38 | + return palette; |
147 | 39 | }; |
148 | 40 |
|
149 | 41 | export default getPalette; |
0 commit comments