Previous in Forum: Mail   Next in Forum: PWM, Joysticks and Visual Basic
Close
Close
Close
27 comments
Rating: Comments: Nested
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468

"Rainbow" Colours in Visual Basic

11/03/2010 6:27 AM

Does anyone know of a simple way to set up a table of RGB colour codes corresponding (roughly) to the visible spectrum? Maybe a wavelength-to-RGBcolor function?

I've started mucking about with Mandelbrot figures by knocking up a bit of VB, and the results are promising, but limited by the colour range I'm using (10-colour manually built look-up table). Ideally, I'd have a table of 1000 values from red to violet.

I could build a table manually by using the Color Dialog Box, or by using the eye-dropper in e.g. Paintshop and copying the values, but either of these would be pretty laborious, and I may want to change the range of colours or the size of the table.

Any help would be appreciated. Here's a sample picture of what I've got so far:

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.

Good Answers:

These comments received enough positive votes to make them "good answers".

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Guru
Engineering Fields - Optical Engineering - Member Engineering Fields - Engineering Physics - Member Engineering Fields - Systems Engineering - Member

Join Date: Apr 2010
Location: Trantor
Posts: 5363
Good Answers: 647
#1

Re: "Rainbow" Colours in Visual Basic

11/03/2010 9:37 AM

I'm not sure that even someone with excellent color vision could detect a thousand separate colors across the spectrum. The spectrum goes from about 380 nm to 780 nm, so there are only a total of 400 steps, 1 nanometers wide, from the deepest blue-violet to deep red. Over the entire color palette, including mixed colors, pastels, etc., a 1000 colors can easily be discerned -- but not over just the rainbow itself. Especially considering that the eye is less sensitive to color shifts in the green part of the spectrum than in the blue or red.

Considering the diagram below, I think for a first try at creating the rainbow of colors I'd linearly interpolate along the blue-green line while holding red fixed at 00. Blue would go from FF to 00 while green goes from 00 to FF. Then I'd linearly interpolate along the green-red line while holding blue at 00. Green would go from FF to 00 while red goes from 00 to FF. This would give you a total of 256 + 255 = 511 colors, while defining a 'rainbow' that goes from blue to green to red.

The 'rainbow' is defined by the curved line that begins near B goes around G and on past R. The triangle is the NTSC color palette -- which may not match your monitor, but I show it here to point out that no monitor can actually produce all the colors seen in nature. The triangle defines the limits of the monitor's color palette. The diagram is the 1931 CIE color space diagram.

__________________
Whiskey, women -- and astrophysics. Because sometimes a problem can't be solved with just whiskey and women.
Register to Reply Score 1 for Good Answer
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#2
In reply to #1

Re: "Rainbow" Colours in Visual Basic

11/03/2010 10:43 AM

Thanks for the reply.

I agree completely about 1000 being overkill for the number of colours - the reasoning is that my program generates an array of numbers, each in the range 0 to 1000, which have to be mapped onto the colours, and it's a nice easy way to start seeing the results. I also realize the shortcomings of colour monitors re. their range of possible colours.

The results I'm after don't need to be anywhere near an accurate representation of the spectrum, just a nice smooth sequence of colours in the right order.

I had a quick play with your suggested approach (using Excel & VBA) with these results:

.. promising, but lacking the yellows. This mod looks a bit better:

I'll keep playing (unless someone comes up with a nice ready-made function). Thanks again.

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#6
In reply to #1

Re: "Rainbow" Colours in Visual Basic

11/03/2010 12:23 PM

This is more like it:

In this, Blue goes from 255 to 0 while Green goes from 0 to 255 (with Red = 0),

then Red goes from 0 to 255 (with Blue = 0 and Green = 255),

then Green goes from 255 to 0 (with Red = 255 and Blue = 0).

