-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion-3.js
70 lines (51 loc) · 1.76 KB
/
question-3.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
65
66
67
68
69
// Question 3: Write a function that converts HEX to RGB. Then Make that function auto-dect the formats so that if you enter HEX color format it returns RGB and if you enter RGB color format it returns HEX.
const input = "#01cd3e";
const input2 = "rgb(10, 213, 155)";
const input3 = '#sdfkjd';
const input4 = 23487;
const input5 = "rgb(23, 34)";
function isHexColor(hex) {
const regExp = /#[0-9a-f]{6}/g;
return String(hex).match(regExp) !== null;
}
function hexToRgb(hex) {
const regExp = /[0-9a-f][0-9a-f]/g;
const colors = String(hex).match(regExp);
const r = parseInt(colors[0], 16);
const g = parseInt(colors[1], 16);
const b = parseInt(colors[2], 16);
return `rgb(${r}, ${g}, ${b})`;
}
function isRgbColor(rgb) {
const prefixCheck = String(rgb).startsWith('rgb(');
const suffixCheck = String(rgb).endsWith(')');
const regExp = /\d+/g;
const colors = String(rgb).match(regExp);
return prefixCheck && suffixCheck && colors.length == 3? true: false;
}
function intToHex(num) {
let hex = parseInt(num).toString(16);
return hex.length === 1? "0"+hex: hex;
}
function rgbToHex(rgb) {
const regExp = /\d+/g;
const colors = String(rgb).match(regExp);
const r = intToHex(colors[0]);
const g = intToHex(colors[1]);
const b = intToHex(colors[2]);
return `#${r}${g}${b}`;
}
function convertToHexOrRgb(color) {
if(isHexColor(color)) {
return hexToRgb(color);
} else if(isRgbColor(color)) {
return rgbToHex(color);
} else {
return "Invalid input";
}
}
console.log(convertToHexOrRgb(input));
console.log(convertToHexOrRgb(input2));
console.log(convertToHexOrRgb(input3));
console.log(convertToHexOrRgb(input4));
console.log(convertToHexOrRgb(input5));