TailwindColor   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 121
c 0
b 0
f 0
wmc 17
lcom 1
cbo 3
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A colors() 0 4 1
A getColors() 0 4 1
A random() 0 6 1
A minContrast() 0 15 3
A merge() 0 13 1
A randomPair() 0 4 1
A getRandomPair() 0 8 1
A getRandomPairMinRatio() 0 9 2
A getDefaultTailwindColors() 0 6 1
A flatten() 0 17 3
A parseDefaultTailwindColors() 0 4 1
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