Sign in to follow this  

code for cycling through hues (colors)

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this