# 256 RGB algorithm

Looking to generate in VB the RGB values for approximately 256 colors starting from RGB(255,255,255) to RGB(0,0,0) white to black.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Do you mean 256 colours that best cover the spectrum?
0
Author Commented:
Yes, 256 unique hues from white to black...or if you prefer, black to white.
0
Commented:
Since you only have 256 colours you would have to assume 6 shades of red green and blue, which gives 216 colours. Steps would be 0, 51, 102, 154, 205, and 255 in each colour. This would give you an even spectrum.
0
Author Commented:
True...but how would you code it in VB to generate those unique values?
0
Software EngineerCommented:
well, when you decide which colors you will use at each section, just loop through and ramp the colors...
c-type pseudocode example:

index 0: blue (0,0,255)
index 51: green (0,255,0)

(this loop will ramp smotthly from red to green)
for (index = 0; index < 51; index++)
{
red = 0;
green = 255.0 * (index / 50.0);
blue = 255.0 - (255.0 * (index / 50.0));
}
continue for rest of colors...
0
Commented:
You should better use the 3-3-2 palette.
3-3-2 means that you use 3 bits to encode intensity of red, 3 bits for green and 2 bits for blue.
You can get the values of intensities for a specific color index as:
red = ((index AND 0xE0) / 0x20) * 255 / 7
green = ((index AND 0x1C) / 0x04) * 255 / 7
blue = (index AND 0x03) * 255 / 3
The bitwise AND selects the bits for appropriate color (R, G, or B), the division moves these bits down (so you get values from 0 to 7 or from 0 to 3 for blue) and the last two operations converts the value to 0-255 scale.
0
Author Commented:
Very crafty...I ask for a solution in VB and I get a study in C.
I appreciate your feedback, now can you show me the code?
0
Commented:
Thought you could translate it yourself. Not every graphics expert knows Visual Basic - neither do I. But I'll try my best.

Set red = ((Index And 224) / 32) * 255 / 7
Set green = ((Index And 28) / 4) * 255 / 7
Set blue = (Index And 3) * 255 / 3
0
Commented:
I believe you can add a single For-cycle from 0 to 255 yourself.
0
Author Commented:
I appreciate the feedback...but perhaps I'm dense or have a mental block; but I really do need a code example in VB to work from.
0
Commented:
If you don't call this VB code...
0
Commented:
The 216 web-safe colours, in vb code (and keeping it simple);

1 create a new form (form1),
2 add a list box (list1),
4 in the command1_click event, cut-n-paste the following code;

For b = 0 To 255 Step 51
For g = 0 To 255 Step 51
For r = 0 To 255 Step 51
colourval = "R:" + Format\$(r, "000") + ", G:" + Format\$(g, "000") + ", B:" + Format\$(b, "000") + "  Hex:#" + Format\$(Hex\$(r), "00") + Format\$(Hex\$(g), "00") + Format\$(Hex\$(b), "00")
Next r
Next g
Next b

6 press the button and hey presto!
7 the listbox is populated with the rgb and hex values.

Cheers - MorFF
0

Experts Exchange Solution brought to you by