1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Trait implementation of Red-Green-Blue (RGB) colour format generation for generator services. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace CryptoManana\Core\Traits\Randomness; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait RgbOutputTrait - Reusable implementation of `RgbOutputInterface`. |
11
|
|
|
* |
12
|
|
|
* @see \CryptoManana\Core\Interfaces\Randomness\RgbOutputInterface The abstract specification. |
13
|
|
|
* |
14
|
|
|
* @package CryptoManana\Core\Traits\Randomness |
15
|
|
|
*/ |
16
|
|
|
trait RgbOutputTrait |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Internal method for converting RGB integer colours to HEX notations. |
20
|
|
|
* |
21
|
|
|
* @param array $rgbColour An array containing three integers between 0 and 255. |
22
|
|
|
* |
23
|
|
|
* @return string The HEX representation of the RGB colour pair. |
24
|
|
|
*/ |
25
|
3 |
|
protected function calculateRgbArrayToHexString(array $rgbColour) |
26
|
|
|
{ |
27
|
3 |
|
$pairCount = count($rgbColour); |
28
|
|
|
|
29
|
3 |
|
for ($i = 0; $i < $pairCount; $i++) { |
30
|
3 |
|
$rgbColour[$i] = ($rgbColour[$i] <= 15) ? '0' . dechex($rgbColour[$i]) : dechex($rgbColour[$i]); |
31
|
|
|
} |
32
|
|
|
|
33
|
3 |
|
return ($pairCount > 0) ? '#' . implode('', $rgbColour) : ''; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Generate a random Red-Green-Blue (RGB) colour combination using all colours. |
38
|
|
|
* |
39
|
|
|
* @param bool $toArray Flag to force array output instead of string (default => true). |
40
|
|
|
* |
41
|
|
|
* @return array|string Randomly generated RGB array or hexadecimal RGB color. |
42
|
|
|
* @throws \Exception Validation errors. |
43
|
|
|
*/ |
44
|
3 |
|
public function getRgbColourPair($toArray = true) |
45
|
|
|
{ |
46
|
3 |
|
$rgb = [$this->getInt(0, 255), $this->getInt(0, 255), $this->getInt(0, 255)]; |
|
|
|
|
47
|
|
|
|
48
|
3 |
|
return ($toArray) ? $rgb : $this->calculateRgbArrayToHexString($rgb); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Generate a random Red-Green-Blue (RGB) colour combination using only greyscale colours. |
53
|
|
|
* |
54
|
|
|
* @param bool $toArray Flag to force array output instead of string (default => true). |
55
|
|
|
* |
56
|
|
|
* @return array|string Randomly generated RGB array or hexadecimal RGB color. |
57
|
|
|
* @throws \Exception Validation errors. |
58
|
|
|
*/ |
59
|
3 |
|
public function getRgbGreyscalePair($toArray = true) |
60
|
|
|
{ |
61
|
|
|
$grayChart = [ |
62
|
3 |
|
[255, 255, 255], // white |
63
|
|
|
[220, 220, 220], // gainsboro |
64
|
|
|
[211, 211, 211], // lightgrey |
65
|
|
|
[192, 192, 192], // silver |
66
|
|
|
[169, 169, 169], // darkgray |
67
|
|
|
[128, 128, 128], // gray |
68
|
|
|
[105, 105, 105], // dimgray |
69
|
|
|
[0, 0, 0] // black |
70
|
|
|
]; |
71
|
|
|
|
72
|
3 |
|
$rgb = $grayChart[$this->getInt(0, 255) % 8]; // A bit faster |
73
|
|
|
|
74
|
3 |
|
return ($toArray) ? $rgb : $this->calculateRgbArrayToHexString($rgb); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Generate a random Red-Green-Blue (RGB) colour combination using only black&white colours. |
79
|
|
|
* |
80
|
|
|
* @param bool $toArray Flag to force array output instead of string (default => true). |
81
|
|
|
* |
82
|
|
|
* @return array|string Randomly generated RGB array or hexadecimal RGB color. |
83
|
|
|
* @throws \Exception Validation errors. |
84
|
|
|
*/ |
85
|
3 |
|
public function getRgbBlackOrWhitePair($toArray = true) |
86
|
|
|
{ |
87
|
3 |
|
$rgb = $this->getBool() ? [0, 0, 0] : [255, 255, 255]; |
|
|
|
|
88
|
|
|
|
89
|
3 |
|
return ($toArray) ? $rgb : $this->calculateRgbArrayToHexString($rgb); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|