Validator::isValidHsl()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 3
rs 10
1
<?php
2
3
namespace MikeAlmond\Color;
4
5
class Validator
6
{
7
    /**
8
     * @param $color
9
     *
10
     * @return bool
11
     */
12 39
    public static function isValidHex(string $color) : bool
13
    {
14 39
        $color = ltrim($color, '#');
15
16 39
        return ctype_xdigit($color) && (strlen($color) === 6 || strlen($color) === 3);
17
    }
18
19
    /**
20
     *
21
     * @param $red
22
     * @param $green
23
     * @param $blue
24
     *
25
     * @return bool|mixed
26
     */
27
    public static function isValidRgb(int $red, int $green, int $blue) : bool
28
    {
29
        // Check to see the values are between 0 and 255 and return false if any are outside the bounds
30 14
        return array_reduce([$red, $green, $blue], function ($carry, $color) {
31 14
            return max(min(intval($color), 255), 0) === $color && $carry === true;
32 14
        }, true);
33
    }
34
35
    /**
36
     *
37
     * @param float $hue
38
     * @param float $saturation
39
     * @param float $lightness
40
     *
41
     * @return bool
42
     */
43
    public static function isValidHsl(float $hue, float $saturation, float $lightness) : bool
44
    {
45
        // Check to see the values are between 0 and 1 and return false if any are outside the bounds
46 3
        return array_reduce([$hue, $saturation, $lightness], function ($carry, $color) {
47 3
            return $color >= 0 && $color <= 1 && $carry === true;
48 3
        }, true);
49
    }
50
51
    /**
52
     *
53
     * @param float $degrees
54
     *
55
     * @return bool|mixed
56
     */
57 5
    public static function isValidAdjustment(float $degrees) : bool
58
    {
59
        // Check to see the values are between -359 and 359 and return false if any are outside the bounds
60 5
        return max(min($degrees, 360), -360) === $degrees;
61
    }
62
}
63