-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
92 lines (69 loc) · 2.63 KB
/
script.js
File metadata and controls
92 lines (69 loc) · 2.63 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Converter App Choose
const appSelector = document.getElementById("app-selector")
const pxRemConv = document.getElementById("px-rem-converter")
const baseConv = document.getElementById("base-converter")
const hexRgbConv = document.getElementById("hex-rgb-converter")
pxRemConv.style.display = "none"
baseConv.style.display = "none"
hexRgbConv.style.display = "none"
appSelector.addEventListener("change",()=>{
pxRemConv.style.display = "none"
baseConv.style.display = "none"
hexRgbConv.style.display = "none"
if(appSelector.value === "px-rem"){
pxRemConv.style.display = "flex"
}else if(appSelector.value === "base-conv"){
baseConv.style.display = "flex"
}else if(appSelector.value === "hex-rgb"){
hexRgbConv.style.display = "flex"
}
});
// PX-REM Logic
const pxInput = document.getElementById("px-value")
const remInput = document.getElementById("rem-value")
pxInput.addEventListener("input", () => {
const val = parseFloat(pxInput.value);
remInput.value = !isNaN(val) ? (val / 16).toFixed(3) : "";
})
remInput.addEventListener('input', () => {
const val = parseFloat(remInput.value);
pxInput.value = !isNaN(val) ? (val * 16).toFixed(0) : "";
});
// Base-Conv Logic
const decInput = document.getElementById("dec-value")
const binInput = document.getElementById("bin-value")
decInput.addEventListener("input", () => {
const val = parseInt(decInput.value);
binInput.value = !isNaN(val) ? (val.toString(2)) : "";
})
binInput.addEventListener("input", () => {
const val = parseInt(binInput.value,2);
decInput.value = !isNaN(val) ? (val) : "";
})
// HEX-RGB Logic
const hexInput = document.getElementById("hex-value");
const rgbInput = document.getElementById("rgb-value");
hexInput.addEventListener("input", () => {
let hex = hexInput.value.replace('#', '');
if (hex.length === 6) {
const r = parseInt(hex.substring(0, 2), 16);
const g = parseInt(hex.substring(2, 4), 16);
const b = parseInt(hex.substring(4, 6), 16);
if (!isNaN(r) && !isNaN(g) && !isNaN(b)) {
rgbInput.value = `${r}, ${g}, ${b}`;
}
}
});
rgbInput.addEventListener("input", () => {
let parts = rgbInput.value.split(',');
if (parts.length === 3) {
let r = parseInt(parts[0]);
let g = parseInt(parts[1]);
let b = parseInt(parts[2]);
const isValid = (n) => !isNaN(n) && n >= 0 && n <= 255;
if (isValid(r) && isValid(g) && isValid(b)) {
const toHex = (n) => n.toString(16).padStart(2, '0').toUpperCase();
hexInput.value = `#${toHex(r)}${toHex(g)}${toHex(b)}`;
}
}
});