|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xls; |
|
4
|
|
|
|
|
5
|
|
|
class Palette |
|
6
|
|
|
{ |
|
7
|
|
|
protected static $colorsMap = array( |
|
8
|
|
|
'aqua' => 0x07, |
|
9
|
|
|
'cyan' => 0x07, |
|
10
|
|
|
'black' => 0x00, |
|
11
|
|
|
'blue' => 0x04, |
|
12
|
|
|
'brown' => 0x10, |
|
13
|
|
|
'magenta' => 0x06, |
|
14
|
|
|
'fuchsia' => 0x06, |
|
15
|
|
|
'gray' => 0x17, |
|
16
|
|
|
'grey' => 0x17, |
|
17
|
|
|
'green' => 0x11, |
|
18
|
|
|
'lime' => 0x03, |
|
19
|
|
|
'navy' => 0x12, |
|
20
|
|
|
'orange' => 0x35, |
|
21
|
|
|
'purple' => 0x14, |
|
22
|
|
|
'red' => 0x02, |
|
23
|
|
|
'silver' => 0x16, |
|
24
|
|
|
'white' => 0x01, |
|
25
|
|
|
'yellow' => 0x05 |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
protected static $x97Palette = array( |
|
29
|
|
|
array(0x00, 0x00, 0x00, 0x00), // 8 |
|
30
|
|
|
array(0xff, 0xff, 0xff, 0x00), // 9 |
|
31
|
|
|
array(0xff, 0x00, 0x00, 0x00), // 10 |
|
32
|
|
|
array(0x00, 0xff, 0x00, 0x00), // 11 |
|
33
|
|
|
array(0x00, 0x00, 0xff, 0x00), // 12 |
|
34
|
|
|
array(0xff, 0xff, 0x00, 0x00), // 13 |
|
35
|
|
|
array(0xff, 0x00, 0xff, 0x00), // 14 |
|
36
|
|
|
array(0x00, 0xff, 0xff, 0x00), // 15 |
|
37
|
|
|
array(0x80, 0x00, 0x00, 0x00), // 16 |
|
38
|
|
|
array(0x00, 0x80, 0x00, 0x00), // 17 |
|
39
|
|
|
array(0x00, 0x00, 0x80, 0x00), // 18 |
|
40
|
|
|
array(0x80, 0x80, 0x00, 0x00), // 19 |
|
41
|
|
|
array(0x80, 0x00, 0x80, 0x00), // 20 |
|
42
|
|
|
array(0x00, 0x80, 0x80, 0x00), // 21 |
|
43
|
|
|
array(0xc0, 0xc0, 0xc0, 0x00), // 22 |
|
44
|
|
|
array(0x80, 0x80, 0x80, 0x00), // 23 |
|
45
|
|
|
array(0x99, 0x99, 0xff, 0x00), // 24 |
|
46
|
|
|
array(0x99, 0x33, 0x66, 0x00), // 25 |
|
47
|
|
|
array(0xff, 0xff, 0xcc, 0x00), // 26 |
|
48
|
|
|
array(0xcc, 0xff, 0xff, 0x00), // 27 |
|
49
|
|
|
array(0x66, 0x00, 0x66, 0x00), // 28 |
|
50
|
|
|
array(0xff, 0x80, 0x80, 0x00), // 29 |
|
51
|
|
|
array(0x00, 0x66, 0xcc, 0x00), // 30 |
|
52
|
|
|
array(0xcc, 0xcc, 0xff, 0x00), // 31 |
|
53
|
|
|
array(0x00, 0x00, 0x80, 0x00), // 32 |
|
54
|
|
|
array(0xff, 0x00, 0xff, 0x00), // 33 |
|
55
|
|
|
array(0xff, 0xff, 0x00, 0x00), // 34 |
|
56
|
|
|
array(0x00, 0xff, 0xff, 0x00), // 35 |
|
57
|
|
|
array(0x80, 0x00, 0x80, 0x00), // 36 |
|
58
|
|
|
array(0x80, 0x00, 0x00, 0x00), // 37 |
|
59
|
|
|
array(0x00, 0x80, 0x80, 0x00), // 38 |
|
60
|
|
|
array(0x00, 0x00, 0xff, 0x00), // 39 |
|
61
|
|
|
array(0x00, 0xcc, 0xff, 0x00), // 40 |
|
62
|
|
|
array(0xcc, 0xff, 0xff, 0x00), // 41 |
|
63
|
|
|
array(0xcc, 0xff, 0xcc, 0x00), // 42 |
|
64
|
|
|
array(0xff, 0xff, 0x99, 0x00), // 43 |
|
65
|
|
|
array(0x99, 0xcc, 0xff, 0x00), // 44 |
|
66
|
|
|
array(0xff, 0x99, 0xcc, 0x00), // 45 |
|
67
|
|
|
array(0xcc, 0x99, 0xff, 0x00), // 46 |
|
68
|
|
|
array(0xff, 0xcc, 0x99, 0x00), // 47 |
|
69
|
|
|
array(0x33, 0x66, 0xff, 0x00), // 48 |
|
70
|
|
|
array(0x33, 0xcc, 0xcc, 0x00), // 49 |
|
71
|
|
|
array(0x99, 0xcc, 0x00, 0x00), // 50 |
|
72
|
|
|
array(0xff, 0xcc, 0x00, 0x00), // 51 |
|
73
|
|
|
array(0xff, 0x99, 0x00, 0x00), // 52 |
|
74
|
|
|
array(0xff, 0x66, 0x00, 0x00), // 53 |
|
75
|
|
|
array(0x66, 0x66, 0x99, 0x00), // 54 |
|
76
|
|
|
array(0x96, 0x96, 0x96, 0x00), // 55 |
|
77
|
|
|
array(0x00, 0x33, 0x66, 0x00), // 56 |
|
78
|
|
|
array(0x33, 0x99, 0x66, 0x00), // 57 |
|
79
|
|
|
array(0x00, 0x33, 0x00, 0x00), // 58 |
|
80
|
|
|
array(0x33, 0x33, 0x00, 0x00), // 59 |
|
81
|
|
|
array(0x99, 0x33, 0x00, 0x00), // 60 |
|
82
|
|
|
array(0x99, 0x33, 0x66, 0x00), // 61 |
|
83
|
|
|
array(0x33, 0x33, 0x99, 0x00), // 62 |
|
84
|
|
|
array(0x33, 0x33, 0x33, 0x00), // 63 |
|
85
|
|
|
); |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Return Excel 97+ default palette |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
|
|
public static function getXl97Palette() |
|
92
|
|
|
{ |
|
93
|
|
|
return self::$x97Palette; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param $index |
|
98
|
|
|
* @param $red |
|
99
|
|
|
* @param $green |
|
100
|
|
|
* @param $blue |
|
101
|
|
|
* |
|
102
|
|
|
* @throws \Exception |
|
103
|
|
|
*/ |
|
104
|
|
|
public static function validateColor($index, $red, $green, $blue) |
|
105
|
|
|
{ |
|
106
|
|
|
// Check that the colour index is the right range |
|
107
|
|
|
if ($index < 8 || $index > 64) { |
|
108
|
|
|
throw new \Exception("Color index $index outside range: 8 <= index <= 64"); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// Check that the colour components are in the right range |
|
112
|
|
|
if (($red < 0 || $red > 255) |
|
113
|
|
|
|| ($green < 0 || $green > 255) |
|
114
|
|
|
|| ($blue < 0 || $blue > 255) |
|
115
|
|
|
) { |
|
116
|
|
|
throw new \Exception("Color component outside range: 0 <= color <= 255"); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param $name |
|
122
|
|
|
* |
|
123
|
|
|
* @return bool |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function isValidColor($name) |
|
126
|
|
|
{ |
|
127
|
|
|
return isset(self::$colorsMap[$name]); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Used to convert a color |
|
132
|
|
|
* string into a number. Color range is 0..63 but we will restrict it |
|
133
|
|
|
* to 8..63 to comply with Gnumeric. Colors 0..7 are repeated in 8..15. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string|integer $name name of the color (i.e.: 'blue', 'red', etc..). Optional. |
|
136
|
|
|
* |
|
137
|
|
|
* @return integer The color index |
|
138
|
|
|
*/ |
|
139
|
|
|
public static function getColor($name) |
|
140
|
|
|
{ |
|
141
|
|
|
$defaultColor = 0x7FFF; |
|
142
|
|
|
|
|
143
|
|
|
// Return the default color, 0x7FFF, if undef, |
|
144
|
|
|
if ($name === '') { |
|
145
|
|
|
return $defaultColor; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
// or the color string converted to an integer, |
|
149
|
|
|
if (self::isValidColor($name)) { |
|
150
|
|
|
return self::$colorsMap[$name]; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// String is unrecognised or arg is outside range, |
|
154
|
|
|
if (preg_match("/\D/", $name) || $name > 63) { |
|
155
|
|
|
return $defaultColor; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
// or an integer in the valid range |
|
159
|
|
|
return $name; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|