Hex to css filter

hex-to-css-filter

Please check our contributing.md to know more about setup and how to contribute.

Setup and installation

Make sure that you are using the NodeJS version is the same as .nvmrc file version. If you don’t have this version please use a version manager such as nvm or n to manage your local nodejs versions.

Please make sure that you are using NodeJS version 6.10.2

Assuming that you are using nvm , please run the commands inside this folder:

$ nvm install $(cat .nvmrc); # install required nodejs version $ nvm use $(cat .nvmrc); # use nodejs version

In Windows, please install NodeJS using one of these options:

Via NVM Windows package: Dowload via this link. After that, run the commands:

$ nvm install $(cat .nvmrc); # install required nodejs version $ nvm use $(cat .nvmrc); # use nodejs version
$ choco install nodejs.install -version 6.10.2

Install yarn

We use yarn as our package manager instead of npm

After that, just navigate to your local repository and run

$ yarn install && yarn husky:install

Demo

Run the tests

Run the build

$ yarn build # run the tests

Run the bundlesize check

$ yarn bundlesize # run the tests

Run the code lint

Usage

Important.

Please make sure the background of the element is #000 for better performance and color similarity.

The reason for this is because all the calcs done by the library to generate a CSS Filter are based on the color #000

Using default options

import  hexToCSSFilter > from 'hex-to-css-filter'; const cssFilter = hexToCSSFilter('#00a4d6'); console.log(cssFilter);

Overriding default options

You can override the default options by passing a second parameter into hexToCSSFilter method. You can also use HexToCssConfiguration for type support on it.

import  hexToCSSFilter, HexToCssConfiguration > from 'hex-to-css-filter'; const config: HexToCssConfiguration =  acceptanceLossPercentage: 1, maxChecks: 10, >; const cssFilter = hexToCSSFilter('#00a4d6', config); console.log(cssFilter); // Calling different colors to create CSS Filters [ hexToCSSFilter('#FFF'), hexToCSSFilter('#000'), hexToCSSFilter('#802e1c'), hexToCSSFilter('#00a4d6'), hexToCSSFilter('#FF0000'), hexToCSSFilter('#173F5E'), hexToCSSFilter('#24639C'), hexToCSSFilter('#3CAEA4'), hexToCSSFilter('#F6D55C'), hexToCSSFilter('#ED553C'), ].forEach(cssFilter =>  console.log(`\n$cssFilter.hex>-[$cssFilter.rgb>]: $cssFilter.filter>`); >);

It returns an object with the values:

  • cache : returns a boolean to confirm if value was previously computed and is coming from local memory cache or not;
  • called : how many times the script was called to solve the color;
  • filter : CSS filter generated based on the HEX color;
  • hex : the received color;
  • loss : percentage loss value for the generated filter;
  • rgb : HEX color in RGB;
  • values : percentage loss per each color type organized in RGB: red , green , blue , h , s , l . Used for debug purposes — if needed;

Options

  • acceptanceLossPercentage : Acceptable color percentage to be lost. Default: 5 ;
  • maxChecks : Maximum checks that needs to be done to return the best value. Default: 10 ;
  • forceFilterRecalculation : Boolean value that forces recalculation for CSS filter generation. Default: false ;

Removing memory cache

In some cases the memory cache is quite handy. However, it doesn’t need to stored after called in some cases. If you’re using it in some frontend libraries/frameworks, have that in memory can become an issue.

In order to solve that, you can now use the function clearCache to remove the memory cache. The method can receive the stored hex color. In this case, only the received key will be removed. E.G.

// Creating CSS filters for `#24639C` and `#FF0000` // They memory cache stored is based on the received hex value const [firstResult, secondResult, thirdResult, forthResult] = [ hexToCSSFilter('#24639C',  forceFilterRecalculation: false > as HexToCssConfiguration), hexToCSSFilter('#24639C',  forceFilterRecalculation: false > as HexToCssConfiguration), hexToCSSFilter('#FF0000',  forceFilterRecalculation: false > as HexToCssConfiguration), hexToCSSFilter('#FF0000',  forceFilterRecalculation: false > as HexToCssConfiguration), ]; // . // ✨ Here is the place where the magic happens in your App ✨ // . // Removing the memory cache only for `#24639C` // It means that `#FF0000` is still cached. // It's quite handy in scenarios of colors that are called for several times, // Having other ones called twice or thrice clearCache('#24639C'); // `fifthResult` will be computed again, since there's no cache // `sixthResult` won't be computed because of the existent memory cache for the value const [fifthResult, sixthResult] = [ hexToCSSFilter('#24639C',  forceFilterRecalculation: false > as HexToCssConfiguration), hexToCSSFilter('#FF0000',  forceFilterRecalculation: false > as HexToCssConfiguration), ];

Also, it covers the scenario of removing all the cache by calling the function with no arguments. E.G.

// Creating CSS filters for `#24639C` and `#FF0000` // They memory cache stored is based on the received hex value const [firstResult, secondResult, thirdResult, forthResult] = [ hexToCSSFilter('#24639C',  forceFilterRecalculation: false > as HexToCssConfiguration), hexToCSSFilter('#24639C',  forceFilterRecalculation: false > as HexToCssConfiguration), hexToCSSFilter('#FF0000',  forceFilterRecalculation: false > as HexToCssConfiguration), hexToCSSFilter('#FF0000',  forceFilterRecalculation: false > as HexToCssConfiguration), ]; // . // ✨ Here is the place where the magic happens in your App ✨ // . // Removing all cached values from memory clearCache(); // `fifthResult` and `sixthResult` will be computed again, since there's no cache const [fifthResult, sixthResult] = [ hexToCSSFilter('#24639C',  forceFilterRecalculation: false > as HexToCssConfiguration), hexToCSSFilter('#FF0000',  forceFilterRecalculation: false > as HexToCssConfiguration), ];

Publish

this project is using np package to publish, which makes things straightforward. EX: np

Author

Wilson Mendes (willmendesneto)

Источник

Hex Color To CSS Filter Converter

Real pixel, color applied through CSS background-color :

Filtered pixel, color applied through CSS filter :

How To Use This Tool

Simply paste your color (as a hex code) into the text input, then click «compute filters».

This will convert your hex color into a css color, and compare it with the «real color».

Then, just copy and paste the CSS code as needed.

Why Convert Colors To CSS Filters?

If you have a black icon set, you can use CSS filters to color them into anything.

Use this tool to generate a stylesheet. For example, you can use this to make a black icon green, with the class of .icon-green and CSS filter color.

This goes for any image that has a transparent background and can be made into a single color.

It’s a lot easier than generating a ton of different colored icons — all you need is a base black one!

Demo: Using CSS Filters To Color An Image

We will use the following css:
filter: invert(33%) sepia(100%) saturate(6230%) hue-rotate(331deg) brightness(101%) contrast(101%);

Credit Where Credit Is Due

This is an adapted fork of Barrett Sonntag’s «CSS filter generator to convert from black to target hex color» Codepen Notebook. We changed a few things and made it easier to use.

A Part Of IsoTools

Isotropic is a content company offering high quality WordPress education and resources via our Blog and Courses.

Источник

Читайте также:  Java push to arraylist
Оцените статью