@lordicon/web 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/parsers.ts ADDED
@@ -0,0 +1,230 @@
1
+ import { ColorMap } from './interfaces';
2
+
3
+ /**
4
+ * List of supported colors.
5
+ */
6
+ const COLORS: ColorMap = {
7
+ 'aliceblue': '#f0f8ff',
8
+ 'antiquewhite': '#faebd7',
9
+ 'aqua': '#00ffff',
10
+ 'aquamarine': '#7fffd4',
11
+ 'azure': '#f0ffff',
12
+ 'beige': '#f5f5dc',
13
+ 'bisque': '#ffe4c4',
14
+ 'black': '#000000',
15
+ 'blanchedalmond': '#ffebcd',
16
+ 'blue': '#0000ff',
17
+ 'blueviolet': '#8a2be2',
18
+ 'brown': '#a52a2a',
19
+ 'burlywood': '#deb887',
20
+ 'cadetblue': '#5f9ea0',
21
+ 'chartreuse': '#7fff00',
22
+ 'chocolate': '#d2691e',
23
+ 'coral': '#ff7f50',
24
+ 'cornflowerblue': '#6495ed',
25
+ 'cornsilk': '#fff8dc',
26
+ 'crimson': '#dc143c',
27
+ 'cyan': '#00ffff',
28
+ 'darkblue': '#00008b',
29
+ 'darkcyan': '#008b8b',
30
+ 'darkgoldenrod': '#b8860b',
31
+ 'darkgray': '#a9a9a9',
32
+ 'darkgreen': '#006400',
33
+ 'darkkhaki': '#bdb76b',
34
+ 'darkmagenta': '#8b008b',
35
+ 'darkolivegreen': '#556b2f',
36
+ 'darkorange': '#ff8c00',
37
+ 'darkorchid': '#9932cc',
38
+ 'darkred': '#8b0000',
39
+ 'darksalmon': '#e9967a',
40
+ 'darkseagreen': '#8fbc8f',
41
+ 'darkslateblue': '#483d8b',
42
+ 'darkslategray': '#2f4f4f',
43
+ 'darkturquoise': '#00ced1',
44
+ 'darkviolet': '#9400d3',
45
+ 'deeppink': '#ff1493',
46
+ 'deepskyblue': '#00bfff',
47
+ 'dimgray': '#696969',
48
+ 'dodgerblue': '#1e90ff',
49
+ 'firebrick': '#b22222',
50
+ 'floralwhite': '#fffaf0',
51
+ 'forestgreen': '#228b22',
52
+ 'fuchsia': '#ff00ff',
53
+ 'gainsboro': '#dcdcdc',
54
+ 'ghostwhite': '#f8f8ff',
55
+ 'gold': '#ffd700',
56
+ 'goldenrod': '#daa520',
57
+ 'gray': '#808080',
58
+ 'green': '#008000',
59
+ 'greenyellow': '#adff2f',
60
+ 'honeydew': '#f0fff0',
61
+ 'hotpink': '#ff69b4',
62
+ 'indianred ': '#cd5c5c',
63
+ 'indigo': '#4b0082',
64
+ 'ivory': '#fffff0',
65
+ 'khaki': '#f0e68c',
66
+ 'lavender': '#e6e6fa',
67
+ 'lavenderblush': '#fff0f5',
68
+ 'lawngreen': '#7cfc00',
69
+ 'lemonchiffon': '#fffacd',
70
+ 'lightblue': '#add8e6',
71
+ 'lightcoral': '#f08080',
72
+ 'lightcyan': '#e0ffff',
73
+ 'lightgoldenrodyellow': '#fafad2',
74
+ 'lightgrey': '#d3d3d3',
75
+ 'lightgreen': '#90ee90',
76
+ 'lightpink': '#ffb6c1',
77
+ 'lightsalmon': '#ffa07a',
78
+ 'lightseagreen': '#20b2aa',
79
+ 'lightskyblue': '#87cefa',
80
+ 'lightslategray': '#778899',
81
+ 'lightsteelblue': '#b0c4de',
82
+ 'lightyellow': '#ffffe0',
83
+ 'lime': '#00ff00',
84
+ 'limegreen': '#32cd32',
85
+ 'linen': '#faf0e6',
86
+ 'magenta': '#ff00ff',
87
+ 'maroon': '#800000',
88
+ 'mediumaquamarine': '#66cdaa',
89
+ 'mediumblue': '#0000cd',
90
+ 'mediumorchid': '#ba55d3',
91
+ 'mediumpurple': '#9370d8',
92
+ 'mediumseagreen': '#3cb371',
93
+ 'mediumslateblue': '#7b68ee',
94
+ 'mediumspringgreen': '#00fa9a',
95
+ 'mediumturquoise': '#48d1cc',
96
+ 'mediumvioletred': '#c71585',
97
+ 'midnightblue': '#191970',
98
+ 'mintcream': '#f5fffa',
99
+ 'mistyrose': '#ffe4e1',
100
+ 'moccasin': '#ffe4b5',
101
+ 'navajowhite': '#ffdead',
102
+ 'navy': '#000080',
103
+ 'oldlace': '#fdf5e6',
104
+ 'olive': '#808000',
105
+ 'olivedrab': '#6b8e23',
106
+ 'orange': '#ffa500',
107
+ 'orangered': '#ff4500',
108
+ 'orchid': '#da70d6',
109
+ 'palegoldenrod': '#eee8aa',
110
+ 'palegreen': '#98fb98',
111
+ 'paleturquoise': '#afeeee',
112
+ 'palevioletred': '#d87093',
113
+ 'papayawhip': '#ffefd5',
114
+ 'peachpuff': '#ffdab9',
115
+ 'peru': '#cd853f',
116
+ 'pink': '#ffc0cb',
117
+ 'plum': '#dda0dd',
118
+ 'powderblue': '#b0e0e6',
119
+ 'purple': '#800080',
120
+ 'rebeccapurple': '#663399',
121
+ 'red': '#ff0000',
122
+ 'rosybrown': '#bc8f8f',
123
+ 'royalblue': '#4169e1',
124
+ 'saddlebrown': '#8b4513',
125
+ 'salmon': '#fa8072',
126
+ 'sandybrown': '#f4a460',
127
+ 'seagreen': '#2e8b57',
128
+ 'seashell': '#fff5ee',
129
+ 'sienna': '#a0522d',
130
+ 'silver': '#c0c0c0',
131
+ 'skyblue': '#87ceeb',
132
+ 'slateblue': '#6a5acd',
133
+ 'slategray': '#708090',
134
+ 'snow': '#fffafa',
135
+ 'springgreen': '#00ff7f',
136
+ 'steelblue': '#4682b4',
137
+ 'tan': '#d2b48c',
138
+ 'teal': '#008080',
139
+ 'thistle': '#d8bfd8',
140
+ 'tomato': '#ff6347',
141
+ 'turquoise': '#40e0d0',
142
+ 'violet': '#ee82ee',
143
+ 'wheat': '#f5deb3',
144
+ 'white': '#ffffff',
145
+ 'whitesmoke': '#f5f5f5',
146
+ 'yellow': '#ffff00',
147
+ 'yellowgreen': '#9acd32'
148
+ };
149
+
150
+ /**
151
+ * Returns a hexadecimal color string for a given color name or hex code.
152
+ *
153
+ * Example:
154
+ * ```js
155
+ * parseColor('red'); // "#ff0000"
156
+ * parseColor('#0f0'); // "#00ff00"
157
+ * ```
158
+ *
159
+ * @param colorName Color name (e.g., "red") or hex string (e.g., "#ff0000" or "#0f0").
160
+ * @returns Hexadecimal color string in the format "#rrggbb".
161
+ */
162
+ export function parseColor(colorName: string): string {
163
+ if (colorName.startsWith('#')) {
164
+ if (colorName.length === 4) {
165
+ // support shorthand
166
+ return `#${colorName[1]}${colorName[1]}${colorName[2]}${colorName[2]}${colorName[3]}${colorName[3]}`;
167
+ } else {
168
+ return colorName;
169
+ }
170
+ } else {
171
+ return COLORS[colorName.toLowerCase()] || '#000000';
172
+ }
173
+ }
174
+
175
+ /**
176
+ * Parses a colors attribute string into a ColorMap object.
177
+ *
178
+ * Example:
179
+ * ```js
180
+ * parseColors('primary:red,secondary:#00ff00');
181
+ * // Returns: { primary: '#ff0000', secondary: '#00ff00' }
182
+ * ```
183
+ *
184
+ * @param colors Colors defined as a comma-separated string (e.g., "primary:red,secondary:#00ff00").
185
+ * @returns Object mapping color names to hex strings, or undefined if input is invalid.
186
+ */
187
+ export function parseColors(colors: any): ColorMap | undefined {
188
+ if (!colors || typeof colors !== 'string') {
189
+ return undefined;
190
+ }
191
+
192
+ const list = colors.split(',').filter(c => c).map(c => c.split(':')).filter(c => c.length == 2);
193
+
194
+ return list.reduce<ColorMap>((p, c) => {
195
+ const a = c[0];
196
+ p[a.toLowerCase()] = parseColor(c[1]);
197
+ return p;
198
+ }, {});
199
+ }
200
+
201
+ /**
202
+ * Parses a stroke attribute value to a supported numeric range.
203
+ *
204
+ * @param value Stroke value as a string or number ("light", 1, "1", "regular", 2, "2", "bold", 3, "3").
205
+ * @returns Stroke value as 1, 2, or 3, or undefined if not valid.
206
+ */
207
+ export function parseStroke(value: any): (1 | 2 | 3 | undefined) {
208
+ if (value === 'light' || value === 1 || value === '1') {
209
+ return 1;
210
+ } else if (value === 'regular' || value === 2 || value === '2') {
211
+ return 2;
212
+ } else if (value === 'bold' || value === 3 || value === '3') {
213
+ return 3;
214
+ }
215
+
216
+ return undefined;
217
+ }
218
+
219
+ /**
220
+ * Parse state attribute.
221
+ * @param value State value.
222
+ * @returns Returns the state as a string if valid, otherwise undefined.
223
+ */
224
+ export function parseState(value: any): (string | undefined) {
225
+ if (typeof value === 'string') {
226
+ return value;
227
+ }
228
+
229
+ return undefined;
230
+ }