Jump to content
  • Advertisement
Sign in to follow this  

code for cycling through hues (colors)

This topic is 4350 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Funny, I just put this together yesterday. It's written for D3D9, but the only change you'd need to fit any other API is on the last line, converting R, G & B to your colour format.
My code takes a hue in the range (0.0, 1.0), assumes your lightness and saturation are both fixed at 1.0 (maximum) and returns a RGB DWORD. To cycle using this function should be trivial.

DWORD SaturatedHue(float Hue) {
float r, g, b;
r = Hue + 1.0f / 3;
g = Hue;
b = Hue - 1.0f / 3;

if (r > 1) r -= 1;
if (b < 0) b += 1;

if (r < 0.16667f) {
r = 1.0f - 6.0f * r;
} else if (r < 0.5f) {
r = 0;
} else if (r < 0.66667f) {
r = 6.0f * r - 3.0f;
} else r = 1;

if (g < 0.16667f) {
g = 1.0f - 6.0f * g;
} else if (g < 0.5f) {
g = 0;
} else if (g < 0.66667f) {
g = 6.0f * g - 3.0f;
} else g = 1;

if (b < 0.16667f) {
b = 1.0f - 6.0f * b;
} else if (b < 0.5f) {
b = 0;
} else if (b < 0.66667f) {
b = 6.0f * b - 3.0f;
} else b = 1;

return D3DCOLOR_ARGB(0xFF, (byte) (r * 0xFF), (byte) (g * 0xFF), (byte) (b * 0xFF));

It's not optimised and there's a fair bit of casting involved, but it's not exactly slow either. If you're using this a lot you may want to make some adjustments.
If you need more control, Wikipedia documents the HSL-RGB conversion thoroughly.


Share this post

Link to post
Share on other sites
thanks guys
sorry about being lazy, but i just wanna see if this looks better with my ingame menus than what i have at present, ie i didnt wanna spend time implimenting something that looks crap so i wouldnt use

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!