how to convert gray color to a given color?

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
linjuming
Posts: 122
Joined: 2011-01-25T00:09:50-07:00
Authentication code: 8675308

how to convert gray color to a given color?

Post by linjuming »

if the original color is not gray ,can use this method to do:
(modulate convert ,why the result is not the same color?)
viewtopic.php?f=1&t=19044

when the original color is gray ,the hsl (h=0,s=0,l=x) is not suitable for "hue/hue" compare ,then can not get the modulate hue or saturation.
Then how to do this covert?

Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to convert gray color to a given color?

Post by fmw42 »

because gray has zero saturation, so -modulate cannot change 0 to something else by using a ratio

To recolor gray, either use -colorize (for solid gray) or +level-colors (for a gray gradient)
Last edited by fmw42 on 2011-07-09T21:49:57-07:00, edited 1 time in total.
linjuming
Posts: 122
Joined: 2011-01-25T00:09:50-07:00
Authentication code: 8675308

Re: how to convert gray color to a given color?

Post by linjuming »

Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to convert gray color to a given color?

Post by fmw42 »

you must first tell us how you created b_gd.png!

If I assume you have created them as:

gray gradient:
convert -size 100x100 gradient:"#b3b3b3-#4d4d4d" tmp1.png

blue gradient:
convert -size 100x100 gradient:"#0897ef-black" tmp2.png


gray to blue gradient:
convert tmp1.png -auto-level +level-colors "black,#0897ef" tmp1_2.png

tmp2.png and tmp1_2.png are the same

you have to stretch the gray gradient to full dynamic range before applying +level-colors with the proper color values.

note the order of the two colors in the gradient and in +level-colors are reversed.

to ensure the correct results you should specify both values. if you don't, then one will be assumed black and the other will be assumed white (at least in the gradient, if not both)
linjuming
Posts: 122
Joined: 2011-01-25T00:09:50-07:00
Authentication code: 8675308

Re: how to convert gray color to a given color?

Post by linjuming »

the b_gd.png is created approximately in photoshop.
the theory is similar with -modulate convert;
Image

and similar with Hue/Saturation in photoshop ,but select the colorize option.

Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to convert gray color to a given color?

Post by fmw42 »

Sorry, you will have to show more steps and what functions in PS you are using. I only have an old PS CS and am not that familiar with it and perhaps it does not have the same functions you are using.

If I just do:

convert -size 100x100 gradient:"#0897ef-#b3b3b3" tmp3.png

I do not reproduce your image. I am not sure what you are doing exactly with hue and saturation. Are you using HSL or HSB? So I cannot even reproduce it in Photoshop. I don't see a colorize option in my old PS.

Does this reproduce your PS result?



h=$((100*243/360))
s=66
echo "h=$h; s=$s"
convert -size 100x100 gradient:"#b3b3b3-#0897ef" -colorspace hsl \
-channel r -evaluate set $h% +channel -channel g -evaluate set $s% \
+channel -colorspace rgb tmp6.png
Last edited by fmw42 on 2011-07-09T23:18:54-07:00, edited 1 time in total.
linjuming
Posts: 122
Joined: 2011-01-25T00:09:50-07:00
Authentication code: 8675308

Re: how to convert gray color to a given color?

Post by linjuming »

1,i firstly ceate the #b3b3b3-#4d4d4d gradient in photoshop (ie:a.png)
2,then use Hue/Saturation tool to get some random color to get the colorize gradient (ie:b_gd.png)
3,the i pick the top color of the colorize gradient (ie:0897ef),and paste a square to get the pure color (ie:b.png)

my thinking is that if i had gray-scale img ,i want to colorize it base on a given color (ie :0897ef) ,the colored png is mainly close to my given color. no need very strick calculation,but seems trending to my given color.

it seems very hard to do this , sorry for my awkward request . if too wasting time ,pls ignore this topic.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to convert gray color to a given color?

Post by fmw42 »

Does this reproduce your PS result?

h=$((100*243/360))
s=66
echo "h=$h; s=$s"
convert -size 100x100 gradient:"#b3b3b3-#0897ef" -colorspace hsl \
-channel r -evaluate set $h% +channel -channel g -evaluate set $s% \
+channel -colorspace rgb tmp6.png

or perhaps this


h=$((100*243/360))
s=66
echo "h=$h; s=$s"
convert -size 100x100 gradient:"#0897ef-#b3b3b3" -colorspace hsb \
-channel r -evaluate set $h% +channel -channel g -evaluate set $s% \
+channel -colorspace rgb tmp6c.png
linjuming
Posts: 122
Joined: 2011-01-25T00:09:50-07:00
Authentication code: 8675308

Re: how to convert gray color to a given color?

Post by linjuming »

yes , great , that is what i want .
Thank you very much.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to convert gray color to a given color?

Post by fmw42 »

which example was it -- the first or the second?
linjuming
Posts: 122
Joined: 2011-01-25T00:09:50-07:00
Authentication code: 8675308

Re: how to convert gray color to a given color?

Post by linjuming »

h=$((100*243/360))
s=66
echo "h=$h; s=$s"
convert -size 100x100 gradient:"#b3b3b3-#0897ef" -colorspace hsl \
-channel r -evaluate set $h% +channel -channel g -evaluate set $s% \
+channel -colorspace rgb tmp6.png

this is good enough
Post Reply