S
S
spido2021-06-13 10:30:10
PHP
spido, 2021-06-13 10:30:10

How to write a condition where the parameter is a color?

Greetings!
I'm looking for a way to write a condition where the parameter is a color in any format, if it would work.

Now I do this:

if ($page_back_color > '#f2f2f1') {
 голова
 } else {
  ноги
}


Oddly enough, in most cases this works, but for some colors it sometimes compares incorrectly.
We need a way to compare colors which is darker or lighter relative to another color, such as in the '#f2f2f1' condition.
Your ideas?

Thanks in advance to those who respond!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2021-06-13
@sergiks

Color is usually characterized by three parameters. Depending on the color model: RGB (red, green, blue); HSV (hue, saturation, lightness); Lab (brightness, red-green axis, blue-yellow axis), etc. More-less can only be compared along one axis.
"darker than #f2f2f1" - can be determined by converting the color from RGB to HSV or Lab (google formulas) and comparing only the brightness component.
Well, or ugly to simplify to three comparisons: each of the R, G, B components: R < 0xF2 || B < 0xF2 || B < 0xF1Here the jamb is that one component (for example, R) is slightly less than the threshold, and the rest are much higher than it - they will give a lighter color, and this approach consider it darker.
str_split('f1f2f3', 2) will give an array of ['f1', 'f2', 'f3'].
intval('f1', 16) will make the number 241 from the string 'f1'.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question