Skip to content

Instantly share code, notes, and snippets.

@ceane
Last active January 1, 2016 13:39
Show Gist options
  • Save ceane/8152504 to your computer and use it in GitHub Desktop.
Save ceane/8152504 to your computer and use it in GitHub Desktop.
An interpretation of Clear's heat map color algorithm

#Clear's heatmap color algorithm

Barebones result

<ul id="list">
  <li>Milk</li>
  <li>Greens</li>
  <li>Flour</li>
  <li>Eggs</li>
  <li>Mushrooms</li>
  <li>Mustard</li>
</ul>
//Works in Firefox, Chrome, IE12 on Win 10 preview (2015)
//remove => and add function for ES5 compat
var heatmap = (el, i, arr) => {
  i = i + 1;
  var base = arr.length > 6 ? arr.length : 6;
  var b = Math.floor(220 * ( Math.sin(i/base) )) + 8 + i;
  
  el.style.backgroundColor = 'rgb(233, ' + b + ', 33)';
};

//for ES5: Array.prototype.slice.call(document.querySelectorAll('#list li'));
var list = [...document.querySelectorAll('#list li')];

list.forEach(heatmap);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment