code for cycling through hues (colors)

Started by
2 comments, last by zedzeek 17 years, 8 months ago
anyone willing to share some code that cycles through through the color hue spectrum thanks zed
Advertisement
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.

Regards
Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.
You need to convert the colors to another color model (i.e. HSV). This allows easy shifting of the hue. Then convert the colors back.

For the conversion see here:
http://www.cs.rit.edu/~ncs/color/t_convert.html
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

This topic is closed to new replies.

Advertisement