• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1235
  • Last Modified:

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.
0
BWATERS
Asked:
BWATERS
  • 5
  • 4
  • 2
  • +2
1 Solution
 
TSMITHCommented:
Do you mean 256 colours that best cover the spectrum?
0
 
BWATERSAuthor Commented:
Yes, 256 unique hues from white to black...or if you prefer, black to white.
Thanks for the reply
0
 
TSMITHCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
BWATERSAuthor Commented:
True...but how would you code it in VB to generate those unique values?
0
 
Jan LouwerensSoftware 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
 
maruzCommented:
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
 
BWATERSAuthor 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
 
maruzCommented:
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
 
maruzCommented:
I believe you can add a single For-cycle from 0 to 255 yourself.
0
 
BWATERSAuthor 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
 
maruzCommented:
If you don't call this VB code...
0
 
MorFFCommented:
The 216 web-safe colours, in vb code (and keeping it simple);

1 create a new form (form1),
2 add a list box (list1),
3 add a button (command1),
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")
            List1.AddItem (colourval)
        Next r
    Next g
Next b

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

Cheers - MorFF
0
 
MorFFCommented:
PS if you want the code explained, let me know.
0
 
BWATERSAuthor Commented:
Excellent, thank you!  And yes, thanks for keeping it simple...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now