Colorize an image with hex-code

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
derMatze

Colorize an image with hex-code

Post by derMatze »

I need to colorize an image with an hex-code. So I have the original image and an hexcode+alpha like 15243C + 90. That should add #15 to the R-Channel, #24 to the G channel and so on.
I tried this with -channel and -fx but it doesn't work at all, not with #15 or 15 or the decimal 21...
-channel R -fx 'r + 15' -channel G -fx 'g + 36' -channel B -fx 'b + 60'
Any ideas? :(

Similar to that: What does "-channel R -fx 'r+0.1'" mean? More red, ok, but how much, 10%?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Colorize an image with hex-code

Post by fmw42 »

derMatze wrote:I need to colorize an image with an hex-code. So I have the original image and an hexcode+alpha like 15243C + 90. That should add #15 to the R-Channel, #24 to the G channel and so on.
I tried this with -channel and -fx but it doesn't work at all, not with #15 or 15 or the decimal 21...
-channel R -fx 'r + 15' -channel G -fx 'g + 36' -channel B -fx 'b + 60'
Any ideas? :(

Similar to that: What does "-channel R -fx 'r+0.1'" mean? More red, ok, but how much, 10%?

-fx uses normalized channel values in the range from 0 to 1. So r+.1 is r plus 10%

So you will need to convert your hex components to that range to do something like you are trying to do. But if you add a value that goes beyond 1, it will clip to 1.

However, you cannot process the image as a sequence of steps as you have. You will have to create a separate image for each component and then -combine them together.

However, you are making it harder than you need as I believe you can do:

convert -size 100x100 xc:black -fx "u+#FF0000" red100.png

if you need to use alpha as well, you need to enable the alpha channel first by doing the following:

convert -size 100x100 xc:black -channel rgba -matte -fx "u+#FF000000" red100.png

You can replace your input image in place of my -size 100x100 xc:black

Note also there is a -colorize function. seehttp://www.imagemagick.org/script/command-line ... p#colorize where you use percent rather absolute color values
derMatze

Re: Colorize an image with hex-code

Post by derMatze »

fmw42 wrote: convert -size 100x100 xc:black -fx "u+#FF0000" red100.png
That's exactly what I needed, so damn easy! :D
Thanks a lot!
Post Reply