ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Color.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Data;
22
29class Color
30{
31 protected int $r;
32 protected int $g;
33 protected int $b;
34
35 public function __construct(int $r, int $g, int $b)
36 {
37 if ($r < 0 || $r > 255) {
38 throw new \InvalidArgumentException("Unexpected value for \$r: '$r'");
39 }
40 if ($g < 0 || $g > 255) {
41 throw new \InvalidArgumentException("Unexpected value for \$g: '$g'");
42 }
43 if ($b < 0 || $b > 255) {
44 throw new \InvalidArgumentException("Unexpected value for \$b: '$b'");
45 }
46 $this->r = $r;
47 $this->g = $g;
48 $this->b = $b;
49 }
50
54 public function r(): int
55 {
56 return $this->r;
57 }
58
62 public function g(): int
63 {
64 return $this->g;
65 }
66
70 public function b(): int
71 {
72 return $this->b;
73 }
74
80 public function asArray(): array
81 {
82 return array(
83 $this->r,
84 $this->g,
85 $this->b
86 );
87 }
88
92 public function asHex(): string
93 {
94 $hex = '#';
95 foreach ($this->asArray() as $value) {
96 $hex .= str_pad(dechex($value), 2, '0', STR_PAD_LEFT);
97 }
98 return $hex;
99 }
100
104 public function asRGBString(): string
105 {
106 return 'rgb(' . implode(', ', $this->asArray()) . ')';
107 }
108
115 public function isDark(): bool
116 {
117 $sum = 0.299 * $this->r + 0.587 * $this->g + 0.114 * $this->b;
118
119 return $sum < 128;
120 }
121}
Color expresses a certain color by giving the mixing ratio in the RGB color space.
Definition: Color.php:30
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:115
g()
Get the valule for green.
Definition: Color.php:62
asHex()
Return color-value in hex-format.
Definition: Color.php:92
asRGBString()
Return string with RGB-notation.
Definition: Color.php:104
r()
Get the valule for red.
Definition: Color.php:54
__construct(int $r, int $g, int $b)
Definition: Color.php:35
asArray()
Return array with RGB-values.
Definition: Color.php:80