-
April 18th, 2007, 08:58 PM
#1
Rainbow RGB gradient
I have problems in trying to make a loop to create a rainbow gradient RGB values.
For example, if i need a gradient of red and green only, i can do like this;
using an index from 0 to 127;
Code:
for (int i=0; i<127; i++){
DWORD dwColor=RGB(i*2, 255-i*2,0);
................
}
When i plot the value of dwColor, it will give me the gradient of color from green to red.
What should i do here if i want a rainbow gradient? I just want to know how should i derive the gradient values of rainbow values.
-
April 18th, 2007, 10:06 PM
#2
Re: Rainbow RGB gradient
Links not handy, but google about for HSB to RGB conversion - it's a simple formula for converting from HSB space to RGB space, but with H - Hue - you can cycle from one color to another over the rainbow, getting RGB out from a conversion function.
-
April 19th, 2007, 09:14 AM
#3
Re: Rainbow RGB gradient
Look at the CSpectrumWnd class in this code: "Customizing the Appearance of CSliderCtrl Using Custom Draw" at http://www.codeproject.com/miscctrl/...SliderCtrl.asp
Mike
Last edited by MikeAThon; April 19th, 2007 at 12:28 PM.
Reason: To correct the name of the class (it wasn't CRainbow--sorry)
-
April 19th, 2007, 11:46 AM
#4
Re: Rainbow RGB gradient
HSB to RBG would be the easiest way to do it if you have a class / function available to do that.
However, if you just want to code it yourself the algorithm is quite simple.
Start with Red and ramp up Green.
When Green is max, ramp down red to zero.
When red is zero, ramp up blue.
When blue is max, ramp down green to zero.
When green is zero, ramp up red.
When red is is max, ramp down blue to zero.
Now you have a rainbow.
-
April 19th, 2007, 12:08 PM
#5
Re: Rainbow RGB gradient
I do not think that will give a proper rainbow....
Code:
for (frequency=INFRARED; frequency<ULTRAVIOLET;++frequency)
{
int red =?
int green = ?
int blue = ?
}
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
April 19th, 2007, 12:36 PM
#6
Re: Rainbow RGB gradient
I don't think the OP is interested in ultraviolet.
Using the CSpectrumWnd example (which uses HLS coordinates as also suggested by JVene), the following would probably work
Code:
#include <shlwapi.h>
#pragma comment( lib, "shlwapi.lib" ) // needed for the ColorHLSToRGB() function
int hue, lum, sat;
lum = 120;
sat = 240;
for (hue=0; hue<240; ++hue)
{
COLORREF rgbColor = ::ColorHLSToRGB( hue, lum, sat );
}
Mike
-
April 19th, 2007, 01:14 PM
#7
Re: Rainbow RGB gradient
Actually it works quite well making a rainbow, I have used it numerous times. However, as I said, and as others have stated, the HBS -> RGB method would be the simplest to implement. However, doesn't the hue go to 360 degrees, not 240?
-
April 19th, 2007, 03:02 PM
#8
Re: Rainbow RGB gradient
Originally Posted by DeepT
.... However, doesn't the hue go to 360 degrees, not 240?
To color scientists, you are correct: in HLS, hue should vary between 0 and 360 degrees, and luminance and saturation should vary between 0.0 and 1.0. See "HSL color space" at http://en.wikipedia.org/wiki/HSL_color_space , and also Microsoft's own description of HLS: "HLS Color Spaces" at http://msdn2.microsoft.com/en-us/library/ms536815.aspx
But Microsoft's ColorHLStoRGB() function takes a hue "value", not a hue "angle". Moreover, this function apparently #define's an HLSMAX constant of 240, which defines the "value" of all three components (i.e., hue, sat and lum), which all range from 0 to 240. See this KB article: "How To Converting Colors Between RGB and HLS (HBS)" at http://support.microsoft.com/kb/29240
Microsoft's function (and their 0-240 range) is not completely unusual, and there are other programs out there (PhotoShop, I think), that also use this range. Others, however, have commented on the non-standard usage: "Color Standardization" at http://en.wikipedia.org/wiki/User:Co...tandardization
However, I just remembered that the Microsoft function has a bug in it, which I reported at http://connect.microsoft.com/VisualS...dbackID=123807 ("ColorHLSToRGB Returns Incorrect Value When Saturation is Zero"). AFAIK, the bug was never fixed. However, it is seen only when the saturation is zero, so it won't affect the code here.
Mike
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|