Craig's Writings‎ > ‎Windows Forms‎ > ‎

Calculating Alternate Colors

Courtesy Shawn Van Ness:

Whevener I'm faced with the need to draw in an unknown color (eg: a color obtained from the user's scheme/profile), and I need to concoct a complementary color -- either to render a gradient, or to paint alternating rows, as you suggest -- I usually just concoct a little transform for the RGB values.

If it's light, I make it a little darker... else, if it's dark, I make it a little lighter.

red2 = (red1 > 0x80) ? red1*8/10 : red1*10/8;

grn2 = (grn1 > 0x80) ? grn1*8/10 : grn1*10/8;

blu2 = (blu1 > 0x80) ? blu1*8/10 : blu1*10/8;

This technique respects the user's general preference of color, but provides a UI that's a little nicer than just plain paint-by-numbers.

You could, of course, apply different factors to each color-plane to make the transform a little warmer or cooler or whatever... you get the idea.