-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (49 loc) · 1.39 KB
/
script.js
File metadata and controls
61 lines (49 loc) · 1.39 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
const clock = document.getElementById("clock");
// ⏱️ Atualiza o relógio
function updateClock() {
const now = new Date();
const time = [
now.getHours(),
now.getMinutes(),
now.getSeconds()
]
.map(n => String(n).padStart(2, "0"))
.join(":");
clock.textContent = time;
}
// 🎨 Aplica tema salvo
function applySavedTheme() {
const savedColor = localStorage.getItem("bgColor");
if (!savedColor) return;
document.body.style.backgroundColor = savedColor;
applyContrast(savedColor);
}
// 🧠 Ajusta contraste automaticamente
function applyContrast(hexColor) {
const [r, g, b] = hexToRgb(hexColor);
const brightness = (r * 299 + g * 587 + b * 114) / 1000;
document.body.style.color = brightness > 125 ? "#000" : "#fff";
}
// 🔧 Converte HEX → RGB
function hexToRgb(hex) {
const clean = hex.replace("#", "");
return [
parseInt(clean.substring(0, 2), 16),
parseInt(clean.substring(2, 4), 16),
parseInt(clean.substring(4, 6), 16)
];
}
// ⏳ Sincroniza com o segundo real (sem drift)
function startClock() {
updateClock();
const delay = 1000 - new Date().getMilliseconds();
setTimeout(() => {
updateClock();
setInterval(updateClock, 1000);
}, delay);
}
// 🚀 Inicialização
document.addEventListener("DOMContentLoaded", () => {
applySavedTheme();
startClock();
});