I have a small table of values like this:

Food     Count   Color
Pumpkin  345     #DB3236
Cabbage  193     ?
Pepper   89      ?
Beetroot 96      ?
Potato   62      ?
Lettuce  35      ?
Carrot   26      ?
Tomato   25      ?
Banana   22      ?
Apple    22      #FADBDB

I want to calculate a color gradient between #DB3236 and #FADBDB based on the COUNT values.

For example "Pumpkin" = 345 and has the strongest color, and "Apple" = 22 which is the weakest color. Even though "Potato" is in the middle of my table it only has a Count value of 62 which means it will be quite weak on the color gradient scale.

I'm not great with numbers so I'm really struggling how to figure this out.

How do I create the color gradient for the other foods on my table?

  • 1
    Colors are not linear so doing this calculation even for a very mathematcally talented is hard since as a definition it makes little sense. But if you want it to act linear then draw ticks by distance next to a gradient.
    – joojaa
    Commented Nov 6, 2017 at 5:22
  • Could you expand further upon the idea of ticks by distance? I don't understand the concept. There is a site here that calculates the gradient steps between two colors perbang.dk/rgbgradient but the highest amount is 64 steps and I need more than that for my data table.
    – TinyTiger
    Commented Nov 6, 2017 at 5:47
  • This site calculates up to 255 steps: herethere.net/~samson/php/color_gradient it the most I can find, but still not enough.
    – TinyTiger
    Commented Nov 6, 2017 at 6:12

2 Answers 2


As commented color is not linear, nor countable, so from a strictly physical point of view it makes little sense to do this. However you can approximate color as being linear for your purpose. While this is potentially misleading you can do it, human senses aren't that precise when it comes to absolute color variation so no worries. This should explain why is may be a bad idea for example for infographics

Anyway there is a simple visual relationship in your data and what you want to do. Just plot the points next to a gradient in y direction with values in y.

enter image description here

Note that you have very little color variation at the bottom. If instead of showing spatial relationship i would only show swatches you wouldn't have much of anything to show for it. So I suggest more different colors in your gradient (which necessitates showing the scale).

Anyway linear is easy to calculate:

color = x * start_color + (x-1) * end_color

where x is (count-min)/(max-min)

#DB3236 is 219 50 54 in decimals and 
#FADBDB 250 219 219 in decimals

So for color the interpolant is simply

red =   (count-22)/323 * 219 + ( 1 - (count-22)/323 ) * 250
green = (count-22)/323 *  50 + ( 1 -  (count-22)/323 ) * 219
blue =  (count-22)/323 *  54 + ( 1 - (count-22)/323 ) * 219
  • Thank you, I used a color picker to grab the colors I needed from your example image. Although I have the same problem again for a different set of data, so I'm trying to figure out how to recreate this again for myself but I can't understand how the code works. The last part (red, green, red, etc) is especially confusing for me. Was your code executed in Python or something similar? Mind sharing the script?
    – TinyTiger
    Commented Nov 6, 2017 at 10:37
  • @bennygill its mathematics. What language do you use?
    – joojaa
    Commented Nov 6, 2017 at 12:09
  • I am learning Python right now if you have a script for that it would be amazing! Maths is not my strong point haha.
    – TinyTiger
    Commented Nov 6, 2017 at 12:26
  • You can find a simnilar code here. But i can edit one example in later @bennygill
    – joojaa
    Commented Nov 6, 2017 at 12:33
  • I tried that example but I can't get it to work. And I've been playing around with lots of other code but still no luck. I'm a novice coder, so any examples you can give for this would be great please.
    – TinyTiger
    Commented Nov 7, 2017 at 11:58

I discovered this JavaScript fiddle which works great!

You input two RGB colors and choose how many color steps you want to output. It will then give you a list of colors along your given gradient: https://jsfiddle.net/002v98LL/

Copy the list of colors into Excel to figure out which color corresponds to which gradient step.

And you can use Google or some other tool if you need to convert between Hex and RGB codes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.