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 🙏

© 2024 – Pkg Stats / Ryan Hefner

contrastrast

v0.2.0

Published

A lightweight tool that parses color strings and recommends text contrast based on WCAG Standards

Downloads

25

Readme

contrastrast

npm version

constrastrast

A lightweight tool that parses color strings and recommends text contrast based on WCAG Standards

Installation

Install constrastrast by running one of the following commands:

npm install --save constrastrast@latest

yarn add constrastrast@latest

pnpm install --save constrastrast@latest

How it works

constrastrast takes a given background color as a string in either HEX, HSL, or RGB format, and (by default) returns "dark" or "light" as a recommended text variant for that given background color

For example, you may use it like this:

import { textContrastForBGColor } from "contrastrast";

const MyColorChangingComponent = (backgroundColor: string) => {
    return <div style={{ backgroundColor }} className={textContrastForBGColor(backgroundColor) === "dark" : "text-black" : "text-white"}>
        This text is readable no matter what the background color is!
    </div>
}

Supported Color Formats

constrastrast supports the following color string formats:

HEX

HEX Notation in either 3 or 6 length format

examples

#ad1232

ad1232

#ada

ada

RGB

Standard RGB notation

examples

rgb(100,200, 230)

rgb(5, 30, 40)

HSL

HSL Notation with or without the symbol markers

examples

hsl(217°, 90%, 61%)

hsl(72°, 90%, 61%)

hsl(121deg, 90%, 61%)

hsl(298, 90, 61)

Alpha Formats

Currently contrastrast doesn't support alpha formats and will log an error and return the default value

Unhandled Formats

If an unhandled string is passed, by default contrastrast will log an error and return the default value ("dark")

Options

textContrastForBGColor takes an ContrastrastOptions object as an optional second parameter, it currently has the following configuration options:

type ContrastrastOptions = {
  fallbackOption?: "dark" | "light"; // Defaults to "dark" if not specified
  throwErrorOnUnhandled?: boolean; // Throws an error instead of returning the `fallbackOption`.  Defaults to `false` if not specific
};

Contributing

Happy for any and all contributions. Please note the project uses pnpm and I prefer to have git commits formatted with gitmoji-cli