Skip to content

Commit

Permalink
chore: AA and AAA WCAG levels
Browse files Browse the repository at this point in the history
  • Loading branch information
tomloprod committed May 26, 2024
1 parent 3388b8c commit b6dee6f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,49 @@ $contrastRatio = $hexColor->getContrastRatio();
$contrastRatio = $hexColor->getContrastRatio(new HexColor('#3FA684'));
```

#### AA & AAA WCAG levels

Below we show you how to check if a contrast ratio meets WCAG AA and AAA levels.

```php
/** @var HexColor $hexColor */
$hexColor = colority()->fromHex('#51B389');

/**
* AA Level for texts
*/
$passsesAALevelForLargeText = ContrasRatioScore::passesTextAALevel(
contrastRatio: $hexColor->getContrastRatio(),
largeText: true
);

$passsesAALevelForNormalText = ContrasRatioScore::passesTextAALevel(
contrastRatio: $hexColor->getContrastRatio(),
largeText: false
);

/**
* AAA Level for texts
*/
$passsesAAALevelForLargeText = ContrasRatioScore::passesTextAAALevel(
contrastRatio: $hexColor->getContrastRatio(),
largeText: true
);

$passsesAAALevelForNormalText = ContrasRatioScore::passesTextAAALevel(
contrastRatio: $hexColor->getContrastRatio(),
largeText: false
);
/**
* AA Level for Graphical Objects and User Interface Components
*/
$passsesAALevelForUI = ContrasRatioScore::passesUIAALevel(
$hexColor->getContrastRatio()
);

```


### Color validation
The concrete `Color` classes have a static method called `getParser()` which returns an instance of `ValueColorParser`.

Expand Down
3 changes: 0 additions & 3 deletions src/Support/Algorithms/LuminosityContrastRatio.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ public function getContrastRatio(array $rgbBackgroundColor, array $rgbForeground
$lum1 = $this->getLuminance($rgbBackgroundColor[0], $rgbBackgroundColor[1], $rgbBackgroundColor[2]);
$lum2 = $this->getLuminance($rgbForegroundColor[0], $rgbForegroundColor[1], $rgbForegroundColor[2]);

// @codeCoverageIgnoreStart
// @codeCoverageIgnoreEnd

// Ensure L1 is the lighter luminance and L2 is the darker luminance
$L1 = max($lum1, $lum2);
$L2 = min($lum1, $lum2);
Expand Down

0 comments on commit b6dee6f

Please sign in to comment.