May need a bit of messing with to extend from blue to violet, and maybe to reduce the undifferentiated ("boring") regions of blue and green. Only 768 points rather than my target 1000. It may be easier to go for 500 instead.

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply Score 1 for Good Answer
Guru
Engineering Fields - Optical Engineering - Member Engineering Fields - Engineering Physics - Member Engineering Fields - Systems Engineering - Member

Join Date: Apr 2010
Location: Trantor
Posts: 5363
Good Answers: 647
#7
In reply to #6

Re: "Rainbow" Colours in Visual Basic

11/03/2010 1:00 PM

Yes, you are correct. I had forgotten about the need to produce yellow at full intensity, which requires G and R both at 255, otherwise 'yellow' looks like a muddy brown. Good catch.

You are also correct about the 'undifferentiated' blue-green regions. If the information is plotted in a Uniform Color Space (CIE - UCS 1976 u', v' Diagram, if you want to google it) the green region of the color space shrinks, to account for the lack of differentiation you noticed. The space is 'uniform' then, because (almost) for any given distance Δs = (Δu'2 + Δv'2)1/2 on the UCS diagram the difference appears as an equal amount of color variation to the eye.

__________________
Whiskey, women -- and astrophysics. Because sometimes a problem can't be solved with just whiskey and women.
Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#8
In reply to #7

Re: "Rainbow" Colours in Visual Basic

11/03/2010 6:24 PM

Thanks for the CIE-UCS hint. May be interesting to try the same regions of the Mandelbrot set mapping represented using a colour temperature (Plankian?, mired?) type scale.

I'm gonna have fun - and I promise I'll post some results.

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Guru

Join Date: Mar 2007
Location: City of Light
Posts: 3943
Good Answers: 183
#3

Re: "Rainbow" Colours in Visual Basic

11/03/2010 10:48 AM

With above suggestion you get 1020 possibilities covering - I presume all possible combinations. The 1st gives white to black and the others by simple linear variation of the parameters in the VB RGB function the different values.

Programming is simple as you will recognize;

Hope it will help

nick

Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#4
In reply to #3

Re: "Rainbow" Colours in Visual Basic

11/03/2010 11:12 AM

Don't think it'll quite cover all the combinations (224) - but I'll give it a try, thanks.

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#5
In reply to #3

Re: "Rainbow" Colours in Visual Basic

11/03/2010 11:57 AM

Pretty, but sadly not much like a rainbow:

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Guru
Hobbies - Musician - New Member Australia - Member - Torn and breading Engineering Fields - Nanoengineering - New Member APIX Pilot Plant Design Project - Member - New Member

Join Date: Jun 2006
Location: Magnetic Island, Queensland, Australia
Posts: 3721
Good Answers: 74
#9

Re: "Rainbow" Colours in Visual Basic

11/03/2010 6:25 PM

OT?

__________________
The Twain Has Met
Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#10
In reply to #9

Re: "Rainbow" Colours in Visual Basic

11/03/2010 6:40 PM

OT? OK

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Guru
Engineering Fields - Marine Engineering - New Member

Join Date: May 2007
Location: Australia.
Posts: 1642
Good Answers: 81
#11

Re: "Rainbow" Colours in Visual Basic

11/03/2010 6:58 PM

Don't know if it will help, but the book "Programming Windows 5th Edition" has a accompanying disk that has a color scroll programme. Three sliding scales 0 to 255, and the resultant color displayed.

Regards JD.

Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#12
In reply to #11

Re: "Rainbow" Colours in Visual Basic

11/03/2010 7:24 PM

Thanks, JD(ret) - the problem with that approach is much the same as with the Color Dialog Box or a colour picker in another program, in that it takes a lot of effort to create (what to me seems should be) a simple colour lookup table.

Think I'm onto it now - will post some images of the results. Watch this space.

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Guru
Engineering Fields - Electromechanical Engineering - Technical Services Manager Canada - Member - Army brat Popular Science - Cosmology - What is Time and what is Energy? Technical Fields - Architecture - Draftsperson Hobbies - RC Aircraft - New Member

