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!
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
int32value like this: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
int32values (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:
The
dparameter 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.