tags:

views:

593

answers:

2

How do you convert an integer to a usable color (for PictureBox.CreateGraphics)?

The color should start at red, cycle to orange, yellow, etc. and come all the way back around to red again.

This is in vb.net. If I cannot do this, how do I use PictureBox.CreateGraphics with a hex code instead of a pen?

Thanks for the help!

+2  A: 

How about using System.Drawing.Color.FromArgb() method ?

Rubens Farias
+1 coz I forgot to before.
MusiGenesis
+1 coz it does the RGB thing and now I know how ;)
Cyclone
+6  A: 

You can use HSB (Hue, Saturation, Brightness) color instead of RGB color. .Net can convert RGB colors to HSB automatically (with the Color.GetHue, .GetSaturation and .GetBrightness methods) but doesn't go in the other direction. Here is a code sample that handles converting HSB colors to RGB:

http://splinter.com.au/blog/?p=29

(this code sample uses "V" instead of "B", probably for "value" instead of "brightness").

The advantage of using HSB color is that the Hue parameter ranges from 0 to 360, and can be interpreted as position on the color wheel, so the values wrap around nicely from 360 back to 0. For your purposes, you could create colors by setting the Saturation and Brightness values to 1.0 (their maximums) and then varying the Hue value to create the different colors of the spectrum.

In regards to your specific question (and to elaborate on Rubens' answer), you can create a Color from any int32 value like this:

int i = 4837429;
Color color = Color.FromArgb(i);

However, this won't achieve the wrap-around color effect that you describe in your question, and in fact much of the variation in your int32 values (assuming you range from the MinValue to the MaxValue) will apply to the alpha channel, or the opacity, which doesn't sound like what you want.

Update: here's something that should do what you need:

private const double ONE_SIXTH = 
    0.16666666666666666666666666666667;
private const double ONE_THIRD = 
    0.33333333333333333333333333333333;
private const double TWO_THIRDS = 
    0.66666666666666666666666666666667;
private const double FIVE_SIXTHS = 
    0.83333333333333333333333333333333;
public Color WheelColor(double d)
{
    if ((d < 0.0) || (d > 1.0))
    {
        throw new ArgumentOutOfRangeException("d",
            d, "d must be between 0.0 and 1.0, inclusive");
    }
    double R = 1;
    double G = 1;
    double B = 1;
    if (d < ONE_SIXTH)
    {
        G = d / ONE_SIXTH;
        B = 0;
    }
    else if (d < ONE_THIRD)
    {
        R = 1 - ((d - ONE_SIXTH) / ONE_SIXTH);
        B = 0;
    }
    else if (d < 0.5)
    {
        R = 0;
        B = (d - ONE_THIRD) / ONE_SIXTH;
    }
    else if (d < TWO_THIRDS)
    {
        R = 0;
        G = 1 - ((d - 0.5) / ONE_SIXTH);
    }
    else if (d < FIVE_SIXTHS)
    {
        R = (d - TWO_THIRDS) / ONE_SIXTH;
        G = 0;
    }
    else
    {
        B = 1 - ((d - FIVE_SIXTHS) / ONE_SIXTH);
        G = 0;
    }
    return Color.FromArgb((int)(R * 255), 
        (int)(G * 255), (int)(B * 255));
}

The d parameter in WheelColor is meant to go from 0.0 to 1.0, and will cycle through the color wheel (sort of), starting at red when d = 0.0 and coming back to red when d = 1.0.

MusiGenesis
+1: ty for your complete answer!
Rubens Farias
Ouch, sounds hard lol...ill see if I can whip up a subroutine thatll convert my integer to a usable number.
Cyclone
Couldn't do it >.>
Cyclone
Also the question was about vb,net, not c#. I can't port the code :S
Cyclone
I added a method that will cycle through the color wheel with an input parameter from 0.0 to 1.0. It should be easy to convert this method to VB.Net.
MusiGenesis
Just noticed you said 0.0 to 1.0, thatll help a bit lol. Thanks so much!!!!!!!
Cyclone