summaryrefslogtreecommitdiff
path: root/colordate.js
blob: e5331eb9a23d02a3a9cf66c429cc43e0e72e5e97 (plain)
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
(function() {
	// in milliseconds
	var now = Date.now();
	var oneday = 1000*60*60*24;

	// maps from a point on iRange to a point on oRange
	var mapRange = function(iRange, oRange, iPoint) {
		var pct = (iPoint - iRange[0])/(iRange[1]-iRange[0]);
		if (pct < 0) {
			pct = 0;
		} else if (pct > 1) {
			pct = 1;
		}
		var oPoint = oRange[0] + (pct * (oRange[1]-oRange[0]));
		return oPoint;
	}


	var rgb = function(r, g, b) {
		return "rgb(" + Math.trunc(r) + "," + Math.trunc(g) + "," + Math.trunc(b) + ")";
	};

	var date2color = function(t) {
		var max = 0xFF;
		var red = mapRange([now-oneday, now-(oneday/2)],
				[max, 0],
				t);
		var green = mapRange([now-(oneday/2), now],
				[0, max],
				t);
		return rgb(max-green, max-red, max-green-red);
	};

	var main = function() {
		document.querySelectorAll('time.daily').forEach(function(time) {
			time.style.backgroundColor = date2color(Date.parse(time.dateTime));
		});
	};

	document.addEventListener("DOMContentLoaded", main, false);
})();