Is there an easy way to change the text color based on the background color?

I have a menu in which the user can select colors, I display them as follows:

Plot colors

Since I write color codes in black, the dark blue background makes it difficult to read, and the black is obviously completely hidden. Firstly, this is not particularly important, color is what interests the user.

I could put the color code in a separate frame on a white background, but out of interest I wondered if there was some standard code to handle this or some simple method that could be applied to ensure that the text is always read ? If anyone could point me to a resource with any help, that would be appreciated.

+4
source share
2 answers

, , . , , .net.

- :

Color ForegroundForBackground(Color bg) {
    if(bg.R*2 + bg.G*7 + bg.B < 500)
        return Color.White;
    else
        return Color.Black;
}

, , . , .

: j.k. Luminance (colorimetry), , ( 1 , ). , .

+7

, .

. - , - , .

- . :

. .

, , , . - SO-, . , , , . , , , - , .

+1

All Articles