|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MikeAlmond\Color; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class PaletteGenerator |
|
9
|
|
|
* @package MikeAlmond\Color |
|
10
|
|
|
*/ |
|
11
|
|
|
class PaletteGenerator |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The default distance in degrees to calculate hue adjustments |
|
15
|
|
|
*/ |
|
16
|
|
|
const DEFAULT_DISTANCE = 40; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var Color |
|
20
|
|
|
*/ |
|
21
|
|
|
public $baseColor; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* PaletteGenerator constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param Color $baseColor |
|
27
|
|
|
*/ |
|
28
|
4 |
|
public function __construct(Color $baseColor) |
|
29
|
|
|
{ |
|
30
|
4 |
|
$this->baseColor = $baseColor; |
|
31
|
4 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param int $steps |
|
35
|
|
|
* |
|
36
|
|
|
* @return Color[] |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function monochromatic(int $steps = 5): array |
|
39
|
|
|
{ |
|
40
|
1 |
|
$colors = []; |
|
41
|
1 |
|
$percentage = 80 / $steps; |
|
42
|
1 |
|
$numberToLighten = floor(($this->baseColor->getHsl()['l'] * 80) / $percentage); |
|
43
|
|
|
|
|
44
|
1 |
|
for ($i = $numberToLighten; $i > 0; $i--) { |
|
45
|
1 |
|
array_push($colors, $this->baseColor->lighten($percentage * $i)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
array_push($colors, $this->baseColor); |
|
49
|
|
|
|
|
50
|
1 |
|
for ($i = 1; $i < $steps - $numberToLighten + 1; $i++) { |
|
51
|
1 |
|
array_push($colors, $this->baseColor->darken($percentage * $i)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
return $colors; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param int $distance |
|
59
|
|
|
* |
|
60
|
|
|
* @return Color[] |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function adjacent(int $distance = self::DEFAULT_DISTANCE): array |
|
63
|
|
|
{ |
|
64
|
|
|
return [ |
|
65
|
1 |
|
$this->baseColor->adjustHue($distance * -1), |
|
66
|
1 |
|
$this->baseColor, |
|
67
|
1 |
|
$this->baseColor->adjustHue($distance), |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param int $distance |
|
73
|
|
|
* |
|
74
|
|
|
* @return Color[] |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function triad(int $distance = self::DEFAULT_DISTANCE): array |
|
77
|
|
|
{ |
|
78
|
|
|
return [ |
|
79
|
1 |
|
$this->baseColor, |
|
80
|
1 |
|
$this->baseColor->adjustHue(180 - $distance), |
|
81
|
1 |
|
$this->baseColor->adjustHue(180 + $distance), |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param int $distance |
|
87
|
|
|
* |
|
88
|
|
|
* @return Color[] |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function tetrad(int $distance = self::DEFAULT_DISTANCE): array |
|
91
|
|
|
{ |
|
92
|
|
|
return [ |
|
93
|
1 |
|
$this->baseColor, |
|
94
|
1 |
|
$this->baseColor->adjustHue(180), |
|
95
|
1 |
|
$this->baseColor->adjustHue($distance * -1), |
|
96
|
1 |
|
$this->baseColor->adjustHue(180 + $distance), |
|
97
|
|
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|