ILIAS  release_8 Revision v8.24
Color.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5/* Copyright (c) 2017 Nils Haagen <nils.haagen@concepts-and-training.de> Extended GPL, see docs/LICENSE */
6
7namespace ILIAS\Data;
8
15class Color
16{
17 protected int $r;
18 protected int $g;
19 protected int $b;
20
21 public function __construct(int $r, int $g, int $b)
22 {
23 if ($r < 0 || $r > 255) {
24 throw new \InvalidArgumentException("Unexpected value for \$r: '$r'");
25 }
26 if ($g < 0 || $g > 255) {
27 throw new \InvalidArgumentException("Unexpected value for \$g: '$g'");
28 }
29 if ($b < 0 || $b > 255) {
30 throw new \InvalidArgumentException("Unexpected value for \$b: '$b'");
31 }
32 $this->r = $r;
33 $this->g = $g;
34 $this->b = $b;
35 }
36
40 public function r(): int
41 {
42 return $this->r;
43 }
44
48 public function g(): int
49 {
50 return $this->g;
51 }
52
56 public function b(): int
57 {
58 return $this->b;
59 }
60
66 public function asArray(): array
67 {
68 return array(
69 $this->r,
70 $this->g,
71 $this->b
72 );
73 }
74
78 public function asHex(): string
79 {
80 $hex = '#';
81 foreach ($this->asArray() as $value) {
82 $hex .= str_pad(dechex($value), 2, '0', STR_PAD_LEFT);
83 }
84 return $hex;
85 }
86
90 public function asRGBString(): string
91 {
92 return 'rgb(' . implode(', ', $this->asArray()) . ')';
93 }
94
101 public function isDark(): bool
102 {
103 $sum = 0.299 * $this->r + 0.587 * $this->g + 0.114 * $this->b;
104
105 return $sum < 128;
106 }
107}
Color expresses a certain color by giving the mixing ratio in the RGB color space.
Definition: Color.php:16
b()
Get the valule for blue.
Definition: Color.php:56
isDark()
Based on https://de.wikipedia.org/wiki/Luminanz this function decides if the color can be considered ...
Definition: Color.php:101
g()
Get the valule for green.
Definition: Color.php:48
asHex()
Return color-value in hex-format.
Definition: Color.php:78
asRGBString()
Return string with RGB-notation.
Definition: Color.php:90
r()
Get the valule for red.
Definition: Color.php:40
__construct(int $r, int $g, int $b)
Definition: Color.php:21
asArray()
Return array with RGB-values.
Definition: Color.php:66