Completed
Push — master ( ebd7bb...65be67 )
by Tony Karavasilev (Тони
04:40
created

RgbOutputTrait::calculateRgbArrayToHexString()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 10
cc 4
nc 6
nop 1
crap 4
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)];
0 ignored issues
show
Bug introduced by
It seems like getInt() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        $rgb = [$this->/** @scrutinizer ignore-call */ getInt(0, 255), $this->getInt(0, 255), $this->getInt(0, 255)];
Loading history...
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];
1 ignored issue
show
Bug introduced by
It seems like getBool() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

87
        $rgb = $this->/** @scrutinizer ignore-call */ getBool() ? [0, 0, 0] : [255, 255, 255];
Loading history...
88
89 3
        return ($toArray) ? $rgb : $this->calculateRgbArrayToHexString($rgb);
90
    }
91
}
92