Skip to content

ArtisanPack UI home
Tutorials

Introducing ArtisanPack UI Accessibility: Color Contrast You Don't Have to Think About

ArtisanPack UI Accessibility is an open-source Laravel package that checks color contrast, picks readable text colors and audits your templates for WCAG issues — so you don't have to eyeball every color combination by hand.

A few years ago, I was building a client site where the client could pick a highlight color for buttons and banners from the admin panel. It seemed like a nice, simple feature until I realized I had no idea what color the text on those buttons should be, because I didn't know what color the background would be until the client chose it.

That's the problem I built ArtisanPack UI Accessibility to solve. It's a Laravel accessibility package focused on color, and it gives you a handful of helpers and commands for checking contrast, picking readable text colors and catching contrast problems before they reach production. In this post, I'll walk through what the package does, how to install it and how it fits into the rest of the ArtisanPack UI ecosystem.

Why color contrast gets tricky

Getting contrast right in a static design is fairly easy, since you can plug two colors into a contrast checker, make adjustments and move on. Things get harder when colors come from somewhere you don't control, like a database, a CMS setting, a user's theme preference or a client who changes their brand color six months after launch.

The Web Content Accessibility Guidelines (WCAG) ask for a contrast ratio of at least 4.5:1 for normal text at the AA level, and checking that by hand every time a color changes isn't realistic. What you really want is for your code to make that decision for you, which is exactly what this package does.

What ArtisanPack UI Accessibility is

ArtisanPack UI Accessibility is a PHP package that helps your web applications meet accessibility standards, with a particular focus on color contrast. It can tell you whether two colors pass WCAG, choose black or white text for any background, generate an accessible tint or shade of a color, audit your templates for contrast issues and build accessible color palettes from a single brand color.

It's also one of the building blocks of the ecosystem. The Forms package, for example, lists Accessibility as a dependency, so if you're using Forms, you're already using it.

The package requires PHP 8.3 or higher and Laravel 12 or 13. If you're still on PHP 8.2 or Laravel 11, you can pin to version ^2.1.2 until you're ready to upgrade.

Installation

You can install the package with Composer:

composer require artisanpack-ui/accessibility

The package registers itself with Laravel automatically, so the helpers are available as soon as Composer finishes. The Getting Started guide covers publishing the config file if you want to change the defaults.

Checking color contrast in Laravel

Most of the time, you'll use the package through four helper functions.

a11yGetContrastColor() takes a background color and returns the hex code for black or white, depending on which one gives you better contrast:

a11yGetContrastColor('#3b82f6'); // Returns '#000000' or '#FFFFFF'

a11yCSSVarBlackOrWhite() does the same thing but returns the word black or white, which is handy when you're setting a CSS custom property:

<div style="--text-color: ;">

a11yCheckContrastColor() checks whether two colors meet the 4.5:1 ratio and returns true or false, so you can validate a color pair before you save it:

if (! a11yCheckContrastColor($textColor, $backgroundColor)) {
    // Warn the user or pick a different color
}

generateAccessibleTextColor() is the most flexible of the four. By default, it returns black or white like a11yGetContrastColor(), but if you pass true as the second argument, it looks for the closest tint or shade of the background color that still passes. That gives you text that matches the color scheme instead of plain black or white:

generateAccessibleTextColor('blue-500');       // Black or white
generateAccessibleTextColor('blue-500', true); // An accessible shade of blue

You might have noticed that last example uses blue-500 instead of a hex code. The package understands Tailwind CSS color names along with hex, rgb() and hsl() values, so you can pass colors in whatever format your project already uses.

A real-world example: an accessible button component

Here's where these helpers really pay off. Say you want a button component that accepts any background color and always has readable text. Start by creating the component:

php artisan make:component Button

In the component class, accept a background color and let the package choose the text color:

public function __construct(string $bgColor = '#3b82f6')
{
    $this->bgColor   = $bgColor;
    $this->textColor = a11yGetContrastColor($this->bgColor);
}

