d_sellers1

Javascript color scale from 0% to 100%, rendering it from red to yellow to green

Aug 22nd, 2022
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // License: MIT - https://opensource.org/licenses/MIT
  2. // Author: Michele Locati <[email protected]>
  3. // Source: https://gist.github.com/mlocati/7210513
  4. function perc2color(perc) {
  5.     var r, g, b = 0;
  6.     if(perc < 50) {
  7.         r = 255;
  8.         g = Math.round(5.1 * perc);
  9.     }
  10.     else {
  11.         g = 255;
  12.         r = Math.round(510 - 5.10 * perc);
  13.     }
  14.     var h = r * 0x10000 + g * 0x100 + b * 0x1;
  15.     return '#' + ('000000' + h.toString(16)).slice(-6);
  16. }
Advertisement
Add Comment
Please, Sign In to add comment