RgbOutputTrait::getRgbBlackOrWhitePair()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

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