Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

font-color-contrast

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

font-color-contrast

JavaScript module to use black or white font according to the given background color

  • 9.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
17K
increased by1.36%
Maintainers
1
Weekly downloads
 
Created
Source

font-color-contrast

npm CI Pipeline Build Status Coverage Status Scrutinizer Code Quality Code Climate Known Vulnerabilities

NodeJS module to select black or white to a font according to the background.

Installation

$ npm i font-color-contrast

Usage

You can use the module 3 ways:

  • with an hexadecimal string color, i.e. fontColor("#f7d4fc")
  • with a RGB separated by comma color (number or string), i.e. fontColor(223, 0, "255")
  • with a RGB array color (number or string), i.e. fontColor([223, 0, "255"])

If any other format is sent, the function will respond with white by default.

var fontColorContrast = require('font-color-contrast');

var lightGreenBackground: "#b6e9be";
var paleYellowBackground = ["255", "246", "199"];
var darkBlueRedBackground = 20;
var darkBlueGreenBackground = 85;
var darkBlueBlueBackground = 91;

var fontColor1 = fontColorContrast(lightGreenBackground);
// fontColor will be a string for black hexadecimal color: "#000000"

var fontColor2 = fontColorContrast(paleYellowBackground);
// fontColor will be a string for black hexadecimal color: "#000000"

var fontColor3 = fontColorContrast(
  darkBlueRedBackground,
  darkBlueGreenBackground,
  darkBlueBlueBackground
);
// fontColor will be a string for white hexadecimal color: "#ffffff"

Tests

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

Examples

WebSafe colors

Sample with WebSafe colors

CSS colors

Sample with CSS colors

Version history

0 -> 8.1.1

JavaScript version, accepting strings for RGB

9.0.0

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.

Keywords

FAQs

Package last updated on 30 Oct 2021

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc