rgb   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 94
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A to_rgb() 0 3 1
A to_hex() 0 8 1
A to_cmyk() 0 15 1
A to_hsb() 0 22 2
B to_hsl() 0 26 3
1
<?php
2
3
namespace projectcleverweb\color\convert;
4
5
class rgb implements \projectcleverweb\color\interfaces\converter {
6
	use \projectcleverweb\color\traits\converter;
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);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
35
		$rgbp = array(
36
			'r' => $rgb['r'] / 255 * 100,
37
			'g' => $rgb['g'] / 255 * 100,
38
			'b' => $rgb['b'] / 255 * 100
39
		);
40
		$k  = 100 - max($rgbp);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 2 spaces

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
41
		return [
42
			'c' => ((100 - $rgbp['r'] - $k) / (100 - $k)) * 100,
43
			'm' => ((100 - $rgbp['g'] - $k) / (100 - $k)) * 100,
44
			'y' => ((100 - $rgbp['b'] - $k) / (100 - $k)) * 100,
45
			'k' => $k
46
		];
47
	}
48
	
49
	public static function to_hsl($input) :array {
50
		$rgb = static::_validate_array_input($input);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
51
		$r     = $rgb['r'] / 255;
52
		$g     = $rgb['g'] / 255;
53
		$b     = $rgb['b'] / 255;
54
		$min   = min($r, $g, $b);
55
		$max   = max($r, $g, $b);
56
		$delta = $max - $min;
57
		$h     = 0;
58
		$s     = 0;
59
		$l     = ($max + $min) / 2;
60
		
61
		if ($max != $min) {
62
			$s = $delta / ($max + $min);
63
			if ($l >= 0.5) {
64
				$s = $delta / (2 - $max - $min);
65
			}
66
			static::_rgbhsl_hue($h, $r, $g, $b, $max, $delta);
67
		}
68
		
69
		return [
70
			'h' => $h * 360,
71
			's' => $s * 100,
72
			'l' => $l * 100
73
		];
74
	}
75
	
76
	public static function to_hsb($input) :array {
77
		$rgb = static::_validate_array_input($input);
78
		$r   = $rgb['r'] / 255;
79
		$g   = $rgb['g'] / 255;
80
		$b   = $rgb['b'] / 255;
81
		
82
		$max = max($r, $g, $b);
83
		$min = min($r, $g, $b);
84
		$v   = $max;
85
		$d   = $max - $min;
86
		$s   = \projectcleverweb\color\regulate::div($d, $max);
87
		$h   = 0; // achromatic
88
		if ($max != $min) {
89
			static::_rgbhsl_hue($h, $r, $g, $b, $max, $d);
90
		}
91
		
92
		$h = $h * 360;
93
		$s = $s * 100;
94
		$v = $v * 100;
95
		
96
		return ['h' => $h, 's' => $s, 'b' => $v];
97
	}
98
}
99