Completed
Pull Request — develop_3.0 (#432)
by Adrien
02:31
created

Color::convertColorComponentToHex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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