|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jackal\ImageMerge\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Jackal\ImageMerge\Exception\InvalidColorException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class Color |
|
9
|
|
|
* @package Jackal\ImageMerge\Model |
|
10
|
|
|
*/ |
|
11
|
|
|
class Color |
|
12
|
|
|
{ |
|
13
|
|
|
const BLACK = '000000'; |
|
14
|
|
|
const WHITE = 'FFFFFF'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $red; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $green; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $blue; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private $colorHex; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Color constructor. |
|
38
|
|
|
* @param $colorHex |
|
39
|
|
|
* @throws InvalidColorException |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct($colorHex) |
|
42
|
|
|
{ |
|
43
|
|
|
if (substr($colorHex, 0, 1) == '#') { |
|
44
|
|
|
$colorHex = substr($colorHex, 1); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
preg_match('/[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}/', $colorHex, $matches); |
|
48
|
|
|
|
|
49
|
|
|
if (!$matches or strlen($colorHex) != strlen($matches[0])) { |
|
50
|
|
|
throw new InvalidColorException(sprintf('Color "%s" is invalid', $colorHex)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$colorHex = $matches[0]; |
|
54
|
|
|
|
|
55
|
|
|
if (strlen($colorHex) == 3) { |
|
56
|
|
|
$c1 = str_repeat(substr($colorHex, 0, 1), 2); |
|
57
|
|
|
$c2 = str_repeat(substr($colorHex, 1, 1), 2); |
|
58
|
|
|
$c3 = str_repeat(substr($colorHex, 2, 1), 2); |
|
59
|
|
|
$colorHex = $c1 . $c2 . $c3; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->colorHex = $colorHex; |
|
63
|
|
|
|
|
64
|
|
|
$this->red = hexdec(substr($colorHex, 0, 2)); |
|
65
|
|
|
$this->green = hexdec(substr($colorHex, 2, 2)); |
|
66
|
|
|
$this->blue = hexdec(substr($colorHex, 4, 2)); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
public function red() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->red; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function green() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->green; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
public function blue() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->blue; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function rgb() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->red() . $this->green() . $this->blue(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function getHex() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->colorHex; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|