-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathColorConverter.php
95 lines (82 loc) · 2.28 KB
/
ColorConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace Codein\ColorConverter;
use Codein\ColorConverter\Color\HEX;
use Codein\ColorConverter\Color\HEXa;
use Codein\ColorConverter\Color\HSVa;
use Codein\ColorConverter\Color\RGB;
use Codein\ColorConverter\Color\RGBa;
class ColorConverter
{
const INPUT_HSVA = '/^((hsva)|hsv)[\D]+([\d.]+)[\D]+([\d.]+)[\D]+([\d.]+)[\D]*?([\d.]+|$)/i';
const INPUT_RGBA = '/^((rgba)|rgb)[\D]+([\d.]+)[\D]+([\d.]+)[\D]+([\d.]+)[\D]*?([\d.]+|$)/i';
const INPUT_HEXA = '/^#?(([\dA-Fa-f]{3,4})|([\dA-Fa-f]{6})|([\dA-Fa-f]{8}))$/i';
/**
* @param $input
* @param $regex
* @return bool
*/
public function isInputValid($input, $regex)
{
return (bool)preg_match($regex, $input);
}
/**
* @param $input
* @return bool
*/
public function stringIsColor($input)
{
return $this->isInputValid($input, self::INPUT_HSVA)
|| $this->isInputValid($input, self::INPUT_RGBA)
|| $this->isInputValid($input, self::INPUT_HEXA);
}
/**
* @param $string
* @return HSVa
*/
public function convertStringToHSVa($string)
{
$matches = [];
if(preg_match(self::INPUT_HSVA, $string, $matches)) {
return HSVa::getFromHSVaMatches($matches);
} elseif(preg_match(self::INPUT_RGBA, $string, $matches)) {
return HSVa::getFromRGBaMatches($matches);
} elseif(preg_match(self::INPUT_HEXA, $string, $matches)) {
return HSVa::getFromHEXaMatches($matches);
}
return new HSVa();
}
/**
* @param HSVa $HSVa
* @return HEXa
*/
public function convertHSVaToHEXa(HSVa $HSVa)
{
$RGBa = $this->convertHSVaToRGBa($HSVa);
return (new HEXa())->fromRGBa($RGBa);
}
/**
* @param HSVa $HSVa
* @return RGBa
*/
public function convertHSVaToRGBa(HSVa $HSVa)
{
return (new RGBa())->fromHSVa($HSVa);
}
/**
* @param HSVa $HSVa
* @return HEX
*/
public function convertHSVaToHEX(HSVa $HSVa)
{
$RGBa = $this->convertHSVaToRGBa($HSVa);
return (new HEX())->fromRGBa($RGBa);
}
/**
* @param HSVa $HSVa
* @return RGB
*/
public function convertHSVaToRGB(HSVa $HSVa)
{
return (new RGB())->fromHSVa($HSVa);
}
}