Solved

# Calculate RGB for False Color Map

Posted on 2004-04-22
1,240 Views
HI,
I think we've all seen false color maps, say maps of
the USA with different elevations assigned different colors,
say from a red for high altitudes down through the spectrum to Blue or even Purple for low altitudes.
I'm trying to figure out how to calculate RGB values for
say the Red down to the Blue values, with 255 intervals.
I was thinking of using a colorpicker App to grab these
off an existing map, but you'd never get exactly 255 values
evenly spaced.
There must be a way to calculate a 255 value spectrum of RGB values from Red Though Blue.
Any comments or references to code out there on the web appreciated.
0
Question by:thunder44
• 5
• 3
• 2
• +2

LVL 26

Expert Comment

ID: 10890340
0

LVL 6

Expert Comment

ID: 10890516
I hope to understand what you need ...

dim colMap(0 to 255) as long

for j = 0 to 255
colmap(j) = rgb(255-j,0,j)
next
0

LVL 4

Expert Comment

ID: 10891087
I think I see what you're saying, but I'm afraid that to get the results you're looking for, you're going to have to come up with a color palette.  Basically, you've got a grescale heightmap (0-255) that you want to have colored fading from one color to the other.  You'd have to change your bitmap to a 256 color bitmap, and add the color palette for the equivilent colors from 0-255.

-Javin
0

Author Comment

ID: 10891537
Heres an example:

http://www.ima.umn.edu/~arnold/interpolation/interpolation.html

see "black-hole-2d-contour.gif"

Another Example:

http://aol.wff.nasa.gov/aoltm/projects/beachmap/97results/970917calvert/

Look at the picture next to "Cliff Region"

Notice the multicolored bar in both pictures. I need to draw, say on a form or in a picturebox, a bar like this. It has to have 255 color shades in it, in a spectrum like the bar in the Examples.
Calculating the correct RGB values to create the bar is the problem.

0

LVL 17

Expert Comment

ID: 10893078
It shouldn't be too difficult (fading). You just need to know the start/end positions for the r,g and b values.

I recently did a question on something similar to this... worth a look to get a basic understanding:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20947594.html

Me.Visible = True
'Autoredraw=true so the lines will paint...
Me.AutoRedraw = True
'Call painting sub...
End Sub
Dim iR As Integer, iG As Integer, iB As Integer, lRGB As Long
iR = 255
iG = 0
iB = 0
Do Until iR = 0
lRGB = RGB(iR, iG, iB)
'Just draw a line on form for testing...
Form1.Line (iB * 15, 0)-(iB * 15, 20 * 15), lRGB
'Increment/Decrement R,G and B values how you require..
'this will just have R go from 255 to 0 and B go from
'0 to 255.. G is not used so the fade will be from red
'to purplish to blue (no green since it's not used)
iR = iR - 1
iB = iB + 1
Loop
End Sub
0

Author Comment

ID: 10896357
Thanks for the replys, if I modify zzzzzoo's code above:

'0 to 255.. G is not used so the fade will be from red
'to purplish to blue (no green since it's not used)
iR = iR - 1
iB = iB + 1
iG = iG + iB                '<<<<<<<Added this line
Loop
End Sub

I get a pretty good spectrum. Theres  255 values there, but they don't
appear to be unique. If I just print out the RGB combinations to the form I note values like rgb(210,1035,45) and rgb(209,1081,46). First theres no RGB values over 255, Secondly these two examples seem to yeild the same yellow color if I use them to set the BackColor of a Picturebox to see them side by side.
Recently used a colorpicker to pick out 109 unique colors out of the second example picture spectrum bar. I suppose its possible that thats the best you can do with RGB values.

mmusante's code if modified a bit produces the same two color fade as the original zzzzzooc code.

0

LVL 17

Assisted Solution

zzzzzooc earned 250 total points
ID: 10896472
My example never has any R, G or B value exceed 255.

>> iG = iG + iB

That's the problem. *You* add iB to iG on each loop. iB increments on it's own to 255 and when you add iB to iG, you're eventually adding 240, 241, 242, etc to iG. So it's logical it'll extend 255. That's why previously I mentioned you needed to know the start/end positions of the r, g and b values.

You won't be able to make a nice "fade" using all types of colors within only 255 colors. The total amount of different RGB colors is over 16 million and to try to limit them all into a 255 color span, you'll have "rough" fading. The best way is just to fade from 2 different colors (ie: red to blue) which my example did and which your question asked for.

0

LVL 6

Accepted Solution

mmusante earned 250 total points
ID: 10896837
To have a fade with all types of color you can try to distribute them in the Hue component of the HSV color space and than translate your colors in the RBG space ...

