1
|
|
|
<?php |
2
|
|
|
namespace w3l\Holt45; |
3
|
|
|
trait Convert { |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Converts red-green-blue(RGB) to hexadecimal |
7
|
|
|
* |
8
|
|
|
* @param array $rgb RGB color |
9
|
|
|
* @return string Hexadecimal color |
10
|
|
|
*/ |
11
|
|
|
public static function rgbhex($rgb) { |
12
|
|
|
$hex = ""; |
13
|
|
|
foreach($rgb AS $color) { |
14
|
|
|
$hex .= str_pad(dechex($color), 2, "0", STR_PAD_LEFT); |
15
|
|
|
} |
16
|
|
|
return $hex; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Converts hexadecimal to red-green-blue(RGB) |
21
|
|
|
* |
22
|
|
|
* @param string $hex Hexadecimal color |
23
|
|
|
* @return null|int[] RGB color |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
public static function hexrgb($hex) { |
26
|
|
|
$hex = preg_replace("/[^0-9A-Fa-f]/", '', $hex); |
27
|
|
|
|
28
|
|
|
$strlenHex = strlen($hex); |
29
|
|
|
|
30
|
|
|
if($strlenHex >= 3) { |
31
|
|
|
if($strlenHex >= 6) { |
32
|
|
|
if($strlenHex > 6) { |
33
|
|
|
$hex = substr($hex,0,6); |
34
|
|
|
} |
35
|
|
|
$hexArray = str_split($hex,2); |
36
|
|
|
} elseif($strlenHex < 6) { |
37
|
|
|
$hexArray = array("$hex[0]$hex[0]", "$hex[1]$hex[1]", "$hex[2]$hex[2]"); |
38
|
|
|
} |
39
|
|
|
return array(hexdec($hexArray[0]), hexdec($hexArray[1]), hexdec($hexArray[2])); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return NULL; |
43
|
|
|
} |
44
|
|
|
/** |
45
|
|
|
* Converts hexadecimal to red-green-blue(RGB) |
46
|
|
|
* |
47
|
|
|
* @param array $arrayRGB RGB color |
48
|
|
|
* @param array $arrayRGB2 RGB color |
49
|
|
|
* @return int[] Blended RGB color |
50
|
|
|
*/ |
51
|
|
|
public static function colorBlend($arrayRGB,$arrayRGB2) { |
52
|
|
|
|
53
|
|
|
$arrayBlend = array(); |
54
|
|
|
|
55
|
|
|
for($i = 0, $size = count($arrayRGB); $i < $size; $i++) { |
56
|
|
|
$arrayBlend[] = round(($arrayRGB[$i]+$arrayRGB2[$i])/2); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $arrayBlend; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.