Earlier this week I was tasked with displaying some data from an excel
spreadsheet, the details of which aren't important. I decided to display the
range of values with a cold-to-hot color model which would then be used to
color objects in Adobe InDesign via scripting. Again, none of this is
important, ignore the software names, this is just an introduction.
Since I have a deep dislike of number-crunching, I didn't want to figure out
what my number ranges should be and assign them to pre-defined colors....like
dark blue represents 1200-1600 widgets, and green represents 2300-2700
widgets, etc. Boring. Tedious. Yuck. Instead I wanted to match a given value
to any point on the cold-to-hot spectrum. In other words, an analog color
scheme, with full blue at 0%, full red at 100% and all the colors in between.
However, in the RGB color model there's no single value that you can
increase proportionally to the percentage increase. For example, if VB had
the HSB model (Hue, Saturation, Brightness), I could simply increase the Hue
value to slide through the spectrum. With RGB you have to alternately
increase and decrease the channels' values to get from a to b, or in this
case B to R :)
By a happy coincidence, the number of steps it takes to get from B to R
coincides with the quarter percentages...2 5%, 50%, 75% and 100%. So:
0% - B
25% - GB
50% - G
75% - RG
100% - R
where each of the letters above represents the color channel at the full
255. So to get from 0 to 25% you have to increase the green, but to get from
25 to 50% you have to decrease the blue, and so on.
Here’s my code:
Private Function ColorValue(ByVa l percent As Double) As Color
Dim R, G, B As Byte
Select Case True
Case percent < 25
R = 0
G = percent / 25 * 255
B = 255
Case percent < 50
R = 0
G = 255
B = (50 - percent) / 50 * 255
Case percent < 75
R = percent / 75 * 255
G = 255
B = 0
Case percent < 100
R = 255
G = (100 - percent) / 100 * 255
B = 0
End Select
ColorValue = Color.FromArgb( R, G, B)
End Function
It works perfectly well, but I wondered if anyone had an alternative
approach. For some reason it just doesn’t feel satisfying and I have this
nagging feeling that there’s a more elegant or clever method that I’m not
thinking of or that I’m simply not aware of. And for the sake of my own
education I’d like to hear any new ideas.
Thanks. Sorry for the length of the post.
spreadsheet, the details of which aren't important. I decided to display the
range of values with a cold-to-hot color model which would then be used to
color objects in Adobe InDesign via scripting. Again, none of this is
important, ignore the software names, this is just an introduction.
Since I have a deep dislike of number-crunching, I didn't want to figure out
what my number ranges should be and assign them to pre-defined colors....like
dark blue represents 1200-1600 widgets, and green represents 2300-2700
widgets, etc. Boring. Tedious. Yuck. Instead I wanted to match a given value
to any point on the cold-to-hot spectrum. In other words, an analog color
scheme, with full blue at 0%, full red at 100% and all the colors in between.
However, in the RGB color model there's no single value that you can
increase proportionally to the percentage increase. For example, if VB had
the HSB model (Hue, Saturation, Brightness), I could simply increase the Hue
value to slide through the spectrum. With RGB you have to alternately
increase and decrease the channels' values to get from a to b, or in this
case B to R :)
By a happy coincidence, the number of steps it takes to get from B to R
coincides with the quarter percentages...2 5%, 50%, 75% and 100%. So:
0% - B
25% - GB
50% - G
75% - RG
100% - R
where each of the letters above represents the color channel at the full
255. So to get from 0 to 25% you have to increase the green, but to get from
25 to 50% you have to decrease the blue, and so on.
Here’s my code:
Private Function ColorValue(ByVa l percent As Double) As Color
Dim R, G, B As Byte
Select Case True
Case percent < 25
R = 0
G = percent / 25 * 255
B = 255
Case percent < 50
R = 0
G = 255
B = (50 - percent) / 50 * 255
Case percent < 75
R = percent / 75 * 255
G = 255
B = 0
Case percent < 100
R = 255
G = (100 - percent) / 100 * 255
B = 0
End Select
ColorValue = Color.FromArgb( R, G, B)
End Function
It works perfectly well, but I wondered if anyone had an alternative
approach. For some reason it just doesn’t feel satisfying and I have this
nagging feeling that there’s a more elegant or clever method that I’m not
thinking of or that I’m simply not aware of. And for the sake of my own
education I’d like to hear any new ideas.
Thanks. Sorry for the length of the post.
Comment