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

text-contrast

v1.0.2

Published

JavaScript Library for Setting Text Color According to Background-Color

Downloads

705

Readme

Text Contrast

Automatically adjust text color based on background color to improve readability. Also provides methods for determining if an rgb, rgba, or hex color code is light or dark.

Basic Example

<style>
    .bg {
        display: inline-block;
        float:   left;
        padding: 25px;
        border:  1px solid black;
        color:   black;
    }

    .bg-green  { background-color: green;  }
    .bg-yellow { background-color: yellow; }
    .bg-purple { background-color: purple; }
    .bg-red    { background-color: red;    }
    .bg-orange { background-color: orange; }
    .bg-black  { background-color: black;  }
    .bg-white  { background-color: white;  }
</style>

<script src="/js/text-contrast.min.js"></script>

<div class="bg bg-green">Some Text</div>
<div class="bg bg-yellow">Some Text</div>
<div class="bg bg-purple">Some Text</div>
<div class="bg bg-red">Some Text</div>
<div class="bg bg-orange">Some Text</div>
<div class="bg bg-black">Some Text</div>
<div class="bg bg-white">Some Text</div>

<script>
    'use strict';

    (() => {

        // Clean up the contrast of all ".bg" elements
        const light = '#EFEFEF',
              dark  = '#232323';

        textContrast.fix('.bg', light, dark);

        // If light/dark colors are omitted, 'white' and 'black' are used
        // textContrast.fix('.bg');

    })();
</script>

textContrast.fix(el[, light[, dark]])

This method will check the background-color of each element found and set it's color to light or dark as appropriate. el parameter can be an element, array of elements, selector, or array of selectors. Does not look at background-color of parent elements.

textContrast.isLight(color)

Returns true if color is considered light, false otherwise. Color format can be hex, rgb, or rgba (note that rgba alpha channels are not considered in determining lightness). Throws error if color cannot be parsed and converted to an RGB value.

Examples

    'use strict';

    // Hex codes can be 3 or 6 characters
    // Hex should match pattern /^#?([0-9a-fA-F]{3}){1,2}$/
    textContrast.isLight('#fff');    // true
    textContrast.isLight('#efefef'); // true

    // Leading octothorpe not required
    textContrast.isLight('fafafa'); // true

    // Can also use rgb/rgba values
    textContrast.isLight('rgb(255,255,255)');      // true
    textContrast.isLight('rgba(255,255,255, .5)'); // true
    // Note that transparency is not considered

    textContrast.isLight('#000'); // false

textContrast.isDark(color)

Returns true if color is considered dark, false otherwise. Color format can be hex, rgb, or rgba (note that rgba alpha channels are not considered in determining lightness). Throws error if supplied color cannot be parsed and converted to an RGB value.

Examples

    'use strict';

    // Hex codes can be 3 or 6 characters
    textContrast.isDark('#000');    // true
    textContrast.isDark('#232323'); // true

    // Leading octothorpe not required
    textContrast.isDark('333333'); // true

    // Can also use rgb/rgba values
    textContrast.isDark('rgb(0,0,0)');      // true
    textContrast.isDark('rgba(0,0,0, .5)'); // true
    // Note that transparency is not considered

    textContrast.isDark('#000'); // true

textContrast.isLightOrDark(color)

Returns 'light' if color is considered light, 'dark' if the color is dark, undefined otherwise.

Examples

    'use strict';

    // Hex codes can be 3 or 6 characters
    textContrast.isLightOrDark('#000');    // 'dark'
    textContrast.isLightOrDark('#232323'); // 'dark'

    // Leading octothorpe not required
    textContrast.isLightOrDark('efefef'); // 'light'

    // Can also use rgb/rgba values
    textContrast.isLightOrDark('rgb(0,0,0)'); // 'dark'
    textContrast.isLightOrDark('rgba(255,255,255, .5)'); // 'light'
    // Note that transparency is not considered

    textContrast.isLightOrDark('#000'); // 'dark'