Page 1 of 1

Convert one color to another including anti-aliased colors but keeping background

Posted: 2015-03-23T08:42:13-07:00
by somebirdie
Hi!

I have several png images (icons for my app) that have different colors and one main color (#1A96E5). I want to replace #1A96E5 everywhere to another color, red (#FF0000).

I do it using such command:
mogrify -path /mypath -format png -alpha off -fill "#FF0000" -opaque "#1A96E5" -alpha on *.png

It works perfectly for images with transparent background, but keeps blue shades around red image when background is not transparent (grey).
Here is an example. It's a source image:
Image

And that's a result:
Image

I understand why it worked that way. But is there any way to convert blue color to red in such images keeping gray background and keeping anti-aliased edges?

Re: Convert one color to another including anti-aliased colors but keeping background

Posted: 2015-03-23T09:28:09-07:00
by snibgo
Your links don't work.

"-fuzz" might help. However, "-opaque" is a binary operator. Each pixel is either changed to the new colour, or it isn't. You might need something more sophisticated.

Re: Convert one color to another including anti-aliased colors but keeping background

Posted: 2015-03-30T03:09:50-07:00
by snibgo
Your images now show.

We have two colours that are blended, and we want to replace the second colour with a new one, with correct blending. Windows BAT syntax:

Code: Select all

convert ^
  blue-source.png ^
  ( -clone 0 -alpha extract +write mpr:ALPH +delete ) ^
  -alpha off ^
  ( -clone 0 -fill rgb(237,239,242) -colorize 100 +write mpr:C1 ) ^
  ( -clone 0 -fill rgb(26,150,229)  -colorize 100 +write mpr:C2 ) ^
  ( -clone 1-2 -compose MinusSrc -composite ) ^
  ( -clone 0,2 -compose MinusSrc -composite ) ^
  -delete 0-2 ^
  -compose Divide -composite ^
  mpr:C1 ^
  ( +clone -fill rgb(100%%,0,0) -colorize 100 ) ^
  -swap 0,2 ^
  -compose Over -composite ^
  mpr:ALPH ^
  -alpha off -compose CopyOpacity -composite ^
  red-arrow.png
There may be an easier way.
Image

Re: Convert one color to another including anti-aliased colors but keeping background

Posted: 2015-03-30T18:55:09-07:00
by snibgo
Yes, I knew there was an easier way:

Code: Select all

convert ^
  blue-source.png ^
  -level-colors rgb(237,239,242),rgb(26,150,229) ^
  +level-colors rgb(237,239,242),rgb(100%%,0,0) ^
  ra.png