1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The 'check' class |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace projectcleverweb\color; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The 'check' class |
10
|
|
|
*/ |
11
|
|
|
class check { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Checks if a RGB color appears as a darker or lighter color using YIQ |
15
|
|
|
* color weights |
16
|
|
|
* |
17
|
|
|
* @param int $r The red value |
18
|
|
|
* @param int $g The green value |
19
|
|
|
* @param int $b The blue value |
20
|
|
|
* @param int $check_score The minimum score to check, a number in the range of 0 - 255 |
21
|
|
|
* @return boolean If the YIQ score is >= $check_score returns FALSE, otherwise TRUE |
22
|
|
|
*/ |
23
|
31 |
|
public static function is_dark(int $r = 0, int $g = 0, int $b = 0, int $check_score = 128) :bool { |
24
|
31 |
|
if (generate::yiq_score($r, $g, $b) >= $check_score) { |
25
|
31 |
|
return FALSE; |
26
|
|
|
} |
27
|
31 |
|
return TRUE; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Measures the visual contrast of 2 RGB colors. |
32
|
|
|
* |
33
|
|
|
* NOTE: most colors do not have a 100% contrasting opposite, but all colors |
34
|
|
|
* do have a contrasting opposite that is at least 50%. |
35
|
|
|
* |
36
|
|
|
* @param array $rgb1 The first color, array where offsets 'r', 'g', & 'b' contain their respective values. |
37
|
|
|
* @param array $rgb2 The second color, array where offsets 'r', 'g', & 'b' contain their respective values. |
38
|
|
|
* @return float The visual contrast as a percentage (e.g. 12.345) |
39
|
|
|
*/ |
40
|
1 |
|
public static function rgb_contrast($rgb1, $rgb2) { |
41
|
1 |
|
$r = (max($rgb1['r'], $rgb2['r']) - min($rgb1['r'], $rgb2['r'])) * 299; |
42
|
1 |
|
$g = (max($rgb1['g'], $rgb2['g']) - min($rgb1['g'], $rgb2['g'])) * 587; |
43
|
1 |
|
$b = (max($rgb1['b'], $rgb2['b']) - min($rgb1['b'], $rgb2['b'])) * 114; |
44
|
|
|
// Sum => Average => Convert to percentage |
45
|
1 |
|
return ($r + $g + $b) / 1000 / 2.55; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|