diff options
author | Luke Shumaker <lukeshu@sbcglobal.net> | 2017-02-06 18:02:11 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@sbcglobal.net> | 2017-02-06 18:02:15 -0500 |
commit | e45ff32aca748b05e0f000daa7536b058d2da089 (patch) | |
tree | beb5421233c7800c4104c7fa6843486df6a6621d /colordate.js | |
parent | 2660b8c3da781f3f582c4e1bb5cf7513f0072f6b (diff) |
Colorize the last-updated timestamps with JS.
Diffstat (limited to 'colordate.js')
-rw-r--r-- | colordate.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/colordate.js b/colordate.js new file mode 100644 index 0000000..a228eb5 --- /dev/null +++ b/colordate.js @@ -0,0 +1,40 @@ +(function() { + // in milliseconds + var now = Date.now(); + var oneday = 1000*60*60*24; + + var mapTo = 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 = 0xF3; + var red = mapTo([now-oneday, now-(oneday/2)], + [max, 0], + t); + var green = mapTo([now-(oneday/2), now], + [0, max], + t); + return rgb(max-green, max-red, max-green-red); + }; + + var main = function() { + document.querySelectorAll('time.colordate').forEach(function(time) { + time.style.backgroundColor = date2color(Date.parse(time.dateTime)); + }); + }; + + document.addEventListener("DOMContentLoaded", main, false); +})(); |