From b54a1c9686eec3c1114e9b58cb67679ba59c45bd Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Wed, 14 Mar 2018 18:18:31 -0400 Subject: directories --- public-src/colordate.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 public-src/colordate.js (limited to 'public-src/colordate.js') 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); +})(); -- cgit v1.2.3