Git Product home page Git Product logo

textcontrast's Introduction

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'

textcontrast's People

Contributors

brycejech avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

textcontrast'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.