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
|
|
|
|