1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class ColorTools. |
5
|
|
|
*/ |
6
|
|
|
class colorTools |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
public function __construct() |
12
|
|
|
{ |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/************************************************************** |
16
|
|
|
* Ajoute ou rtir un icrement sur chaque compsante RGB d'une couleur |
17
|
|
|
* Les valeur sont limiter au bornes inférieure et supérieure 0 et 255 |
18
|
|
|
************************************************************** |
19
|
|
|
* |
20
|
|
|
* @param $colorHexa |
21
|
|
|
* @param $incrementRouge |
22
|
|
|
* @param $incrementVert |
23
|
|
|
* @param $incrementBleu |
24
|
|
|
* @param int $plancherRouge |
25
|
|
|
* @param int $plafondRouge |
26
|
|
|
* @param int $plancherVert |
27
|
|
|
* @param int $plafondVert |
28
|
|
|
* @param int $plancherBleu |
29
|
|
|
* @param int $plafondBleu |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function modifierCouleur($colorHexa, $incrementRouge, $incrementVert, $incrementBleu, $plancherRouge = 0, $plafondRouge = 255, $plancherVert = 0, $plafondVert = 255, $plancherBleu = 0, $plafondBleu = 255) |
34
|
|
|
{ |
35
|
|
|
$t10 = static::hexa2rgbA($colorHexa); |
36
|
|
|
|
37
|
|
|
$t10[1] = static::bornerValeur($t10[1] + $incrementRouge, $plancherRouge, $plafondRouge); |
38
|
|
|
$t10[2] = static::bornerValeur($t10[2] + $incrementVert, $plancherVert, $plafondVert); |
39
|
|
|
$t10[3] = static::bornerValeur($t10[3] + $incrementBleu, $plancherBleu, $plafondBleu); |
40
|
|
|
|
41
|
|
|
$newColorHexa = static::getHexaColorFromA($t10); |
42
|
|
|
|
43
|
|
|
return $newColorHexa; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/************************************************************** |
47
|
|
|
* Eclairci une couleur |
48
|
|
|
* elle borné pa un plancher et un plafond pur évite le tout blanc ou tout blanc |
49
|
|
|
* ou les blocage sur une couleur pur (ex #FF0000) |
50
|
|
|
************************************************************** |
51
|
|
|
* @param $colorHexa |
52
|
|
|
* @param int $plancher |
53
|
|
|
* @param int $plafond |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public static function eclaircir($colorHexa, $plancher = 0, $plafond = 255) |
57
|
|
|
{ |
58
|
|
|
$tMin = array('', $plancher, $plancher, $plancher); |
59
|
|
|
$tMax = array('', $plafond, $plafond, $plafond); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$t10 = static::hexa2rgbA($colorHexa); |
62
|
|
|
// echo "<hr>"; |
63
|
|
|
// ext_echoArray($t10); |
|
|
|
|
64
|
|
|
$max = $plancher; |
65
|
|
View Code Duplication |
for ($h = 1; $h <= 3; ++$h) { |
|
|
|
|
66
|
|
|
if ($max < $t10[$h]) { |
67
|
|
|
$max = $t10[$h]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$increment = $plafond - $max; |
72
|
|
|
|
73
|
|
|
// $t10[1] = $t10[1] + $increment; |
|
|
|
|
74
|
|
|
// $t10[2] = $t10[2] + $increment; |
|
|
|
|
75
|
|
|
// $t10[3] = $t10[3] + $increment; |
|
|
|
|
76
|
|
|
|
77
|
|
|
$min = 0; |
78
|
|
View Code Duplication |
for ($h = 1; $h <= 3; ++$h) { |
|
|
|
|
79
|
|
|
$t10[$h] += $increment; |
80
|
|
|
if ($t10[$h] < $tMin[$h] && $min < ($tMin[$h] - $t10[$h])) { |
81
|
|
|
$min = ($tMin[$h] - $t10[$h]); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// echo "{$colorHexa}-{$plancher}-{$plafond}<br>"; |
86
|
|
|
// echo "{$min}-{$max}-{$increment}<br>"; |
87
|
|
|
|
88
|
|
|
$t10[1] = static::bornerValeur($t10[1] + $min, $plancher, $plafond); |
89
|
|
|
$t10[2] = static::bornerValeur($t10[2] + $min, $plancher, $plafond); |
90
|
|
|
$t10[3] = static::bornerValeur($t10[3] + $min, $plancher, $plafond); |
91
|
|
|
|
92
|
|
|
// ext_echoArray($t10); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$newColorHexa = static::getHexaColorFromA($t10); |
95
|
|
|
// echo "colorHexa = {$newColorHexa}-{$colorHexa}<br>"; |
96
|
|
|
return $newColorHexa; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/************************************************************** |
100
|
|
|
* Fonce une couleur |
101
|
|
|
* elle borné pa un plancher et un plafond pur évite le tout blanc ou tout blanc |
102
|
|
|
* ou les blocage sur une couleur pur (ex #FFFF00) |
103
|
|
|
************************************************************** |
104
|
|
|
* @param $colorHexa |
105
|
|
|
* @param int $plancher |
106
|
|
|
* @param int $plafond |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function foncer($colorHexa, $plancher = 0, $plafond = 255) |
110
|
|
|
{ |
111
|
|
|
$tMin = array('', $plancher, $plancher, $plancher); |
|
|
|
|
112
|
|
|
$tMax = array('', $plafond, $plafond, $plafond); |
113
|
|
|
|
114
|
|
|
$t10 = static::hexa2rgbA($colorHexa); |
115
|
|
|
$max = 255; |
116
|
|
|
|
117
|
|
View Code Duplication |
for ($h = 1; $h <= 3; ++$h) { |
|
|
|
|
118
|
|
|
if ($max > $t10[$h]) { |
119
|
|
|
$max = $t10[$h]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$increment = -$max; |
124
|
|
|
|
125
|
|
|
// $t10[1] = $t10[1] + $increment; |
|
|
|
|
126
|
|
|
// $t10[2] = $t10[2] + $increment; |
|
|
|
|
127
|
|
|
// $t10[3] = $t10[3] + $increment; |
|
|
|
|
128
|
|
|
|
129
|
|
|
$min = 0; |
130
|
|
View Code Duplication |
for ($h = 1; $h <= 3; ++$h) { |
|
|
|
|
131
|
|
|
$t10[$h] += $increment; |
132
|
|
|
if ($t10[$h] > $tMax[$h] && $min < ($t10[$h] - $tMax[$h])) { |
133
|
|
|
$min = ($t10[$h] - $tMax[$h]); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$t10[1] = static::bornerValeur($t10[1] - $min, $plancher, $plafond); |
138
|
|
|
$t10[2] = static::bornerValeur($t10[2] - $min, $plancher, $plafond); |
139
|
|
|
$t10[2] = static::bornerValeur($t10[3] - $min, $plancher, $plafond); |
140
|
|
|
|
141
|
|
|
$colorHexa = static::getHexaColorFromA($t10); |
142
|
|
|
|
143
|
|
|
return $colorHexa; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/************************************************************** |
147
|
|
|
* Renvoi une couleur RGB en hexa a partir du tableau passe en parametr |
148
|
|
|
* Description du tableau |
149
|
|
|
* 0 = doit contenir '#' ou '' |
150
|
|
|
* 1 = int red |
151
|
|
|
* 2 = int vert |
152
|
|
|
* 3 = int bleu |
153
|
|
|
************************************************************** |
154
|
|
|
* @param $aColors |
155
|
|
|
* @return string |
156
|
|
|
*/ |
157
|
|
|
public static function getHexaColorFromA($aColors) |
158
|
|
|
{ |
159
|
|
|
$tHex = array('', '', '', ''); |
160
|
|
|
|
161
|
|
|
$tHex[0] = $aColors[0]; |
162
|
|
|
$tHex[1] = substr('00'.dechex($aColors[1]), -2); |
163
|
|
|
$tHex[2] = substr('00'.dechex($aColors[2]), -2); |
164
|
|
|
$tHex[3] = substr('00'.dechex($aColors[3]), -2); |
165
|
|
|
|
166
|
|
|
$colorHexa = implode('', $tHex); |
167
|
|
|
|
168
|
|
|
return $colorHexa; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/************************************************************** |
172
|
|
|
* Transforme les composante d'une couleur en valeu hexa |
173
|
|
|
* prefixe doit contenir '#' ou '' |
174
|
|
|
************************************************************** |
175
|
|
|
* @param $r |
176
|
|
|
* @param $g |
177
|
|
|
* @param $b |
178
|
|
|
* @param string $prefixe |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
public function rgb2hexa($r, $g, $b, $prefixe = '') |
182
|
|
|
{ |
183
|
|
|
$colorHexa = static::getHexaColorFromA(array($prefixe, $r, $g, $b)); |
184
|
|
|
|
185
|
|
|
return $colorHexa; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/************************************************************** |
189
|
|
|
* renvoi un tableau d'entier des valeur rgbd d'une couleur a partir d'un hexa |
190
|
|
|
* Description du tableau renvoyé |
191
|
|
|
* 0 = contient '#' ou '' (selon premier cactere de la couleur hexa) |
192
|
|
|
* 1 = int red |
193
|
|
|
* 2 = int vert |
194
|
|
|
* 3 = int bleu |
195
|
|
|
************************************************************** |
196
|
|
|
* @param $colorHexa |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
|
|
public static function hexa2rgbA($colorHexa) |
200
|
|
|
{ |
201
|
|
|
$t = array('', '', '', ''); |
202
|
|
|
|
203
|
|
|
if (0 === strpos($colorHexa, '#')) { |
204
|
|
|
$t[0] = '#'; |
205
|
|
|
$offsetCar = 1; |
206
|
|
|
} else { |
207
|
|
|
$t[0] = ''; |
208
|
|
|
$offsetCar = 0; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$t[1] = hexdec(substr($colorHexa, $offsetCar + 0, 2)); |
212
|
|
|
$t[2] = hexdec(substr($colorHexa, $offsetCar + 2, 2)); |
213
|
|
|
$t[3] = hexdec(substr($colorHexa, $offsetCar + 4, 2)); |
214
|
|
|
|
215
|
|
|
return $t; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/************************************************************** |
219
|
|
|
* Renvoi les composante rgb d'une couleur par référence |
220
|
|
|
************************************************************** |
221
|
|
|
* @param $colorHexa |
222
|
|
|
* @param $r |
223
|
|
|
* @param $v |
224
|
|
|
* @param $b |
225
|
|
|
* @param $diese |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
|
|
public function hexa2rgb($colorHexa, &$r, &$v, &$b, &$diese) |
|
|
|
|
229
|
|
|
{ |
230
|
|
|
$t = static::hexa2rgbA($colorHexa); |
231
|
|
|
$r = $t[1]; |
232
|
|
|
$v = $t[2]; |
233
|
|
|
$v = $t[3]; |
234
|
|
|
$diese = $t[0]; |
235
|
|
|
|
236
|
|
|
return true; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/************************************************************** |
240
|
|
|
* Borne les valeurs max et min d'une valeur |
241
|
|
|
************************************************************** |
242
|
|
|
* @param $val |
243
|
|
|
* @param $min |
244
|
|
|
* @param $max |
245
|
|
|
* @return mixed |
246
|
|
|
*/ |
247
|
|
|
public static function bornerValeur($val, $min, $max) |
248
|
|
|
{ |
249
|
|
|
if ($val < $min) { |
250
|
|
|
$val = $min; |
251
|
|
|
} elseif ($val > $max) { |
252
|
|
|
$val = $max; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $val; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
//-------------------------------------------------------- |
259
|
|
|
} // --- fin de la classe colors |
260
|
|
|
//-------------------------------------------------------- |
261
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.