|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Barryvanveen\CCA\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use Barryvanveen\CCA\Config; |
|
6
|
|
|
use Barryvanveen\CCA\Coordinate; |
|
7
|
|
|
use Barryvanveen\CCA\State; |
|
8
|
|
|
|
|
9
|
|
|
abstract class Image |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var Config */ |
|
12
|
|
|
protected $config; |
|
13
|
|
|
|
|
14
|
|
|
/** @var array */ |
|
15
|
|
|
protected $grid; |
|
16
|
|
|
|
|
17
|
|
|
/** @var mixed */ |
|
18
|
|
|
protected $image; |
|
19
|
|
|
|
|
20
|
|
|
/** @var int[] */ |
|
21
|
|
|
protected $colors; |
|
22
|
|
|
|
|
23
|
|
|
protected function __construct(Config $config, State $state) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->config = $config; |
|
26
|
|
|
|
|
27
|
|
|
$this->grid = $state->toArray(); |
|
28
|
|
|
|
|
29
|
|
|
$this->initImage(); |
|
30
|
|
|
|
|
31
|
|
|
$this->initColors(); |
|
32
|
|
|
|
|
33
|
|
|
$this->createImage(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
abstract public static function createFromState(Config $config, State $state); |
|
37
|
|
|
|
|
38
|
|
|
protected function initImage() |
|
39
|
|
|
{ |
|
40
|
|
|
$this->image = imagecreatetruecolor($this->getImageWidth(), $this->getImageHeight()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function getImageWidth(): int |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->config->columns() * $this->config->imageCellSize(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function getImageHeight(): int |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->config->rows() * $this->config->imageCellSize(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function initColors() |
|
54
|
|
|
{ |
|
55
|
|
|
$colors = Colors::getColors($this->config); |
|
56
|
|
|
|
|
57
|
|
|
foreach ($colors as $color) { |
|
58
|
|
|
$this->colors[] = imagecolorallocate($this->image, $color->getRed(), $color->getGreen(), $color->getBlue()); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function createImage() |
|
63
|
|
|
{ |
|
64
|
|
|
$this->fillBackground(); |
|
65
|
|
|
|
|
66
|
|
|
for ($row = 0; $row < $this->config->rows(); $row++) { |
|
67
|
|
|
for ($column = 0; $column < $this->config->columns(); $column++) { |
|
68
|
|
|
$state = $this->getStateFromGrid($row, $column); |
|
69
|
|
|
|
|
70
|
|
|
if ($state === 0) { |
|
71
|
|
|
continue; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->fillCell($row, $column, $state); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function fillBackground() |
|
80
|
|
|
{ |
|
81
|
|
|
imagefilledrectangle( |
|
82
|
|
|
$this->image, |
|
83
|
|
|
0, |
|
84
|
|
|
0, |
|
85
|
|
|
$this->getImageWidth(), |
|
86
|
|
|
$this->getImageHeight(), |
|
87
|
|
|
$this->colors[0] |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
protected function getStateFromGrid(int $row, int $column): int |
|
92
|
|
|
{ |
|
93
|
|
|
$coordinate = new Coordinate($row, $column, $this->config->columns()); |
|
94
|
|
|
|
|
95
|
|
|
return $this->grid[$coordinate->position()]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function fillCell(int $row, int $column, int $state) |
|
99
|
|
|
{ |
|
100
|
|
|
list($x1, $y1) = $this->getCellTopLeft($row, $column); |
|
101
|
|
|
|
|
102
|
|
|
list($x2, $y2) = $this->getCellBottomRight($row, $column); |
|
103
|
|
|
|
|
104
|
|
|
$color = $this->getColorFromState($state); |
|
105
|
|
|
|
|
106
|
|
|
imagefilledrectangle($this->image, $x1, $y1, $x2, $y2, $color); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
protected function getCellTopLeft(int $row, int $column): array |
|
110
|
|
|
{ |
|
111
|
|
|
$x = $column * $this->config->imageCellSize(); |
|
112
|
|
|
$y = $row * $this->config->imageCellSize(); |
|
113
|
|
|
|
|
114
|
|
|
return [$x, $y]; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
protected function getCellBottomRight(int $row, int $column): array |
|
118
|
|
|
{ |
|
119
|
|
|
list($x, $y) = $this->getCellTopLeft($row, $column); |
|
120
|
|
|
|
|
121
|
|
|
$x += ($this->config->imageCellSize() - 1); |
|
122
|
|
|
$y += ($this->config->imageCellSize() - 1); |
|
123
|
|
|
|
|
124
|
|
|
return [$x, $y]; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
protected function getColorFromState(int $state): int |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->colors[$state]; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function get() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->image; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
abstract public function save(); |
|
138
|
|
|
} |
|
139
|
|
|
|