tailwindcss-autocontrast
v0.0.3
Published
Tailwind CSS plugins for automatic contrast color generation
Readme
tailwindcss-autocontrast
Tailwind CSS plugin for automatic contrast color generation. Automatically generates high-contrast text colors (on-* variants) for your color scales.
Features
- ✅ Automatic contrast colors: Generates
on-{color}-{shade}CSS variables for optimal text contrast - ✅ WCAG compliant: Calculates contrast ratios to ensure readable text
- ✅ Modern color spaces: Supports OKLCH, HSL, RGB, HEX, and more
- ✅ Zero configuration: Works out of the box with your existing color scales
Installation
pnpm add tailwindcss-autocontrastUsage
Add the plugin to your CSS file:
@import "tailwindcss";
@plugin "tailwindcss-autocontrast";Define your color scales in CSS:
:root {
--accent-50: oklch(0.8706 0.0701 241.56);
--accent-500: oklch(0.5 0.15 241);
--accent-900: oklch(0.3 0.1 241);
}The plugin automatically generates contrast colors:
:root {
/* Your colors */
--accent-50: oklch(0.8706 0.0701 241.56);
--accent-500: oklch(0.5 0.15 241);
--accent-900: oklch(0.3 0.1 241);
/* Auto-generated */
--on-accent-50: black;
--on-accent-500: white;
--on-accent-900: white;
}Configuration
The plugin works by scanning your CSS files for color scales. You can configure the file location:
@plugin "tailwindcss-autocontrast" {
cssFile: "./styles/globals.css",
source: ["./src/**/*.css"],
logLevel: "warn"
}Options
cssFile(string): Path to your root CSS filesource(string | string[]): Directories or files to search for CSSlogLevel("warn" | "silent"): Control logging output
How it Works
- Scans your CSS files for color scale variables (e.g.,
--accent-50,--danger-200) - Parses color values (supports all modern formats including OKLCH)
- Calculates luminance and contrast ratios
- Generates optimal text colors (black or white) for maximum readability
- Injects
--on-{color}-{shade}variables into your CSS
Testing
Run tests:
pnpm testWatch mode:
pnpm test:watchLicense
MIT
