Test Failed
Push — master ( 9d3a5c...c5f273 )
by Sebastian
08:26
created

ColorChannel::decimal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * File containing the class {@see \AppUtils\RGBAColor\ColorChannel}.
4
 *
5
 * @package Application Utils
6
 * @subpackage RGBAColor
7
 * @see \AppUtils\RGBAColor\ColorChannel
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils\RGBAColor;
13
14
use AppUtils\RGBAColor\ColorChannel\AlphaChannel;
15
use AppUtils\RGBAColor\ColorChannel\EightBitChannel;
16
use AppUtils\RGBAColor\ColorChannel\HexadecimalChannel;
17
use AppUtils\RGBAColor\ColorChannel\HueChannel;
18
use AppUtils\RGBAColor\ColorChannel\BrightnessChannel;
19
use AppUtils\RGBAColor\ColorChannel\PercentChannel;
20
use AppUtils\RGBAColor\ColorChannel\SaturationChannel;
21
use AppUtils\RGBAColor\ColorChannel\SevenBitChannel;
22
23
/**
24
 * Abstract base class for individual color channels.
25
 * Acts as factory class for channels with the following
26
 * methods:
27
 *
28
 * - {@see self::hexadecimal()}
29
 * - {@see self::eightBit()}
30
 * - {@see self::sevenBit()}
31
 * - {@see self::percent()}
32
 * - {@see self::alpha()}
33
 * - {@see self::hue()}
34
 * - {@see self::saturation()}
35
 * - {@see self::brightness()}
36
 *
37
 * @package Application Utils
38
 * @subpackage RGBAColor
39
 * @author Sebastian Mordziol <[email protected]>
40
 *
41
 * @see AlphaChannel
42
 * @see EightBitChannel
43
 * @see HexadecimalChannel
44
 * @see PercentChannel
45
 * @see SevenBitChannel
46
 */
47
abstract class ColorChannel
48
{
49
    /**
50
     * @return mixed
51
     */
52
    abstract public function getValue();
53
54
55
    /**
56
     * @return int 0 to 255
57
     */
58
    abstract public function get8Bit() : int;
59
60
    /**
61
     * @return int 0 to 127
62
     */
63
    abstract public function get7Bit() : int;
64
65
    /**
66
     * @return float 0.0 to 1.0
67
     */
68
    abstract public function getAlpha() : float;
69
70
    /**
71
     * @return float 0 to 100
72
     */
73
    abstract public function getPercent() : float;
74
75
    public function getPercentRounded() : int
76
    {
77
        return (int)round($this->getPercent());
78
    }
79
80
    public function getHexadecimal() : string
81
    {
82
        return UnitsConverter::int2hex($this->get8Bit());
83
    }
84
85
    /**
86
     * @return ColorChannel
87
     */
88
    abstract public function invert() : ColorChannel;
89
90
    /**
91
     * @param string|HexadecimalChannel $hex A double or single hex character. e.g. "FF".
92
     *                    For a single character, it is assumed it should
93
     *                    be duplicated. Example: "F" > "FF". If an existing hexadecimal
94
     *                    channel instance is given, this is returned.
95
     * @return HexadecimalChannel
96
     * @throws ColorException
97
     */
98
    public static function hexadecimal($hex) : HexadecimalChannel
99
    {
100
        if($hex instanceof HexadecimalChannel) {
101
            return $hex;
102
        }
103
104
        return new HexadecimalChannel($hex);
105
    }
106
107
    /**
108
     * @param int|EightBitChannel $value 0 to 255
109
     * @return EightBitChannel
110
     */
111
    public static function eightBit($value) : EightBitChannel
112
    {
113
        if($value instanceof EightBitChannel) {
114
            return $value;
115
        }
116
117
        return new EightBitChannel($value);
118
    }
119
120
    /**
121
     * @param int|SevenBitChannel $value 0 to 127
122
     * @return SevenBitChannel
123
     */
124
    public static function sevenBit($value) : SevenBitChannel
125
    {
126
        if($value instanceof SevenBitChannel) {
127
            return $value;
128
        }
129
130
        return new SevenBitChannel($value);
131
    }
132
133
    /**
134
     * @param int|float|PercentChannel $percent 0 to 100
135
     * @return PercentChannel
136
     */
137
    public static function percent($percent) : PercentChannel
138
    {
139
        if($percent instanceof PercentChannel) {
140
            return $percent;
141
        }
142
143
        return new PercentChannel($percent);
144
    }
145
146
    /**
147
     * @param float|AlphaChannel|NULL $alpha 0.0 to 1.0
148
     * @return AlphaChannel
149
     */
150
    public static function alpha($alpha) : AlphaChannel
151
    {
152
        if($alpha instanceof AlphaChannel) {
153
            return $alpha;
154
        }
155
156
        if($alpha === null) {
157
            $alpha = 0.0;
158
        }
159
160
        return new AlphaChannel($alpha);
161
    }
162
163
    /**
164
     * @param int|float|HueChannel $hue 0 to 360
165
     * @return HueChannel
166
     */
167
    public static function hue($hue) : HueChannel
168
    {
169
        if($hue instanceof HueChannel) {
170
            return $hue;
171
        }
172
173
        return new HueChannel($hue);
174
    }
175
176
    /**
177
     * @param int|float|BrightnessChannel $brightness 0 to 100
178
     * @return BrightnessChannel
179
     */
180
    public static function brightness($brightness) : BrightnessChannel
181
    {
182
        if($brightness instanceof BrightnessChannel) {
183
            return $brightness;
184
        }
185
186
        return new BrightnessChannel($brightness);
187
    }
188
189
    /**
190
     * @param int|float|SaturationChannel $saturation 0 to 100
191
     * @return SaturationChannel
192
     */
193
    public static function saturation($saturation) : SaturationChannel
194
    {
195
        if($saturation instanceof SaturationChannel) {
196
            return $saturation;
197
        }
198
199
        return new SaturationChannel($saturation);
200
    }
201
}
202