Completed
Branch master (55a138)
by Michael
03:21
created

ColorTools   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 265
Duplicated Lines 8.3 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 22
loc 265
rs 10
c 0
b 0
f 0
wmc 22
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A modifierCouleur() 0 22 1
B eclaircir() 11 43 6
B foncer() 11 36 6
A getHexaColorFromA() 0 13 1
A rgb2hexa() 0 6 1
A hexa2rgbA() 0 18 2
A hexa2rgb() 0 10 1
A bornerValeur() 0 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace XoopsModules\Extcal;
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(
34
        $colorHexa,
35
        $incrementRouge,
36
        $incrementVert,
37
        $incrementBleu,
38
        $plancherRouge = 0,
39
        $plafondRouge = 255,
40
        $plancherVert = 0,
41
        $plafondVert = 255,
42
        $plancherBleu = 0,
43
        $plafondBleu = 255
44
    ) {
45
        $t10 = static::hexa2rgbA($colorHexa);
46
47
        $t10[1] = static::bornerValeur($t10[1] + $incrementRouge, $plancherRouge, $plafondRouge);
48
        $t10[2] = static::bornerValeur($t10[2] + $incrementVert, $plancherVert, $plafondVert);
49
        $t10[3] = static::bornerValeur($t10[3] + $incrementBleu, $plancherBleu, $plafondBleu);
50
51
        $newColorHexa = static::getHexaColorFromA($t10);
52
53
        return $newColorHexa;
54
    }
55
56
    /**************************************************************
57
     * Eclairci une couleur
58
     * elle borné pa un plancher et un plafond pur évite le tout blanc ou tout blanc
59
     * ou les blocage sur une couleur pur (ex #FF0000)
60
     **************************************************************
61
     * @param     $colorHexa
62
     * @param int $plancher
63
     * @param int $plafond
64
     * @return string
65
     */
66
    public static function eclaircir($colorHexa, $plancher = 0, $plafond = 255)
67
    {
68
        $tMin = ['', $plancher, $plancher, $plancher];
69
        $tMax = ['', $plafond, $plafond, $plafond];
0 ignored issues
show
Unused Code introduced by
$tMax is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
70
71
        $t10 = static::hexa2rgbA($colorHexa);
72
        // echo "<hr>";
73
        // ext_echoArray($t10);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
74
        $max = $plancher;
75 View Code Duplication
        for ($h = 1; $h <= 3; ++$h) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
            if ($max < $t10[$h]) {
77
                $max = $t10[$h];
78
            }
79
        }
80
81
        $increment = $plafond - $max;
82
83
        //     $t10[1] = $t10[1] + $increment;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
84
        //     $t10[2] = $t10[2] + $increment;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
85
        //     $t10[3] = $t10[3] + $increment;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
86
87
        $min = 0;
88 View Code Duplication
        for ($h = 1; $h <= 3; ++$h) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
            $t10[$h] += $increment;
90
            if ($t10[$h] < $tMin[$h] && $min < ($tMin[$h] - $t10[$h])) {
91
                $min = ($tMin[$h] - $t10[$h]);
92
            }
93
        }
94
95
        // echo "{$colorHexa}-{$plancher}-{$plafond}<br>";
96
        // echo "{$min}-{$max}-{$increment}<br>";
97
98
        $t10[1] = static::bornerValeur($t10[1] + $min, $plancher, $plafond);
99
        $t10[2] = static::bornerValeur($t10[2] + $min, $plancher, $plafond);
100
        $t10[3] = static::bornerValeur($t10[3] + $min, $plancher, $plafond);
101
102
        // ext_echoArray($t10);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
104
        $newColorHexa = static::getHexaColorFromA($t10);
105
106
        // echo "colorHexa = {$newColorHexa}-{$colorHexa}<br>";
107
        return $newColorHexa;
108
    }
109
110
    /**************************************************************
111
     * Fonce une couleur
112
     * elle borné pa un plancher et un plafond pur évite le tout blanc ou tout blanc
113
     * ou les blocage sur une couleur pur (ex #FFFF00)
114
     **************************************************************
115
     * @param     $colorHexa
116
     * @param int $plancher
117
     * @param int $plafond
118
     * @return string
119
     */
120
    public function foncer($colorHexa, $plancher = 0, $plafond = 255)