Join Date: Sep 2006
Location: Clive, Alberta, Canada
Posts: 5916
Good Answers: 204
#13
In reply to #12

Re: "Rainbow" Colours in Visual Basic

11/03/2010 11:35 PM

Most of this is over my head but here is a cool set of color charts, if of any help, I don't know.

http://free-textures.got3d.com/color-chart.html
http://free-textures.got3d.com/wheel.html
http://www.ficml.org/jemimap/style/color/wheel.html

Also remember DCad's post on Fill Color in VBA. He is pretty knowledgeable about color stuff too.

Chris

Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#17
In reply to #13

Re: "Rainbow" Colours in Visual Basic

11/05/2010 7:26 AM

Thanks Chris - useful charts there.

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Anonymous Poster
#23
In reply to #13

Re: "Rainbow" Colours in Visual Basic

11/11/2010 10:04 AM

Hi Chris,

Interesting and useful. Thanks for the info, Gil.

Register to Reply
Guru

Join Date: Dec 2009
Posts: 581
Good Answers: 15
#14

Re: "Rainbow" Colours in Visual Basic

11/04/2010 12:09 PM

I think what you're looking for is for a hue-saturation-value color model, not rgb. By cranking up the "saturation" to max, setting about a 2/3rds "value" and then varying the "hue", you'll get the whole variety of bright colors. Play with the color pickers on Photoshop or the Gimp for a little while and you'll see what I mean.

There is source code available on the web for converting from rgb to hsv and back. It's a kind of complicated algorithm, but you might even find one in VB.

__________________
Ignorance is no sin. Willful ignorance is unforgiveable.
Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#18
In reply to #14

Re: "Rainbow" Colours in Visual Basic

11/05/2010 7:30 AM

