|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Breadthe\PhpContrast; |
|
4
|
|
|
|
|
5
|
|
|
use Tightenco\Collect\Support\Collection; |
|
6
|
|
|
|
|
7
|
|
|
class TailwindColor implements Color |
|
8
|
|
|
{ |
|
9
|
|
|
protected $colors; |
|
10
|
|
|
|
|
11
|
|
|
protected $minRatio; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct() |
|
14
|
|
|
{ |
|
15
|
|
|
$this->colors = $this->getDefaultTailwindColors(); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Returns all the default Tailwind colors. |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function colors(): array |
|
22
|
|
|
{ |
|
23
|
|
|
return (new static)->colors->toArray(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function getColors(): array |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->colors->toArray(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Returns a random Tailwind color. |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function random(): HexColor |
|
35
|
|
|
{ |
|
36
|
|
|
$tailwindColors = (new static)->colors; |
|
37
|
|
|
|
|
38
|
|
|
return $tailwindColors[rand(0, count($tailwindColors) - 1)]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public static function minContrast($ratio): self |
|
42
|
|
|
{ |
|
43
|
|
|
if ($ratio < HexColorPair::MIN_RATIO) { |
|
44
|
|
|
$ratio = HexColorPair::MIN_RATIO; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ($ratio > HexColorPair::MAX_RATIO) { |
|
48
|
|
|
$ratio = HexColorPair::MAX_RATIO; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$twColor = new static; |
|
52
|
|
|
$twColor->minRatio = $ratio; |
|
53
|
|
|
|
|
54
|
|
|
return $twColor; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public static function merge(array $customPalette): self |
|
58
|
|
|
{ |
|
59
|
|
|
$twColor = new static; |
|
60
|
|
|
|
|
61
|
|
|
$customPalette = collect($customPalette) |
|
62
|
|
|
->map(function ($hex, $name) { |
|
63
|
|
|
return HexColor::make($hex, $name); |
|
64
|
|
|
}); |
|
65
|
|
|
|
|
66
|
|
|
$twColor->colors = $twColor->colors->merge($customPalette->flatten()); |
|
67
|
|
|
|
|
68
|
|
|
return $twColor; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns a random pair of accessible (min. contrast 3:1) Tailwind colors. |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function randomPair(): HexColorPair |
|
75
|
|
|
{ |
|
76
|
|
|
return (new static)->getRandomPair(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getRandomPair(): HexColorPair |
|
80
|
|
|
{ |
|
81
|
|
|
$minRatio = $this->minRatio ?? HexColorPair::MIN_RATIO; |
|
82
|
|
|
|
|
83
|
|
|
$fg = static::random(); |
|
84
|
|
|
|
|
85
|
|
|
return $this->getRandomPairMinRatio($fg, $minRatio); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function getRandomPairMinRatio($fg, $minRatio): HexColorPair |
|
89
|
|
|
{ |
|
90
|
|
|
do { |
|
91
|
|
|
$bg = static::random(); |
|
92
|
|
|
$pair = HexColorPair::make($fg, $bg); |
|
93
|
|
|
} while ($pair->ratio <= $minRatio); |
|
94
|
|
|
|
|
95
|
|
|
return $pair; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function getDefaultTailwindColors(): Collection |
|
99
|
|
|
{ |
|
100
|
|
|
$twColorsArr = $this->parseDefaultTailwindColors(); |
|
101
|
|
|
|
|
102
|
|
|
return $this->flatten($twColorsArr['colors']); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function flatten(array $twColorsArr): Collection |
|
106
|
|
|
{ |
|
107
|
|
|
$colors = new Collection(); |
|
108
|
|
|
|
|
109
|
|
|
collect($twColorsArr) |
|
110
|
|
|
->each(function ($color, $label) use ($colors) { |
|
111
|
|
|
if (is_array($color)) { |
|
112
|
|
|
foreach ($color as $shade => $value) { |
|
113
|
|
|
$colors->push(HexColor::make($value, "{$label}-{$shade}")); |
|
114
|
|
|
} |
|
115
|
|
|
} else { |
|
116
|
|
|
$colors->push(HexColor::make($color, $label)); |
|
117
|
|
|
} |
|
118
|
|
|
}); |
|
119
|
|
|
|
|
120
|
|
|
return $colors; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected function parseDefaultTailwindColors() |
|
124
|
|
|
{ |
|
125
|
|
|
return json_decode(file_get_contents(__DIR__.'/../stubs/twcolors.json'), true); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|