Completed
Push — master ( 8a5f3d...1f6ac0 )
by Edgar
03:21
created

UniformGradient::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace nstdio\svg\gradient;
3
4
use nstdio\svg\container\ContainerInterface;
5
use nstdio\svg\ElementInterface;
6
7
/**
8
 * Class UniformGradient
9
 *
10
 * @package nstdio\svg\gradient
11
 * @author  Edgar Asatryan <[email protected]>
12
 */
13
abstract class UniformGradient extends Gradient
14
{
15
16
    public static function gradient(ElementInterface $svg, array $colors, $id = null, $gradientType = 'linear')
17
    {
18
        $gradient = self::getGradient($svg, $id, $gradientType);
19
        if (count($colors) <= 1) {
20
            $colors = array_merge($colors, $colors);
21
            if (empty($colors)) {
22
                $colors = ['white', 'black'];
23
            }
24
        }
25
        $step = 1 / (count($colors) - 1);
26
        $offsets = array_map(function ($item) {
27
            return $item % 1 !== 0 ? sprintf('%0.2f', $item) : $item;
28
        }, range(0, 1, $step));
29
30
        foreach ($colors as $key => $color) {
31
            $ret = [];
32
            $ret['offset'] = $offsets[$key];
33
            $ret['stop-color'] = $color;
34
            $gradient->appendStop(new Stop($gradient, $ret));
35
        }
36
37
        return $gradient;
38
    }
39
40
    public static function verticalFromTop(ContainerInterface $container, array $colors, $id = null)
41
    {
42
        return self::gradient($container, $colors, $id)->apply(Direction::get('topBottom'));
43
    }
44
45
    public static function verticalFromBottom(ContainerInterface $container, array $colors, $id = null)
46
    {
47
        return self::gradient($container, $colors, $id)->apply(Direction::get('bottomTop'));
48
    }
49
50
    public static function diagonalFromTopLeft(ContainerInterface $container, array $colors, $id = null)
51
    {
52
        return self::gradient($container, $colors, $id)->apply(Direction::get('topLeftBottomRight'));
53
    }
54
55
    public static function diagonalFromBottomRight(ContainerInterface $container, array $colors, $id = null)
56
    {
57
        return self::gradient($container, $colors, $id)->apply(Direction::get('bottomRightTopLeft'));
58
    }
59
60
    public static function diagonalFromBottomLeft(ContainerInterface $container, array $colors, $id = null)
61
    {
62
        return self::gradient($container, $colors, $id)->apply(Direction::get('bottomLeftTopRight'));
63
    }
64
65
    public static function diagonalFromTopRight(ContainerInterface $container, array $colors, $id = null)
66
    {
67
        return self::gradient($container, $colors, $id)->apply(Direction::get('topRightBottomLeft'));
68
    }
69
70
    public static function horizontalFromLeft(ContainerInterface $container, array $colors, $id = null)
71
    {
72
        return self::gradient($container, $colors, $id)->apply(Direction::get('leftRight'));
73
    }
74
75
    public static function horizontalFromRight(ContainerInterface $container, array $colors, $id = null)
76
    {
77
        return self::gradient($container, $colors, $id)->apply(Direction::get('rightLeft'));
78
    }
79
80
    public static function radialTopLeft(ContainerInterface $container, array $colors, $id = null)
81
    {
82
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialTopLeft'));
83
    }
84
85
86
    public static function radialTopRight(ContainerInterface $container, array $colors, $id = null)
87
    {
88
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialTopRight'));
89
    }
90
91
    public static function radialBottomLeft(ContainerInterface $container, array $colors, $id = null)
92
    {
93
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialBottomLeft'));
94
    }
95
96
    public static function radialBottomRight(ContainerInterface $container, array $colors, $id = null)
97
    {
98
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialBottomRight'));
99
    }
100
101
    public static function radialTopCenter(ContainerInterface $container, array $colors, $id = null)
102
    {
103
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialTopCenter'));
104
    }
105
106
    public static function radialLeftCenter(ContainerInterface $container, array $colors, $id = null)
107
    {
108
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialLeftCenter'));
109
    }
110
111
    public static function radialBottomCenter(ContainerInterface $container, array $colors, $id = null)
112
    {
113
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialBottomCenter'));
114
    }
115
116
    public static function radialRightCenter(ContainerInterface $container, array $colors, $id = null)
117
    {
118
        return self::gradient($container, $colors, $id, self::RADIAL)->apply(Direction::get('radialRightCenter'));
119
    }
120
121
    /**
122
     * @param ElementInterface $svg
123
     * @param                  $id
124
     * @param                  $gradientType
125
     *
126
     * @return LinearGradient|RadialGradient
127
     */
128
    private static function getGradient(ElementInterface $svg, $id, $gradientType)
129
    {
130
        return $gradientType === self::LINEAR ? new LinearGradient($svg, $id) : new RadialGradient($svg, $id);
131
    }
132
}