Karen Cate
Karen Cate

Reputation: 272

How can I calculate a THIRD color given a foreground and background color?

I am re-asking this question because my first attempt was erroneously marked as a duplicate, and I still have hope that someone might have an answer for me.

I am having to write my own mouse-over code for a particular control in a C++/VCL application. The question is, given ANY combination of foreground and background colors, how do you calculate a highlight color to indicate that the mouse is over that control?

I am NOT asking how to choose a font/text color based on the background color. That question has certainly been answered. What I need to do is choose a third color to use to highlight the item when the mouse is over it.

I was thinking I probably want to do something like take the background color, find its opposite on the color wheel (and I have no idea how to do that). If that color is too close (however you determine that) to the text color, make it lighter or darker.

Does anyone have an algorithm they are willing to share?

Thanks!

Upvotes: 0

Views: 213

Answers (1)

user1118321
user1118321

Reputation: 26375

If you want to find the complementary color (opposite on the color wheel), you can convert the foreground color to HSV space (or any of a number of other color spaces such as Y'CbCr, Y'IQ, etc.), rotate the hue component 180° and convert back to RGB.

As you suspect, this might not work if the background color is too close to the resulting color. That gives you some options, though. If you convert both colors (fg & bg) to HSV, you can find 2 colors with hues that are exactly between the foreground and background colors. Just find the angle midway between the 2 input colors' hues to get the first possible color, and then add 180° to that to get the other possible color.

But the bigger question is why you're trying to set the selection color at all? You should be using the system highlight color unless you have a really good reason not to.

Upvotes: 1

Related Questions