Giter Site home page Giter Site logo

canbax / contra-color Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 101 KB

For a given color, find a contrasting color (not optimal).

Home Page: https://canbax.github.io/contra-color/

License: MIT License

JavaScript 14.43% TypeScript 85.57%
color colors contrast contrast-ratio contrast-finder

contra-color's Introduction

contra-color

For a given color, find a contrasting color (not optimal). Try it NOW!

Sample Screen Shot

Usage

Use npm

Run command npm i contra-color

import ESM module

import { getContrastingColor, getContrast } from 'contra-color';

import CommonJS module

const contra = require("contra-color");
console.log(contra.getContrastingColor("#0f0f0f"));

import UMD module

Add <script src="https://unpkg.com/contra-color/dist/contra-color.umd.js"></script> to your HTML file.

Then you can call API functions like contraColor.getContrastingColor('#f0f0f0')

API

function getContrastingColor(c: string, isLinearLuminance?: boolean, contrastDiff?: number): IContraColor

  • get a contrasting color for a given color
  • If isLinearLuminance is true, it will use the linear luminance formula. Otherwise, it will use power curve formula.
  • Set contrastDiff as 0 to find color with maximum contrast. If you pass a number in range (0,20], it will find a color with a the smallest contrast that is greater then contrastDiff

function getContrast(c1: string, c2: string, isLinearLuminance?: boolean): number

  • Find contrast of two colors.
  • If you pass isLinearLuminance, it will calculate luminance with power curve formula.

Problem

What is the best font color for a certain background color? AFAIK, this question doesn't have a scientifically proven answer for now (29 October 2021).

Background

World Wide Web Consortium (W3C) publishes some guidelines like Web Content Accessibility Guidelines (WCAG). According to these, contrast ratio is defined as

(L1 + 0.05) / (L2 + 0.05)

where L1 is the relative luminance of the lighter of the colors, and L2 is the relative luminance of the darker of the colors. Mozilla Developer Network (MDN) provides explanations about calculating relative luminance.

In the below codes, luminance function defines the relative luminance. If you give RBG values which are in range [0,255], the function will return a luminance value.

function luminance(R: number, G: number, B: number) {
  const r = sRGBtoLin(R);
  const g = sRGBtoLin(G);
  const b = sRGBtoLin(B);
  return r * 0.2126 + g * 0.7152 + b * 0.0722;
}

function sRGBtoLin(colorChannel: number) {
  colorChannel = colorChannel / 255;
  if (colorChannel <= 0.04045) {
    return colorChannel / 12.92;
  } else {
    return Math.pow((colorChannel + 0.055) / 1.055, 2.4);
  }
}

According to the above definitions of contrast ratio and relative luminance, the contrast ratio can be 1 at minimum, 21 at maximum.

Human Perception of Luminance

According to MDN, human perception is roughly a power curve. For this reason, we also let the user use non-linear luminance values.

Too High Contrast Ratio

According to some reseearch too high contrast difference is not that good.

Among the contrast revisions, provide guidance on excessive contrast โ€” too high of a contrast causes reading problems and eye strain as well.

source: w3c/wcag#695 (comment)

Solution

Brute Force

Brute force search for finding the best contrasting color needs three for loops for each color channel. This means 256256256 = 16777216 lines of execution. This takes time!.

Greedy Search

We make a greedy search. We iterate over each channel just once. We start from the most important channel to the least important channel. The most important channel is green since its coefficient is the largest 0.7152 in the equation, the least important channel is blue since its coefficient is the smallest 0.0722 in the equation. Since greedy search does not give an optimal solution, we also start from different colors like black, white and, a random color.

Limiting Contrast Difference

To limit the contrast ratio, we start from the current color and then we diverge. As mentioned before too high contrast might not be ideal for readability.

Comments and Discussion

  • The algorithm might not give always the color with maximum contrast.
  • If you specify a contrastDiff again it might not give a color with a high contrast.
  • There are some discussions regarding Contrast Ratio Math. So even the formulas accepted by W3C are questionable and can be changed in the future.
  • For me, it is very interesting that this is an active research question.

contra-color's People

Contributors

canbax avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

contra-color's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.