Create Colors based on # of Items

Preferably using PHP, I’d like to create X number of colors based on X number of items and using a predefined color range. For example:


Color Range
red = largest value
green = medium value
blue = smallest value

Item 1 gets color red
Item 2 gets whatever color naturally comes between red and green
Item 3 gets color green
Item 4 gets whatever color naturally comes between green and blue
Item 5 gets color blue

The code should be able to handle any number of items. 5 items was just an easy example.

Some Thoughts

  • Number of items = number of colors
  • How to divide a color range into number of items?
  • Colors can be represented in decimal format: red=255,000,000 blue=000,000,255
  • Can a formula be created, based on the color decimal format, to solve this problem?

I think it’s the math portion of the problem that I’m stuck on right now.
Thanks for your help,


Sponsor our Newsletter | Privacy Policy | Terms of Service