-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdusk.js
34 lines (24 loc) · 1.04 KB
/
dusk.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
'use strict';
var random = 0;
var colors = [0, 0, 0];
var animationDuration = 2000;
var hour = 0;
var color = 0;
function changeColor(elementDOM) {
hour = new Date().getHours();
//Will get an valid rgb color
color = parseInt(255 / 24 * hour);
for (var i = 0; i < colors.length; i++) {
// random number for color based on time of day
random = Math.floor((Math.random() * (hour * 3)) + 1);
// adds more randomness, if even number adds, otherwise, subtracts
color = (random % 2 == 0) ? color + random : color - random;
// if the values goes off the limits this will cap between 0 - 255
color = (color > 255) ? 255 : color < 0 ? 0 : color;
colors[i] = color;
}
if (document.getElementById(elementDOM)) document.getElementById(elementDOM).style.color = "rgb(" + colors[0] + "," + colors[1] + "," + colors[2] + ")";
}
function dusk(elementDOM, interval){
if(document.getElementById(elementDOM)) setInterval(changeColor, interval, elementDOM);
}