Search code examples
c++winapicolorswtl

Calculating a gradient fill's start and end colours given a base colour


I have a WTL C++ application and I want the user to be able to select different colours for some of the UI elements and apply a gradient fill using the GradientFill API call. However, instead of letting the user pick the start and end colours for the gradient, I'd like them to be able to select a 'base' colour and for my application to calculate suitable start/end colours automatically. I want to end up with a fill that is similar to the one Windows uses for various themed elements. The base colour could be the 'middle' colour of the gradient with my application somehow computing a slightly lighter colour for the gradient start and a slightly darker colour for the gradient end. Alternatively I could use the base as the start colour and compute the end or vice-versa.

I'm sure this would be relatively easy with some RGB macro magic but I really don't know where to start. Any ideas would be welcome.


Solution

  • The RGB color space is not suitable for this. Use HSL or HSV: it is easy to auto-generate a good looking gradient by varying an individual component. Converting from HSL/V to an RGB triplet you can use in the API is simple.