
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
font-color-contrast
Advanced tools
JavaScript module to use black or white font according to the given background color
JavaScript module to select black or white to a font according to the background for the best possible contrast.
Calculates the best color (black or white) to contrast with the passed color using the algorithm from https://alienryderflex.com/hsp.html
$ npm i font-color-contrast
You can use the module 4 ways, with an optional parameter (from 0 to 1) for contrast threshold:
Example:
import fontColorContrast from 'font-color-contrast'
const myStringBg = '#00CC99'
const fontColor = fontColorContrast(myStringBg) // '#000000'
const myNumberBg = 0x00cc99
const fontColor = fontColorContrast(myNumberBg) // '#000000'
const red = 0
const green = 204
const blue = 153
const fontColor = fontColorContrast(red, green, blue) // '#000000'
const myArrayBg = [0, green: 204, blue: 153]
const fontColor = fontColorContrast(myArrayBg) // '#000000'
Optionally, you can pass the contrast threshold (defaults to 0.5). This will affect the resulting font color. The use of this parameter is to control the WCAG conformance levels.
import fontColorContrast from 'font-color-contrast'
fontColorContrast('#645466', 0) // '#000000'
fontColorContrast('#645466', 1) // '#ffffff'
The 4 possible overflows are described next:
/**
* Analyses the color (normally used in the background) and retrieves what color (black or white) has a better contrast.
* @param hex The hex color number that must be a valid hexadecimal color number, with 6 characters, to work correctly.
* @param threshold Contrast threshold to control the resulting font color, float values from 0 to 1. Default is 0.5.
* @example fontColorContrast(0XF3DC56) === fontColorContrast(15981654)
*/
function fontColorContrast (hex: number, threshold?: number): '#ffffff' | '#000000'
/**
* Analyses the color (normally used in the background) and retrieves what color (black or white) has a better contrast.
* @param hex The hex color string that must be a valid hexadecima color number to work correctly. Works with or without '#', with 3 or 6 color chars.
* @param threshold Contrast threshold to control the resulting font color, float values from 0 to 1. Default is 0.5.
* @example fontColorContrast('00FFDD') === fontColorContrast('0FD') === fontColorContrast('#00FFDD') === fontColorContrast('#0FD')
*/
function fontColorContrast (hex: string, threshold?: number): '#ffffff' | '#000000'
/**
* Analyses the color (normally used in the background) and retrieves what color (black or white) has a better contrast.
* @param red The red portion of the color. Must be a number between 0 and 255.
* @param green The green portion of the color. Must be a number between 0 and 255.
* @param blue The blue portion of the color. Must be a number between 0 and 255.
* @example fontColorContrast('00', 'F3', D8) === fontColorContrast(0, 243, 216) === fontColorContrast(0x0, 0xF3, 0xd8).
* @param threshold Contrast threshold to control the resulting font color, float values from 0 to 1. Default is 0.5.
*/
function fontColorContrast (red: number, green: number, blue: number, threshold?: number): '#ffffff' | '#000000'
/**
* Analyses the color (normally used in the background) and retrieves what color (black or white) has a better contrast.
* @param redGreenBlue Array with red, green and blue. Each value must be a number between 0 and 255.
* @param threshold Contrast threshold to control the resulting font color, float values from 0 to 1. Default is 0.5.
* @example fontColorContrast(['00', 'F3', 'D8']) === fontColorContrast([0, 243, 216]) === fontColorContrast([0x0, 0xF3, 0xd8])
*/
function fontColorContrast(redGreenBlue: number[], threshold?: number): '#ffffff' | '#000000'
Tests made using Jest to check color format possibilities and contrast, including all CSS colors and WebSafe (90's stuff) colors as shown in the image below
These examples are using the default 0.5 threshold.
JavaScript version, accepting strings for RGB
TypeScript version.
Only numbers are now accepted as params when using array or RGB, because it was impossible to know if the string was decimal or hexadecimal. Accepting only numbers we can be sure the correct values are being used to calculate the contrast.
Updated algorithm from https://alienryderflex.com/hsp.html with new thresholds for better contrast.
Included the optional threshold parameter (thanks, franciscohanna92).
FAQs
JavaScript module to use black or white font according to the given background color
The npm package font-color-contrast receives a total of 16,439 weekly downloads. As such, font-color-contrast popularity was classified as popular.
We found that font-color-contrast demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.