npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@roussos/contrast-checker

v0.2.6

Published

Given an image it returns a color with the best contrast ratio.

Readme

Contrast Checker

ColorChecker is a JavaScript utility for analyzing and comparing colors in relation to DOM elements. It computes the average color of a DOM element’s subregion (e.g. a child element) and matches it to the closest color from a provided palette based on luminance similarity.

Features

  • Computes the average color of the background image within the region of a DOM element.
  • Compares the average color to a given list of colors and finds the color with the most contrast.
  • Debug mode appends the calculated average color to the DOM and logs the results.
  • Lightweight and easy to integrate.

Demo

Check out the demo here

Installation

You can include the script in your project by installing it from npm:

npm install @roussos/contrast-checker

Usage

Initialization

Create a new instance of ColorChecker and set the parent and child elements.

  • If you initialize the instance without the parent option, it will try to find the closest element containing an image.

  • You can also initialize it without any options, but in that case, you must call the updateOptions method manually.

const checker = new ColorChecker({
    child: '#childElement',
    parent: document.querySelector('#parentElement')
})

Compare Colors

Use the compare method to find the closest matching color from an array of colors.

The colors in the array MUST be in hexadecimal form.

const closestColor = checker.compare(['#ffffff', '#d23d11', '#02004b']);
console.log('Closest Color:', closestColor);

Update Elements

Use the updateOptions method to change the provided element options.

checker.updateOptions({
    child: '#newChildElement',
    parent: document.querySelector('#newParentElement')
})

Debug Mode

Enable debug mode to append the calculated average color to the DOM and log the results.

const closestColor = checker.compare(['#ffffff', '#d23d11', '#02004b'], true);

API

ColorChecker(options)

Creates a new instance of ColorChecker.

Parameters:

Options | Type | Description
-------- | ------------------- | ------------------------------------------------------------------------------------------------------------ child | HTMLElement, String | The child element whose background is analyzed. parent | HTMLElement, String | The parent element behind the child. Must be an element or an element containing one.


compare(color, debug)

Finds the closest matching color from the provided array.

Parameters: Options | Type | Description
-------- | ------------------- | ------------------------------------------------------------------------------------------------------------ color | String[] | Array of hexadecimal colors to compare. debug | Boolean | Optional. If true, appends the calculated average color to the DOM. Default is false.

Returns: The color from the array that has the most contrast to the average color of the parent element.


compareAsync(color, debug)

Same as compare but it returns a promise with the computed color.


updateOptions(options)

Updates the instance's options with new Child and Parent elements.

Parameters: Options | Type | Description
-------- | ------------------- | ------------------------------------------------------------------------------------------------------------ child | HTMLElement, String | The child element whose background is analyzed.
parent | HTMLElement, String | The parent element behind the child. Must be an element or an element containing one.

Returns: The ColorChecker instance, allowing method chaining.


Example

import ColorChecker from '@roussos/contrast-checker';

const checker = new ColorChecker({
    child: '#lorem',
})

document.querySelector('#lorem').style.color = checker.compare([
    '#ffffff', 
    '#d23d11', 
    '#02004b'
])

License

This project is licensed under the MIT License.