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

UnitsConverter::intSevenBit2Float()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
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 FormatsConverter}.
4
 *
5
 * @see FormatsConverter
6
 *@subpackage RGBAColor
7
 * @package AppUtils
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils\RGBAColor;
13
14
/**
15
 * The converter static class is used to convert between color
16
 * information formats.
17
 *
18
 * @package AppUtils
19
 * @subpackage RGBAColor
20
 * @author Sebastian Mordziol <[email protected]>
21
 */
22
class UnitsConverter
23
{
24
    private static int $floatPrecision = 2;
25
26
    /**
27
     * @param string $hex
28
     * @return int 0 to 255
29
     */
30
    public static function hex2int(string $hex) : int
31
    {
32
        return (int)hexdec($hex);
33
    }
34
35
    /**
36
     * Converts a color value to a percentage.
37
     * @param int $eightBit 0 to 255
38
     * @return float 0 to 100
39
     */
40
    public static function intEightBit2Percent(int $eightBit) : float
41
    {
42
        return $eightBit * 100 / 255;
43
    }
44
45
    /**
46
     * @param int $colorValue 0 to 127
47
     * @return float 0 to 100
48
     */
49
    public static function intSevenBit2Percent(int $colorValue) : float
50
    {
51
        return $colorValue * 100 / 127;
52
    }
53
54
    /**
55
     * Converts a percentage to an integer color value.
56
     * @param float $percent 0 to 100
57
     * @return int 0 to 255
58
     */
59
    public static function percent2IntEightBit(float $percent) : int
60
    {
61
        $value = $percent * 255 / 100;
62
        return (int)round($value, 0, PHP_ROUND_HALF_UP);
63
    }
64
65
    /**
66
     * @param float $percent 0 to 100
67
     * @return int 0 to 127
68
     */
69
    public static function percent2IntSevenBit(float $percent) : int
70
    {
71
        $value = $percent * 127 / 100;
72
        return (int)round($value, 0, PHP_ROUND_HALF_UP);
73
    }
74
75
    /**
76
     * Converts an alpha value based on a 0-255 numeric
77
     * value to a 0-1 based float value.
78
     *
79
     * @param int $eightBit 255
80
     * @return float 0.0 to 1.0
81
     */
82
    public static function intEightBit2Alpha(int $eightBit) : float
83
    {
84
        return round($eightBit / 255, self::$floatPrecision);
85
    }
86
87
    /**
88
     * @param int $eightBit 0 to 255
89
     * @return int 0 to 127
90
     */
91
    public static function intEightBit2IntSevenBit(int $eightBit) : int
92
    {
93
        return (int)round($eightBit * 127 / 255);
94
    }
95
96
    /**
97
     * @param int $sevenBit 0 to 127
98
     * @return int 0 to 255
99
     */
100
    public static function intSevenBit2IntEightBit(int $sevenBit) : int
101
    {
102
        return (int)round($sevenBit * 255 / 127);
103
    }
104
105
    /**
106
     * @param int $sevenBit 0 to 127
107
     * @return float 0.0 to 1.0
108
     */
109
    public static function intSevenBit2Alpha(int $sevenBit) : float
110
    {
111
        return round($sevenBit / 127, self::$floatPrecision);
112
    }
113
114
    /**
115
     * @param float $alpha 0.0 to 1.0
116
     * @return int 0 to 255
117
     */
118
    public static function alpha2IntEightBit(float $alpha) : int
119
    {
120
        return (int)round($alpha * 255);
121
    }
122
123
    /**
124
     * @param float $alpha 0.0 to 1.0
125
     * @return int 0 to 127
126
     */
127
    public static function alpha2IntSevenBit(float $alpha) : int
128
    {
129
        return (int)round($alpha * 127);
130
    }
131
132
    /**
133
     * @param float $percent 0-100
134
     * @return float 0.0-1.0
135
     */
136
    public static function percent2Alpha(float $percent) : float
137
    {
138
        return round($percent/100, self::$floatPrecision);
139
    }
140
141
    /**
142
     * @param float $alpha 0.0-1.0
143
     * @return float 0-100
144
     */
145
    public static function alpha2percent(float $alpha) : float
146
    {
147
        return $alpha * 100;
148
    }
149
150
    /**
151
     * Converts an integer to a HEX color string. This differs
152
     * from the native `dechex()` function, in that it will
153
     * return `00` for the color string instead of the default
154
     * `0` provided by `dechex()`.
155
     *
156
     * @param int $int
157
     * @return string
158
     */
159
    public static function int2hex(int $int) : string
160
    {
161
        $str = dechex($int);
162
163
        if (strlen($str) === 1)
164
        {
165
            $str = str_pad($str, 2, '0', STR_PAD_LEFT);
166
        }
167
168
        return strtoupper($str);
169
    }
170
171
    /**
172
     * @param float $hue 0-360
173
     * @return int 127
174
     */
175
    public static function hue2IntSevenBit(float $hue) : int
176
    {
177
        return (int)round($hue * 360 / 127);
178
    }
179
180
    /**
181
     * @param float $hue 0-360
182
     * @return int 0-255
183
     */
184
    public static function hue2IntEightBit(float $hue) : int
185
    {
186
        return (int)round($hue * 360 / 255);
187
    }
188
189
    /**
190
     * @param float $hue 0-360
191
     * @return float 0.0-1.0
192
     */
193
    public static function hue2Alpha(float $hue) : float
194
    {
195
        return round($hue / 360, self::$floatPrecision);
196
    }
197
198
    /**
199
     * @param float $hue 0-360
200
     * @return float 0-100
201
     */
202
    public static function hue2Percent(float $hue) : float
203
    {
204
        return $hue * 100 / 360;
205
    }
206
}
207