šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Sign inDemoInstall
Socket

wcag-contrast-utils

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wcag-contrast-utils

WCAG Contrast Utils is a TypeScript library for calculating color contrast ratios and checking WCAG 2.1 compliance. Ensure your web designs meet accessibility standards with easy-to-use functions for developers and designers.

1.0.1
latest
Source
npm
Version published
Weekly downloads
1.5M
690603.57%
Maintainers
0
Weekly downloads
Ā 
Created
Source

WCAG Contrast Utils

Description

WCAG Contrast Utils is a TypeScript library for calculating the contrast ratio between two colors and checking whether it meets WCAG 2.1 accessibility standards.

This library is useful for developers and designers who want to ensure that their websites or applications comply with accessibility standards, improving readability and usability for all users, including those with visual impairments.

Installation

You can install the library via npm or yarn:

npm install wcag-contrast-utils

or

yarn add wcag-contrast-utils

Usage

Importing

import { contrastRatio, passesWCAG } from "wcag-contrast-utils";

Calculating Contrast Ratio

The contrastRatio function calculates the contrast ratio between two colors, which can be provided as hexadecimal strings or RGB arrays.

const ratio1 = contrastRatio("#000000", "#ffffff"); // 21
const ratio2 = contrastRatio([0, 0, 0], [255, 255, 255]); // 21
const ratio3 = contrastRatio("#777777", "#ffffff"); // 4.47

Checking WCAG Compliance

The passesWCAG function takes a contrast ratio value and returns the highest WCAG compliance level met.

console.log(passesWCAG(2));   // "Not WCAG Compliant"
console.log(passesWCAG(3));   // "WCAG 2 AA Compliant Large Text"
console.log(passesWCAG(4.5)); // "WCAG 2 AAA Compliant Large Text"
console.log(passesWCAG(7));   // "WCAG 2 AAA Compliant"

WCAG 2.1 - Contrast Requirements

According to WCAG 2.1 guidelines, the required contrast ratios are:

  • AA (Normal text): 4.5:1
  • AA (Large text): 3:1
  • AAA (Normal text): 7:1
  • AAA (Large text): 4.5:1

Contributing

If you want to contribute to the project, feel free to open an issue or submit a pull request!

License

This project is released under the MIT license.

Keywords

WCAG

FAQs

Package last updated on 02 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts