Completed
Push — master ( 3ca6ea...a7bbc6 )
by Nicholas
03:00
created

generate::blend()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 9
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
4
namespace projectcleverweb\color;
5
6
7
class generate {
8
	
9 1
	public static function rgb_contrast(int $r = 0, int $g = 0, int $b = 0) :array {
10
		return [
11 1
			'r' => ($r < 128) ? 255 : 0,
12 1
			'g' => ($g < 128) ? 255 : 0,
13 1
			'b' => ($b < 128) ? 255 : 0
14
		];
15
	}
16
	
17 1
	public static function rgb_invert(int $r = 0, int $g = 0, int $b = 0) :array {
18
		return [
19 1
			'r' => 255 - $r,
20 1
			'g' => 255 - $g,
21 1
			'b' => 255 - $b
22
		];
23
	}
24
	
25 2
	public static function yiq_score(int $r = 0, int $g = 0, int $b = 0) :float {
26 2
		return (($r * 299) + ($g * 587) + ($b * 114)) / 1000;
27
	}
28
	
29
	public static function rgb_rand(int $min_r = 0, int $max_r = 255, int $min_g = 0, int $max_g = 255, int $min_b = 0, int $max_b = 255) :array {
30
		return [
31
			'r' => rand(abs((int) $min_r) % 256, abs((int) $max_r) % 256),
32
			'g' => rand(abs((int) $min_g) % 256, abs((int) $max_g) % 256),
33
			'b' => rand(abs((int) $min_b) % 256, abs((int) $max_b) % 256)
34
		];
35
	}
36
	
37
	public static function hsl_rand(int $min_h = 0, int $max_h = 255, int $min_s = 0, int $max_s = 255, int $min_l = 0, int $max_l = 255) :array {
38
		return [
39
			'h' => rand(abs((int) $min_h) % 256, abs((int) $max_h) % 256),
40
			's' => rand(abs((int) $min_s) % 256, abs((int) $max_s) % 256),
41
			'l' => rand(abs((int) $min_l) % 256, abs((int) $max_l) % 256)
42
		];
43
	}
44
	
45 1
	public static function blend(float $r1, float $g1, float $b1, float $a1, float $r2, float $g2, float $b2, float $a2, float $amount = 50.0) :array {
46 1
		$x1 = regulate::div(100 - $amount, 100);
47 1
		$x2 = regulate::div($amount, 100);
48
		return [
49 1
			'r' => round(($r1 * $x1) + ($r2 * $x2), 0),
50 1
			'g' => round(($g1 * $x1) + ($g2 * $x2), 0),
51 1
			'b' => round(($b1 * $x1) + ($b2 * $x2), 0),
52 1
			'a' => ($a1 * $x1) + ($a2 * $x2)
53
		];
54
	}
55
	
56
	public static function web_safe(int $r = 0, int $g = 0, int $b = 0) :string {
57
		return convert::rgb_to_hex(
58
			round($r / 0x33) * 0x33,
59
			round($g / 0x33) * 0x33,
60
			round($b / 0x33) * 0x33
61
		);
62
	}
63
}
64