{"id":13493365,"url":"https://github.com/turakvlad/replace-color","last_synced_at":"2025-03-28T11:32:39.446Z","repository":{"id":38387597,"uuid":"147077124","full_name":"turakvlad/replace-color","owner":"turakvlad","description":"Replace color with another one pixel by pixel.","archived":false,"fork":false,"pushed_at":"2022-12-08T00:03:26.000Z","size":207,"stargazers_count":138,"open_issues_count":11,"forks_count":27,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-10T13:04:43.918Z","etag":null,"topics":["cie76","cie94","ciede2000","color-difference","color-replacement","delta-e","image-processing","pixel-by-pixel","remove-watermark","watermark-detection"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/turakvlad.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-02T10:56:23.000Z","updated_at":"2024-08-22T04:26:27.000Z","dependencies_parsed_at":"2023-01-25T01:15:51.066Z","dependency_job_id":null,"html_url":"https://github.com/turakvlad/replace-color","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/turakvlad%2Freplace-color","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/turakvlad%2Freplace-color/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/turakvlad%2Freplace-color/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/turakvlad%2Freplace-color/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/turakvlad","download_url":"https://codeload.github.com/turakvlad/replace-color/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246021392,"owners_count":20710928,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cie76","cie94","ciede2000","color-difference","color-replacement","delta-e","image-processing","pixel-by-pixel","remove-watermark","watermark-detection"],"created_at":"2024-07-31T19:01:14.528Z","updated_at":"2025-03-28T11:32:39.182Z","avatar_url":"https://github.com/turakvlad.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Replace Color [![Travis Build Status](https://img.shields.io/travis/turakvlad/replace-color.svg)](https://travis-ci.org/turakvlad/replace-color) [![JavaScript Standard Style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](https://github.com/feross/standard) [![NPM Version](https://img.shields.io/npm/v/replace-color.svg)](https://www.npmjs.com/package/replace-color)\n\n`replace-color` replaces color with another one pixel by pixel. Especially this will be helpful if you want to remove the watermarks from the images. This package is built on top of [Jimp](https://github.com/oliver-moran/jimp).\n\n## Install\n\n```sh\nnpm install --save replace-color\n```\n\n## Basic usage\n\n`replace-color` supports both Node.js error-first callbacks and promises. The package returns a Jimp's instance which you can use to execute some other [image manipulations methods](https://github.com/oliver-moran/jimp#image-manipulation-methods) or save it with Jimp's [`write`](https://github.com/oliver-moran/jimp/tree/master/packages/jimp#writing-to-files-and-buffers) method.\n\n### Node.js error-first callback example\n\n```javascript\nconst replaceColor = require('replace-color')\n\nreplaceColor({\n  image: './input.jpg',\n  colors: {\n    type: 'hex',\n    targetColor: '#FF0000',\n    replaceColor: '#FFFFFF'\n  }\n}, (err, jimpObject) =\u003e {\n  if (err) return console.log(err)\n  jimpObject.write('./output.jpg', (err) =\u003e {\n    if (err) return console.log(err)\n  })\n})\n```\n\n### Promise example\n\n```javascript\nconst replaceColor = require('replace-color')\n\nreplaceColor({\n  image: './input.jpg',\n  colors: {\n    type: 'hex',\n    targetColor: '#FF0000',\n    replaceColor: '#FFFFFF'\n  }\n})\n  .then((jimpObject) =\u003e {\n    jimpObject.write('./output.jpg', (err) =\u003e {\n      if (err) return console.log(err)\n    })\n  })\n  .catch((err) =\u003e {\n    console.log(err)\n  })\n```\n\n## API\n\n### replaceColor(options, [callback])\n\n* `options` *[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) (required)* - the options.\n  * `image` *[Buffer](https://nodejs.org/api/buffer.html#buffer_class_buffer) | [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) | [String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) (required)* - an image being processed. It can be a **buffer**, **Jimp's instance**, a **path to an image on your host machine** or a **URL address to an image on the internet**. Please, take a look at the [tests](https://github.com/turakvlad/replace-color/tree/master/test/replace-color.js) to understand all these options.\n  * `colors` *[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) (required)* - the colors.\n    * `type` *[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) (required)* - a `targetColor` and `replaceColor` type. Supported values are `hex` and `rgb`.\n    * `targetColor` *[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) | [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) (required)* - a color you want to replace. A 7-symbol string in case of `hex` type (e.g. `#000000`, `#FFFFFF`). An array of `3` integers from `0` to `255` in case of `rgb` type (e.g. `[0, 0, 0]`, `[255, 255, 255]`).\n    * `replaceColor` *[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) | [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) (required)* - a new color which will be used instead of a `targetColor` color. A 7-symbol string in case of `hex` type (e.g. `#000000`, `#FFFFFF`). An array of `3` integers from `0` to `255` in case of `rgb` type (e.g. `[0, 0, 0]`, `[255, 255, 255]`). You can also define a transparent channel for a `replaceColor` color. To achieve this, you can use a 9-symbol string in case of `hex` type (e.g. `#00000000`, `#FFFFFFFF`). Based on [this Stack Overflow answer](https://stackoverflow.com/questions/23201134/transparent-argb-hex-value/23201304#23201304), an alpha channel is controlled by the first pair of digits in a hex code (e.g., `00` means fully transparent, `7F` means 50%, `FF` means fully opaque). Also, you can use an array of `4` integers in case of `rgb` type. The first `3` integers must be from `0` to `255` and the last one must be from `0` to `1` (e.g., `0` means fully transparent, `0.5` means 50%, `1` means fully opaque).\n  * `formula` *[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) (optional)* - one of the three formulas to calculate the [color difference](https://en.wikipedia.org/wiki/Color_difference). Supported values are [`E76`](https://en.wikipedia.org/wiki/Color_difference#CIE76), [`E94`](https://en.wikipedia.org/wiki/Color_difference#CIE94) and [`E00`](https://en.wikipedia.org/wiki/Color_difference#CIEDE2000). The default value is `E00` (the best algorithm).\n  * `deltaE` *[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) (optional)* - a `deltaE` value which corresponds to a [JND](https://en.wikipedia.org/wiki/Just-noticeable_difference). The default value is `2.3`. Please, read more about `deltaE` [here](http://zschuessler.github.io/DeltaE/learn/). Generaly speaking, if the processed by the `replace-color` package image still has the watermarks, you should increase the `deltaE` value.\n* `callback` *[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) (optional)* - a Node.js error-first callback.\n\n## Examples\n\n### Remove a watermark\n\nLet's try to remove a watermark from [this](https://i.imgur.com/XqNTuzp.jpg) picture.\n\n```javascript\nconst replaceColor = require('replace-color')\n\nreplaceColor({\n  image: 'https://i.imgur.com/XqNTuzp.jpg',\n  colors: {\n    type: 'hex',\n    targetColor: '#FFB3B7',\n    replaceColor: '#FFFFFF'\n  },\n  deltaE: 20\n})\n  .then((jimpObject) =\u003e {\n    jimpObject.write('./output.jpg', (err) =\u003e {\n      if (err) return console.log(err)\n    })\n  })\n  .catch((err) =\u003e {\n    console.log(err)\n  })\n```\n\n#### Result\n![Example](https://i.imgur.com/d5PNhnt.jpg)\n\n### Change a background color from a green to a blue one\n\nLet's try to change a background color for [this](https://i.imgur.com/aCxZpaq.png) picture.\n\n```javascript\nconst replaceColor = require('replace-color')\n\nreplaceColor({\n  image: 'https://i.imgur.com/aCxZpaq.png',\n  colors: {\n    type: 'hex',\n    targetColor: '#66AE74',\n    replaceColor: '#63A4FF'\n  },\n  deltaE: 10\n})\n  .then((jimpObject) =\u003e {\n    jimpObject.write('./output.png', (err) =\u003e {\n      if (err) return console.log(err)\n    })\n  })\n  .catch((err) =\u003e {\n    console.log(err)\n  })\n```\n\n#### Result\n![Example](https://i.imgur.com/c0JT5tn.png)\n\n### Change a background color from a green to a transparent one (using `hex` type)\n\nLet's try to change a background color for [this](https://i.imgur.com/aCxZpaq.png) picture.\n\n```javascript\nconst replaceColor = require('replace-color')\n\nreplaceColor({\n  image: 'https://i.imgur.com/aCxZpaq.png',\n  colors: {\n    type: 'hex',\n    targetColor: '#66AE74',\n    replaceColor: '#00000000'\n  },\n  deltaE: 10\n})\n  .then((jimpObject) =\u003e {\n    jimpObject.write('./output.png', (err) =\u003e {\n      if (err) return console.log(err)\n    })\n  })\n  .catch((err) =\u003e {\n    console.log(err)\n  })\n```\n\n#### Result\n![Example](https://i.imgur.com/jmzpmBa.png)\n\n### Change a background color from a green to a 50% transparent green (using `rgb` type)\n\nLet's try to change a background color for [this](https://i.imgur.com/aCxZpaq.png) picture.\n\n```javascript\nconst replaceColor = require('replace-color')\n\nreplaceColor({\n  image: 'https://i.imgur.com/aCxZpaq.png',\n  colors: {\n    type: 'rgb',\n    targetColor: [102, 174, 116],\n    replaceColor: [102, 174, 116, 0.5]\n  },\n  deltaE: 10\n})\n  .then((jimpObject) =\u003e {\n    jimpObject.write('./output.png', (err) =\u003e {\n      if (err) return console.log(err)\n    })\n  })\n  .catch((err) =\u003e {\n    console.log(err)\n  })\n```\n\n#### Result\n![Example](https://i.imgur.com/lL1ox0G.png)\n\n## Error handling\n\nTo indicate the `replace-color`'s errors you should use the `err instanceof replaceColor.ReplaceColorError` class.\n\n```javascript\nreplaceColor({}, (err, jimpObject) =\u003e {\n  if (err instanceof replaceColor.ReplaceColorError) {\n    //  A replace-color's error occurred. \n  } else if (err) {\n    // An unknown error occurred.\n  }\n  \n  // Everything went fine.\n})\n```\n\nA `replace-color`'s `error` instance has the `code` and `field` properties. For now, the package has two codes: `PARAMETER_INVALID` and `PARAMETER_REQUIRED`. The `field` property shows which exact property was not passed or is invalid using the glob notation (e.g. `options.colors.type`). Please, take a look at the [tests](https://github.com/turakvlad/replace-color/tree/master/test/error-handling.js) to see all the possible cases.\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fturakvlad%2Freplace-color","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fturakvlad%2Freplace-color","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fturakvlad%2Freplace-color/lists"}