ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
Color.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 2017 Nils Haagen <nils.haagen@concepts-and-training.de> Extended GPL, see docs/LICENSE */
3 
4 namespace ILIAS\Data;
5 
12 class Color
13 {
14 
18  protected $r;
19 
23  protected $g;
24 
28  protected $b;
29 
30 
31  public function __construct($r, $g, $b)
32  {
33  if (!is_integer($r) or $r < 0 || $r > 255) {
34  throw new \InvalidArgumentException("Unexpected value for \$r: '$r'");
35  }
36  if (!is_integer($g) or $g < 0 || $g > 255) {
37  throw new \InvalidArgumentException("Unexpected value for \$g: '$g'");
38  }
39  if (!is_integer($b) or $b < 0 || $b > 255) {
40  throw new \InvalidArgumentException("Unexpected value for \$b: '$b'");
41  }
42  $this->r = $r;
43  $this->g = $g;
44  $this->b = $b;
45  }
46 
52  public function r()
53  {
54  return $this->r;
55  }
61  public function g()
62  {
63  return $this->g;
64  }
70  public function b()
71  {
72  return $this->b;
73  }
74 
80  public function asArray()
81  {
82  return array(
83  $this->r,
84  $this->g,
85  $this->b
86  );
87  }
88 
94  public function asHex()
95  {
96  $hex = '#';
97  foreach ($this->asArray() as $value) {
98  $hex .= str_pad(dechex($value), 2, '0', STR_PAD_LEFT);
99  }
100  return $hex;
101  }
102 
108  public function asRGBString()
109  {
110  return 'rgb('
111  . implode(', ', $this->asArray())
112  . ')';
113  }
114 
123  public function isDark()
124  {
125  $sum = 0.299 * $this->r + 0.587 * $this->g + 0.114 * $this->b;
126  if ($sum < 128) {
127  return true;
128  }
129  return false;
130  }
131 }
g()
Get the valule for green.
Definition: Color.php:61
Color expresses a certain color by giving the mixing ratio in the RGB color space.
Definition: Color.php:12
__construct($r, $g, $b)
Definition: Color.php:31
asHex()
Return color-value in hex-format.
Definition: Color.php:94
asRGBString()
Return string with RGB-notation.
Definition: Color.php:108
b()
Get the valule for blue.
Definition: Color.php:70
isDark()
Based on https://de.wikipedia.org/wiki/Luminanz this function decides if the color can be considered ...
Definition: Color.php:123
asArray()
Return array with RGB-values.
Definition: Color.php:80
r()
Get the valule for red.
Definition: Color.php:52