-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.js
27 lines (22 loc) · 812 Bytes
/
convert.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
// Elements
const elm_rgb = document.getElementById('rgb');
const elm_hex = document.getElementById('hex');
// Function
const convert = (rgb) => {
const rep = (match, r, g, b) => {
return '#' + ('0' + parseInt(r).toString(16)).slice(-2) + ('0' + parseInt(g).toString(16)).slice(-2) + ('0' + parseInt(b).toString(16)).slice(-2);
};
rgb = rgb.replace(/RGB\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/gi, rep);
rgb = rgb.replace(/\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/gi, rep);
return rgb;
};
// Events
window.onload = () => {
elm_hex.value = convert(elm_rgb.value);
};
elm_rgb.addEventListener('input', () => {
elm_hex.value = convert(elm_rgb.value);
});
elm_hex.addEventListener('change', () => {
elm_rgb.value = convert(elm_hex.value);
});