1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Barryvanveen\CCA; |
6
|
|
|
|
7
|
|
|
use Barryvanveen\CCA\Config\NeighborhoodOptions; |
8
|
|
|
use Barryvanveen\CCA\Config\Options; |
9
|
|
|
use Barryvanveen\CCA\Config\Validator; |
10
|
|
|
use Phim\Color\RgbColor; |
11
|
|
|
|
12
|
|
|
class Config |
13
|
|
|
{ |
14
|
|
|
protected $config = [ |
15
|
|
|
Options::COLUMNS => 10, |
16
|
|
|
Options::IMAGE_CELL_SIZE => 2, |
17
|
|
|
Options::IMAGE_COLORS => null, |
18
|
|
|
Options::IMAGE_HUE => null, |
19
|
|
|
Options::NEIGHBORHOOD_TYPE => NeighborhoodOptions::NEIGHBORHOOD_TYPE_MOORE, |
20
|
|
|
Options::NEIGHBORHOOD_SIZE => 1, |
21
|
|
|
Options::ROWS => 10, |
22
|
|
|
Options::SEED => null, |
23
|
|
|
Options::STATES => 3, |
24
|
|
|
Options::THRESHOLD => 3, |
25
|
|
|
]; |
26
|
|
|
|
27
|
9 |
|
public function __construct(array $config) |
28
|
|
|
{ |
29
|
|
|
// set defaults |
30
|
9 |
|
$this->config[Options::IMAGE_HUE] = $this->makeHue(); |
31
|
9 |
|
$this->config[Options::SEED] = $this->makeSeed(); |
32
|
|
|
|
33
|
|
|
// validate config |
34
|
9 |
|
Validator::validate($config); |
35
|
|
|
|
36
|
|
|
// override defaults |
37
|
6 |
|
$this->config = array_merge($this->config, $config); |
38
|
6 |
|
} |
39
|
|
|
|
40
|
6 |
|
protected function makeHue(): int |
41
|
|
|
{ |
42
|
6 |
|
return rand(0, 360); |
43
|
|
|
} |
44
|
|
|
|
45
|
6 |
|
protected function makeSeed(): int |
46
|
|
|
{ |
47
|
6 |
|
list($usec, $sec) = explode(' ', microtime()); |
48
|
|
|
|
49
|
6 |
|
return intval($sec + $usec * 1000000); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the number of columns in the grid. |
54
|
|
|
* |
55
|
|
|
* @param int number of columns |
56
|
|
|
* |
57
|
|
|
* @return int |
58
|
|
|
*/ |
59
|
6 |
|
public function columns(): int |
60
|
|
|
{ |
61
|
6 |
|
return $this->config[Options::COLUMNS]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the size of each cell in the image that is created. |
66
|
|
|
* |
67
|
|
|
* @return int |
68
|
|
|
*/ |
69
|
3 |
|
public function imageCellSize(): int |
70
|
|
|
{ |
71
|
3 |
|
return $this->config[Options::IMAGE_CELL_SIZE]; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the colors that are used when generating images from states. |
76
|
|
|
* |
77
|
|
|
* @return RgbColor[]|null |
78
|
|
|
*/ |
79
|
3 |
|
public function imageColors() |
80
|
|
|
{ |
81
|
3 |
|
return $this->config[Options::IMAGE_COLORS]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the hue (color) that is used when generating images from states. |
86
|
|
|
* |
87
|
|
|
* @return int |
88
|
|
|
*/ |
89
|
3 |
|
public function imageHue(): int |
90
|
|
|
{ |
91
|
3 |
|
return $this->config[Options::IMAGE_HUE]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the size (eg range) of the neighborhood. |
96
|
|
|
* |
97
|
|
|
* @return int |
98
|
|
|
*/ |
99
|
3 |
|
public function neighborhoodSize(): int |
100
|
|
|
{ |
101
|
3 |
|
return $this->config[Options::NEIGHBORHOOD_SIZE]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the neighborhood function, eg Moore or Neumann |
106
|
|
|
* |
107
|
|
|
* @return string type of neighborhood |
108
|
|
|
*/ |
109
|
3 |
|
public function neighborhoodType(): string |
110
|
|
|
{ |
111
|
3 |
|
return $this->config[Options::NEIGHBORHOOD_TYPE]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the number of rows in the grid. |
116
|
|
|
* |
117
|
|
|
* @return int |
118
|
|
|
*/ |
119
|
6 |
|
public function rows(): int |
120
|
|
|
{ |
121
|
6 |
|
return $this->config[Options::ROWS]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get a seed for the random number generator. Use this for reproducable of runs. |
126
|
|
|
* |
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
3 |
|
public function seed(): int |
130
|
|
|
{ |
131
|
3 |
|
return $this->config[Options::SEED]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the number of states that are cycled through. |
136
|
|
|
* |
137
|
|
|
* @return int |
138
|
|
|
*/ |
139
|
3 |
|
public function states(): int |
140
|
|
|
{ |
141
|
3 |
|
return $this->config[Options::STATES]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get the threshold of the cells. |
146
|
|
|
* |
147
|
|
|
* @return int |
148
|
|
|
*/ |
149
|
3 |
|
public function threshold(): int |
150
|
|
|
{ |
151
|
3 |
|
return $this->config[Options::THRESHOLD]; |
152
|
|
|
} |
153
|
|
|
|
154
|
3 |
|
public function toArray(): array |
155
|
|
|
{ |
156
|
3 |
|
return $this->config; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|