Completed
Push — master ( 470825...143e87 )
by Joram van den
06:11
created

ColorSpace.php ➔ fu()   A

Complexity

Conditions 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
5
    Color Space : v1.25 : 2009.04.20
6
    ————————————————————————————————
7
            XYZ <-> Luv <-> LCHuv
8
            XYZ <-> Lab <-> LCHab
9
    RGB <-> XYZ <-> xyY
10
    RGB <-> HSL
11
    RGB <-> HSV <-> RYB
12
    RGB <-> CMY <-> CMYK
13
    RGB <-> HEX <-> STRING
14
15
*/
16
17
class ColorSpace
18
{
19
    private $RYB_H = [];
20
    private $H_RYB = [];
21
    private $_XYZ_RGB = [];
22
    private $White;
23
    private $_xyY;
24
25
    public function __construct()
26
    {
27
        $wheel = [
28
            [0, 0],
29
            [15, 8], // Red
30
            [30, 17],
31
            [45, 26], // Orange
32
            [60, 34],
33
            [75, 41], // Yellow
34
            [90, 48],
35
            [105, 54], // Lime
36
            [120, 60],
37
            [135, 81], // Green
38
            [150, 103],
39
            [165, 123], // Teal
40
            [180, 138],
41
            [195, 155], // Cyan
42
            [210, 171],
43
            [225, 187], // Azure
44
            [240, 204],
45
            [255, 219], // Blue
46
            [270, 234],
47
            [285, 251], // Indigo
48
            [300, 267],
49
            [315, 282], // Purple
50
            [330, 298],
51
            [345, 329], // Pink
52
            [360, 0],
53
        ];
54
55
        $a;
0 ignored issues
show
Bug introduced by
The variable $a seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
56
        $b;
0 ignored issues
show
Bug introduced by
The variable $b seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
57
        $i;
0 ignored issues
show
Bug introduced by
The variable $i seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
58
        $H;
0 ignored issues
show
Bug introduced by
The variable $H seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
59
        $_H;
0 ignored issues
show
Bug introduced by
The variable $_H seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
60
        $H_;
0 ignored issues
show
Bug introduced by
The variable $H_ seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
61
62
        for ($H = 0; $H < 360; $H++) {
63
            $H_ = false;
64
            $_H = false;
65
66
            for ($i = 0; $i < 24; $i++) {
67
                $a = $wheel[$i];
68
                $b = $wheel[$i + 1];
69
70
                if ($b && $b[1] < $a[1]) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $b of type integer[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
71
                    $b[1] += 360;
72
                }
73
74 View Code Duplication
                if (!$H_ && $a[0] <= $H && $b[0] > $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...
75
                    $this->H_RYB[$H] = (($a[1] + ($b[1] - $a[1]) * ($H - $a[0]) / ($b[0] - $a[0])) % 360);
76
                    $H_ = true;
77
                }
78
79 View Code Duplication
                if (!$_H && $a[1] <= $H && $b[1] > $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...
80
                    $this->RYB_H[$H] = (($a[0] + ($b[0] - $a[0]) * ($H - $a[1]) / ($b[1] - $a[1])) % 360);
81
                    $_H = true;
82
                }
83
84
                if ($H_ == true && _H == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
85
                    break;
86
                }
87
            }
88
        }
89
        $this->White = $this->illuminant('2', 'D65');
90
        $this->profile('sRGB');
91
//		echo "this->White = ";
92
//		print_r($this->White);
93
    }
94
95 View Code Duplication
    public function RYB_HSV($o)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
96
    {
97
        $n = floor($o['H']);
98
        $x = $n > 0 ? $o['H'] % $n : 0;
99
        $a = $this->RYB_H[$n % 360];
100
        $b = $this->RYB_H[ceil($o['H']) % 360];
101
102
        return [
103
            'H' => $a + ($b - $a) * $x,
104
            'S' => $o['S'],
105
            'V' => $o['V'],
106
        ];
107
    }
108
109 View Code Duplication
    public function HSV_RYB($o)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
110
    {
111
        $n = floor($o['H']);
112
        $x = $n > 0 ? $o['H'] % $n : 0;
113
        $a = $this->H_RYB[$n % 360];
114
        $b = $this->H_RYB[ceil($o['H']) % 360];
115
116
        return [
117
            'H' => $a + ($b - $a) * $x,
118
            'S' => $o['S'],
119
            'V' => $o['V'],
120
        ];
121
    }
122
123
    public function STRING_HEX($o)
124
    {
125
        return 0 + ('0x'.$o);
126
    }
127
128
    public function HEX_STRING($o)
129
    {
130
        $str = sprintf('%X', $o);
131
        $n = strlen($str);
132
        while ($n < 6) {
133
            $str = '0'.$str;
134
            $n++;
135
        }
136
137
        return $str;
138
    }
139
140
    public function HEX_RGB($o)
141
    {
142
        return [
143
            'R' => ($o >> 16),
144
            'G' => ($o >> 8) & 0xFF,
145
            'B' => $o & 0xFF,
146
        ];
147
    }
148
149
    public function HEX32_RGBA($o)
150
    {
151
        return [
152
            'R' => $o >> 16 & 0xFF,
153
            'G' => $o >> 9 & 0xFF,
154
            'B' => $o & 0xFF,
155
            'A' => $o >> 24,
156
        ];
157
    }
158
159
    public function RGBA_HEX32($o)
160
    {
161
        return ($o['A'] << 24 | $o['R'] << 16 | $o['G'] << 8 | $o['B']) >> 0;
162
    }
163
164
    public function HEX32_rgbaa($o)
165
    {
166
        return 'rgba('.($o >> 16 & 0xFF).','.
167
        ($o >> 8 & 0xFF).','.($o & 0xFF).','.
168
        (($o >> 24) / 255).')';
169
    }
170
171
    /*
172
    function RGB_STRING($o)
173
    {
174
        return $this->HEX_STRING($this->RGB_HEX($o));
175
    }
176
    function RGB_rgbaa($o)
177
    {
178
        return 'rgba('. $o['R'] .','.$o['G'].','.$o['B'].','.$o['A'].')';
179
    }
180
    function HEX_rgba($o)
181
    {
182
        return $this->HEX32_rgbaa($o);
183
    }
184
     */
185
    public function RGB_HEX($o)
186
    {
187
        return $o['R'] << 16 | $o['G'] << 8 | $o['B'];
188
    }
189
190
    public function RGB_CMY($o)
191
    {
192
        return [
193
            'C' => 1 - ($o['R'] / 255),
194
            'M' => 1 - ($o['G'] / 255),
195
            'Y' => 1 - ($o['B'] / 255),
196
        ];
197
    }
198
199
    public function RGB_HSL($o)
200
    {
201
        $_R = $o['R'] / 255;
202
        $_G = $o['G'] / 255;
203
        $_B = $o['B'] / 255;
204
        $min = min($_R, $_G, $_B);
205
        $max = max($_R, $_G, $_B);
206
        $D = $max - $min;
207
        $L = ($max + $min) / 2;
208
209
        if ($D == 0) {
210
            $H = 0;
211
            $S = 0;
212
        } // No Chroma
213
214
        else {
215 View Code Duplication
            if ($L < 0.5) {
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...
216
                $S = $D / ($max + $min);
217
            } else {
218
                $S = $D / (2 - $max - $min);
219
            }
220
221
            $DR = ((($max - $_R) / 6) + ($D / 2)) / $D;
222
            $DG = ((($max - $_G) / 6) + ($D / 2)) / $D;
223
            $DB = ((($max - $_B) / 6) + ($D / 2)) / $D;
224
225 View Code Duplication
            if ($_R == $max) {
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...
226
                $H = $DB - $DG;
227
            } else {
228
                if ($_G == $max) {
229
                    $H = (1 / 3) + $DR - $DB;
230
                } else {
231
                    if ($_B == $max) {
232
                        $H = (2 / 3) + $DG - $DR;
233
                    }
234
                }
235
            }
236
237
            if ($H < 0) {
238
                $H += 1;
0 ignored issues
show
Bug introduced by
The variable $H does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
239
            }
240
            if ($H > 1) {
241
                $H -= 1;
242
            }
243
        }
244
245
        return [
246
            'H' => $H * 360,
247
            'S' => $S * 100,
248
            'L' => $L * 100,
249
        ];
250
    }
251
252
    public function RGB_HSV($o)
253
    {
254
        $_R = $o['R'] / 255;
255
        $_G = $o['G'] / 255;
256
        $_B = $o['B'] / 255;
257
258
        $min = min($_R, $_G, $_B);
259
        $max = max($_R, $_G, $_B);
260
        $D = $max - $min;
261
        $V = $max;
262
263
        if ($D == 0) {
264
            $H = 0;
265
            $S = 0;
266
        } // No chroma
267
268
        else { // Chromatic data
269
270
            $S = $D / $max;
271
272
            $DR = ((($max - $_R) / 6) + ($D / 2)) / $D;
273
            $DG = ((($max - $_G) / 6) + ($D / 2)) / $D;
274
            $DB = ((($max - $_B) / 6) + ($D / 2)) / $D;
275
276 View Code Duplication
            if ($_R == $max) {
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...
277
                $H = $DB - $DG;
278
            } else {
279
                if ($_G == $max) {
280
                    $H = (1 / 3) + $DR - $DB;
281
                } else {
282
                    if ($_B == $max) {
283
                        $H = (2 / 3) + $DG - $DR;
284
                    }
285
                }
286
            }
287
288
            if ($H < 0) {
289
                $H += 1;
0 ignored issues
show
Bug introduced by
The variable $H does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
290
            }
291
            if ($H > 1) {
292
                $H -= 1;
293
            }
294
        }
295
296
        return ['H' => $H * 360, 'S' => $S * 100, 'V' => $V * 100];
297
    }
298
299
    public function RGB_XYZ($o)
300
    {
301
        $M = $this->_RGB_XYZ;
302
        $z = [];
303
304
        $R = $o['R'] / 255;
305
        $G = $o['G'] / 255;
306
        $B = $o['B'] / 255;
307
308
        if ($this->_Space == 'sRGB') {
309
            $R = ($R > 0.04045) ? pow((($R + 0.055) / 1.055), 2.4) : $R / 12.92;
310
            $G = ($G > 0.04045) ? pow((($G + 0.055) / 1.055), 2.4) : $G / 12.92;
311
            $B = ($B > 0.04045) ? pow((($B + 0.055) / 1.055), 2.4) : $B / 12.92;
312
        } else {
313
            $R = pow($R, $this->_Gamma);
314
            $G = pow($G, $this->_Gamma);
315
            $B = pow($B, $this->_Gamma);
316
        }
317
318
        $z['X'] = $R * $M[0] + $G * $M[3] + $B * $M[6];
319
        $z['Y'] = $R * $M[1] + $G * $M[4] + $B * $M[7];
320
        $z['Z'] = $R * $M[2] + $G * $M[5] + $B * $M[8];
321
322
        return $z;
323
    }
324
325
    public function CMY_RGB($o)
326
    {
327
        return [
328
            'R' => max(0, (1 - $o['C']) * 255),
329
            'G' => max(0, (1 - $o['M']) * 255),
330
            'B' => max(0, (1 - $o['Y']) * 255),
331
        ];
332
    }
333
334
    public function CMY_CMYK($o)
335
    {
336
        $C = $o['C'];
337
        $M = $o['M'];
338
        $Y = $o['Y'];
339
        $K = min($Y, min($M, min($C, 1)));
340
341
        $C = round(($C - $K) / (1 - $K) * 100);
342
        $M = round(($M - $K) / (1 - $K) * 100);
343
        $Y = round(($Y - $K) / (1 - $K) * 100);
344
        $K = round($K * 100);
345
346
        return ['C' => $C, 'M' => $M, 'Y' => $Y, 'K' => $K];
347
    }
348
349
    // CMYK = C: Cyan / M: Magenta / Y: Yellow / K: Key (black)
350
351
    public function CMYK_CMY($o)
352
    {
353
        return [
354
            'C' => ($o['C'] * (1 - $o['K']) + $o['K']),
355
            'M' => ($o['M'] * (1 - $o['K']) + $o['K']),
356
            'Y' => ($o['Y'] * (1 - $o['K']) + $o['K']),
357
        ];
358
    }
359
360
    // HSL (1978) = H: Hue / S: Saturation / L: Lightess
361
362 View Code Duplication
    public function Hue_2_RGB($v1, $v2, $vH)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
363
    {
364
        if ($vH < 0) {
365
            $vH += 1;
366
        }
367
        if ($vH > 1) {
368
            $vH -= 1;
369
        }
370
        if ((6 * $vH) < 1) {
371
            return $v1 + ($v2 - $v1) * 6 * $vH;
372
        }
373
        if ((2 * $vH) < 1) {
374
            return $v2;
375
        }
376
        if ((3 * $vH) < 2) {
377
            return $v1 + ($v2 - $v1) * ((2 / 3) - $vH) * 6;
378
        }
379
380
        return $v1;
381
    }
382
383
    public function HSL_RGB($o)
384
    {
385
        $H = $o['H'] / 360;
386
        $S = $o['S'] / 100;
387
        $L = $o['L'] / 100;
388
        $R;
0 ignored issues
show
Bug introduced by
The variable $R seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
389
        $G;
0 ignored issues
show
Bug introduced by
The variable $G seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
390
        $B;
0 ignored issues
show
Bug introduced by
The variable $B seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
391
        $_1;
0 ignored issues
show
Bug introduced by
The variable $_1 seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
392
        $_2;
0 ignored issues
show
Bug introduced by
The variable $_2 seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
393
394
        if ($S == 0) { // HSL from 0 to 1
395
396
            $R = $L * 255;
397
            $G = $L * 255;
398
            $B = $L * 255;
399
        } else {
400
            if ($L < 0.5) {
401
                $_2 = $L * (1 + $S);
402
            } else {
403
                $_2 = ($L + $S) - ($S * $L);
404
            }
405
406
            $_1 = 2 * $L - $_2;
407
408
            $R = 255 * $this->Hue_2_RGB($_1, $_2, $H + (1 / 3));
409
            $G = 255 * $this->Hue_2_RGB($_1, $_2, $H);
410
            $B = 255 * $this->Hue_2_RGB($_1, $_2, $H - (1 / 3));
411
        }
412
413
        return ['R' => $R, 'G' => $G, 'B' => $B];
414
    }
415
416
    // HSV (1978) = H: Hue / S: Saturation / V: Value
417
418
    public function HSV_RGB($o)
419
    {
420
        $H = $o['H'] / 360;
421
        $S = $o['S'] / 100;
422
        $V = $o['V'] / 100;
423
        $R;
0 ignored issues
show
Bug introduced by
The variable $R seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
424
        $G;
0 ignored issues
show
Bug introduced by
The variable $G seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
425
        $B;
0 ignored issues
show
Bug introduced by
The variable $B seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
426
427
        if ($S == 0) {
428
            $R = $G = $B = round($V * 255);
429
        } else {
430
            if ($H >= 1) {
431
                $H = 0;
432
            }
433
434
            $H = 6 * $H;
435
            $D = $H - floor($H);
436
            $A = round(255 * $V * (1 - $S));
437
            $B = round(255 * $V * (1 - ($S * $D)));
438
            $C = round(255 * $V * (1 - ($S * (1 - $D))));
439
            $V = round(255 * $V);
440
441
            switch (floor($H)) {
442
443
                case 0:
444
                    $R = $V;
445
                    $G = $C;
446
                    $B = $A;
447
                    break;
448
                case 1:
449
                    $R = $B;
450
                    $G = $V;
451
                    $B = $A;
452
                    break;
453
                case 2:
454
                    $R = $A;
455
                    $G = $V;
456
                    $B = $C;
457
                    break;
458
                case 3:
459
                    $R = $A;
460
                    $G = $B;
461
                    $B = $V;
462
                    break;
463
                case 4:
464
                    $R = $C;
465
                    $G = $A;
466
                    $B = $V;
467
                    break;
468
                case 5:
469
                    $R = $V;
470
                    $G = $A;
471
                    $B = $B;
0 ignored issues
show
Bug introduced by
Why assign $B to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

Loading history...
472
                    break;
473
            }
474
        }
475
476
        return ['R' => $R, 'G' => $G, 'B' => $B];
0 ignored issues
show
Bug introduced by
The variable $R does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $G does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
477
    }
478
479
    // CIE (Commission International de L’Eclairage)
480
481
    // CIE-XYZ (1931) = Y: Luminescence / XZ: Spectral Weighting Curves (Spectral Locus)
482
483
    public function XYZ_RGB($o)
484
    {
485
        $M = $this->_XYZ_RGB;
486
        $z = [];
487
488
        $z['R'] = $o['X'] * $M[0] + $o['Y'] * $M[3] + $o['Z'] * $M[6];
489
        $z['G'] = $o['X'] * $M[1] + $o['Y'] * $M[4] + $o['Z'] * $M[7];
490
        $z['B'] = $o['X'] * $M[2] + $o['Y'] * $M[5] + $o['Z'] * $M[8];
491
492
        if ($this->_Space == 'sRGB') {
493
            $z['R'] = ($z['R'] > 0.0031308) ? (1.055 * pow($z['R'], 1 / 2.4)) - 0.055 : 12.92 * $z['R'];
494
            $z['G'] = ($z['G'] > 0.0031308) ? (1.055 * pow($z['G'], 1 / 2.4)) - 0.055 : 12.92 * $z['G'];
495
            $z['B'] = ($z['B'] > 0.0031308) ? (1.055 * pow($z['B'], 1 / 2.4)) - 0.055 : 12.92 * $z['B'];
496
        } else {
497
            $z['R'] = pow($z['R'], 1 / $this->_Gamma);
498
            $z['G'] = pow($z['G'], 1 / $this->_Gamma);
499
            $z['B'] = pow($z['B'], 1 / $this->_Gamma);
500
        }
501
502
        return ['R' => round($z['R'] * 255), 'G' => round($z['G'] * 255), 'B' => round($z['B'] * 255)];
503
    }
504
505
    public function XYZ_xyY($o)
506
    {
507
        $n = $o['X'] + $o['Y'] + $o['Z'];
508
509
        if ($n == 0) {
510
            return ['x' => 0, 'y' => 0, 'Y' => $o['Y']];
511
        }
512
513
        return ['x' => $o['X'] / $n, 'y' => $o['Y'] / $n, 'Y' => $o['Y']];
514
    }
515
516
    public function XYZ_HLab($o)
517
    {
518
        $n = sqrt($o['Y']);
519
520
        return [
521
            'L' => 10 * $n,
522
            'a' => 17.5 * (((1.02 * $o['X']) - $o['Y']) / $n),
523
            'b' => 7 * (($o['Y'] - (0.847 * $o['Z'])) / $n),
524
        ];
525
    }
526
527
    public function XYZ_Lab($o)
528
    {
529
        $r = $this->White;
530
531
        function fu($n)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
532
        {
533
            if ($n > 0.008856) {
534
                return pow($n, 1 / 3);
535
            } else {
536
                return (7.787 * $n) + (16 / 116);
537
            }
538
        }
539
540
        $X = fu($o['X'] / $r['X']);
541
        $Y = fu($o['Y'] / $r['Y']);
542
        $Z = fu($o['Z'] / $r['Z']);
543
544
        return ['L' => (116 * $Y) - 16, 'a' => 500 * ($X - $Y), 'b' => 200 * ($Y - $Z)];
545
    }
546
547
    public function XYZ_Luv($o)
548
    {
549
        $r = $this->White;
550
551
        $U = (4 * $o['X']) / ($o['X'] + (15 * $o['Y']) + (3 * $o['Z']));
552
        $V = (9 * $o['Y']) / ($o['X'] + (15 * $o['Y']) + (3 * $o['Z']));
553
554
        if ($o['Y'] > 0.008856) {
555
            $o['Y'] = pow($o['Y'], 1 / 3);
556
        } else {
557
            $o['Y'] = (7.787 * $o['Y']) + (16 / 116);
558
        }
559
560
        $_L = (116 * $o['Y']) - 16;
561
        $_U = (4 * $r['X']) / ($r['X'] + (15 * $r['Y']) + (3 * $r['Z']));
562
        $_V = (9 * $r['Y']) / ($r['X'] + (15 * $r['Y']) + (3 * $r['Z']));
563
564
        return ['L' => $_L, 'u' => 13 * $_L * ($U - $_U), 'v' => 13 * $_L * ($V - $_V)];
565
    }
566
567
    // CIE-xyY (1931) = Y: Luminescence / xy: Chromaticity Co-ordinates (Spectral Locus)
568
569
    public function xyY_XYZ($o)
570
    {
571
        return [
572
            'X' => ($o['x'] * $o['Y']) / $o['y'],
573
            'Y' => $o['Y'],
574
            'Z' => ((1 - $o['x'] - $o['y']) * $o['Y']) / $o['y'],
575
        ];
576
    }
577
578
    // Hunter-L*ab (1948) = L: Lightness / ab: Color-opponent Dimensions
579
580
    public function HLab_XYZ($o)
581
    {
582
        $_Y = $o['L'] / 10;
583
        $_X = ($o['a'] / 17.5) * ($o['L'] / 10);
584
        $_Z = ($o['b'] / 7) * ($o['L'] / 10);
585
586
        $Y = pow($_Y, 2);
587
        $X = ($_X + $Y) / 1.02;
588
        $Z = -1 * ($_Z - $Y) / 0.847;
589
590
        return ['X' => $X, 'Y' => $Y, 'Z' => $Z];
591
    }
592
593
    // CIE-L*ab (1976) = L: Luminescence / a: Red / Green / b: Blue / Yellow
594
595
    public function Lab_XYZ($o)
596
    {
597
        $r = $this->White;
598
599
        $Y = ($o['L'] + 16) / 116;
600
        $X = $o['a'] / 500 + $Y;
601
        $Z = $Y - $o['b'] / 200;
602
603
        $Y = pow($Y, 3) > 0.008856 ? pow($Y, 3) : ($Y - 16 / 116) / 7.787;
604
        $X = pow($X, 3) > 0.008856 ? pow($X, 3) : ($X - 16 / 116) / 7.787;
605
        $Z = pow($Z, 3) > 0.008856 ? pow($Z, 3) : ($Z - 16 / 116) / 7.787;
606
607
        return ['X' => $r['X'] * $X, 'Y' => $r['Y'] * $Y, $Z => $r['Z'] * $Z];
608
    }
609
610 View Code Duplication
    public function Lab_LCHab($o)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
611
    {
612
        $H = atan2($o['b'], $o['a']) * (180 / PI);
613
614
        if ($H < 0) {
615
            $H += 360;
616
        } else {
617
            if ($H > 360) {
618
                $H -= 360;
619
            }
620
        }
621
622
        return ['L' => $o['L'], 'C' => sqrt($o['a'] * $o['a'] + $o['b'] * $o['b']), 'H' => $H];
623
    }
624
625
    // CIE-L*uv (1976) = L: Luminescence / u: Saturation / v: Hue
626
627
    public function Luv_XYZ($o)
628
    {
629
        $r = $White;
0 ignored issues
show
Bug introduced by
The variable $White does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
630
631
        $Y = ($o['L'] + 16) / 116;
632
        $Y = (pow($Y, 3) > 0.008856) ? pow($Y, 3) : (($Y - 16 / 116) / 7.787);
633
634
        $_U = (4 * $r['X']) / ($r['X'] + (15 * $r['Y']) + (3 * $r['Z']));
635
        $_V = (9 * $r['Y']) / ($r['X'] + (15 * $r['Y']) + (3 * $r['Z']));
636
637
        $U = $o['u'] / (13 * $o['L']) + $_U;
638
        $V = $o['v'] / (13 * $o['L']) + $_V;
639
640
        $X = -(9 * $Y * $U) / (($U - 4) * $V - $U * $V);
641
        $Z = (9 * $Y - (15 * $V * $Y) - ($V * $X)) / (3 * $V);
642
643
        return ['X' => $X, 'Y' => $Y, 'Z' => $Z];
644
    }
645
646 View Code Duplication
    public function Luv_LCHuv($o)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
647
    {
648
        $H = atan2($o['v'], $o['u']) * (180 / PI);
649
650
        if ($H < 0) {
651
            $H += 360;
652
        } else {
653
            if ($H > 360) {
654
                $H -= 360;
655
            }
656
        }
657
658
        return ['L' => $o['L'], 'C' => sqrt($o['u'] * $o['u'] + $o['v'] * $o['v']), 'H' => $H];
659
    }
660
661
    // CIE-L*CH (1986) = L: Luminescece / C: Chromacity / H: Hue
662
663
    public function LCHab_Lab($o)
664
    {
665
        $rad = $o['H'] * (PI / 180);
666
667
        return ['L' => $o['L'], 'a' => cos($rad) * $o['C'], 'b' => sin($rad) * $o['C']];
668
    }
669
670
    public function LCHuv_Luv($o)
671
    {
672
        $rad = $o['H'] * (PI / 180);
673
674
        return ['L' => $o['L'], 'u' => cos($rad) * $o['C'], 'v' => sin($rad) * $o['C']];
675
    }
676
677
    public function adapt($o, $type)
678
    {
679
        $r = [ // Adaption methods
680
            'XYZ scaling' => [
681
                'A' => [[1, 0, 0], [0, 1, 0], [0, 0, 1]],
682
                'Z' => [[1, 0, 0], [0, 1, 0], [0, 0, 1]],
683
            ],
684
            'Von Kries' => [
685
                'A' => [[0.400240, -0.226300, 0], [0.707600, 1.165320, 0], [-0.080810, 0.045700, 0.918220]],
686
                'Z' => [[1.859936, 0.361191, 0], [-1.129382, 0.638812, 0], [0.219897, -0.000006, 1.089064]],
687
            ],
688
            'Bradford' => [
689
                'A' => [
690
                    [0.895100, 0.26640000, -0.16139900],
691
                    [-0.75019900, 1.71350, 0.0367000],
692
                    [0.03889900, -0.0685000, 1.02960000],
693
                ],
694
                'Z' => [
695
                    [0.986993, -0.14705399, 0.15996299],
696
                    [0.43230499, 0.51836, 0.0492912],
697
                    [-0.00852866, 0.0400428, 0.96848699],
698
                ],
699
            ],
700
        ];
701
702
        $WS = $this->_xyY;
703
        $WD = $this->White; // White Point Source + Destination
704
//		echo 'WS = ';
705
//		print_r($WS);
706
//		echo 'WD = ';
707
//		print_r($WD);
708
709
        $A = $r[$type]['A'];
710
        $Z = $r[$type]['Z']; // Load Matrices
711
712
        $CRD = $this->multiply($A, [[$WD['X']], [$WD['Y']], [$WD['Z']]]); // Convert to cone responce domain
713
        $CRS = $this->multiply($A, [[$WS['X']], [$WS['Y']], [$WS['Z']]]);
714
        //print_r($CRD);
715
        //print_r($CRS);
716
717
        $M = [
718
            [$CRD[0][0] / $CRS[0][0], 0, 0],
719
            [0, $CRD[1][0] / $CRS[1][0], 0],
720
            [0, 0, $CRD[2][0] / $CRS[2][0]],
721
        ]; // Scale Vectors
722
723
        $z = $this->multiply($Z,
724
            $this->multiply($M, $this->multiply($A, [[$o['X']], [$o['Y']], [$o['Z']]]))); // Back to XYZ
725
726
        return ['X' => $z[0][0], 'Y' => $z[1][0], 'Z' => $z[2][0]];
727
    }
728
729
    public function f($o)
730
    {
731
        $x = $this->xyY_XYZ($o);
732
//		echo 'o = ';
733
//		print_r($o);
734
//		echo 'x = ';
735
//		print_r($x);
736
        return $this->adapt($x, 'Bradford');
737
    }
738
739
    public function illuminant($observer, $type)
740
    {
741
        $o = $this->_illuminant[$type];
742
743
        $o = ($observer == 2) ? ['x' => $o[0], 'y' => $o[1], 'Y' => 1] : ['x' => $o[2], 'y' => $o[3], 'Y' => 1];
744
745
        //print_r($o);
746
747
        return $this->xyY_XYZ($o);
748
    }
749
750
    private $_Space;
751
    private $_Gamma;
752
    private $_White;
753
    private $_Matrix;
754
755
    public function profile($i)
756
    {
757
        $m = $this->_profile[$i];
758
        //print_r($m);
759
760
        $this->_Space = $i;
761
        $this->_Gamma = $m[0];
762
        $this->_White = $m[1];
763
        $this->_Matrix = $m;
764
765
        // Input Illuminant
766
767
        $this->_xyY = $this->illuminant('2', $m[1]);
768
        //print_r($this->_xyY);
769
770
        $R = $this->f(['x' => $m[2], 'y' => $m[3], 'Y' => $m[4]]);
771
        $G = $this->f(['x' => $m[5], 'y' => $m[6], 'Y' => $m[7]]);
772
        $B = $this->f(['x' => $m[8], 'y' => $m[9], 'Y' => $m[10]]);
773
//		print_r($R);
774
//		print_r($G);
775
//		print_r($B);
776
777
        $this->_RGB_XYZ = [$R['X'], $R['Y'], $R['Z'], $G['X'], $G['Y'], $G['Z'], $B['X'], $B['Y'], $B['Z']];
778
//		print_r($this->_RGB_XYZ);
779
780
        $this->_XYZ_RGB = $this->inverse($this->_RGB_XYZ);
781
    }
782
783
    private $_RGB_XYZ;
784
785
    private $_profile = [ // [ Gamma, Illuminant, Matrix ]
786
        'Adobe (1998)' => [2.2, 'D65', 0.64, 0.33, 0.297361, 0.21, 0.71, 0.627355, 0.15, 0.06, 0.075285],
787
        // Adobe
788
        'Apple RGB' => [1.8, 'D65', 0.625, 0.34, 0.244634, 0.28, 0.595, 0.672034, 0.155, 0.07, 0.083332],
789
        // Apple, a.k.a. SGI
790
        'BestRGB' => [2.2, 'D50', 0.7347, 0.2653, 0.228457, 0.215, 0.775, 0.737352, 0.13, 0.035, 0.034191],
791
        // Don Hutcheson
792
        'Beta RGB' => [2.2, 'D50', 0.6888, 0.3112, 0.303273, 0.1986, 0.7551, 0.663786, 0.1265, 0.0352, 0.032941],
793
        // Bruce Lindbloom
794
        'Bruce RGB' => [2.2, 'D65', 0.64, 0.33, 0.240995, 0.28, 0.65, 0.683554, 0.15, 0.06, 0.075452],
795
        // Bruce Fraser
796
        'CIE RGB' => [2.2, 'E', 0.735, 0.265, 0.176204, 0.274, 0.717, 0.812985, 0.167, 0.009, 0.010811],
797
        // CIE
798
        'ColorMatch' => [1.8, 'D50', 0.63, 0.34, 0.274884, 0.295, 0.605, 0.658132, 0.15, 0.075, 0.066985],
799
        // Radius
800
        'DonRGB4' => [2.2, 'D50', 0.696, 0.3, 0.27835, 0.215, 0.765, 0.68797, 0.13, 0.035, 0.03368],
801
        'eciRGB'  => [1.8, 'D50', 0.67, 0.33, 0.32025, 0.21, 0.71, 0.602071, 0.14, 0.08, 0.077679],
802
        // European Colour Initiative
803
        'Ekta Space PS5' => [2.2, 'D50', 0.695, 0.305, 0.260629, 0.26, 0.7, 0.734946, 0.11, 0.005, 0.004425],
804
        // Joseph Holmes
805
        'Generic RGB'   => [1.8, 'D65', 0.6295, 0.3407, 0.232546, 0.2949, 0.6055, 0.672501, 0.1551, 0.0762, 0.094952],
806
        'HDTV (HD-CIF)' => [1.95, 'D65', 0.64, 0.33, 0.212673, 0.3, 0.6, 0.715152, 0.15, 0.06, 0.072175],
807
        // a.k.a. ITU-R BT.701
808
        'NTSC' => [2.2, 'C', 0.67, 0.33, 0.298839, 0.21, 0.71, 0.586811, 0.14, 0.08, 0.11435],
809
        // National Television System Committee (NTSC), a.k.a. Y'I'Q'
810
        'PAL / SECAM' => [2.2, 'D65', 0.64, 0.33, 0.222021, 0.29, 0.6, 0.706645, 0.15, 0.06, 0.071334],
811
        // European Broadcasting Union (EBU), a.k.a. Y'U'V'
812
        'ProPhoto' => [1.8, 'D50', 0.7347, 0.2653, 0.28804, 0.1596, 0.8404, 0.711874, 0.0366, 0.0001, 0.000086],
813
        // Kodak, a.k.a. ROMM RGB
814
        'SGI'        => [1.47, 'D65', 0.625, 0.34, 0.244651, 0.28, 0.595, 0.672030, 0.155, 0.07, 0.083319],
815
        'SMPTE-240M' => [1.92, 'D65', 0.63, 0.34, 0.212413, 0.31, 0.595, 0.701044, 0.155, 0.07, 0.086543],
816
        'SMPTE-C'    => [2.2, 'D65', 0.63, 0.34, 0.212395, 0.31, 0.595, 0.701049, 0.155, 0.07, 0.086556],
817
        // Society of Motion Picture and Television Engineers (SMPTE)
818
        'sRGB' => [2.2, 'D65', 0.64, 0.33, 0.212656, 0.3, 0.6, 0.715158, 0.15, 0.06, 0.072186],
819
        // Microsoft & Hewlett - Packard
820
        'Wide Gamut' => [2.2, 'D50', 0.7347, 0.2653, 0.258187, 0.1152, 0.8264, 0.724938, 0.1566, 0.0177, 0.016875],
821
        // Adobe
822
    ];
823
824
    private $_illuminant = [ // [ x2°, y2°, x10°, y10°, CCT (Kelvin) ]
825
826
        'A'   => [0.44757, 0.40745, 0.45117, 0.40594, 2856],  // Incandescent tungsten
827
        'B'   => [0.34842, 0.35161, 0.3498, 0.3527, 4874],  // Obsolete, direct sunlight at noon
828
        'C'   => [0.31006, 0.31616, 0.31039, 0.31905, 6774],  // Obsolete, north sky daylight
829
        'D50' => [0.34567, 0.35850, 0.34773, 0.35952, 5003],  // ICC Profile PCS. Horizon light.
830
        'D55' => [0.33242, 0.34743, 0.33411, 0.34877, 5503],  // Compromise between incandescent and daylight
831
        'D65' => [0.31271, 0.32902, 0.31382, 0.33100, 6504],  // Daylight, sRGB color space
832
        'D75' => [0.29902, 0.31485, 0.29968, 0.31740, 7504],  // North sky day light
833
        'E'   => [0.33333, 0.33333, 0.33333, 0.33333, 5454],  // Equal energy
834
        'F1'  => [0.31310, 0.33727, 0.31811, 0.33559, 6430],  // Daylight Fluorescent
835
        'F2'  => [0.37208, 0.37529, 0.37925, 0.36733, 4230],  // Cool White Fluorescent
836
        'F3'  => [0.40910, 0.39430, 0.41761, 0.38324, 3450],  // White Fluorescent
837
        'F4'  => [0.44018, 0.40329, 0.44920, 0.39074, 2940],  // Warm White Fluorescent
838
        'F5'  => [0.31379, 0.34531, 0.31975, 0.34246, 6350],  // Daylight Fluorescent
839
        'F6'  => [0.37790, 0.38835, 0.38660, 0.37847, 4150],  // Lite White Fluorescent
840
        'F7'  => [0.31292, 0.32933, 0.31569, 0.32960, 6500],  // D65 simulator, day light simulator
841
        'F8'  => [0.34588, 0.35875, 0.34902, 0.35939, 5000],  // D50 simulator, Sylvania F40 Design
842
        'F9'  => [0.37417, 0.37281, 0.37829, 0.37045, 4150],  // Cool White Deluxe Fluorescent
843
        'F10' => [0.34609, 0.35986, 0.35090, 0.35444, 5000],  // Philips TL85, Ultralume 50
844
        'F11' => [0.38052, 0.37713, 0.38541, 0.37123, 4000],  // Philips TL84, Ultralume 40
845
        'F12' => [0.43695, 0.40441, 0.44256, 0.39717, 3000],
846
    ]; // Philips TL83, Ultralume 30
847
848
    public function multiply($m1, $m2)
849
    {
850
        $ni = count($m1);
851
        $ki = $ni;
852
        $i;
0 ignored issues
show
Bug introduced by
The variable $i seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
853
        $nj;
0 ignored issues
show
Bug introduced by
The variable $nj seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
854
        $kj = count($m2[0]);
855
        $j;
0 ignored issues
show
Bug introduced by
The variable $j seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
856
        $cols = count($m1[0]);
857
        $M = [];
858
        $sum;
0 ignored issues
show
Bug introduced by
The variable $sum seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
859
        $nc;
0 ignored issues
show
Bug introduced by
The variable $nc seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
860
        $c;
0 ignored issues
show
Bug introduced by
The variable $c seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
861
        do {
862
            $i = $ki - $ni;
863
            $M[$i] = [];
864
            $nj = $kj;
865
            do {
866
                $j = $kj - $nj;
867
                $sum = 0;
868
                $nc = $cols;
869
                do {
870
                    $c = $cols - $nc;
871
                    $sum += $m1[$i][$c] * $m2[$c][$j];
872
                } while (($nc -= 1));
873
                $M[$i][$j] = $sum;
874
            } while (($nj -= 1));
875
        } while (($ni -= 1));
876
877
        return $M;
878
    }
879
880
    public function determinant($m)
881
    { // 3x3
882
883
//		print_r($m);
884
        return $m[0] * ($m[4] * $m[8] - $m[5] * $m[7]) -
885
        $m[1] * ($m[3] * $m[8] - $m[5] * $m[6]) +
886
        $m[2] * ($m[3] * $m[7] - $m[4] * $m[6]);
887
    }
888
889
    public function inverse($m)
890
    { // 3x3
891
892
        $d = 1.0 / $this->determinant($m);
893
894
        return [
895
            $d * ($m[4] * $m[8] - $m[5] * $m[7]),
896
            $d * (-1 * ($m[1] * $m[8] - $m[2] * $m[7])),
897
            $d * ($m[1] * $m[5] - $m[2] * $m[4]),
898
            $d * (-1 * ($m[3] * $m[8] - $m[5] * $m[6])),
899
            $d * ($m[0] * $m[8] - $m[2] * $m[6]),
900
            $d * (-1 * ($m[0] * $m[5] - $m[2] * $m[3])),
901
            $d * ($m[3] * $m[7] - $m[4] * $m[6]),
902
            $d * (-1 * ($m[0] * $m[7] - $m[1] * $m[6])),
903
            $d * ($m[0] * $m[4] - $m[1] * $m[3]),
904
        ];
905
    }
906
}
907