-
Notifications
You must be signed in to change notification settings - Fork 1
/
WCAGContrastRating.php
87 lines (77 loc) · 1.85 KB
/
WCAGContrastRating.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace HarmsTyler\ContrastRatioCalculator;
/**
* Class WCAGContrastRating
*/
class WCAGContrastRating
{
const FAIL = 'fail';
const AA_LARGE = 'aa-large';
const AA = 'aa';
const AAA = 'aaa';
/**
* @var array
*/
private $ratings;
/**
* W3Rating constructor.
*/
public function __construct()
{
$this->ratings = [
self::FAIL => [
'range' => [0, 3]
],
self::AA_LARGE => [
'range' => [3, 4.5]
],
self::AA => [
'range' => [4.5, 7]
],
self::AAA => [
'range' => [7, 22]
]
];
}
/**
* @return array
*/
public function getRatings(): array
{
return $this->ratings;
}
/**
* optionally provide an alternate set of ratings
*
* @param array $ratings
*/
public function setRatings(array $ratings): void
{
$this->ratings = $ratings;
}
/**
* return the contrast ratio rating ('fail', 'aa-large', 'aa', 'aaa')
*
* @param $primaryColor
* @param $secondaryColor
* @return string
*/
public function rateColors(Color $primaryColor, Color $secondaryColor)
{
$contrastRatio = new ContrastRatio($primaryColor, $secondaryColor);
return $this->rateContrastRatio($contrastRatio);
}
/**
* @param ContrastRatio $contrastRatio
* @return string
*/
public function rateContrastRatio(ContrastRatio $contrastRatio): string
{
foreach ($this->ratings as $levelName => $ratingLevel) {
if ($contrastRatio->getRatio() > $ratingLevel['range'][0] && $contrastRatio->getRatio() < $ratingLevel['range'][1]) {
return $levelName;
}
}
return 'error';
}
}