473,382 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Color spectrum algorithm

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...25%, 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(ByVal 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.

Nov 21 '05 #1
3 10401
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 :)


hsb code:
https://planet-source-code.com/vb/sc...txtCodeId=3314
&lngWId=10

Nov 21 '05 #2
There is HSB code inthe GDI+ FAQ.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Neil West" <Ne******@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
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...25%, 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(ByVal 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.

Nov 21 '05 #3
Another thing you might wish to do is to create a 1 bit high bitmap, draw a
colour spectrum across it using a gradient and then pick a colour using the
X coordinate as an index. I've done this before with a gradient brush that
used a ColorBlend to create a wide rainbow of shades.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Neil West" <Ne******@discussions.microsoft.com> wrote in message
news:34**********************************@microsof t.com...
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...25%, 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(ByVal 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.

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andrew Poulos | last post by:
I need to build a colour wheel. It has these specs: - The user needs to be able to select a colour from it - It should show as many colours as reasonable. The client wanted the entire 16 million+...
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
2
by: riscy | last post by:
Has anyone know good webpage or book covering C# code in transformation of real time or sampled time data into FFT spectrum display (using GDI+). I like to learn FFT processing in abc way...can...
2
by: Julio C. Hernandez Castro | last post by:
Dear all, We have just developped a new block cipher called Raiden, following a Feistel Network structure by means of genetic programming. Our intention now consists on getting as much feedback...
1
by: David S. Zuza | last post by:
Hi, I am an intermediate vb developer but I am an infant to DirectX programming, I want to develop a spectrum analyzer for audio whether it be from microphone or audio file...but there doesn't...
2
by: Holger | last post by:
Dear all, I need to do a FFT on an array of 20k real values. Origin of the sampled data is a sinus wave with light harmonics. The goal is an frequency spectrum with the magnitudes of the first...
5
vdraceil
by: vdraceil | last post by:
Hi everyone,i wish to make a spectrum bar dance for an audio song,that is if i play an audio song the spectrum bars must dance according to the frequency of the beats in the song..also the bars must...
0
by: serdar | last post by:
Hi I'm designing a spectrum analyzer for a flash media player using the new computeSpectrum method in AS3. There are two basic types of visualizers I'm working on: -wave-type (displays a sound...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.