Skip to content

Instantly share code, notes, and snippets.

@mlocati
Last active June 13, 2026 23:21
Show Gist options
  • Select an option

  • Save mlocati/7210513 to your computer and use it in GitHub Desktop.

Select an option

Save mlocati/7210513 to your computer and use it in GitHub Desktop.
Javascript color scale from 0% to 100%, rendering it from red to yellow to green
// License: MIT - https://opensource.org/licenses/MIT
// Author: Michele Locati <michele@locati.it>
// Source: https://gist.github.com/mlocati/7210513
function perc2color(perc) {
var r, g, b = 0;
if(perc < 50) {
r = 255;
g = Math.round(5.1 * perc);
}
else {
g = 255;
r = Math.round(510 - 5.10 * perc);
}
var h = r * 0x10000 + g * 0x100 + b * 0x1;
return '#' + ('000000' + h.toString(16)).slice(-6);
}
@mlocati

mlocati commented Jun 16, 2016

Copy link
Copy Markdown
Author

Here's the color scale (from 0 to 100):

output

@Majo1996

Majo1996 commented Jun 1, 2017

Copy link
Copy Markdown

Nice, thank you!

@kishore-indraganti

Copy link
Copy Markdown

What if i want to use red to green to blue ?

@anandbhaskaran

Copy link
Copy Markdown

Thanks!

@ruaanvds

Copy link
Copy Markdown

@mlocati, thanks for a succinct, clean solution!

@roboriaan

roboriaan commented Feb 14, 2019

Copy link
Copy Markdown

A slight modification to pass in a min and max.
It should calculate the same as excel does with it's color scales :

function perc2color(perc,min,max) {
            var base = (max - min);

            if (base == 0) { perc = 100; }
            else {
                perc = (perc - min) / base * 100; 
            }
            var r, g, b = 0;
            if (perc < 50) {
                r = 255;
                g = Math.round(5.1 * perc);
            }
            else {
                g = 255;
                r = Math.round(510 - 5.10 * perc);
            }
            var h = r * 0x10000 + g * 0x100 + b * 0x1;
            return '#' + ('000000' + h.toString(16)).slice(-6);
        }

@dozzes

dozzes commented May 22, 2019

Copy link
Copy Markdown

How to draw a rectangle with this gradient color filling in HTML?

@vimtaai

vimtaai commented Oct 19, 2019

Copy link
Copy Markdown

A modern solution using HSL colors would be:

function percentageToColor(percentage, maxHue = 120, minHue = 0) {
  const hue = percentage * (maxHue - minHue) + minHue;
  return `hsl(${hue}, 100%, 50%)`;
}

@ujLion

ujLion commented Nov 14, 2019

Copy link
Copy Markdown

Stating the too obvious: to invert - subtract the input value from 100

@ujLion

ujLion commented Nov 14, 2019

Copy link
Copy Markdown

@vimtaai

vimtaai commented Nov 14, 2019

Copy link
Copy Markdown

There is no license mentioned.
https://opensource.stackexchange.com/questions/1720/what-can-i-assume-if-a-publicly-published-project-has-no-license

Technically, this is not a repo, just a Gist. As for my solution, I consider it Public Domain 😄

@ujLion

ujLion commented Nov 14, 2019

Copy link
Copy Markdown

There is no license mentioned.
https://opensource.stackexchange.com/questions/1720/what-can-i-assume-if-a-publicly-published-project-has-no-license

Technically, this is not a repo, just a Gist. As for my solution, I consider it Public Domain 😄

Okay cool. This was exactly what I was looking for my project
Thanks 😊

@rigogsilva

Copy link
Copy Markdown

Good one!

@therakeshm

Copy link
Copy Markdown

Sorry, but how to print it on HTML, anyone please show me a way.

@andyg2

andyg2 commented Jun 21, 2020

Copy link
Copy Markdown

Sorry, but how to print it on HTML, anyone please show me a way.

var color = perc2color(40);
document.body.style.backgroundColor = color;

@crepehat

crepehat commented Jul 3, 2020

Copy link
Copy Markdown

You've made my day. Thankyou.

@afarbman

Copy link
Copy Markdown

Thank you very much! awesome gist

@EsraaQandel

Copy link
Copy Markdown

wanna use a different gradient? more flexibility with colors?
Check this example out
http://jsfiddle.net/dj0xmazk/13/

@aymyo

aymyo commented Jan 13, 2021

Copy link
Copy Markdown

Exactly what I needed, thank you!

@octameter

Copy link
Copy Markdown

Nice!

@aderbas

aderbas commented Jan 31, 2022

Copy link
Copy Markdown

Can anyone help with the reverse? From green to red (0 - green .... 100 - red)

@mlocati

mlocati commented Jan 31, 2022

Copy link
Copy Markdown
Author

@aderbas simply call perc2color(100 - yourPercentage)

@aderbas

aderbas commented Feb 1, 2022

Copy link
Copy Markdown

How did I not think of that? Thank you very much. =)

@sambenne

Copy link
Copy Markdown

A PHP version

function percentageToColour($percentage): string
{
    $r = $g = $b = 0;
    if ($percentage < 50) {
        $r = 255;
        $g = round(5.1 * $percentage);
    } else {
        $g = 255;
        $r = round(510 - 5.10 * $percentage);
    }
    $h = $r * 0x10000 + $g * 0x100 + $b * 0x1;
    return '#'.substr(('000000'.dechex($h)), -6);
}

@aktentasche

Copy link
Copy Markdown

cool thanks

@jbundziow

Copy link
Copy Markdown

Great! It works well in my project. Thanks.

@HamedChief

Copy link
Copy Markdown

Nice. Thanks Bro

@quozl

quozl commented Jun 13, 2026

Copy link
Copy Markdown

Tracked a bug down in my own code related to this gist, if perc is not a number, you get NaN in the output. Adding this as a defense and modernising;

function perc2color(perc) {
    let cleanPerc = parseFloat(perc);
    if (isNaN(cleanPerc)) cleanPerc = 0;

    cleanPerc = Math.max(0, Math.min(100, cleanPerc));

    let r = 0, g = 0, b = 0;
    if (cleanPerc < 50) {
        r = 255;
        g = Math.round(5.1 * cleanPerc);
    } else {
        g = 255;
        r = Math.round(510 - 5.10 * cleanPerc);
    }
    
    const h = (r << 16) + (g << 8) + b;
    return '#' + h.toString(16).padStart(6, '0');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment