-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
89 lines (83 loc) · 2.21 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dark Color Generator</title>
<style>
body {
margin: 0;
font-family: 'Arial', sans-serif;
}
main {
height: 100vh;
transition: background 1s;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 2em;
box-sizing: border-box;
text-align: center;
}
h1 {
margin: 0;
font-weight: 400;
color: #fff;
transition: color 1s;
line-height: 2;
}
footer {
text-align: center;
padding: 0.5rem 0;
background-color: #eaeaea90;
}
footer p {
font-size: 0.75rem;
margin: 0.25rem 0;
color: #221133;
}
footer a {
text-decoration: none;
color: inherit;
}
</style>
</head>
<body>
<main id="background">
<h1 id="header">
Generate Random Dark Colors with JavaScript
</h1>
</main>
<script>
const background = document.getElementById("background");
const h1 = document.getElementById("header");
const getRandomNumber = (min, max) => {
return Math.floor(Math.random() * (max - min +1)) + min;
};
const hslToHex = (h, s, l) => {
l /= 100;
const a = s * Math.min(l, 1 - l) / 100;
const f = n => {
const k = (n + h / 30) % 12;
const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
return Math.round(255 * color).toString(16).padStart(2, '0');
};
return `#${f(0)}${f(8)}${f(4)}`;
};
const setBackgroundColor = () => {
const h = getRandomNumber(0, 360);
const s = getRandomNumber(30, 80);
const randomColor = `hsl(${h}deg, ${s}%, 50%)`;
background.style.backgroundColor = randomColor;
background.style.color = randomColor;
h1.innerHTML = hslToHex(h, s, 50) + "<br />" + randomColor;
};
setBackgroundColor();
setInterval(() => {
setBackgroundColor();
}, 3000);
</script>
</body>
</html>