1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The 'generate' class |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace projectcleverweb\color; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The 'generate' class |
10
|
|
|
*/ |
11
|
|
|
class generate { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Generate the most contrasting color for any RGB color |
15
|
|
|
* |
16
|
|
|
* @param int $r The red value |
17
|
|
|
* @param int $g The green value |
18
|
|
|
* @param int $b The blue value |
19
|
|
|
* @return array The most contrasting color as an RGB array |
20
|
|
|
*/ |
21
|
1 |
|
public static function rgb_contrast(int $r = 0, int $g = 0, int $b = 0) :array { |
22
|
|
|
return [ |
23
|
1 |
|
'r' => ($r < 128) ? 255 : 0, |
24
|
1 |
|
'g' => ($g < 128) ? 255 : 0, |
25
|
1 |
|
'b' => ($b < 128) ? 255 : 0 |
26
|
|
|
]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Generate the mathematical opposite color for any RGB color |
31
|
|
|
* |
32
|
|
|
* @param int $r The red value |
33
|
|
|
* @param int $g The green value |
34
|
|
|
* @param int $b The blue value |
35
|
|
|
* @return array The mathematical opposite color as an RGB array |
36
|
|
|
*/ |
37
|
1 |
|
public static function rgb_invert(int $r = 0, int $g = 0, int $b = 0) :array { |
38
|
|
|
return [ |
39
|
1 |
|
'r' => 255 - $r, |
40
|
1 |
|
'g' => 255 - $g, |
41
|
1 |
|
'b' => 255 - $b |
42
|
|
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Generate the YIQ score for an RGB color |
47
|
|
|
* |
48
|
|
|
* @param int $r The red value |
49
|
|
|
* @param int $g The green value |
50
|
|
|
* @param int $b The blue value |
51
|
|
|
* @return float The YIQ score |
52
|
|
|
*/ |
53
|
32 |
|
public static function yiq_score(int $r = 0, int $g = 0, int $b = 0) :float { |
54
|
32 |
|
return (($r * 299) + ($g * 587) + ($b * 114)) / 1000; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Generate a random RGB color |
59
|
|
|
* |
60
|
|
|
* @param int $min_r The min red to allow |
61
|
|
|
* @param int $max_r The max red to allow |
62
|
|
|
* @param int $min_g The min green to allow |
63
|
|
|
* @param int $max_g The max green to allow |
64
|
|
|
* @param int $min_b The min blue to allow |
65
|
|
|
* @param int $max_b The max blue to allow |
66
|
|
|
* @return array The resulting color as a RGB array |
67
|
|
|
*/ |
68
|
2 |
|
public static function rgb_rand(int $min_r = 0, int $max_r = 255, int $min_g = 0, int $max_g = 255, int $min_b = 0, int $max_b = 255) :array { |
69
|
|
|
return [ |
70
|
2 |
|
'r' => rand(abs((int) $min_r) % 256, abs((int) $max_r) % 256), |
71
|
2 |
|
'g' => rand(abs((int) $min_g) % 256, abs((int) $max_g) % 256), |
72
|
2 |
|
'b' => rand(abs((int) $min_b) % 256, abs((int) $max_b) % 256) |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Generate a random HSL color |
78
|
|
|
* |
79
|
|
|
* @param int $min_r The min hue to allow |
|
|
|
|
80
|
|
|
* @param int $max_r The max hue to allow |
|
|
|
|
81
|
|
|
* @param int $min_g The min saturation to allow |
|
|
|
|
82
|
|
|
* @param int $max_g The max saturation to allow |
|
|
|
|
83
|
|
|
* @param int $min_b The min light to allow |
|
|
|
|
84
|
|
|
* @param int $max_b The max light to allow |
|
|
|
|
85
|
|
|
* @return array The resulting color as a HSL array |
86
|
|
|
*/ |
87
|
1 |
|
public static function hsl_rand(int $min_h = 0, int $max_h = 359, int $min_s = 0, int $max_s = 100, int $min_l = 0, int $max_l = 100) :array { |
88
|
|
|
return [ |
89
|
1 |
|
'h' => rand(abs((int) $min_h) % 360, abs((int) $max_h) % 360), |
90
|
1 |
|
's' => rand(abs((int) $min_s) % 101, abs((int) $max_s) % 101), |
91
|
1 |
|
'l' => rand(abs((int) $min_l) % 101, abs((int) $max_l) % 101) |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Blend 2 RGB colors |
97
|
|
|
* |
98
|
|
|
* @param float $r1 The red value from color 1 |
99
|
|
|
* @param float $g1 The green value from color 1 |
100
|
|
|
* @param float $b1 The blue value from color 1 |
101
|
|
|
* @param float $a1 The alpha value from color 1 |
102
|
|
|
* @param float $r2 The red value from color 2 |
103
|
|
|
* @param float $g2 The green value from color 2 |
104
|
|
|
* @param float $b2 The blue value from color 2 |
105
|
|
|
* @param float $a2 The alpha value from color 2 |
106
|
|
|
* @param float $amount The percentage of color 2 to mix in (defaults to 50 for even blending) |
107
|
|
|
* @return array The resulting color as a RGB array |
108
|
|
|
*/ |
109
|
1 |
|
public static function blend(float $r1, float $g1, float $b1, float $a1, float $r2, float $g2, float $b2, float $a2, float $amount = 50.0) :array { |
110
|
1 |
|
$x1 = regulate::div(100 - $amount, 100); |
111
|
1 |
|
$x2 = regulate::div($amount, 100); |
112
|
|
|
return [ |
113
|
1 |
|
'r' => round(($r1 * $x1) + ($r2 * $x2), 0), |
114
|
1 |
|
'g' => round(($g1 * $x1) + ($g2 * $x2), 0), |
115
|
1 |
|
'b' => round(($b1 * $x1) + ($b2 * $x2), 0), |
116
|
1 |
|
'a' => ($a1 * $x1) + ($a2 * $x2) |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Generate a gradient range between 2 RGB colors |
122
|
|
|
* |
123
|
|
|
* @param int $r1 The red value from color 1 |
124
|
|
|
* @param int $g1 The green value from color 1 |
125
|
|
|
* @param int $b1 The blue value from color 1 |
126
|
|
|
* @param int $r2 The red value from color 2 |
127
|
|
|
* @param int $g2 The green value from color 2 |
128
|
|
|
* @param int $b2 The blue value from color 2 |
129
|
|
|
* @param int $steps The size of array to produce, 0 will dynamically |
130
|
|
|
* @return array [TODO] |
|
|
|
|
131
|
|
|
*/ |
132
|
1 |
|
public static function gradient_range(int $r1 = 0, int $g1 = 0, int $b1 = 0, int $r2 = 0, int $g2 = 0, int $b2 = 0, int $steps = 0) :array { |
133
|
|
|
$diff = [ |
134
|
1 |
|
'r' => $r1 - $r2, |
135
|
1 |
|
'g' => $g1 - $g2, |
136
|
1 |
|
'b' => $b1 - $b2 |
137
|
|
|
]; |
138
|
1 |
|
$div = max(abs($r1 - $r2), abs($g1 - $g2), abs($b1 - $b2)); |
|
|
|
|
139
|
1 |
|
if ($steps != 0) { |
140
|
1 |
|
$div = abs($steps) - 1; |
141
|
|
|
} |
142
|
1 |
|
$result = []; |
143
|
1 |
|
foreach (range(0 , $div) as $i) { |
144
|
1 |
|
$result[] = [ |
145
|
1 |
|
'r' => $r1 + round(regulate::div(-$diff['r'], $div) * $i), |
146
|
1 |
|
'g' => $g1 + round(regulate::div(-$diff['g'], $div) * $i), |
147
|
1 |
|
'b' => $b1 + round(regulate::div(-$diff['b'], $div) * $i) |
148
|
|
|
]; |
149
|
|
|
} |
150
|
1 |
|
return $result; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Round a RGB input to a 'websafe' color hex |
155
|
|
|
* |
156
|
|
|
* @param int $r The red value |
157
|
|
|
* @param int $g The green value |
158
|
|
|
* @param int $b The blue value |
159
|
|
|
* @return string The resulting color as a hex string |
160
|
|
|
*/ |
161
|
2 |
|
public static function web_safe(int $r = 0, int $g = 0, int $b = 0) :string { |
162
|
2 |
|
return convert\rgb::to_hex( |
163
|
2 |
|
round($r / 0x33) * 0x33, |
164
|
2 |
|
round($g / 0x33) * 0x33, |
|
|
|
|
165
|
2 |
|
round($b / 0x33) * 0x33 |
166
|
|
|
); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Converts a hue to the "Y" spectrum |
171
|
|
|
* |
172
|
|
|
* @param float $hue The hue to convert |
173
|
|
|
* @return float The resulting Y |
174
|
|
|
*/ |
175
|
9 |
View Code Duplication |
public static function hue_to_yiq(float $hue) :float { |
176
|
9 |
|
if ($hue < 60) { |
177
|
9 |
|
return $hue * 2.5254237288136; |
178
|
9 |
|
} elseif ($hue < 180) { |
179
|
9 |
|
return 150 + ($hue - 60) * 4.9243697478992; |
180
|
9 |
|
} elseif ($hue < 300) { |
181
|
9 |
|
return 737 + ($hue - 180) * 0.94957983193277; |
182
|
|
|
} |
183
|
9 |
|
return 851 + ($hue - 300) * 2.5254237288136; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Converts a "Y" to the hue spectrum |
188
|
|
|
* |
189
|
|
|
* @param float $yiq The "Y" to convert |
190
|
|
|
* @return float The resulting hue |
191
|
|
|
*/ |
192
|
9 |
View Code Duplication |
public static function yiq_to_hue(float $yiq) :float { |
|
|
|
|
193
|
9 |
|
if ($yiq < 150) { |
194
|
8 |
|
return $yiq * 0.39597315436242; |
195
|
9 |
|
} elseif ($yiq < 737) { |
196
|
9 |
|
return 60 + ($yiq - 150) * 0.20307167235495; |
197
|
9 |
|
} elseif ($yiq < 851) { |
198
|
8 |
|
return 180 + ($yiq - 737) * 1.0530973451327; |
199
|
|
|
} |
200
|
8 |
|
return 300 + ($yiq - 851) * 0.39597315436242; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.