Sounds like a good approach - I may come back to it later (think I've got a reasonable "spectrum" to work with, for now).

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Commentator

Join Date: Dec 2009
Location: Coffeyville, KS
Posts: 59
#15

Re: "Rainbow" Colours in Visual Basic

11/04/2010 1:05 PM

If you send me an e-mail (olsen.pete at gmail dot com) I will send you an excel spreadsheet that I wrote after reading your request that covers 2^24 vba colors. You use scroll bars to change RGB values and it updates cell A1 after each execute of the calculate functions (Let go of the mouse). It will also show you the hex, decimal and windows values of that color.

__________________
Regards, Pete Olsen
Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#19
In reply to #15

Re: "Rainbow" Colours in Visual Basic

11/05/2010 7:31 AM

Hi, Pete - will be interested to see what you've got.

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#21
In reply to #15

Re: "Rainbow" Colours in Visual Basic

11/05/2010 11:32 AM

Nice little bit of code, thanks Pete.

While messing with the sliders, I came across a curious discontinuity of observed colours, and cross-checked it with my spreadsheet (which also uses the VBA RGB function) - here are the results:

The same numbers fed into the RGB function to colour the background of a label in VB6 don't show the discontinuity:

Do you (or does anyone else) find the same effect? FYI, I'm using Excel 2003 SP3.

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Guru

Join Date: Dec 2009
Posts: 581
Good Answers: 15
#22
In reply to #21

Re: "Rainbow" Colours in Visual Basic

11/05/2010 12:18 PM

Such discontinuities are unavoidable because of the different color models. A good algorithm can correct for them, (just like a poor algorithm can produce more anomalies) but there's not a 1-1 correlation between every RGB color and every HSV (or CMYK) color. 24-bit RGB only creates 16 million different colors, after all, and there are certainly more colors than that in the world. (Color being analog and all.)

__________________
Ignorance is no sin. Willful ignorance is unforgiveable.
Register to Reply
Anonymous Poster
#16

Re: "Rainbow" Colours in Visual Basic

11/04/2010 1:36 PM

Check the circuits on a digital television receiver or transmitter.

Register to Reply Score 1 for Off Topic
2
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#20

Re: "Rainbow" Colours in Visual Basic

11/05/2010 7:50 AM

Here's my "spectrum":

... and a couple of results:

I'm now playing with the way the numbers map onto the colours - my results don't seem nearly as spectacular (to me) as Pietgen's images reproduced in Dewdney's article from 1985.

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply Good Answer (Score 2)
Participant

Join Date: Sep 2012
Posts: 1
#24
In reply to #20

Re: "Rainbow" Colours in Visual Basic

09/19/2012 7:46 PM

JohnDG,

Would you mind share your VBA code? Thanks.

Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#27
In reply to #24

Re: "Rainbow" Colours in Visual Basic

11/15/2012 4:32 AM

If you're still around: I haven't forgotten, I'm just up to the eyeballs with work. I'll send you something when I get time.

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply
Participant

Join Date: Nov 2012
Posts: 1
#25
In reply to #20

Re: "Rainbow" Colours in Visual Basic

11/14/2012 7:52 PM

Hi JohnDG,

Can you show your algorithm for spacing the rainbow colors out nicely and adding indigo and violet? This is the perfect color legend.

Thanks a lot,

Dave

Register to Reply
Guru
United Kingdom - Member - Not a New Member Hobbies - Musician - New Member Hobbies - Fishing - New Member

Join Date: May 2006
Location: Reading, Berkshire, UK. Going under cover.
Posts: 9684
Good Answers: 468
#26
In reply to #25

Re: "Rainbow" Colours in Visual Basic

11/15/2012 4:27 AM

Hope this makes sense; CR4 editor has an annoying habit of getting rid of whitespace, which makes it very difficult to post code. It took a lot of fiddling about with the numbers to get the result - it's probably not the best, but it gives a pleasing result:

---------------------------------------------

Public Spectrum(0 to 999) as long

Public Sub MakeSpectrum()
Dim r, g, b, i

i = 0

'i=0 to 102, R and B increase together from 24 to 126, with G=0
For b = 24 To 126
Spectrum(i) = RGB(b, 0, b)
i = i + 1
Next b

'i=103 to 231, R decreases from 126 to 0 while B increases from 126 to 255, with G=0
For r = 127 To 0 Step -1
Spectrum(i) = RGB(r, 0, 255 - r)
i = i + 1
Next r

'i=232 to 486, G increases from 0 to 255 while B decreases from 255 to 0, with R=0
For g = 0 To 255
Spectrum(i) = RGB(0, g, 255 - g)
i = i + 1
Next g

'i=487 to 742, R increases from 0 to 255, with G=255 and B=0
For r = 0 To 255
Spectrum(i) = RGB(r, 255, 0)
i = i + 1
Next r

'i=743 to 998, G decreases from 255 to 0, with R=255 and B=0
For g = 255 To 0 Step -1
Spectrum(i) = RGB(255, g, 0)
i = i + 1
Next g

Spectrum(i) = RGB(255, 0, 0) ' padding

End Sub

---------------------------------------------

Note: I originally did it without comments, and added comments this morning. There could be mistakes in the comments, but the code bit works!

__________________
"Love justice, you who rule the world" - Dante Alighieri
Register to Reply Score 1 for Good Answer
Register to Reply 27 comments

Good Answers:

These comments received enough positive votes to make them "good answers".

"Almost" Good Answers:

Check out these comments that don't yet have enough votes to be "official" good answers and, if you agree with them, vote them!
Copy to Clipboard

Users who posted comments:

Anonymous Poster (2); chrisg288 (1); honfui (1); jdretired (1); JohnDG (14); ky (1); Lynn.Wallace (2); nick name (1); peteolsen (1); spacely (1); Usbport (2)

Previous in Forum: Mail   Next in Forum: PWM, Joysticks and Visual Basic

Advertisement