-
Notifications
You must be signed in to change notification settings - Fork 0
/
question3.js
64 lines (53 loc) · 1.23 KB
/
question3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
let hex = "F2AB7D"; // Equivalent to rgb(242,171,125)
let rgb = [242, 171, 125]; // Equivalente to the upper hex value
const hexTable = number => {
if (parseInt(number) < 10) return parseInt(number);
else {
switch(number) {
case 10: return "A";
break;
case 11: return "B";
break;
case 12: return "C";
break;
case 13: return "D";
break;
case 14: return "E";
break;
case 15: return "F";
break;
case "A": return 10;
break;
case "B": return 11;
break;
case "C": return 12;
break;
case "D": return 13;
break;
case "E": return 14;
break;
case "F": return 15;
break;
}
}
}
const HEXandRGB = value => {
//if is hex / TO RGB
if (value.length === 6) {
let rgb = new Array(3);
rgb[0] = (hexTable(value[0])*16) + hexTable(value[1]);
rgb[1] = (hexTable(value[2])*16) + hexTable(value[3]);
rgb[2] = (hexTable(value[4])*16) + hexTable(value[5]);
return rgb;
}
// if is rgb / TO HEX
else if (value.length === 3) {
let hex = "";
for (let i=0; i<3; i++) hex += hexTable(Math.floor(value[i]/16)) + hexTable(value[i]%16);
return hex;
}
else return undefined;
}
// Testing HEXandRGB function
console.log(HEXandRGB(hex));
console.log(HEXandRGB(rgb));