Skip to content

Commit 6ead5ae

Browse files
Made target more flexible with custom options
There are new options which are configurable to allow to modify the target.
1 parent ad1b6d2 commit 6ead5ae

File tree

1 file changed

+89
-23
lines changed

1 file changed

+89
-23
lines changed

src/Target.php

+89-23
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,32 @@ class Target
4242
* @var int the number of ring
4343
*/
4444
private $ringCount;
45-
46-
/**
47-
* Target constructor.
48-
*
49-
* @param float $diameter10
50-
* @param float|null $diameterInner10
51-
* @param float $ringSpacing
52-
* @param int $firstInnerRing
53-
* @param int $ringCount
54-
* @param Hit[] $hits
55-
*/
56-
public function __construct($diameter10 = 0.5, $diameterInner10 = 0.5, $ringSpacing = 2.5, $firstInnerRing = 4, $ringCount = 10, $hits = [])
45+
46+
/**
47+
* @var array some options like color settings
48+
*/
49+
private $options;
50+
51+
/**
52+
* Target constructor.
53+
*
54+
* @param float $diameter10
55+
* @param float|null $diameterInner10
56+
* @param float $ringSpacing
57+
* @param int $firstInnerRing
58+
* @param int $ringCount
59+
* @param Hit[] $hits
60+
* @param array $options
61+
*/
62+
public function __construct($diameter10 = 0.5, $diameterInner10 = 0.5, $ringSpacing = 2.5, $firstInnerRing = 4, $ringCount = 10, $hits = [], $options = [])
5763
{
5864
$this->diameter10 = $diameter10;
5965
$this->diameterInner10 = $diameterInner10;
6066
$this->ringSpacing = $ringSpacing;
6167
$this->firstInnerRing = $firstInnerRing;
6268
$this->ringCount = $ringCount;
6369
$this->setHits($hits);
70+
$this->setOptions($options);
6471
}
6572

