K
K
kafeman2012-07-31 14:38:02
Algorithms
kafeman, 2012-07-31 14:38:02

How to determine if a color is dark?

There is a color, or rather, three numbers that define this color in RBG.

How to determine whether this color is dark or light? At first, I thought that dark is when at least one of the numbers is less than 127 (~ 255/2). But now I noticed that after all #FF000 can be called light, but according to my algorithm it will be dark.

There is an idea that it is necessary to compare two numbers at once with each other in turn, no one has a ready-made algorithm? :-)

Answer the question

In order to leave comments, you need to log in

7 answer(s)
M
Maxim Kuzovlev, 2012-08-01
@kafeman

With brightness, everything is correctly selected, that's just the calculation of brightness ...
If you are interested in a more accurate definition of brightness, here is an article in English about calculating brightness.

K
kafeman, 2012-07-31
@kafeman

I'm sorry, I already found a similar question on Stack Overflow myself.
Here, who needs it, is an example in C, which I seem to have earned:

if (1 - (0.299 * red + 0.587 * green + 0.114 * blue) / 255 < 0.5)
    // светлый
else
    // темный

@
@sledopit, 2012-07-31
_

RGB→ HSL .
Instructions on how to convert on the Internet are a dime a dozen.
Well, then it's a matter of technology.

A
ag666, 2012-07-31
@ag666

Of course, I may not understand something, but. "Sum" R, G, B - determines only the color. Lightness or darkness is determined by the value of each of their components. Light is when at least 1 component has a value above 127 (255/2). Dark - when ALL values ​​are less than 127 (individually, not the sum). Moreover, lordship is always determined by the highest component.

M
Maximus5, 2012-07-31
@Maximus5

In my ConEmu, when drawing the cursor, I consider the color "dark" if all 3 components <= 0xC0. It works to a first approximation.
Although for good, you need to convert it to Lab / HSV / HSL and look at the brightness component :) Well, it depends on what task ...

O
olekl, 2012-07-31
@olekl

It seems to be generally accepted as something like Gray = 0.3R + 0.59G + 0.11B, and the visual average of the grayscale brightness is 18%, i.e. somewhere 256 * 0.18 = 46.

S
Sergey Eremin, 2012-07-31
@Sergei_Erjemin

en.wikipedia.org/wiki/HSL_and_HSV
For RGB the equation is correct, but for NTSC other coefficients…

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question