Page 1 of 1

Replacing multiple RGB colors using color-matrix?

Posted: 2016-11-29T02:37:35-07:00
by nwera
Hello there!

First of all, I would like to say that I have tried searching for this on Google and forums a lot, but I haven't been able to find a solution. I apologize if this topic has already been solved somewhere else.

So, I want to replace pure RGB colors (#ff0000, #00ff00, #0000ff) by 3 other colors (#b748b2, #a03c9b, #db77b7). I'm trying to use the "-color-matrix" command line, but I'm not sure if it's possible and what I have to put to the matrix.

Here is what I tried:

Code: Select all

colorR.r/255 colorR.g/255 colorR.b/255 0 0,
colorG.r/255 colorA.G.g/255 colorG.b/255 0 0,
colorB.r/255 colorB.g/255 colorB.b/255 0 0,
0 0 0 1 0,
0 0 0 0 0
with colorR = #b748b (183, 72, 178), colorG = #a03c9b (160, 60, 155) and colorB = #db77b7 (219, 119, 183)

So my final formula is something like this:

Code: Select all

convert in.png -color-matrix "00.282352941 0.717647059 0.301960784 0 0, 0.37254902 0.764705882 0.392156863 0 0, 0.141176471 0.533333333 0.282352941 0 0, 0 0 0 1 0, 0 0 0 0 0" out.png
The result is not what I'm expecting, of course, and I'm wondering if someone could explain to me how I can replace those colors.

I'm using ImageMagick 6.9.6-6 Q16 using macOS command line.

Many thanks!

Re: Replacing multiple RGB colors using color-matrix?

Posted: 2016-11-29T09:25:59-07:00
by snibgo
"-color-matrix" tweaks all colours, according to a formula. But you want to tweak only three colours.

"-fill #b748b2 -opaque #ff0000" will do the first. Repeat as often as you like for other colours.

Re: Replacing multiple RGB colors using color-matrix?

Posted: 2016-11-29T09:43:22-07:00
by nwera
Thank you for your reply!

Actually, I already tried to replace the three colors using fill, but this is not working great since I don't want to only replace those 3 colors.
Sorry, I forgot to explain something important: the image contains the 3 colors but there are some colors mixing lines:

Image

Can I use a "-color-matrix" formula to tweak all colors based on a pattern (#ff0000 -> #b748b2, #00ff00 -> #a03c9b, #0000ff -> #db77b7)?

Thank you!

Re: Replacing multiple RGB colors using color-matrix?

Posted: 2016-11-29T10:23:36-07:00
by snibgo
nwera wrote:Can I use a "-color-matrix" formula to tweak all colors based on a pattern (#ff0000 -> #b748b2, #00ff00 -> #a03c9b, #0000ff -> #db77b7)?
That would be a useful feature for IM. It amounts to a distortion of colour space, which has three dimensions. Distortion methods could include the usual: Shepards, triangulation, and so on. Sadly, IM doesn't have this.

What do you want to happen to pixels that are green+red, red+blue etc? If you want them to become exactly the new colours, just use "-fuzz". If you want the appropriate mixing, then the task is more complex.

As your input colours use the 3 channels, you can separate, giving you three masks. Each mask shows the amount of one of the new colours to have in the result. Make a solid image of the first colour (#b748b2) that has transparency from its mask. Repeat for the other two colours. Then "-layers merge" the three results.

EDIT: Scrub my last paragraph. You image doesn't contain pure red or green or blue.

I don't know what you want to do.

Re: Replacing multiple RGB colors using color-matrix?

Posted: 2016-11-30T04:27:12-07:00
by nwera
Hi, thank you for your reply again!

I would like to generate a lot of different assets from some rgb sources like the one I attached.
For each asset, the color mapping is different.

Actually, the image should contain pure red, green and blue colors. Is it not right?

Ideally, the green+red or red+blue pixels would be a mix of the new colors.
I'm trying to use -fuzz, but seems like there is an issue with color opacity. For example, ImageMagick finds red color into blue gradient:

Image
Image

Any clue?

Thank you for your help!

Re: Replacing multiple RGB colors using color-matrix?

Posted: 2016-11-30T12:33:05-07:00
by snibgo
Any clue?
Your command can be adjusted. But you didn't bother to say what command you used.