summaryrefslogtreecommitdiff
path: root/public-src/colordate.js
diff options
context:
space:
mode:
Diffstat (limited to 'public-src/colordate.js')
-rw-r--r--public-src/colordate.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/public-src/colordate.js b/public-src/colordate.js
new file mode 100644
index 0000000..e5331eb
--- /dev/null
+++ b/public-src/colordate.js
@@ -0,0 +1,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);
+})();