Skip to content

Commit 6a70e5a

Browse files
authored
feat(ui5-color-picker): initial implementation (#2769)
1 parent d237401 commit 6a70e5a

File tree

9 files changed

+1281
-1
lines changed

9 files changed

+1281
-1
lines changed
+370
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,370 @@
1+
/**
2+
*
3+
* @param {String} color Color in one of the following formats: RGBA or HEX
4+
*/
5+
6+
/**
7+
* Map of CSS colors to hex representation
8+
*/
9+
const CSSColors = {
10+
aliceblue: "f0f8ff",
11+
antiquewhite: "faebd7",
12+
aqua: "00ffff",
13+
aquamarine: "7fffd4",
14+
azure: "f0ffff",
15+
beige: "f5f5dc",
16+
bisque: "ffe4c4",
17+
black: "000000",
18+
blanchedalmond: "ffebcd",
19+
blue: "0000ff",
20+
blueviolet: "8a2be2",
21+
brown: "a52a2a",
22+
burlywood: "deb887",
23+
cadetblue: "5f9ea0",
24+
chartreuse: "7fff00",
25+
chocolate: "d2691e",
26+
coral: "ff7f50",
27+
cornflowerblue: "6495ed",
28+
cornsilk: "fff8dc",
29+
crimson: "dc143c",
30+
cyan: "00ffff",
31+
darkblue: "00008b",
32+
darkcyan: "008b8b",
33+
darkgoldenrod: "b8860b",
34+
darkgray: "a9a9a9",
35+
darkgrey: "a9a9a9",
36+
darkgreen: "006400",
37+
darkkhaki: "bdb76b",
38+
darkmagenta: "8b008b",
39+
darkolivegreen: "556b2f",
40+
darkorange: "ff8c00",
41+
darkorchid: "9932cc",
42+
darkred: "8b0000",
43+
darksalmon: "e9967a",
44+
darkseagreen: "8fbc8f",
45+
darkslateblue: "483d8b",
46+
darkslategray: "2f4f4f",
47+
darkslategrey: "2f4f4f",
48+
darkturquoise: "00ced1",
49+
darkviolet: "9400d3",
50+
deeppink: "ff1493",
51+
deepskyblue: "00bfff",
52+
dimgray: "696969",
53+
dimgrey: "696969",
54+
dodgerblue: "1e90ff",
55+
firebrick: "b22222",
56+
floralwhite: "fffaf0",
57+
forestgreen: "228b22",
58+
fuchsia: "ff00ff",
59+
gainsboro: "dcdcdc",
60+
ghostwhite: "f8f8ff",
61+
gold: "ffd700",
62+
goldenrod: "daa520",
63+
gray: "808080",
64+
grey: "808080",
65+
green: "008000",
66+
greenyellow: "adff2f",
67+
honeydew: "f0fff0",
68+
hotpink: "ff69b4",
69+
indianred: "cd5c5c",
70+
indigo: "4b0082",
71+
ivory: "fffff0",
72+
khaki: "f0e68c",
73+
lavender: "e6e6fa",
74+
lavenderblush: "fff0f5",
75+
lawngreen: "7cfc00",
76+
lemonchiffon: "fffacd",
77+
lightblue: "add8e6",
78+
lightcoral: "f08080",
79+
lightcyan: "e0ffff",
80+
lightgoldenrodyellow: "fafad2",
81+
lightgray: "d3d3d3",
82+
lightgrey: "d3d3d3",
83+
lightgreen: "90ee90",
84+
lightpink: "ffb6c1",
85+
lightsalmon: "ffa07a",
86+
lightseagreen: "20b2aa",
87+
lightskyblue: "87cefa",
88+
lightslategray: "778899",
89+
lightslategrey: "778899",
90+
lightsteelblue: "b0c4de",
91+
lightyellow: "ffffe0",
92+
lime: "00ff00",
93+
limegreen: "32cd32",
94+
linen: "faf0e6",
95+
magenta: "ff00ff",
96+
maroon: "800000",
97+
mediumaquamarine: "66cdaa",
98+
mediumblue: "0000cd",
99+
mediumorchid: "ba55d3",
100+
mediumpurple: "9370db",
101+
mediumseagreen: "3cb371",
102+
mediumslateblue: "7b68ee",
103+
mediumspringgreen: "00fa9a",
104+
mediumturquoise: "48d1cc",
105+
mediumvioletred: "c71585",
106+
midnightblue: "191970",
107+
mintcream: "f5fffa",
108+
mistyrose: "ffe4e1",
109+
moccasin: "ffe4b5",
110+
navajowhite: "ffdead",
111+
navy: "000080",
112+
oldlace: "fdf5e6",
113+
olive: "808000",
114+
olivedrab: "6b8e23",
115+
orange: "ffa500",
116+
orangered: "ff4500",
117+
orchid: "da70d6",
118+
palegoldenrod: "eee8aa",
119+
palegreen: "98fb98",
120+
paleturquoise: "afeeee",
121+
palevioletred: "db7093",
122+
papayawhip: "ffefd5",
123+
peachpuff: "ffdab9",
124+
peru: "cd853f",
125+
pink: "ffc0cb",
126+
plum: "dda0dd",
127+
powderblue: "b0e0e6",
128+
purple: "800080",
129+
red: "ff0000",
130+
rosybrown: "bc8f8f",
131+
royalblue: "4169e1",
132+
saddlebrown: "8b4513",
133+
salmon: "fa8072",
134+
sandybrown: "f4a460",
135+
seagreen: "2e8b57",
136+
seashell: "fff5ee",
137+
sienna: "a0522d",
138+
silver: "c0c0c0",
139+
skyblue: "87ceeb",
140+
slateblue: "6a5acd",
141+
slategray: "708090",
142+
slategrey: "708090",
143+
snow: "fffafa",
144+
springgreen: "00ff7f",
145+
steelblue: "4682b4",
146+
tan: "d2b48c",
147+
teal: "008080",
148+
thistle: "d8bfd8",
149+
tomato: "ff6347",
150+
turquoise: "40e0d0",
151+
violet: "ee82ee",
152+
wheat: "f5deb3",
153+
white: "ffffff",
154+
whitesmoke: "f5f5f5",
155+
yellow: "ffff00",
156+
yellowgreen: "9acd32",
157+
transparent: "00000000",
158+
};
159+
160+
const getRGBColor = color => {
161+
if (color.startsWith("rgba")) {
162+
return RGBAToRGB(color);
163+
}
164+
165+
if (color.startsWith("rgb")) {
166+
return RGBToRGB(color);
167+
}
168+
169+
// HEX
170+
if (color.indexOf("#") === 0) {
171+
// Shorthand Syntax
172+
if (color.length === 4) {
173+
color = `${color[1]}${color[1]}${color[2]}${color[2]}${color[3]}${color[3]}`;
174+
} else {
175+
color = color.slice(1, color.length);
176+
}
177+
}
178+
179+
// Css Color
180+
if (Object.keys(CSSColors).includes(color)) {
181+
color = CSSColors[color];
182+
}
183+
184+
return HEXToRGB(color);
185+
};
186+
187+
/**
188+
* Return an object with the properties for each of the main colors(red, green, blue)
189+
* @param {String} color Receives a color in the following format: "rgba(0, 0, 0, 1)
190+
*/
191+
const RGBAToRGB = color => {
192+
const openingBracketIndex = color.indexOf("("),
193+
commasIndexes = [];
194+
195+
for (let i = 0; i < color.length; i++) {
196+
if (color[i] === ",") {
197+
commasIndexes.push(i);
198+
}
199+
}
200+
201+
return {
202+
r: parseInt(color.slice(openingBracketIndex + 1, commasIndexes[0]).trim()),
203+
g: parseInt(color.slice(commasIndexes[0] + 1, commasIndexes[1]).trim()),
204+
b: parseInt(color.slice(commasIndexes[1] + 1, commasIndexes[2]).trim()),
205+
};
206+
};
207+
208+
/**
209+
* Return an object with the properties for each of the main colors(red, green, blue)
210+
* @param {String} color Receives a color in the following format: "rgb(0, 0, 0)
211+
*/
212+
const RGBToRGB = color => {
213+
const openingBracketIndex = color.indexOf("("),
214+
closingBraketIndex = color.indexOf(")"),
215+
commasIndexes = [];
216+
217+
for (let i = 0; i < color.length; i++) {
218+
if (color[i] === ",") {
219+
commasIndexes.push(i);
220+
}
221+
}
222+
223+
return {
224+
r: parseInt(color.slice(openingBracketIndex + 1, commasIndexes[0]).trim()),
225+
g: parseInt(color.slice(commasIndexes[0] + 1, commasIndexes[1]).trim()),
226+
b: parseInt(color.slice(commasIndexes[1] + 1, closingBraketIndex).trim()),
227+
};
228+
};
229+
230+
const HSLToRGB = (color = {
231+
h: undefined,
232+
s: undefined,
233+
l: undefined,
234+
}) => {
235+
// Formula taken from https://www.rapidtables.com/convert/color/hsl-to-rgb.html
236+
const C = (1 - Math.abs((2 * color.l) - 1)) * color.s,
237+
X = C * (1 - Math.abs(((color.h / 60) % 2) - 1)),
238+
m = color.l - C / 2;
239+
240+
let tempColor = {};
241+
switch (Math.round(color.h / 60)) {
242+
// 0 ≤ H < 60
243+
case 0:
244+
tempColor = {
245+
r: C,
246+
g: X,
247+
b: 0,
248+
};
249+
break;
250+
251+
// 60 ≤ H < 120
252+
case 1:
253+
tempColor = {
254+
r: X,
255+
g: C,
256+
b: 0,
257+
};
258+
break;
259+
260+
// 120 ≤ H < 180
261+
case 2:
262+
tempColor = {
263+
r: 0,
264+
g: C,
265+
b: X,
266+
};
267+
break;
268+
269+
// 180 ≤ H < 240
270+
case 3:
271+
tempColor = {
272+
r: 0,
273+
g: X,
274+
b: C,
275+
};
276+
break;
277+
278+
// 240 ≤ H < 300
279+
case 4:
280+
tempColor = {
281+
r: X,
282+
g: 0,
283+
b: C,
284+
};
285+
break;
286+
287+
// 300 ≤ H < 360
288+
default:
289+
tempColor = {
290+
r: C,
291+
g: 0,
292+
b: X,
293+
};
294+
}
295+
296+
return {
297+
r: Math.floor((tempColor.r + m) * 255),
298+
g: Math.floor((tempColor.g + m) * 255),
299+
b: Math.floor((tempColor.b + m) * 255),
300+
};
301+
};
302+
303+
const HEXToRGB = hex => {
304+
// Please make sure you pass a valid 6 digit hex color
305+
// In the implementation of this method we assume that the hex argument is a 6 digit valid hex color
306+
307+
const rgbValues = {
308+
r: hex.substr(0, 2),
309+
g: hex.substr(2, 2),
310+
b: hex.substr(4, 2),
311+
};
312+
313+
const rgbKeys = Object.keys(rgbValues);
314+
315+
rgbKeys.forEach(key => {
316+
rgbValues[key] = parseInt(rgbValues[key], 16);
317+
});
318+
319+
return rgbValues;
320+
};
321+
322+
const RGBToHSL = (color = {
323+
r: undefined,
324+
g: undefined,
325+
b: undefined,
326+
}) => {
327+
const R = color.r / 255,
328+
G = color.g / 255,
329+
B = color.b / 255,
330+
max = Math.max(R, G, B),
331+
min = Math.min(R, G, B),
332+
delta = max - min;
333+
334+
let h,
335+
s;
336+
337+
// Hue calculation
338+
if (delta === 0) {
339+
h = 0;
340+
} else if (max === R) {
341+
h = 60 * (((G - B) / delta) % 6);
342+
} else if (max === G) {
343+
h = 60 * (((B - R) / delta) + 2);
344+
} else if (max === B) {
345+
h = 60 * (((R - G) / delta) + 4);
346+
}
347+
348+
// Lightness calculation
349+
const l = (max + min) / 2;
350+
351+
// Saturation calculation
352+
if (delta === 0) {
353+
s = 0;
354+
} else {
355+
s = delta / (1 - Math.abs(2 * l - 1));
356+
}
357+
358+
return {
359+
h,
360+
s,
361+
l,
362+
};
363+
};
364+
365+
export {
366+
getRGBColor,
367+
HSLToRGB,
368+
HEXToRGB,
369+
RGBToHSL,
370+
};

packages/main/bundle.esm.js

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import Carousel from "./dist/Carousel.js";
4949
import CheckBox from "./dist/CheckBox.js";
5050
import ColorPalette from "./dist/ColorPalette.js";
5151
import ColorPaletteItem from "./dist/ColorPaletteItem.js";
52+
import ColorPicker from "./dist/ColorPicker.js";
5253
import ComboBox from "./dist/ComboBox.js";
5354
import DatePicker from "./dist/DatePicker.js";
5455
import DateRangePicker from "./dist/DateRangePicker.js";

0 commit comments

Comments
 (0)