1.0.1 • Published 3 years ago

change-image-color v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

change-image-color

Manipulate the colors in image.

Installation

// npm
npm install change-image-color

// yarn
yarn add change-image-color

Usage

const ChangeColor = require('change-image-color');

// with a callback
ChangeColor({ /* your configuration */ }, (err, res) => {
    if (err) {
        /* error handling */
    } else {
        processResult(res);
    }
})

// as a Promise
ChangeColor({ /* your configuration */ })
    .then(res => {
        processResult(res);
    })
    .catch(err => {
        /* error handling */
    })

result is an array containing the Jimp instance of the image and its source. Check jimp to see what you can do with the instance.

const processResult = (res) => {
    res.map(({ src, image }) => {
        // src: your image source
        // image: Jimp instance

        /* your function */
    })
}

Options

This repo uses jimp to modify the color. You may want to check more with Jimp.

nametyperequireddefaultdescription
srcstring \| Buffer \| Objecttrue--Source of images
colorsObjecttrue--Colors to be modified
deltaEnumberfalse2.3Color difference ΔE
formula"76" \| "94" \| "00"false00Color difference formula
transparencybooleanfalsetrueMatch color with the same transparency
recursivebooleanfalsefalseSearch directory recursively

options.src

options.src accepts many anything that is a valid input in Jimp.read(src) (Usage): path to a file, URL, a Jimp instance or a buffer.

Files are processed one by one to prevent a sudden drain in CPU and memory

// local file
ChangeColor({ src: 'path/to/your/image.png' })

// URL
ChangeColor({ src: 'https://path/to/your/image.png' })

// Jimp instance
ChangeColor({ src: jimpInstance })

// buffer
ChangeColor({ src: buffer })

To process multiple images, you can specify path of a directory. Or even an array of the accepted input.

// local directory
ChangeColor({ src: 'path/to/your/directory' })

// an array
ChangeColor({
    src: [
        'path/to/your/directory',
        'https://path/to/your/image.png',
        buffer,
    ]
})

options.colors

options.colors can be set in different formats.

// in array of object
ChangeColor({
    colors: [{
        from: from_color1,
        to: to_color1,
    }, {
        from: from_color2,
        to: to_color2,
    }]
})

// in object
ChangeColor({
    colors: {
        from_color1: to_color1,
        from_color2: to_color2,
    }
})

A valid color could be a string or object that is valid as a input in colord. For more detail, please check Colord.

Plugins of names, cmyk, hwb, lab, lch, xyz are enabled.

color = '#333' // hex

color = 'rgba(33, 33, 22, 0.5)' // rgb string

color = { r: 33, g: 33, b: 33, a: 0.5 } // rgb object

// check `colord` for other expressions

options.deltaE

options.deltaE should be a number between 0 and 100. Colors with Delta E below 2 are hard for people to notice.

For more, check here.

options.formula

options.formula specify the formula to calculate Delta E. You can choose between CIE76, CIE94 and CIE00.

For more, check here.

options.transparency

Only the colors with Delta E smaller than options.deltaE AND identical transparency will be replaced.

To bypass transparency check, set options.transparency to false.

color in imagecolor to matchoptions.transparencyresult
#000000#000000trueMatch
#00000077#000000trueNOT Match
#000000#00000077trueNOT Match
#000000#00000077falseMatch

options.recursive

options.recursive only works when specify directory in options.src.

It will loop recursively in that directory to find all the images. However, it will ignore all the sub-directories with name node_modules, .git or .vscode.

Command Line

// installed globally
change-image-color

// installed locally
npx change-image-color

Run the script and answer the questions.

If the output file already exist, it will add a suffix -cic to the filename.

License

This project is licensed under the terms of the MIT license.

Support

If you find this repo useful, please share to your friends. Or you can buy me a coffee: