updated a couple packages (#1567)
[openemr.git] / vendor / phpoffice / phpspreadsheet / src / PhpSpreadsheet / Style / Color.php
blob8a1812d20b79f5ce8c3001839566b07ea08dfb8e
1 <?php
3 namespace PhpOffice\PhpSpreadsheet\Style;
5 use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
7 class Color extends Supervisor
9 // Colors
10 const COLOR_BLACK = 'FF000000';
11 const COLOR_WHITE = 'FFFFFFFF';
12 const COLOR_RED = 'FFFF0000';
13 const COLOR_DARKRED = 'FF800000';
14 const COLOR_BLUE = 'FF0000FF';
15 const COLOR_DARKBLUE = 'FF000080';
16 const COLOR_GREEN = 'FF00FF00';
17 const COLOR_DARKGREEN = 'FF008000';
18 const COLOR_YELLOW = 'FFFFFF00';
19 const COLOR_DARKYELLOW = 'FF808000';
21 /**
22 * Indexed colors array.
24 * @var array
26 protected static $indexedColors;
28 /**
29 * ARGB - Alpha RGB.
31 * @var string
33 protected $argb;
35 /**
36 * Create a new Color.
38 * @param string $pARGB ARGB value for the colour
39 * @param bool $isSupervisor Flag indicating if this is a supervisor or not
40 * Leave this value at default unless you understand exactly what
41 * its ramifications are
42 * @param bool $isConditional Flag indicating if this is a conditional style or not
43 * Leave this value at default unless you understand exactly what
44 * its ramifications are
46 public function __construct($pARGB = self::COLOR_BLACK, $isSupervisor = false, $isConditional = false)
48 // Supervisor?
49 parent::__construct($isSupervisor);
51 // Initialise values
52 if (!$isConditional) {
53 $this->argb = $pARGB;
57 /**
58 * Get the shared style component for the currently active cell in currently active sheet.
59 * Only used for style supervisor.
61 * @return Color
63 public function getSharedComponent()
65 switch ($this->parentPropertyName) {
66 case 'endColor':
67 return $this->parent->getSharedComponent()->getEndColor();
68 case 'color':
69 return $this->parent->getSharedComponent()->getColor();
70 case 'startColor':
71 return $this->parent->getSharedComponent()->getStartColor();
75 /**
76 * Build style array from subcomponents.
78 * @param array $array
80 * @return array
82 public function getStyleArray($array)
84 return $this->parent->getStyleArray([$this->parentPropertyName => $array]);
87 /**
88 * Apply styles from array.
90 * <code>
91 * $spreadsheet->getActiveSheet()->getStyle('B2')->getFont()->getColor()->applyFromArray(['rgb' => '808080']);
92 * </code>
94 * @param array $pStyles Array containing style information
96 * @throws PhpSpreadsheetException
98 * @return Color
100 public function applyFromArray(array $pStyles)
102 if ($this->isSupervisor) {
103 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($this->getStyleArray($pStyles));
104 } else {
105 if (isset($pStyles['rgb'])) {
106 $this->setRGB($pStyles['rgb']);
108 if (isset($pStyles['argb'])) {
109 $this->setARGB($pStyles['argb']);
113 return $this;
117 * Get ARGB.
119 * @return string
121 public function getARGB()
123 if ($this->isSupervisor) {
124 return $this->getSharedComponent()->getARGB();
127 return $this->argb;
131 * Set ARGB.
133 * @param string $pValue see self::COLOR_*
135 * @return Color
137 public function setARGB($pValue)
139 if ($pValue == '') {
140 $pValue = self::COLOR_BLACK;
142 if ($this->isSupervisor) {
143 $styleArray = $this->getStyleArray(['argb' => $pValue]);
144 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
145 } else {
146 $this->argb = $pValue;
149 return $this;
153 * Get RGB.
155 * @return string
157 public function getRGB()
159 if ($this->isSupervisor) {
160 return $this->getSharedComponent()->getRGB();
163 return substr($this->argb, 2);
167 * Set RGB.
169 * @param string $pValue RGB value
171 * @return Color
173 public function setRGB($pValue)
175 if ($pValue == '') {
176 $pValue = '000000';
178 if ($this->isSupervisor) {
179 $styleArray = $this->getStyleArray(['argb' => 'FF' . $pValue]);
180 $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
181 } else {
182 $this->argb = 'FF' . $pValue;
185 return $this;
189 * Get a specified colour component of an RGB value.
191 * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
192 * @param int $offset Position within the RGB value to extract
193 * @param bool $hex Flag indicating whether the component should be returned as a hex or a
194 * decimal value
196 * @return string The extracted colour component
198 private static function getColourComponent($RGB, $offset, $hex = true)
200 $colour = substr($RGB, $offset, 2);
201 if (!$hex) {
202 $colour = hexdec($colour);
205 return $colour;
209 * Get the red colour component of an RGB value.
211 * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
212 * @param bool $hex Flag indicating whether the component should be returned as a hex or a
213 * decimal value
215 * @return string The red colour component
217 public static function getRed($RGB, $hex = true)
219 return self::getColourComponent($RGB, strlen($RGB) - 6, $hex);
223 * Get the green colour component of an RGB value.
225 * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
226 * @param bool $hex Flag indicating whether the component should be returned as a hex or a
227 * decimal value
229 * @return string The green colour component
231 public static function getGreen($RGB, $hex = true)
233 return self::getColourComponent($RGB, strlen($RGB) - 4, $hex);
237 * Get the blue colour component of an RGB value.
239 * @param string $RGB The colour as an RGB value (e.g. FF00CCCC or CCDDEE
240 * @param bool $hex Flag indicating whether the component should be returned as a hex or a
241 * decimal value
243 * @return string The blue colour component
245 public static function getBlue($RGB, $hex = true)
247 return self::getColourComponent($RGB, strlen($RGB) - 2, $hex);
251 * Adjust the brightness of a color.
253 * @param string $hex The colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE)
254 * @param float $adjustPercentage The percentage by which to adjust the colour as a float from -1 to 1
256 * @return string The adjusted colour as an RGBA or RGB value (e.g. FF00CCCC or CCDDEE)
258 public static function changeBrightness($hex, $adjustPercentage)
260 $rgba = (strlen($hex) == 8);
262 $red = self::getRed($hex, false);
263 $green = self::getGreen($hex, false);
264 $blue = self::getBlue($hex, false);
265 if ($adjustPercentage > 0) {
266 $red += (255 - $red) * $adjustPercentage;
267 $green += (255 - $green) * $adjustPercentage;
268 $blue += (255 - $blue) * $adjustPercentage;
269 } else {
270 $red += $red * $adjustPercentage;
271 $green += $green * $adjustPercentage;
272 $blue += $blue * $adjustPercentage;
275 if ($red < 0) {
276 $red = 0;
277 } elseif ($red > 255) {
278 $red = 255;
280 if ($green < 0) {
281 $green = 0;
282 } elseif ($green > 255) {
283 $green = 255;
285 if ($blue < 0) {
286 $blue = 0;
287 } elseif ($blue > 255) {
288 $blue = 255;
291 $rgb = strtoupper(
292 str_pad(dechex($red), 2, '0', 0) .
293 str_pad(dechex($green), 2, '0', 0) .
294 str_pad(dechex($blue), 2, '0', 0)
297 return (($rgba) ? 'FF' : '') . $rgb;
301 * Get indexed color.
303 * @param int $pIndex Index entry point into the colour array
304 * @param bool $background Flag to indicate whether default background or foreground colour
305 * should be returned if the indexed colour doesn't exist
307 * @return Color
309 public static function indexedColor($pIndex, $background = false)
311 // Clean parameter
312 $pIndex = (int) $pIndex;
314 // Indexed colors
315 if (self::$indexedColors === null) {
316 self::$indexedColors = [
317 1 => 'FF000000', // System Colour #1 - Black
318 2 => 'FFFFFFFF', // System Colour #2 - White
319 3 => 'FFFF0000', // System Colour #3 - Red
320 4 => 'FF00FF00', // System Colour #4 - Green
321 5 => 'FF0000FF', // System Colour #5 - Blue
322 6 => 'FFFFFF00', // System Colour #6 - Yellow
323 7 => 'FFFF00FF', // System Colour #7- Magenta
324 8 => 'FF00FFFF', // System Colour #8- Cyan
325 9 => 'FF800000', // Standard Colour #9
326 10 => 'FF008000', // Standard Colour #10
327 11 => 'FF000080', // Standard Colour #11
328 12 => 'FF808000', // Standard Colour #12
329 13 => 'FF800080', // Standard Colour #13
330 14 => 'FF008080', // Standard Colour #14
331 15 => 'FFC0C0C0', // Standard Colour #15
332 16 => 'FF808080', // Standard Colour #16
333 17 => 'FF9999FF', // Chart Fill Colour #17
334 18 => 'FF993366', // Chart Fill Colour #18
335 19 => 'FFFFFFCC', // Chart Fill Colour #19
336 20 => 'FFCCFFFF', // Chart Fill Colour #20
337 21 => 'FF660066', // Chart Fill Colour #21
338 22 => 'FFFF8080', // Chart Fill Colour #22
339 23 => 'FF0066CC', // Chart Fill Colour #23
340 24 => 'FFCCCCFF', // Chart Fill Colour #24
341 25 => 'FF000080', // Chart Line Colour #25
342 26 => 'FFFF00FF', // Chart Line Colour #26
343 27 => 'FFFFFF00', // Chart Line Colour #27
344 28 => 'FF00FFFF', // Chart Line Colour #28
345 29 => 'FF800080', // Chart Line Colour #29
346 30 => 'FF800000', // Chart Line Colour #30
347 31 => 'FF008080', // Chart Line Colour #31
348 32 => 'FF0000FF', // Chart Line Colour #32
349 33 => 'FF00CCFF', // Standard Colour #33
350 34 => 'FFCCFFFF', // Standard Colour #34
351 35 => 'FFCCFFCC', // Standard Colour #35
352 36 => 'FFFFFF99', // Standard Colour #36
353 37 => 'FF99CCFF', // Standard Colour #37
354 38 => 'FFFF99CC', // Standard Colour #38
355 39 => 'FFCC99FF', // Standard Colour #39
356 40 => 'FFFFCC99', // Standard Colour #40
357 41 => 'FF3366FF', // Standard Colour #41
358 42 => 'FF33CCCC', // Standard Colour #42
359 43 => 'FF99CC00', // Standard Colour #43
360 44 => 'FFFFCC00', // Standard Colour #44
361 45 => 'FFFF9900', // Standard Colour #45
362 46 => 'FFFF6600', // Standard Colour #46
363 47 => 'FF666699', // Standard Colour #47
364 48 => 'FF969696', // Standard Colour #48
365 49 => 'FF003366', // Standard Colour #49
366 50 => 'FF339966', // Standard Colour #50
367 51 => 'FF003300', // Standard Colour #51
368 52 => 'FF333300', // Standard Colour #52
369 53 => 'FF993300', // Standard Colour #53
370 54 => 'FF993366', // Standard Colour #54
371 55 => 'FF333399', // Standard Colour #55
372 56 => 'FF333333', // Standard Colour #56
376 if (isset(self::$indexedColors[$pIndex])) {
377 return new self(self::$indexedColors[$pIndex]);
380 if ($background) {
381 return new self(self::COLOR_WHITE);
384 return new self(self::COLOR_BLACK);
388 * Get hash code.
390 * @return string Hash code
392 public function getHashCode()
394 if ($this->isSupervisor) {
395 return $this->getSharedComponent()->getHashCode();
398 return md5(
399 $this->argb .
400 __CLASS__