Completed
Pull Request — master (#557)
by Adrien
03:10
created

Color   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 74
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A rgb() 0 12 1
A throwIfInvalidColorComponentValue() 0 6 4
A convertColorComponentToHex() 0 4 1
A toARGB() 0 4 1
1
<?php
2
3
namespace Box\Spout\Common\Entity\Style;
4
5
use Box\Spout\Common\Exception\InvalidColorException;
6
7
/**
8
 * Class Color
9
 * This class provides constants and functions to work with colors
10
 */
11
class Color
12
{
13
    /** Standard colors - based on Office Online */
14
    const BLACK = '000000';
15
    const WHITE = 'FFFFFF';
16
    const RED = 'FF0000';
17
    const DARK_RED = 'C00000';
18
    const ORANGE = 'FFC000';
19
    const YELLOW = 'FFFF00';
20
    const LIGHT_GREEN = '92D040';
21
    const GREEN = '00B050';
22
    const LIGHT_BLUE = '00B0E0';
23
    const BLUE = '0070C0';
24
    const DARK_BLUE = '002060';
25
    const PURPLE = '7030A0';
26
27
    /**
28
     * Returns an RGB color from R, G and B values
29
     *
30
     * @param int $red Red component, 0 - 255
31
     * @param int $green Green component, 0 - 255
32
     * @param int $blue Blue component, 0 - 255
33
     * @return string RGB color
34
     */
35 34
    public static function rgb($red, $green, $blue)
36
    {
37 34
        self::throwIfInvalidColorComponentValue($red);
38 29
        self::throwIfInvalidColorComponentValue($green);
39 24
        self::throwIfInvalidColorComponentValue($blue);
40
41 19
        return strtoupper(
42 19
            self::convertColorComponentToHex($red) .
43 19
            self::convertColorComponentToHex($green) .
44 19
            self::convertColorComponentToHex($blue)
45
        );
46
    }
47
48
    /**
49
     * Throws an exception is the color component value is outside of bounds (0 - 255)
50
     *
51
     * @param int $colorComponent
52
     * @throws \Box\Spout\Common\Exception\InvalidColorException
53
     * @return void
54
     */
55 34
    protected static function throwIfInvalidColorComponentValue($colorComponent)
56
    {
57 34
        if (!is_int($colorComponent) || $colorComponent < 0 || $colorComponent > 255) {
58 15
            throw new InvalidColorException("The RGB components must be between 0 and 255. Received: $colorComponent");
59
        }
60 29
    }
61
62
    /**
63
     * Converts the color component to its corresponding hexadecimal value
64
     *
65
     * @param int $colorComponent Color component, 0 - 255
66
     * @return string Corresponding hexadecimal value, with a leading 0 if needed. E.g "0f", "2d"
67
     */
68 19
    protected static function convertColorComponentToHex($colorComponent)
69
    {
70 19
        return str_pad(dechex($colorComponent), 2, '0', STR_PAD_LEFT);
71
    }
72
73
    /**
74
     * Returns the ARGB color of the given RGB color,
75
     * assuming that alpha value is always 1.
76
     *
77
     * @param string $rgbColor RGB color like "FF08B2"
78
     * @return string ARGB color
79
     */
80 36
    public static function toARGB($rgbColor)
81
    {
82 36
        return 'FF' . $rgbColor;
83
    }
84
}
85