Completed
Push — master ( c14a81...f86bb9 )
by Nicholas
02:53
created

main::alpha()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
4
namespace projectcleverweb\color;
5
6
7
class main implements \Serializable {
8
	
9
	public $color;
10
	public $hsl_result_accuracy = 0;
11
	
12
	public function __construct($color) {
13
		$this->set($color);
14
	}
15
	
16
	public function __clone() {
17
		$rgb = $this->color->rgb + ['a' => $this->color->alpha()];
18
		unset($this->color);
19
		$this->set($rgb, 'rgb');
20
	}
21
	
22
	public function serialize() :string {
23
		return $this->color->serialize();
24
	}
25
	
26
	public function unserialize($serialized) {
27
		$unserialized = (array) json_decode((string) $serialized);
28
		regulate::rgb_array($unserialized);
29
		$this->set($unserialized, 'rgb');
30
	}
31
	
32
	public function set($color, string $type = '') {
33
		if ($color instanceof color) {
34
			$this->color = $color;
35
		} else {
36
			$this->color = new color($color, $type);
37
		}
38
	}
39
	
40
	public function rgb() :array {
41
		return (array) $this->color->rgb + ['a' => $this->color->alpha()];
42
	}
43
	
44
	public function hsl() :array {
45
		$color = [];
46
		foreach((array) $this->color->hsl as $key => $value) {
47
			$color[$key] = round($value, abs($this->hsl_result_accuracy));
48
		}
49
		return $color + ['a' => $this->color->alpha()];
50
	}
51
	
52
	public function cmyk() :array {
53
		$rgb = $this->color->rgb;
54
		return generate::rgb_to_cmyk($rgb['r'], $rgb['g'], $rgb['b']) + ['a' => $this->color->alpha()];
55
	}
56
	
57
	public function hex() :string {
58
		return $this->color->hex;
59
	}
60
	
61
	public function css() :string {
62
		return css::best($this->color);
63
	}
64
	
65
	/**
66
	 * Get (and set) the alpha channel
67
	 * 
68
	 * @param  mixed $new_alpha If numeric, the alpha channel is set to this value
69
	 * @return float            The current alpha value
70
	 */
71
	public function alpha($new_alpha) {
72
		return $this->color->alpha($new_alpha);
73
	}
74
	
75
	public function is_dark(int $check_score = 128) :bool {
76
		$rgb = $this->color->rgb;
77
		return check::is_dark($rgb['r'], $rgb['g'], $rgb['b'], $check_score);
78
	}
79
	
80
	public function red(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
81
		return modify::rgb($this->color, 'red', $adjustment, $as_percentage, $set_absolute);
82
	}
83
	
84
	public function green(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
85
		return modify::rgb($this->color, 'green', $adjustment, $as_percentage, $set_absolute);
86
	}
87
	
88
	public function blue(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
89
		return modify::rgb($this->color, 'blue', $adjustment, $as_percentage, $set_absolute);
90
	}
91
	
92
	public function hue(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
93
		return modify::hsl($this->color, 'hue', $adjustment, $as_percentage, $set_absolute);
94
	}
95
	
96
	public function saturation(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
97
		return modify::hsl($this->color, 'saturation', $adjustment, $as_percentage, $set_absolute);
98
	}
99
	
100
	public function light(float $adjustment, bool $as_percentage = FALSE, bool $set_absolute = TRUE) {
101
		return modify::hsl($this->color, 'light', $adjustment, $as_percentage, $set_absolute);
102
	}
103
	
104
	public function rgb_scheme(string $scheme_name = '') :array {
105
		return static::_convert_scheme(
106
			static::hsl_scheme($scheme_name),
107
			[new generate, 'hsl_to_rgb']
108
		);
109
	}
110
	
111
	public function hsl_scheme(string $scheme_name = '') :array {
112
		if (is_callable($callable = [new scheme, $scheme_name])) {
113
			$hsl = $this->color->hsl;
114
			return call_user_func($callable, $hsl['h'], $hsl['s'], $hsl['l']);
115
		}
116
		error::call(sprintf(
117
			'The $scheme_name "%s" is not a valid scheme name',
118
			$scheme_name,
119
			__CLASS__,
120
			__FUNCTION__
121
		));
122
		return [];
123
	}
124
	
125
	public function hex_scheme(string $scheme_name = '') :array {
126
		return static::_convert_scheme(
127
			static::rgb_scheme($scheme_name),
128
			[new generate, 'rgb_to_hex']
129
		);
130
	}
131
	
132
	public function cmyk_scheme(string $scheme_name = '') :array {
133
		return static::_convert_scheme(
134
			static::rgb_scheme($scheme_name),
135
			[new generate, 'rgb_to_cmyk']
136
		);
137
	}
138
	
139
	protected static function _convert_scheme(array $scheme, callable $callback) {
140
		$scheme = array_values($scheme);
141
		foreach ($scheme as &$color) {
142
			$color = call_user_func_array($callback, $color);
143
		}
144
		return $scheme;
145
	}
146
}
147