Passed
Push — dev ( e9cf15...64faf7 )
by Fike
03:26
created

Color::blend()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 2
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace AmaTeam\Image\Projection\Image\Utility;
4
5
class Color
6
{
7
    /**
8
     * @param int $target
9
     * @param int $source
10
     * @return int
11
     */
12
    public static function blend($target, $source)
13
    {
14
        $targetAlpha = $target & 0xFF;
15
        $sourceAlpha = $source & 0xFF;
16
        $influence = $sourceAlpha / $targetAlpha;
17
        $result = ((int) ($targetAlpha + (255 - $targetAlpha) * ($sourceAlpha / 255))) & 0xFF;
18
        for ($i = 3; $i >= 1; $i--) {
19
            $shift = $i * 8;
20
            $targetColor = ($target >> $shift) & 0xFF;
21
            $sourceColor = ($source >> $shift) & 0xFF;
22
            $difference = $sourceColor - $targetColor;
23
            $color = $targetColor + $difference * $influence;
24
            $result |= (((int) $color) & 0xFF) << $shift;
25
        }
26
        return $result;
27
    }
28
}
29