-
Notifications
You must be signed in to change notification settings - Fork 31
/
GradientList.php
213 lines (191 loc) · 6.07 KB
/
GradientList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
/**
* Copyright (C) 2019-2022 Graham Breach
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* For more information, please contact <[email protected]>
*/
namespace Goat1000\SVGGraph;
/**
* Creates and stores gradients
*/
class GradientList {
private $graph;
private $gradients = [];
private $gradient_map = [];
public function __construct(&$graph)
{
$this->graph =& $graph;
}
/**
* Adds a gradient to the list, returning the element ID for use in url
*/
public function addGradient($colours, $key = null, $radial = false)
{
if($key === null || !isset($this->gradients[$key])) {
if($radial) {
// if this is a radial gradient, it must end with 'r'
$last = count($colours) - 1;
if(strlen($colours[$last]) == 1)
$colours[$last] = 'r';
else
$colours[] = 'r';
}
// find out if this gradient already stored
$hash = md5(serialize($colours));
if(isset($this->gradient_map[$hash]))
return $this->gradient_map[$hash];
$id = $this->graph->newID();
if($key === null)
$key = $id;
$this->gradients[$key] = ['id' => $id, 'colours' => $colours];
$this->gradient_map[$hash] = $id;
return $id;
}
return $this->gradients[$key]['id'];
}
/**
* Creates a gradient element
*/
private function makeGradient($key)
{
$stops = '';
$direction = 'v';
$type = 'linearGradient';
$colours = $this->gradients[$key]['colours'];
$id = $this->gradients[$key]['id'];
if(in_array($colours[count($colours)-1], ['h', 'v', 'r']))
$direction = array_pop($colours);
if($direction == 'r') {
$type = 'radialGradient';
$gradient = ['id' => $id];
} else {
$x2 = $direction == 'v' ? 0 : '100%';
$y2 = $direction == 'h' ? 0 : '100%';
$gradient = ['id' => $id, 'x1' => 0, 'x2' => $x2, 'y1' => 0, 'y2' => $y2];
}
$segments = $this->decompose($colours);
foreach($segments as $segment) {
list($offset, $colour, $opacity) = $segment;
$stop = ['offset' => $offset . '%', 'stop-color' => $colour];
if(is_numeric($opacity))
$stop['stop-opacity'] = $opacity;
$stops .= $this->graph->element('stop', $stop);
}
return $this->graph->element($type, $gradient, null, $stops);
}
/**
* Calculates a colour component from a gradient step
*/
private function calcComponent($c0, $c1, $o0, $o1, $d)
{
return $c0 + ($c1 - $c0) * ($d - $o0) / ($o1 - $o0);
}
/**
* Returns the colour at a position in the gradient
*/
public function getColour($key, $position)
{
if(!isset($this->gradients[$key]))
return '';
$colours = $this->gradients[$key]['colours'];
if(in_array($colours[count($colours)-1], ['h', 'v', 'r']))
$direction = array_pop($colours);
$segments = $this->decompose($colours);
// function to produce colour string
$make_colour = function($r, $g, $b, $o) {
$str = "rgb({$r},{$g},{$b})";
if(!is_numeric($o) || $o >= 1)
return $str;
if($o <= 0)
return 'none';
$str .= ':' . $o;
return $str;
};
$position = min(100, max(0, $position));
$seg0 = $segments[0];
$seg1 = null;
foreach($segments as $seg) {
if(abs($seg[0] - $position) < 0.1) {
// stop colour at or close to requested position
$c = $seg[1]->rgb();
$c_string = $make_colour($c[0], $c[1], $c[2], $seg[2]);
$colour = new Colour($this->graph, $c_string);
return $colour;
}
if($seg[0] > $position) {
$seg1 = $seg;
break;
}
$seg0 = $seg;
}
if($seg1 === null) {
$seg1 = array_pop($segments);
$seg1[0] = 100;
}
$c0 = $seg0[1]->rgb();
$c1 = $seg1[1]->rgb();
$vr = $this->calcComponent($c0[0], $c1[0], $seg0[0], $seg1[0], $position);
$vg = $this->calcComponent($c0[1], $c1[1], $seg0[0], $seg1[0], $position);
$vb = $this->calcComponent($c0[2], $c1[2], $seg0[0], $seg1[0], $position);
$o0 = $seg0[2] === null ? 1 : $seg0[2];
$o1 = $seg1[2] === null ? 1 : $seg1[2];
$vo = $this->calcComponent($o0, $o1, $seg0[0], $seg1[0], $position);
$c_string = $make_colour($vr, $vg, $vb, $vo);
$colour = new Colour($this->graph, $c_string);
return $colour;
}
/**
* Breaks gradient array down into components
*/
public function decompose($colours)
{
$col_mul = 100 / (count($colours) - 1);
$offset = 0;
$decomposed = [];
foreach($colours as $pos => $colour) {
$opacity = null;
$poffset = $pos * $col_mul;
if(strpos($colour, ':') !== false) {
// opacity, stop offset or both
$parts = explode(':', $colour);
if(is_numeric($parts[0]) || count($parts) == 3) {
$poffset = array_shift($parts);
}
// stick the other parts back together and let the Colour class
// figure it out
$colour = new Colour($this->graph, implode(':', $parts));
$opacity = $colour->opacity();
if($opacity == 1)
$opacity = null;
} else {
$colour = new Colour($this->graph, $colour);
}
// set the offset to the most meaningful number
$offset = min(100, max(0, $offset, $poffset));
$decomposed[] = [$offset, $colour, $opacity];
}
return $decomposed;
}
/**
* Defines the list of gradients
*/
public function makeGradients(&$defs)
{
foreach($this->gradients as $key => $gradient)
$defs->add($this->makeGradient($key));
}
}