/
ColorVariable.php
103 lines (85 loc) · 1.83 KB
/
ColorVariable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
namespace Concrete\Core\StyleCustomizer\Normalizer;
use Doctrine\Common\Collections\ArrayCollection;
class ColorVariable implements VariableInterface
{
protected $name;
protected $r;
protected $g;
protected $b;
protected $a = 1;
/**
* NumberVariable constructor.
* @param $r
* @param $g
* @param $b
* @param $a
*/
public function __construct($name, $r, $g, $b, $a = 1)
{
$this->name = $name;
$this->r = $r;
$this->g = $g;
$this->b = $b;
$this->a = $a;
if ($a === null) {
$this->a = 1;
}
}
/**
* @return mixed
*/
public function getName(): string
{
return $this->name;
}
/**
* @return mixed
*/
public function getRed()
{
return $this->r;
}
/**
* @return mixed
*/
public function getGreen()
{
return $this->g;
}
/**
* @return mixed
*/
public function getBlue()
{
return $this->b;
}
/**
* @return int|mixed
*/
public function getAlpha()
{
return $this->a;
}
public function getValue()
{
if ($this->getAlpha() === 1) {
$value = sprintf('rgb(%s, %s, %s)', $this->getRed(), $this->getGreen(), $this->getBlue());
} else {
$value = sprintf('rgba(%s, %s, %s, %s)', $this->getRed(), $this->getGreen(), $this->getBlue(), $this->getAlpha());
}
return $value;
}
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return [
'type' => 'color',
'name' => $this->getName(),
'r' => $this->getRed(),
'g' => $this->getGreen(),
'b' => $this->getBlue(),
'a' => $this->getAlpha(),
];
}
}