Skip to content

Commit 4e529ae

Browse files
authored
Refactor gis color (#17895)
* Use int srid in gis tests Signed-off-by: Maximilian Krög <maxi_kroeg@web.de> * Typecheck gis color Only use array<int> as color Signed-off-by: Maximilian Krög <maxi_kroeg@web.de> Signed-off-by: Maximilian Krög <maxi_kroeg@web.de>
1 parent 22d07e3 commit 4e529ae

20 files changed

Lines changed: 279 additions & 490 deletions

libraries/classes/Gis/GisGeometry.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,25 @@ abstract class GisGeometry
3232
*
3333
* @param string $spatial GIS data object
3434
* @param string $label label for the GIS data object
35-
* @param string $color color for the GIS data object
35+
* @param int[] $color color for the GIS data object
3636
* @param array $scale_data data related to scaling
3737
*
3838
* @return string the code related to a row in the GIS dataset
3939
*/
40-
abstract public function prepareRowAsSvg($spatial, $label, $color, array $scale_data);
40+
abstract public function prepareRowAsSvg($spatial, $label, array $color, array $scale_data);
4141

4242
/**
4343
* Adds to the PNG image object, the data related to a row in the GIS dataset.
4444
*
4545
* @param string $spatial GIS POLYGON object
4646
* @param string|null $label Label for the GIS POLYGON object
47-
* @param string $color Color for the GIS POLYGON object
47+
* @param int[] $color Color for the GIS POLYGON object
4848
* @param array $scale_data Array containing data related to scaling
4949
*/
5050
abstract public function prepareRowAsPng(
5151
$spatial,
5252
?string $label,
53-
$color,
53+
array $color,
5454
array $scale_data,
5555
ImageWrapper $image
5656
): ImageWrapper;
@@ -60,7 +60,7 @@ abstract public function prepareRowAsPng(
6060
*
6161
* @param string $spatial GIS data object
6262
* @param string|null $label label for the GIS data object
63-
* @param string $color color for the GIS data object
63+
* @param int[] $color color for the GIS data object
6464
* @param array $scale_data array containing data related to scaling
6565
* @param TCPDF $pdf TCPDF instance
6666
*
@@ -69,7 +69,7 @@ abstract public function prepareRowAsPng(
6969
abstract public function prepareRowAsPdf(
7070
$spatial,
7171
?string $label,
72-
$color,
72+
array $color,
7373
array $scale_data,
7474
$pdf
7575
);
@@ -81,7 +81,7 @@ abstract public function prepareRowAsPdf(
8181
* @param string $spatial GIS data object
8282
* @param int $srid spatial reference ID
8383
* @param string $label label for the GIS data object
84-
* @param array $color color for the GIS data object
84+
* @param int[] $color color for the GIS data object
8585
* @param array $scale_data array containing data related to scaling
8686
*
8787
* @return string the JavaScript related to a row in the GIS dataset
@@ -90,7 +90,7 @@ abstract public function prepareRowAsOl(
9090
$spatial,
9191
int $srid,
9292
$label,
93-
$color,
93+
array $color,
9494
array $scale_data
9595
);
9696

libraries/classes/Gis/GisGeometryCollection.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ public function scaleRow($spatial)
109109
*
110110
* @param string $spatial GIS POLYGON object
111111
* @param string|null $label Label for the GIS POLYGON object
112-
* @param string $color Color for the GIS POLYGON object
112+
* @param int[] $color Color for the GIS POLYGON object
113113
* @param array $scale_data Array containing data related to scaling
114114
*/
115115
public function prepareRowAsPng(
116116
$spatial,
117117
?string $label,
118-
$color,
118+
array $color,
119119
array $scale_data,
120120
ImageWrapper $image
121121
): ImageWrapper {
@@ -148,13 +148,13 @@ public function prepareRowAsPng(
148148
*
149149
* @param string $spatial GIS GEOMETRYCOLLECTION object
150150
* @param string|null $label label for the GIS GEOMETRYCOLLECTION object
151-
* @param string $color color for the GIS GEOMETRYCOLLECTION object
151+
* @param int[] $color color for the GIS GEOMETRYCOLLECTION object
152152
* @param array $scale_data array containing data related to scaling
153153
* @param TCPDF $pdf TCPDF instance
154154
*
155155
* @return TCPDF the modified TCPDF instance
156156
*/
157-
public function prepareRowAsPdf($spatial, ?string $label, $color, array $scale_data, $pdf)
157+
public function prepareRowAsPdf($spatial, ?string $label, array $color, array $scale_data, $pdf)
158158
{
159159
// Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
160160
$goem_col = mb_substr($spatial, 19, -1);
@@ -185,12 +185,12 @@ public function prepareRowAsPdf($spatial, ?string $label, $color, array $scale_d
185185
*
186186
* @param string $spatial GIS GEOMETRYCOLLECTION object
187187
* @param string $label label for the GIS GEOMETRYCOLLECTION object
188-
* @param string $color color for the GIS GEOMETRYCOLLECTION object
188+
* @param int[] $color color for the GIS GEOMETRYCOLLECTION object
189189
* @param array $scale_data array containing data related to scaling
190190
*
191191
* @return string the code related to a row in the GIS dataset
192192
*/
193-
public function prepareRowAsSvg($spatial, $label, $color, array $scale_data)
193+
public function prepareRowAsSvg($spatial, $label, array $color, array $scale_data)
194194
{
195195
$row = '';
196196

@@ -225,12 +225,12 @@ public function prepareRowAsSvg($spatial, $label, $color, array $scale_data)
225225
* @param string $spatial GIS GEOMETRYCOLLECTION object
226226
* @param int $srid spatial reference ID
227227
* @param string $label label for the GIS GEOMETRYCOLLECTION object
228-
* @param array $color color for the GIS GEOMETRYCOLLECTION object
228+
* @param int[] $color color for the GIS GEOMETRYCOLLECTION object
229229
* @param array $scale_data array containing data related to scaling
230230
*
231231
* @return string JavaScript related to a row in the GIS dataset
232232
*/
233-
public function prepareRowAsOl($spatial, int $srid, $label, $color, array $scale_data)
233+
public function prepareRowAsOl($spatial, int $srid, $label, array $color, array $scale_data)
234234
{
235235
$row = '';
236236

libraries/classes/Gis/GisLineString.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
use TCPDF;
1212

1313
use function count;
14-
use function hexdec;
1514
use function json_encode;
1615
use function mb_substr;
1716
use function round;
17+
use function sprintf;
1818
use function trim;
1919

2020
/**
@@ -66,22 +66,19 @@ public function scaleRow($spatial)
6666
*
6767
* @param string $spatial GIS POLYGON object
6868
* @param string|null $label Label for the GIS POLYGON object
69-
* @param string $line_color Color for the GIS POLYGON object
69+
* @param int[] $color Color for the GIS POLYGON object
7070
* @param array $scale_data Array containing data related to scaling
7171
*/
7272
public function prepareRowAsPng(
7373
$spatial,
7474
?string $label,
75-
$line_color,
75+
array $color,
7676
array $scale_data,
7777
ImageWrapper $image
7878
): ImageWrapper {
7979
// allocate colors
8080
$black = $image->colorAllocate(0, 0, 0);
81-
$red = (int) hexdec(mb_substr($line_color, 1, 2));
82-
$green = (int) hexdec(mb_substr($line_color, 3, 2));
83-
$blue = (int) hexdec(mb_substr($line_color, 4, 2));
84-
$color = $image->colorAllocate($red, $green, $blue);
81+
$line_color = $image->colorAllocate(...$color);
8582

8683
$label = trim($label ?? '');
8784

@@ -97,7 +94,7 @@ public function prepareRowAsPng(
9794
(int) round($temp_point[1]),
9895
(int) round($point[0]),
9996
(int) round($point[1]),
100-
$color
97+
$line_color
10198
);
10299
}
103100

@@ -123,25 +120,17 @@ public function prepareRowAsPng(
123120
*
124121
* @param string $spatial GIS LINESTRING object
125122
* @param string|null $label Label for the GIS LINESTRING object
126-
* @param string $line_color Color for the GIS LINESTRING object
123+
* @param int[] $color Color for the GIS LINESTRING object
127124
* @param array $scale_data Array containing data related to scaling
128125
* @param TCPDF $pdf TCPDF instance
129126
*
130127
* @return TCPDF the modified TCPDF instance
131128
*/
132-
public function prepareRowAsPdf($spatial, ?string $label, $line_color, array $scale_data, $pdf)
129+
public function prepareRowAsPdf($spatial, ?string $label, array $color, array $scale_data, $pdf)
133130
{
134-
// allocate colors
135-
$red = hexdec(mb_substr($line_color, 1, 2));
136-
$green = hexdec(mb_substr($line_color, 3, 2));
137-
$blue = hexdec(mb_substr($line_color, 4, 2));
138131
$line = [
139132
'width' => 1.5,
140-
'color' => [
141-
$red,
142-
$green,
143-
$blue,
144-
],
133+
'color' => $color,
145134
];
146135

147136
$label = trim($label ?? '');
@@ -174,19 +163,19 @@ public function prepareRowAsPdf($spatial, ?string $label, $line_color, array $sc
174163
*
175164
* @param string $spatial GIS LINESTRING object
176165
* @param string $label Label for the GIS LINESTRING object
177-
* @param string $line_color Color for the GIS LINESTRING object
166+
* @param int[] $color Color for the GIS LINESTRING object
178167
* @param array $scale_data Array containing data related to scaling
179168
*
180169
* @return string the code related to a row in the GIS dataset
181170
*/
182-
public function prepareRowAsSvg($spatial, $label, $line_color, array $scale_data)
171+
public function prepareRowAsSvg($spatial, $label, array $color, array $scale_data)
183172
{
184173
$line_options = [
185174
'name' => $label,
186175
'id' => $label . $this->getRandomId(),
187176
'class' => 'linestring vector',
188177
'fill' => 'none',
189-
'stroke' => $line_color,
178+
'stroke' => sprintf('#%02x%02x%02x', ...$color),
190179
'stroke-width' => 2,
191180
];
192181

@@ -216,15 +205,15 @@ public function prepareRowAsSvg($spatial, $label, $line_color, array $scale_data
216205
* @param string $spatial GIS LINESTRING object
217206
* @param int $srid Spatial reference ID
218207
* @param string $label Label for the GIS LINESTRING object
219-
* @param array $line_color Color for the GIS LINESTRING object
208+
* @param int[] $color Color for the GIS LINESTRING object
220209
* @param array $scale_data Array containing data related to scaling
221210
*
222211
* @return string JavaScript related to a row in the GIS dataset
223212
*/
224-
public function prepareRowAsOl($spatial, int $srid, $label, $line_color, array $scale_data)
213+
public function prepareRowAsOl($spatial, int $srid, $label, array $color, array $scale_data)
225214
{
226215
$stroke_style = [
227-
'color' => $line_color,
216+
'color' => $color,
228217
'width' => 2,
229218
];
230219

libraries/classes/Gis/GisMultiLineString.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
use function count;
1414
use function explode;
15-
use function hexdec;
1615
use function json_encode;
1716
use function mb_substr;
1817
use function round;
18+
use function sprintf;
1919
use function trim;
2020

2121
/**
@@ -75,22 +75,19 @@ public function scaleRow($spatial)
7575
*
7676
* @param string $spatial GIS POLYGON object
7777
* @param string|null $label Label for the GIS POLYGON object
78-
* @param string $line_color Color for the GIS POLYGON object
78+
* @param int[] $color Color for the GIS POLYGON object
7979
* @param array $scale_data Array containing data related to scaling
8080
*/
8181
public function prepareRowAsPng(
8282
$spatial,
8383
?string $label,
84-
$line_color,
84+
array $color,
8585
array $scale_data,
8686
ImageWrapper $image
8787
): ImageWrapper {
8888
// allocate colors
8989
$black = $image->colorAllocate(0, 0, 0);
90-
$red = (int) hexdec(mb_substr($line_color, 1, 2));
91-
$green = (int) hexdec(mb_substr($line_color, 3, 2));
92-
$blue = (int) hexdec(mb_substr($line_color, 4, 2));
93-
$color = $image->colorAllocate($red, $green, $blue);
90+
$line_color = $image->colorAllocate(...$color);
9491

9592
$label = trim($label ?? '');
9693

@@ -110,7 +107,7 @@ public function prepareRowAsPng(
110107
(int) round($temp_point[1]),
111108
(int) round($point[0]),
112109
(int) round($point[1]),
113-
$color
110+
$line_color
114111
);
115112
}
116113

@@ -140,25 +137,17 @@ public function prepareRowAsPng(
140137
*
141138
* @param string $spatial GIS MULTILINESTRING object
142139
* @param string|null $label Label for the GIS MULTILINESTRING object
143-
* @param string $line_color Color for the GIS MULTILINESTRING object
140+
* @param int[] $color Color for the GIS MULTILINESTRING object
144141
* @param array $scale_data Array containing data related to scaling
145142
* @param TCPDF $pdf TCPDF instance
146143
*
147144
* @return TCPDF the modified TCPDF instance
148145
*/
149-
public function prepareRowAsPdf($spatial, ?string $label, $line_color, array $scale_data, $pdf)
146+
public function prepareRowAsPdf($spatial, ?string $label, array $color, array $scale_data, $pdf)
150147
{
151-
// allocate colors
152-
$red = hexdec(mb_substr($line_color, 1, 2));
153-
$green = hexdec(mb_substr($line_color, 3, 2));
154-
$blue = hexdec(mb_substr($line_color, 4, 2));
155148
$line = [
156149
'width' => 1.5,
157-
'color' => [
158-
$red,
159-
$green,
160-
$blue,
161-
],
150+
'color' => $color,
162151
];
163152

164153
$label = trim($label ?? '');
@@ -199,18 +188,18 @@ public function prepareRowAsPdf($spatial, ?string $label, $line_color, array $sc
199188
*
200189
* @param string $spatial GIS MULTILINESTRING object
201190
* @param string $label Label for the GIS MULTILINESTRING object
202-
* @param string $line_color Color for the GIS MULTILINESTRING object
191+
* @param int[] $color Color for the GIS MULTILINESTRING object
203192
* @param array $scale_data Array containing data related to scaling
204193
*
205194
* @return string the code related to a row in the GIS dataset
206195
*/
207-
public function prepareRowAsSvg($spatial, $label, $line_color, array $scale_data)
196+
public function prepareRowAsSvg($spatial, $label, array $color, array $scale_data)
208197
{
209198
$line_options = [
210199
'name' => $label,
211200
'class' => 'linestring vector',
212201
'fill' => 'none',
213-
'stroke' => $line_color,
202+
'stroke' => sprintf('#%02x%02x%02x', ...$color),
214203
'stroke-width' => 2,
215204
];
216205

@@ -247,15 +236,15 @@ public function prepareRowAsSvg($spatial, $label, $line_color, array $scale_data
247236
* @param string $spatial GIS MULTILINESTRING object
248237
* @param int $srid Spatial reference ID
249238
* @param string $label Label for the GIS MULTILINESTRING object
250-
* @param array $line_color Color for the GIS MULTILINESTRING object
239+
* @param int[] $color Color for the GIS MULTILINESTRING object
251240
* @param array $scale_data Array containing data related to scaling
252241
*
253242
* @return string JavaScript related to a row in the GIS dataset
254243
*/
255-
public function prepareRowAsOl($spatial, int $srid, $label, $line_color, array $scale_data)
244+
public function prepareRowAsOl($spatial, int $srid, $label, array $color, array $scale_data)
256245
{
257246
$stroke_style = [
258-
'color' => $line_color,
247+
'color' => $color,
259248
'width' => 2,
260249
];
261250

0 commit comments

Comments
 (0)