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