Giter VIP home page Giter VIP logo

colourbrightness2.js's Introduction

colourBrightness2.js

colourBrightness2.js

Fork of colourBrightness.js, the Colour Brightness 2 offers additional features to the plugin.

A lightweight and easy-to-use jQuery plugin that determines if the background colour of an element is light or dark.

Demo

Look Colour Brightness 2 :: Demo

Install

NPM
npm install --save colourBrightness2
Bower
bower install --save colourBrightness2

Additional resources:

  • isDark: Performs a callback, if it is dark.
  • isLigth: Performs a callback, if it is clear.
  • then: Performs a callback for both cases, receiving true dark case and false clear case.
  • setClass: The classic! Adds the light and dark class to the element.
<script src="path/to/jquery.min.js"></script>
<script src="path/to/jquery.colourBrightness2.js"></script>
<script>
  $(document).ready(function(){
    $('.lightbox').colourBrightness2().setClass();// .dark, .light
    
    $('.lightbox').colourBrightness2().isDark(function(){
      alert('It\'s dark!')
    });
    
    $('.lightbox').colourBrightness2().isLight(function(){
      alert('It\'s light!')
    });
    
    $('.lightbox').colourBrightness2().then(function(isDark){
      if(isDark){
        alert('It\'s dark!')
      }
      else{
        alert('It\'s light!')  
      }
    });
    
    /**
    * check color
    **/
    $.colourBrightness2("#FFFFFF").then(function(isDark){
      if(isDark){
        alert('It\'s dark!')
      }
      else{
        alert('It\'s light!')  
      }
    })
  });
</script>

Check color directly

Using $.colourBrightness2 you can check the color without depending on an element, passing directly as a parameter. You can use all methods.

  $.colourBrightness2("#FFFFFF").then(function(isDark){
        if(isDark){
          alert('It\'s dark!')
        }
        else{
          alert('It\'s light!')  
        }
      })

.then(cb)

Execute a callback passing true to dark and false to light as parameter

.isDark(cb), .isLight(cb)

Executes a callback if the dark (in Dark) or clear (in isLight)

.setClass()

It'll grab the background colour of that element and uses a small algorithm to determine the brightness of the colour and will add the class light if the colour is light and dark if the colour is dark. From there, you can add what colour you'd like to display depending on the background in your CSS.

// Dark text
.light {
  color: #000;
}

// Light text
.dark {
  color: #fff;
}

colourbrightness2.js's People

Watchers

Philippe Assis avatar

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.