|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package Application Utils |
|
4
|
|
|
* @subpackage RGBAColor |
|
5
|
|
|
* @see \AppUtils\RGBAColor\ColorChannel\AlphaChannel |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace AppUtils\RGBAColor\ColorChannel; |
|
11
|
|
|
|
|
12
|
|
|
use AppUtils\RGBAColor\ColorChannel; |
|
13
|
|
|
use AppUtils\RGBAColor\UnitsConverter; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Color channel with values from 0.0 to +1.0. |
|
17
|
|
|
* |
|
18
|
|
|
* Native value: {@see self::getAlpha()}. |
|
19
|
|
|
* |
|
20
|
|
|
* @package Application Utils |
|
21
|
|
|
* @subpackage RGBAColor |
|
22
|
|
|
* @author Sebastian Mordziol <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class AlphaChannel extends ColorChannel |
|
25
|
|
|
{ |
|
26
|
|
|
public const VALUE_MIN = 0.0; |
|
27
|
|
|
public const VALUE_MAX = 1.0; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var float |
|
31
|
|
|
*/ |
|
32
|
|
|
private float $value; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(float $value) |
|
35
|
|
|
{ |
|
36
|
|
|
if($value < self::VALUE_MIN) { $value = self::VALUE_MIN; } |
|
37
|
|
|
if($value > self::VALUE_MAX) { $value = self::VALUE_MAX; } |
|
38
|
|
|
|
|
39
|
|
|
$this->value = $value; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return float 0.0-1.0 |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getValue() : float |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->value; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getAlpha() : float |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->value; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function get8Bit() : int |
|
56
|
|
|
{ |
|
57
|
|
|
return UnitsConverter::alpha2IntEightBit($this->value); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function get7Bit() : int |
|
61
|
|
|
{ |
|
62
|
|
|
return UnitsConverter::alpha2IntSevenBit($this->value); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getPercent() : float |
|
66
|
|
|
{ |
|
67
|
|
|
return UnitsConverter::alpha2percent($this->value); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function invert() : AlphaChannel |
|
71
|
|
|
{ |
|
72
|
|
|
// Float calculations are fuzzy, which is why we multiply |
|
73
|
|
|
// the values to be able to work with whole numbers. Then |
|
74
|
|
|
// we can divide it again to get a float. |
|
75
|
|
|
return ColorChannel::alpha((1000 - ($this->value * 1000)) / 1000); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|