ColorTools   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 258
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 22
eloc 74
dl 0
loc 258
rs 10
c 0
b 0
f 0

9 Methods

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

238
    public function hexa2rgb($colorHexa, &$r, &$v, /** @scrutinizer ignore-unused */ &$b, &$diese)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
239
    {
240
        $t     = static::hexa2rgbA($colorHexa);
241
        $r     = $t[1];
242
        $v     = $t[2];
243
        $v     = $t[3];
244
        $diese = $t[0];
245
246
        return true;
247
    }
248
249
    /**************************************************************
250
     * Borne les valeurs max et min d'une valeur
251
     **************************************************************
252
     * @param $val
253
     * @param $min
254
     * @param $max
255
     * @return mixed
256
     */
257
    public static function bornerValeur($val, $min, $max)
258
    {
259
        if ($val < $min) {
260
            $val = $min;
261
        } elseif ($val > $max) {
262
            $val = $max;
263
        }
264
265
        return $val;
266
    }
267
    //--------------------------------------------------------
268
} // --- fin de la classe colors
269
//--------------------------------------------------------
270