|
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 $floatPrecision = 2; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Converts a color value to a percentage. |
|
28
|
|
|
* @param int $colorValue 0-255 |
|
29
|
|
|
* @return float |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function intEightBit2Percent(int $colorValue) : float |
|
32
|
|
|
{ |
|
33
|
|
|
return $colorValue * 100 / 255; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function intSevenBit2Percent(int $colorValue) : float |
|
37
|
|
|
{ |
|
38
|
|
|
return $colorValue * 100 / 127; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Converts a percentage to an integer color value. |
|
43
|
|
|
* @param float $percent |
|
44
|
|
|
* @return int 0-255 |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function percent2IntEightBit(float $percent) : int |
|
47
|
|
|
{ |
|
48
|
|
|
$value = $percent * 255 / 100; |
|
49
|
|
|
return (int)round($value, 0, PHP_ROUND_HALF_UP); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param float $percent |
|
54
|
|
|
* @return int 0-127 |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function percent2IntSevenBit(float $percent) : int |
|
57
|
|
|
{ |
|
58
|
|
|
$value = $percent * 127 / 100; |
|
59
|
|
|
return (int)round($value, 0, PHP_ROUND_HALF_UP); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Converts an alpha value based on a 0-255 numeric |
|
64
|
|
|
* value to a 0-1 based float value. |
|
65
|
|
|
* |
|
66
|
|
|
* @param int $alpha |
|
67
|
|
|
* @return float |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function intEightBit2Float(int $alpha) : float |
|
70
|
|
|
{ |
|
71
|
|
|
return round($alpha / 255, self::$floatPrecision); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public static function intEightBit2IntSevenBit(int $alpha) : int |
|
75
|
|
|
{ |
|
76
|
|
|
return (int)round($alpha * 127 / 255); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public static function intSevenBit2IntEightBit(int $alpha) : int |
|
80
|
|
|
{ |
|
81
|
|
|
return (int)round($alpha * 255 / 127); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public static function intSevenBit2Float(int $alpha) : float |
|
85
|
|
|
{ |
|
86
|
|
|
return round($alpha / 127, self::$floatPrecision); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public static function float2IntEightBit(float $alpha) : int |
|
90
|
|
|
{ |
|
91
|
|
|
return (int)round($alpha * 255); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public static function float2IntSevenBit(float $alpha) : int |
|
95
|
|
|
{ |
|
96
|
|
|
return (int)round($alpha * 127); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public static function percent2Float(float $percent) : float |
|
100
|
|
|
{ |
|
101
|
|
|
return round($percent/100, self::$floatPrecision); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public static function float2percent(float $value) : float |
|
105
|
|
|
{ |
|
106
|
|
|
return $value * 100; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Converts an integer to a HEX color string. This differs |
|
111
|
|
|
* from the native `dechex()` function, in that it will |
|
112
|
|
|
* return `00` for the color string instead of the default |
|
113
|
|
|
* `0` provided by `dechex()`. |
|
114
|
|
|
* |
|
115
|
|
|
* @param int $int |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public static function int2hex(int $int) : string |
|
119
|
|
|
{ |
|
120
|
|
|
$str = dechex($int); |
|
121
|
|
|
if (strlen($str) === 1) |
|
122
|
|
|
{ |
|
123
|
|
|
$str .= $str; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $str; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|