1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace projectcleverweb\color\convert; |
4
|
|
|
|
5
|
|
|
class rgb implements \projectcleverweb\color\interfaces\converter { |
6
|
|
|
use \projectcleverweb\color\traits\convert\meta; |
7
|
|
|
|
8
|
|
|
protected static $valid_keys = array( |
9
|
|
|
'r', |
10
|
|
|
'g', |
11
|
|
|
'b' |
12
|
|
|
); |
13
|
|
|
|
14
|
|
|
protected static $default_value = array( |
15
|
|
|
'r' => 0, |
16
|
|
|
'g' => 0, |
17
|
|
|
'b' => 0 |
18
|
|
|
); |
19
|
|
|
|
20
|
|
|
public static function to_rgb($input) :array { |
21
|
|
|
return static::_validate_array_input($input); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function to_hex($input) :string { |
25
|
|
|
$rgb = static::_validate_array_input($input); |
26
|
|
|
return strtoupper( |
27
|
|
|
str_pad(dechex($rgb['r']), 2, '0', STR_PAD_LEFT) |
28
|
|
|
.str_pad(dechex($rgb['g']), 2, '0', STR_PAD_LEFT) |
29
|
|
|
.str_pad(dechex($rgb['b']), 2, '0', STR_PAD_LEFT) |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public static function to_cmyk($input) :array { |
34
|
|
|
$rgb = static::_validate_array_input($input); |
35
|
|
|
$c = (255 - $rgb['r']) / 255 * 100; |
|
|
|
|
36
|
|
|
$m = (255 - $rgb['g']) / 255 * 100; |
|
|
|
|
37
|
|
|
$y = (255 - $rgb['b']) / 255 * 100; |
|
|
|
|
38
|
|
|
$k = min(array($c,$m,$y)); |
|
|
|
|
39
|
|
|
$c -= $k; |
|
|
|
|
40
|
|
|
$m -= $k; |
|
|
|
|
41
|
|
|
$y -= $k; |
|
|
|
|
42
|
|
|
return [ |
43
|
|
|
'c' => round($c), |
44
|
|
|
'm' => round($m), |
45
|
|
|
'y' => round($y), |
46
|
|
|
'k' => round($k) |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function to_hsl($input) :array { |
51
|
|
|
$rgb = static::_validate_array_input($input); |
|
|
|
|
52
|
|
|
$r = $rgb['r'] / 255; |
53
|
|
|
$g = $rgb['g'] / 255; |
54
|
|
|
$b = $rgb['b'] / 255; |
55
|
|
|
$min = min($r, $g, $b); |
56
|
|
|
$max = max($r, $g, $b); |
57
|
|
|
$delta = $max - $min; |
58
|
|
|
$h = 0; |
59
|
|
|
$s = 0; |
60
|
|
|
$l = ($max + $min) / 2; |
61
|
|
|
|
62
|
|
View Code Duplication |
if ($max != $min) { |
|
|
|
|
63
|
|
|
$s = $delta / ($max + $min); |
64
|
|
|
if ($l >= 0.5) { |
65
|
|
|
$s = $delta / (2 - $max - $min); |
66
|
|
|
} |
67
|
|
|
static::_rgbhsl_hue($h, $r, $g, $b, $max, $delta); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return [ |
71
|
|
|
'h' => $h * 360, |
72
|
|
|
's' => $s * 100, |
73
|
|
|
'l' => $l * 100 |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function to_hsb($input) :array { |
78
|
|
|
$rgb = static::_validate_array_input($input); |
79
|
|
|
$r = $rgb['r'] / 255; |
80
|
|
|
$g = $rgb['g'] / 255; |
81
|
|
|
$b = $rgb['b'] / 255; |
82
|
|
|
|
83
|
|
|
$max = max($r, $g, $b); |
84
|
|
|
$min = min($r, $g, $b); |
85
|
|
|
$v = $max; |
86
|
|
|
$d = $max - $min; |
87
|
|
|
$s = \projectcleverweb\color\regulate::div($d, $max); |
88
|
|
|
$h = 0; // achromatic |
89
|
|
|
if ($max != $min) { |
90
|
|
|
static::_rgbhsl_hue($h, $r, $g, $b, $max, $d); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$h = $h * 360; |
94
|
|
|
$s = $s * 100; |
95
|
|
|
$v = $v * 100; |
96
|
|
|
|
97
|
|
|
return ['h' => $h, 's' => $s, 'b' => $v]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.