Skip to content

check if color is light or dark to change the text color for instance

Notifications You must be signed in to change notification settings

amans199/the-simplest-color-checker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Contributors Forks Stargazers Issues MIT License LinkedIn


Check if the color is light or Dark in a second

This (text inside the boxes are all the same color because you can't know what is the background of the dynamic box will be) :


dynamic_backgrounds_before_using_color_checker


will turn into this : (the color of the text inside the boxes are changing depending on the background of the dynamic boxes )

dynamic_backgrounds_after_using_color_checker


see example sourcecode


Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents

Installation

npm install the-simplest-color-checker --save
import {checkColor} from 'the-simplest-color-checker'

Usage


Use it as below

1- import the checkColor module into your project and add the selector as following :

import {checkColor} from 'the-simplest-color-checker'
checkColor({
  selector:".colors_box" //add any selector,
  optimize_text_color: true, //change text color depending on the background
  ifDark:{
    textColor:"#f00"
  },
  ifLight:{
    textColor:"#00f"
  }
});

2- Add data-color to all elements with the value of the color you wanna test;

data-color="#000"


the previous line will result with a data-brightness="dark"

Examples

This :

    <span data-color="#000" class="colors_box"></span>
    <span data-color="#fff" class="colors_box"></span>
    <span data-color="#f00" class="colors_box"></span>

will result :

    <span data-color="#000" class="colors_box" data-brightness="dark"></span>
    <span data-color="#fff" class="colors_box" data-brightness="light"></span>
    <span data-color="#f00" class="colors_box" data-brightness="light"></span>


Using the-simplest-color-checker in React app:


import { Component } from 'react';
import {checkColor} from 'the-simplest-color-checker'
class App extends Component {
  componentDidMount(){
    checkColor({
      selector: ".colors_box", //add any selector
      optimize_text_color: true, //change text color depending on the background,
      ifDark:{
        textColor:"#fff"
      },
      ifLight:{
        textColor:"#000"
      }
    })
  }
      render() {
        return (
          <div className="App">
              <div className="boxes">
                <span data-color="#000" className="colors_box">color box</span>
                  <span data-color="#fff" className="colors_box">color box</span>
                  <span data-color="#0f0" className="colors_box">color box</span>
                  <span data-color="#00f" className="colors_box">color box</span>
                  <span data-color="#f00" className="colors_box">color box</span>
              </div>
          </div>
        );
    }
}

export default App;


Using the-simplest-color-checker in Vue app:

import { checkColor } from "the-simplest-color-checker";
export default {
 mounted() {
    checkColor({
      selector: ".colors_box", //add any selector
      optimize_text: true,
      ifDark:{
        textColor:"#fff"
      },
      ifLight:{
        textColor:"#000"
      }
    });
  },
};



See the [open issues](https://github.com/amans199/the-simplest-color-checker/issues) for a list of proposed features (and known issues).

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Ahmed Mansour - @amans199 - ahmed.ouda1997@gmail.com

Project Link: https://github.com/amans199/the-simplest-color-checker

About

check if color is light or dark to change the text color for instance

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published