PluginProbe
E2Pdf – Export Pdf Tool for WordPress / 1.32.17
E2Pdf – Export Pdf Tool for WordPress v1.32.17
1.32.49 1.32.48 1.32.43 1.32.40 1.32.34 1.32.32 1.32.31 1.32.26 1.32.22 1.32.23 1.32.18 1.32.17 1.32.15 trunk 1.00.00 1.00.13 1.01.01 1.02.02 1.03.07 1.04.07 1.05.03 1.06.02 1.07.11 1.08.00 1.08.06 All 72 releases
e2pdf / vendors / svggraph / Colour.php

Colour.php in E2Pdf – Export Pdf Tool for WordPress 1.32.17, at vendors/svggraph/Colour.php

240 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Copyright (C) 2019-2020 Graham Breach
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19 * For more information, please contact <graham@goat1000.com>
20 */
21
22 namespace Goat1000\SVGGraph;
23
24 /**
25 * Class for parsing colour/gradient/pattern
26 */
27 class Colour {
28 private $graph;
29 private $colour = '#000';
30 private $opacity = 1;
31 private $as_string = '';
32 private $gradient = false;
33 private $pattern = false;
34 private $radial = false;
35 private $key = null;
36
37 public function __construct(&$graph, $colour, $allow_gradient = true,
38 $allow_pattern = true, $radial_gradient = false)
39 {
40 if($colour === null || $colour === 'none') {
41 $this->colour = $this->as_string = 'none';
42 return;
43 }
44
45 if(is_object($colour) && get_class($colour) === 'Goat1000\\SVGGraph\\Colour')
46 $colour = $colour->colour;
47
48 if(is_string($colour)) {
49 $this->extract($colour);
50 return;
51 }
52
53 // if not a string, must be an array with a colour at index 0
54 $valid = false;
55 if(is_array($colour)) {
56 if(is_string($colour[0])) {
57 $valid = true;
58 } elseif(isset($colour['pattern'])) {
59 // allow gradients as first colour of pattern
60 if(is_array($colour[0]) && count($colour[0]) > 1) {
61 $valid = true;
62 foreach($colour[0] as $i) {
63 if(!is_string($i))
64 $valid = false;
65 }
66 }
67 }
68 }
69
70 if(!$valid)
71 throw new \InvalidArgumentException('Malformed colour value: ' .
72 serialize($colour));
73
74 if(count($colour) < 2 || (!$allow_gradient && !$allow_pattern)) {
75 $this->extract($colour[0]);
76 return;
77 }
78
79 $this->graph =& $graph;
80 $this->colour = $colour;
81 if(isset($colour['pattern'])) {
82 if(!$allow_pattern)
83 $this->extract($colour[0]);
84 $this->pattern = true;
85 return;
86 }
87
88 if(!$allow_gradient) {
89 $this->extract($colour[0]);
90 return;
91 }
92
93 $err = array_diff_key($colour, array_keys(array_keys($colour)));
94 if($err)
95 throw new \InvalidArgumentException('Malformed gradient/pattern: ' .
96 serialize($colour));
97 $this->gradient = true;
98
99 $last = count($colour) - 1;
100 $this->radial = $radial_gradient || $colour[$last] == 'r';
101 }
102
103 /**
104 * Extract the colour and opacity into this instance
105 */
106 private function extract($colour)
107 {
108 $filters = '';
109 $fpos = strpos($colour, '/');
110 if($fpos !== false) {
111 $filters = substr($colour, $fpos + 1);
112 $colour = substr($colour, 0, $fpos);
113 }
114 list($colour, $opacity) = $this->extractOpacity($colour);
115 $this->colour = $this->as_string = $colour;
116 $this->opacity = $opacity;
117
118 // filters don't work on 'none' and 'transparent', so don't try
119 if($filters !== '' && $colour !== 'none' && $colour !== 'transparent') {
120 $cf = new ColourFilter($colour, $filters);
121 $this->colour = $this->as_string = (string)$cf;
122 }
123 }
124
125 /**
126 * Returns an array containing the colour and opacity from a string
127 */
128 public static function extractOpacity($colour)
129 {
130 $opacity = 1.0;
131 if(strpos($colour, ':') !== false) {
132 $parts = explode(':', $colour);
133 if(is_numeric($parts[0]) || count($parts) == 3)
134 $gstop = array_shift($parts);
135
136 $c = array_shift($parts);
137 $ovalue = array_shift($parts);
138 if($ovalue !== null) {
139 if(!is_numeric($ovalue))
140 throw new \Exception('Non-numeric opacity in colour: ' . $colour);
141 $opacity = min(1.0, max(0.0, 1.0 * $ovalue));
142 }
143 $colour = $c;
144 }
145 return [$colour, $opacity];
146 }
147
148 /**
149 * Output for SVG values
150 */
151 public function __toString()
152 {
153 if($this->as_string !== '')
154 return $this->as_string;
155
156 if($this->gradient) {
157 $gradient_id = $this->graph->defs->addGradient($this->colour, $this->key,
158 $this->radial);
159 $this->as_string = 'url(#' . $gradient_id . ')';
160 } elseif($this->pattern) {
161 $pattern_id = $this->graph->defs->addPattern($this->colour);
162 $this->as_string = 'url(#' . $pattern_id . ')';
163 }
164
165 return $this->as_string;
166 }
167
168 /**
169 * Sets the key for use with gradients
170 */
171 public function setGradientKey($key)
172 {
173 $this->key = $key;
174 }
175
176 /**
177 * Returns the opacity value
178 */
179 public function opacity($as_number = false)
180 {
181 if($as_number)
182 return new Number($this->opacity);
183 return $this->opacity;
184 }
185
186 /**
187 * Returns the solid colour
188 */
189 public function solid()
190 {
191 if(!$this->gradient && !$this->pattern)
192 return $this->colour;
193
194 list($solid) = $this->extractOpacity($this->colour[0]);
195 return $solid;
196 }
197
198 /**
199 * Returns the R,G,B for the colour
200 */
201 public function rgb()
202 {
203 $rgb = new RGBColour($this->solid());
204 return $rgb->getRGB();
205 }
206
207 /**
208 * Returns true if the colour is a gradient
209 */
210 public function isGradient()
211 {
212 return $this->gradient;
213 }
214
215 /**
216 * Returns true if the colour is a pattern
217 */
218 public function isPattern()
219 {
220 return $this->pattern;
221 }
222
223 /**
224 * Returns true if the colour is 'none'
225 */
226 public function isNone()
227 {
228 return $this->colour === 'none';
229 }
230
231 /**
232 * Returns true if a radial gradient
233 */
234 public function isRadial()
235 {
236 return $this->gradient && $this->radial;
237 }
238 }
239
240