Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
22 / 24
50.00% covered (warning)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractEffect
91.67% covered (success)
91.67%
22 / 24
50.00% covered (warning)
50.00%
1 / 2
7.03
0.00% covered (danger)
0.00%
0 / 1
 getBlend
91.30% covered (success)
91.30%
21 / 23
0.00% covered (danger)
0.00%
0 / 1
6.02
 calculateSteps
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Image\Effect;
16
17use Pop\Image\AbstractEditObject;
18use Pop\Color\Color;
19
20/**
21 * Abstract effect class
22 *
23 * @category   Pop
24 * @package    Pop\Image
25 * @author     Nick Sagona, III <dev@noladev.com>
26 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
27 * @license    https://www.popphp.org/license     New BSD License
28 * @version    5.0.0
29 */
30abstract class AbstractEffect extends AbstractEditObject implements EffectInterface
31{
32
33    /**
34     * Get the blend between 2 colors
35     *
36     * @param  Color\ColorInterface $color1
37     * @param  Color\ColorInterface $color2
38     * @param  int                  $tween
39     * @throws Exception
40     * @return array
41     */
42    public function getBlend(Color\ColorInterface $color1, Color\ColorInterface $color2, int $tween): array
43    {
44        if (!($color1 instanceof Color\Rgb)) {
45            if (!method_exists($color1, 'toRgb')) {
46                throw new Exception('Error: The color object does not support conversion to RGB.');
47            }
48            $color1 = $color1->toRgb();
49        }
50        if (!($color2 instanceof Color\Rgb)) {
51            if (!method_exists($color2, 'toRgb')) {
52                throw new Exception('Error: The color object does not support conversion to RGB.');
53            }
54            $color2 = $color2->toRgb();
55        }
56
57        $blend = ['r' => [], 'g' => [], 'b' => []];
58
59        $r1 = $color1->getR();
60        $g1 = $color1->getG();
61        $b1 = $color1->getB();
62
63        $r2 = $color2->getR();
64        $g2 = $color2->getG();
65        $b2 = $color2->getB();
66
67        $rTotal = $r2 - $r1;
68        $gTotal = $g2 - $g1;
69        $bTotal = $b2 - $b1;
70
71        for ($i = 0; $i <= $tween; $i++) {
72            $blend['r'][] = (int)round($this->calculateSteps($i, $r1, $rTotal, $tween));
73            $blend['g'][] = (int)round($this->calculateSteps($i, $g1, $gTotal, $tween));
74            $blend['b'][] = (int)round($this->calculateSteps($i, $b1, $bTotal, $tween));
75        }
76
77        return $blend;
78    }
79
80    /**
81     * Calculate the steps between two points
82     *
83     * @param  int $curStep
84     * @param  int $start
85     * @param  int $end
86     * @param  int $totalSteps
87     * @return int|float
88     */
89    public function calculateSteps(int $curStep, int $start, int $end, int $totalSteps): int|float
90    {
91        return ($end * ($curStep / $totalSteps)) + $start;
92    }
93
94}