Then apply both colors in the view:

<button 
    style="background-color: ; color: ;">
    
</button>

Now anyone on your team can drop in a button with any color, and the text will stay readable:

<x-button>Click Me</x-button>
<x-button bg-color="#dc2626">Delete</x-button>
<x-button bg-color="rgb(16, 185, 129)">Success</x-button>

[SCREENSHOT: The three buttons side by side with their contrast ratios labeled]

Running a Laravel accessibility audit

The helpers handle colors while your app is running, but you'll probably also want to know about contrast problems already sitting in your templates. That's what the a11y:audit-colors command is for. It scans your CSS and Blade files for color contrast issues and writes a report in JSON, HTML or Markdown:

php artisan a11y:audit-colors --format=html

By default, reports land in storage/app/a11y. You can change the WCAG level with --strictness (A, AA or AAA), point the scan at specific folders with --path and decide what counts as a failure with --fail-on.

That last option makes the command useful in continuous integration. The command exits with a code of 2 when it finds violations that match your policy, so you can fail a build on contrast problems the same way you would on a failing test. If you're working with an older codebase and can't fix everything at once, --fail-on=threshold:10 lets a few issues through while you chip away at them.

[SCREENSHOT: The HTML audit report]

Generating an accessible color palette

The other command, a11y:generate-palette, is one I find myself using at the start of client projects. Give it a primary brand color, and it builds a palette of tints and shades (nine by default) and checks them against WCAG:

php artisan a11y:generate-palette --primary=#0ea5e9

You can add a secondary color and accent colors, choose the WCAG level and export the result as Markdown, HTML or JSON. Running it before design work starts is a quick way to find out which versions of a client's brand color will actually work for text and which ones are better saved for backgrounds and decorative elements. If you need to go further, the package also includes dynamic theming tools for generating and validating full themes, which are covered in the Dynamic Theming docs.

Making it your own with hooks

Like the rest of the ecosystem, Accessibility uses the Hooks package so you can change how it behaves without forking anything. It currently provides three filters:

  • ap.accessibility.contrastThreshold lets you adjust the contrast ratio required for each WCAG level.
  • ap.accessibility.contrastColorMap lets you add or replace color names in the Tailwind color map.
  • ap.accessibility.textColorGenerated gives you the final say on any generated text color.
    The color map filter is the one most projects will reach for first, since it lets you use your own brand color names with every helper:
addFilter('ap.accessibility.contrastColorMap', function (array $map) {
    $map['brand-primary'] = '#123456';

    return $map;
});

After that, generateAccessibleTextColor('brand-primary') works just like it would with a built-in Tailwind color.

Optional AI features

If you also have the ArtisanPack UI AI package installed, Accessibility includes three AI agents that can help with the parts of accessibility that are harder to check with code. The content analysis agent looks for problems like unclear link text and vague headings, the ARIA suggestion agent recommends roles, states and keyboard interactions for custom components, and the contrast explanation agent explains why a color pair fails and suggests alternatives that pass.

Each agent comes with ready-made Livewire, React and Vue components, and each one stays off until you turn it on, so installing the package won't change anything about your app until you decide to use them.

What this package doesn't cover

I want to be upfront about the scope here. ArtisanPack UI Accessibility handles color really well, but color is only one part of building an accessible site. It won't check whether your images have alt text, whether your forms are labeled or whether someone can use your site with only a keyboard.

Automated tools in general only catch a portion of accessibility issues, and keyboard testing and screen reader testing still matter. I think of this package as one layer in the process, taking a whole category of problems off your plate so you can spend your time on the parts that need a human.

Get started

You can install ArtisanPack UI Accessibility with composer require artisanpack-ui/accessibility and start using the helpers right away.

The maker's dispatch

One good build email a week

New blocks, patterns and tips. No spam, unsubscribe anytime.

We never share your email. Unsubscribe anytime.