here the rgb <-> hsv conversion ...(http://www.aboutvb.de/khw/artikel/khwrgbhlshsv.htm) (sorry not in english)

0

LVL 6

Expert Comment

ID: 10896922
take a look here (http://dev.midar.com/Tutorials/Computers/Programming/VB/HSVtoRGB.asp)

download the zip file you can find a lot of useful color handling utilities inside ...
i think you can use the HSV function inside this project in this way ...

dim colMap(0 to 255) as long

for j = 0 to 255
colmap(j) = hsv(j,1,1)
next

this will produce a kind of red -> blue fade
0

Author Comment

ID: 10903728
zzzzzooc's comment about not fitting a smooth fade into only 255 values is correct.
Had a chance to modify zzzzzooc's code a bit more:

Dim iR As Integer, iG As Integer, iB As Integer, lRGB As Long

iR = 255
iG = 0
iB = 0
Do Until iG = 255
lRGB = RGB(iR, iG, iB)
frmDrawFade1.Line (iG * 15, 0)-(iG * 15, 30 * 15), lRGB
iG = iG + 1
Loop

iR = 255
iG = 255
iB = 0
Do Until iB = 255
lRGB = RGB(iR, iG, iB)
frmDrawFade1.Line (iB * 15, 500)-(iB * 15, 30 * 15 + 500), lRGB
iR = iR - 1
iB = iB + 1
Loop

iR = 0
iG = 255
iB = 255
Do Until iG = 0
lRGB = RGB(iR, iG, iB)
frmDrawFade1.Line (iG * 15, 1000)-(iG * 15, 30 * 15 + 1000), lRGB
iG = iG - 1
Loop

iR = 0
iG = 0
iB = 255
Do Until iB = 0
lRGB = RGB(iR, iG, iB)
frmDrawFade1.Line (iB * 15, 1500)-(iB * 15, 30 * 15 + 1500), lRGB
iR = iR + 1
iB = iB - 1
Loop

End Sub

Noticed on the second example picture I mentioned that the color bar is not a smooth fade. I noticed that the RGB values were in four sections of fades. The Sub above recreates these four sections. (note the last two are reversed due to variable deincrementation). You start at Red , go though the spectrum and end back up at red again. If you wanted to create a Fade through the spectrum, within 255 values, you would, as zzzzzooc suggests, have to do a sort of step thu the values at increments.
What I wanted in the first place are more or less evenly spaced colors/RGB values over a 255 range. Now that I understand what that range is (the 4 sections that if lined up properly represent the spectrum colors/RGB values), getting that won't be a problem.

0

Author Comment

ID: 10903852
Decided to close the question. Split the points between zzzzzooc and mmusante.  Pretty much a dead heat, zzzzzooc's code got my brain whirring, mmusantes references were excellent. HSV is something I need to check out. Thanks also to EDDYKT anf Javin007.
0

Author Comment

ID: 10905707
Heres a version of the Sub that uses the first 3 fade sections  to create a Spectrum fade from red at one end to blue on the other. You can use the purple area on the fourth section but the math is more complex to end up with exactly 255 colors.

Dim iR As Integer, iG As Integer, iB As Integer, inc As Integer, lRGB As Long

'Each one of the three 'Do Until' loops goes thru 85
'increments. 85 * 3 = 255

'three 85 element sections of evenly incremented
'colors shown at top, put together at bottom into
'spectrum of 255 color values

'Note array wastes space by keeping some values that
'don't change

iR = 255
iG = 0
iB = 0
Do Until iG = 255
inc = inc + 1
lRGB = RGB(iR, iG, iB)
frmDrawFade3.Line (iG * 30, 0)-(iG * 30, 10 * 30), lRGB
iG = iG + 3
Loop

iR = 255
iG = 255
iB = 0
Do Until iB = 255
inc = inc + 1
lRGB = RGB(iR, iG, iB)
frmDrawFade3.Line (iB * 30, 500)-(iB * 30, 10 * 30 + 500), lRGB
iR = iR - 3
iB = iB + 3
Loop

iR = 0
iG = 255
iB = 255
Do Until iG = 0
inc = inc + 1
lRGB = RGB(iR, iG, iB)
frmDrawFade3.Line (iG * 30, 1000)-(iG * 30, 10 * 30 + 1000), lRGB
iG = iG - 3
Loop

For inc = 1 To 255
lRGB = RGB(iR, iG, iB)
frmDrawFade3.Line (inc * 30, 1500)-(inc * 30, 10 * 30 + 1500), lRGB
Next inc

End Sub

0

## Featured Post

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…