Anand
Anand

Reputation: 10400

How to generate a Color Sequence in javascript?

I have to display a number whose color changes from Red to Green uniformly as the value of the number decreases.

Is there any way to generate the font color of that number according to the value ?

Upvotes: 4

Views: 2293

Answers (2)

Morten Bergfall
Morten Bergfall

Reputation: 2316

Decreasing number equals red to green, I'd guess?

Why not make it simple, start out with your all red #FF0000, and work yourself down to green at #00FF00?

Between the two there are FF*FF (256*256 = 65536) steps; adapt this to your needs and the JavaScript needed to get color should be simple.

ColorCalc - Nice tool for playing with/understanding hex v colors

Upvotes: 1

David Yell
David Yell

Reputation: 11855

I would approach this using CSS. I would create a set of styles, in the gradients of the colours that you need. Then using javascript I would apply the style which matched the number.

This way you can seperate your presentation, structure and js logic from each other, whilst keeping your js simple :)

Upvotes: 0

Related Questions