121
    {
122
        $tMin = ['', $plancher, $plancher, $plancher];
0 ignored issues
show
Unused Code introduced by
$tMin is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
123
        $tMax = ['', $plafond, $plafond, $plafond];
124
125
        $t10 = static::hexa2rgbA($colorHexa);
126
        $max = 255;
127
128 View Code Duplication
        for ($h = 1; $h <= 3; ++$h) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
            if ($max > $t10[$h]) {
130
                $max = $t10[$h];
131
            }
132
        }
133
134
        $increment = -$max;
135
136
        //     $t10[1] = $t10[1] + $increment;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
137
        //     $t10[2] = $t10[2] + $increment;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
138
        //     $t10[3] = $t10[3] + $increment;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
139
140
        $min = 0;
141 View Code Duplication
        for ($h = 1; $h <= 3; ++$h) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
142
            $t10[$h] += $increment;
143
            if ($t10[$h] > $tMax[$h] && $min < ($t10[$h] - $tMax[$h])) {
144
                $min = ($t10[$h] - $tMax[$h]);
145
            }
146
        }
147
148
        $t10[1] = static::bornerValeur($t10[1] - $min, $plancher, $plafond);
149
        $t10[2] = static::bornerValeur($t10[2] - $min, $plancher, $plafond);
150
        $t10[2] = static::bornerValeur($t10[3] - $min, $plancher, $plafond);
151
152
        $colorHexa = static::getHexaColorFromA($t10);
153
154
        return $colorHexa;
155
    }
156
157
    /**************************************************************
158
     * Renvoi une couleur RGB en hexa a partir du tableau passe en parametr
159
     * Description du tableau
160
     * 0 = doit contenir '#' ou ''
161
     * 1 = int red
162
     * 2 = int vert
163
     * 3 = int bleu
164
     **************************************************************
165
     * @param $aColors
166
     * @return string
167
     */
168
    public static function getHexaColorFromA($aColors)
169
    {
170
        $tHex = ['', '', '', ''];
171
172
        $tHex[0] = $aColors[0];
173
        $tHex[1] = substr('00' . dechex($aColors[1]), -2);
174
        $tHex[2] = substr('00' . dechex($aColors[2]), -2);
175
        $tHex[3] = substr('00' . dechex($aColors[3]), -2);
176
177
        $colorHexa = implode('', $tHex);
178
179
        return $colorHexa;
180
    }
181
182
    /**************************************************************
183
     * Transforme les composante d'une couleur en valeu hexa
184
     * prefixe doit contenir '#'  ou ''
185
     **************************************************************
186
     * @param        $r
187
     * @param        $g
188
     * @param        $b
189
     * @param string $prefixe
190
     * @return string
191
     */
192
    public function rgb2hexa($r, $g, $b, $prefixe = '')
193
    {
194
        $colorHexa = static::getHexaColorFromA([$prefixe, $r, $g, $b]);
195
196
        return $colorHexa;
197
    }
198
199
    /**************************************************************
200
     * renvoi un tableau d'entier des valeur rgbd d'une couleur a partir d'un hexa
201
     * Description du tableau renvoyé
202
     * 0 = contient '#' ou ''  (selon premier cactere de la couleur hexa)
203
     * 1 = int red
204
     * 2 = int vert
205
     * 3 = int bleu
206
     **************************************************************
207
     * @param $colorHexa
208
     * @return array
209
     */
210
    public static function hexa2rgbA($colorHexa)
211
    {
212
        $t = ['', '', '', ''];
213
214
        if (0 === strpos($colorHexa, '#')) {
215
            $t[0]      = '#';
216
            $offsetCar = 1;
217
        } else {
218
            $t[0]      = '';
219
            $offsetCar = 0;
220
        }
221
222
        $t[1] = hexdec(substr($colorHexa, $offsetCar + 0, 2));
223
        $t[2] = hexdec(substr($colorHexa, $offsetCar + 2, 2));
224
        $t[3] = hexdec(substr($colorHexa, $offsetCar + 4, 2));
225
226
        return $t;
227
    }
228
229
    /**************************************************************
230
     * Renvoi les composante rgb d'une couleur par référence
231
     **************************************************************
232
     * @param $colorHexa
233
     * @param $r
234
     * @param $v
235
     * @param $b
236
     * @param $diese
237
     * @return bool
238
     */
239
    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.

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

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