color-contrast-finder
v1.0.2
Published
The Color Contrast Finder is a library that automatically selects text colors that contrast with a given color.
Downloads
3,226
Maintainers
Readme
Color Contrast Finder
The Color Contrast Finder is a JavaScript library designed to automatically select text colors that provide sufficient contrast with a given background color. The library supports various color formats, such as HEX, RGB, and RGBA, and also allows for user-defined thresholds and customizable default color settings. In addition, it now supports CSS color names, making it more versatile and easy to use.
https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
Features
- Support for HEX, RGB, and RGBA color formats
Easily handle different color formats for flexible color manipulation. - Color contrast calculation with transparency (Alpha)
Accurately calculates contrast by considering alpha transparency, ensuring that the selected text color is always readable. - Support for user-defined brightness thresholds
Customize brightness thresholds to determine when to use high or low contrast colors. - Customizable high and low brightness colors
Define specific colors to use in high or low brightness scenarios for better visual clarity. - CSS Color Names Support
Conveniently use named colors defined in CSS, such as "red", "blue", "green", etc., for quick color application. - Easy to use and flexible settings
The library is designed to be simple to implement while providing flexibility in customization.
Installation
# with npm
$ npm i color-contrast-finder
# with yarn
$ yarn add color-contrast-finder
# with pnpm
$ pnpm add color-contrast-finder
Usage
Include the library in your project and use it to determine the best text color for any given background color, ensuring that your web content is accessible and easy to read.
// module
import { findContrastColor } from 'color-contrast-finder';
/**
* `options.color` types:
* - `#f00` // Short HEX code (no alpha), interpreted as #ff0000 (RGB: 255, 0, 0)
* - `#f00f` // Short HEX code with alpha, interpreted as #ff0000 with 100% opacity (RGBA: 255, 0, 0, 1)
* - `#ffffff` // Full HEX code (no alpha), interpreted as RGB: 255, 255, 255
* - `#ffffffff` // Full HEX code with alpha, interpreted as RGBA: 255, 255, 255, 1
* - `rgb(255, 255, 255)` // RGB function, interpreted as RGB: 255, 255, 255
* - `rgba(255, 255, 255, 0.3)` // RGBA function, interpreted as RGBA: 255, 255, 255, 0.3
* - `'red'` // CSS color name, interpreted as the corresponding color (e.g., 'red' = RGB: 255, 0, 0)
*/
const backgroundColor = '#3498db';
const options = {
color: backgroundColor, // The input color (HEX, RGB, RGBA, or CSS color name)
threshold: 0.5, // The brightness threshold to decide between highColor and lowColor (default: 0.5)
highColor: 'black', // Text color to use when brightness is above the threshold (default: #000000)
lowColor: 'white' // Text color to use when brightness is below the threshold (default: #FFFFFF)
};
const contrastColor = findContrastColor(options);
console.log(contrastColor); // Outputs 'black' or 'white' depending on the contrast
// umd
/**
* html code
* <script type="text/javascript" src="{path}/color-contrast-finder.umd.cjs"></script>
*/
const backgroundColor = '#3498db';
const options = {
color: backgroundColor,
threshold: 0.5,
highColor: 'black',
lowColor: 'white'
};
const contrastColor = window['color-contrast-finder'].findContrastColor(options);
console.log(contrastColor); // Outputs 'black' or 'white' depending on the contrast
License
MIT.
TODO
- [x] Function findContrastColor
- [x] Library build
- [x] Changesets setup
- [x] CI (lint)
- [x] CD (npm publish)
- [x] NPM publish
How to use publish (About me)
# 1. pnpm changeset
$ pnpm cs
# 2. Input next version
# 3. New file created in the .changeset folder
# 4. Commit & push new files
# 5. Automatically generate, merge, and publish PRs with CD jobs running.