-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
53 lines (47 loc) · 1.15 KB
/
script.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
function myFunction() {
let input = prompt("Please enter the digit: ");
debugger;
const hex = '[0-9a-fA-F]';
const re = new RegExp(`^(${hex}{4}|${hex}{7})$`);
if(re.test(input)) {
return alert(hexToRGB(input));
}
else {
return alert(RGBToHex(input));
}
}
myFunction();
function hexToRGB(h) {
let r = 0, g = 0, b = 0;
// 3 digits
if (h.length == 4) {
r = "0x" + h[1] + h[1];
g = "0x" + h[2] + h[2];
b = "0x" + h[3] + h[3];
// 6 digits
} else if (h.length == 7) {
r = "0x" + h[1] + h[2];
g = "0x" + h[3] + h[4];
b = "0x" + h[5] + h[6];
}
return "rgb("+ +r + "," + +g + "," + +b + ")"; //variables are prepended with + to convert string back to numbers
}
//hexToRGB('2222')
//hexToRGB('4616625')
//debugger;
function RGBToHex(r,g,b) {
let x = r.split(',').map(Number);
r = x[0].toString(16);
g = x[1].toString(16);
b = x[2].toString(16);
if(r.length == 1) {
r = "0" + r;
}
else if (g.length == 1) {
g = "0" + g;
}
else {
b = "0" + b;
}
return "#" + r + g + b;
}