|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Convert.php |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace w3l\Holt45; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Convert and manipulate values. |
|
9
|
|
|
*/ |
|
10
|
|
|
trait Convert |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Converts red-green-blue(RGB) to hexadecimal |
|
14
|
|
|
* |
|
15
|
|
|
* @param array $rgb RGB color |
|
|
|
|
|
|
16
|
|
|
* @return string Hexadecimal color |
|
17
|
|
|
*/ |
|
18
|
|
|
public static function rgbhex(...$rgb) |
|
19
|
|
|
{ |
|
20
|
|
|
// If first value is array, then create array from first value |
|
21
|
|
|
if ((array)$rgb[0] === $rgb[0]) { |
|
22
|
|
|
$rgb = $rgb[0]; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
$hex = ""; |
|
26
|
|
|
foreach ($rgb as $color) { |
|
27
|
|
|
$hex .= str_pad(dechex($color), 2, "0", STR_PAD_LEFT); |
|
28
|
|
|
} |
|
29
|
|
|
return $hex; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Converts hexadecimal to red-green-blue(RGB) |
|
34
|
|
|
* |
|
35
|
|
|
* @used-by Holt45::rainbowText(); |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $hex Hexadecimal color |
|
38
|
|
|
* @return null|int[] RGB color |
|
|
|
|
|
|
39
|
|
|
*/ |
|
40
|
|
|
public static function hexrgb($hex) |
|
41
|
|
|
{ |
|
42
|
|
|
$hex = preg_replace("/[^0-9A-Fa-f]/", '', $hex); |
|
43
|
|
|
|
|
44
|
|
|
$strlenHex = strlen($hex); |
|
45
|
|
|
|
|
46
|
|
|
if ($strlenHex >= 3) { |
|
47
|
|
|
if ($strlenHex >= 6) { |
|
48
|
|
|
if ($strlenHex > 6) { |
|
49
|
|
|
$hex = substr($hex, 0, 6); |
|
50
|
|
|
} |
|
51
|
|
|
$hexArray = str_split($hex, 2); |
|
52
|
|
|
} elseif ($strlenHex < 6) { |
|
53
|
|
|
$hexArray = array("$hex[0]$hex[0]", "$hex[1]$hex[1]", "$hex[2]$hex[2]"); |
|
54
|
|
|
} |
|
55
|
|
|
if (isset($hexArray)) { |
|
56
|
|
|
return array(hexdec($hexArray[0]), hexdec($hexArray[1]), hexdec($hexArray[2])); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Converts hexadecimal to red-green-blue(RGB) |
|
65
|
|
|
* |
|
66
|
|
|
* @used-by Holt45::rainbowText(); |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $arrayRGB RGB color |
|
69
|
|
|
* @param array $arrayRGB2 RGB color |
|
70
|
|
|
* @return int[] Blended RGB color |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function colorBlend($arrayRGB, $arrayRGB2) |
|
73
|
|
|
{ |
|
74
|
|
|
$arrayBlend = array(); |
|
75
|
|
|
|
|
76
|
|
|
for ($i = 0, $size = count($arrayRGB); $i < $size; $i++) { |
|
77
|
|
|
$arrayBlend[] = round(($arrayRGB[$i]+$arrayRGB2[$i])/2); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $arrayBlend; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|