6673
/**
@@ -87,7 +94,7 @@ public function __construct($diameter10 = 0.5, $diameterInner10 = 0.5, $ringSpac
8794
*/
8895
public function draw($unit = 20, $type = self::DRAW_TYPE_PNG, $font = 5, $filename = null, $quality = null, $filters = null)
8996
{
90-
$size = (($this->diameter10 / 2) + (($this->ringCount - 1) * $this->ringSpacing)) * 2 * $unit;
97+
$size = $this->getTargetSize() * $unit;
9198
$image = imagecreatetruecolor($size, $size);
9299

93100
$transparent = imagecolorallocatealpha($image, 0, 0, 0, 127);
@@ -98,18 +105,18 @@ public function draw($unit = 20, $type = self::DRAW_TYPE_PNG, $font = 5, $filena
98105
$white = imagecolorallocate($image, 255, 255, 255);
99106
$red = imagecolorallocate($image, 255, 0, 0);
100107

101-
/*
108+
/**
102109
* Rings
103110
*/
104111
for ($x = $this->ringCount - 1; $x >= 0; $x--) {
105112
$diameter = (($x * $this->ringSpacing * 2) + $this->diameter10) * $unit;
106113

107-
imagefilledellipse($image, $size / 2, $size / 2, $diameter, $diameter, $x > ($this->ringCount - $this->firstInnerRing) ? $black : $white);
108-
imagefilledellipse($image, $size / 2, $size / 2, $diameter - 3, $diameter - 3, $x > ($this->ringCount - $this->firstInnerRing) ? $white : $black);
114+
imagefilledellipse($image, $size / 2, $size / 2, $diameter, $diameter, $x > ($this->ringCount - $this->firstInnerRing) ? $this->hexColorAllocate($image, $this->getOption('outer_ring_border_color')) : $this->hexColorAllocate($image, $this->getOption('inner_ring_border_color')));
115+
imagefilledellipse($image, $size / 2, $size / 2, $diameter - 3, $diameter - 3, $x > ($this->ringCount - $this->firstInnerRing) ? $this->hexColorAllocate($image, $this->getOption('outer_ring_color')) : $this->hexColorAllocate($image, $this->getOption('inner_ring_color')));
109116

110117
if ($this->ringCount - $x < 9) {
111118
$text = $this->ringCount - $x;
112-
$color = $x > ($this->ringCount - $this->firstInnerRing) ? $black : $white;
119+
$color = $x > ($this->ringCount - $this->firstInnerRing) ? $this->hexColorAllocate($image, $this->getOption('outer_ring_text_color')) : $this->hexColorAllocate($image, $this->getOption('inner_ring_text_color'));
113120

114121
if (is_numeric($font)) {
115122
$width = (imagefontwidth($font) * strlen($text)) / 2;
@@ -144,20 +151,19 @@ public function draw($unit = 20, $type = self::DRAW_TYPE_PNG, $font = 5, $filena
144151
* Inner 10.
145152
*/
146153
$diameter = ($this->diameterInner10 / 2) * 2 * $unit;
147-
imagefilledellipse($image, $size / 2, $size / 2, $diameter, $diameter, $white);
154+
imagefilledellipse($image, $size / 2, $size / 2, $diameter, $diameter, $this->hexColorAllocate($image, $this->getOption('inner_ring_border_color')));
148155
if (($this->diameterInner10 / 2) * 2 >= 3) {
149-
imagefilledellipse($image, $size / 2, $size / 2, $diameter - 2, $diameter - 2, $black);
156+
imagefilledellipse($image, $size / 2, $size / 2, $diameter - 2, $diameter - 2, $this->hexColorAllocate($image, $this->getOption('inner_ring_color')));
150157
}
151158

152159
foreach ($this->hits as $number => $hit) {
153160
$x = $hit->getX() * 0.01 * $unit;
154161
$y = $hit->getY() * 0.01 * $unit;
155162
$text = $hit->getLabel() ?: ($number + 1);
156-
$color = $hit->getColor() !== null ? $this->hexColorAllocate($image, $hit->getColor()) : $red;
157-
$rgbColor = $this->hexColorToRGB($hit->getColor());
158-
$borderColor = $white;
163+
$color = $hit->getColor() !== null ? $this->hexColorAllocate($image, $hit->getColor()) : $this->hexColorAllocate($image, $this->getOption('hit_color'));
164+
$rgbColor = $this->hexColorToRGB($color);
159165

160-
imagefilledellipse($image, $size / 2 + $x, $size / 2 - $y, 4.5 * $unit, 4.5 * $unit, $borderColor);
166+
imagefilledellipse($image, $size / 2 + $x, $size / 2 - $y, 4.5 * $unit, 4.5 * $unit, $this->hexColorAllocate($image, $this->getOption('hit_border_color')));
161167
imagefilledellipse($image, $size / 2 + $x, $size / 2 - $y, 4.5 * $unit - 3, 4.5 * $unit - 3, $color);
162168

163169
if (is_numeric($font)) {
@@ -182,6 +188,18 @@ public function draw($unit = 20, $type = self::DRAW_TYPE_PNG, $font = 5, $filena
182188

183189
return imagepng($image, $filename, $quality, $filters);
184190
}
191+
192+
/**
193+
* Get the target size in mm
194+
*
195+
* @param bool $asDiameter return the diameter if true else the whole size
196+
*
197+
* @return float
198+
*/
199+
public function getTargetSize($asDiameter = false)
200+
{
201+
return (($this->diameter10 / 2) + (($this->ringCount - 1) * $this->ringSpacing)) * ($asDiameter ? 1 : 2);
202+
}
185203

186204
/**
187205
* Get the hits array.
@@ -259,4 +277,52 @@ private function hexColorToRGB($hex)
259277
$a + $b + $c,
260278
];
261279
}
280+
281+
/**
282+
* @return array
283+
*/
284+
public function getOptions()
285+
{
286+
return $this->options;
287+
}
288+
289+
/**
290+
* Get an option
291+
*
292+
* @param string $key the key to get
293+
* @param mixed $default the default value
294+
*
295+
* @return mixed if key exist then this or else the default value
296+
*/
297+
public function getOption($key, $default = null)
298+
{
299+
$key = strtolower($key);
300+
return array_key_exists($key, $this->options) ? $this->options[$key] : $default;
301+
}
302+
303+
/**
304+
* @param array $options
305+
*/
306+
public function setOptions($options)
307+
{
308+
$this->options = array_merge($this->getDefaultOptions(), $options);
309+
}
310+
311+
/**
312+
* @return array default options
313+
*/
314+
private function getDefaultOptions()
315+
{
316+
return array(
317+
'outer_ring_color' => '#fff',
318+
'outer_ring_text_color' => '#000',
319+
'outer_ring_border_color' => '#000',
320+
'inner_ring_color' => '#000',
321+
'inner_ring_text_color' => '#fff',
322+
'inner_ring_border_color' => '#fff',
323+
'ring_texts' => true,
324+
'hit_color' => '#ff0000',
325+
'hit_border_color' => '#fff'
326+
);
327+
}
262328
}

0 commit comments

Comments
 (0)