how to color a black-none png file?

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 color a black-none png file?

Post by linjuming »

Code: Select all

<?php
	$color="A3E7EF";
	include_once("../color_converter/color_converter.class.php");
	$cc=new colorConverter;
	$hsl=$cc->HEX2HSL($color);
	$h=$hsl[0]/360*100;

	exec("convert 1.png -colorspace hsl " .
			"-channel r -evaluate set $h% +channel " .
			"-channel g -evaluate set $hsl[1]% +channel " .
			"a.png");
	echo "<img src='1.png' /><img src='a.png' />";
	echo "<br>";

	exec("convert -size 100x100 gradient:black-none 2.png");
	exec("convert 2.png -colorspace hsl " .
			"-channel r -evaluate set $h% +channel " .
			"-channel g -evaluate set $hsl[1]% +channel " .
			"b.png");
	echo "<img src='2.png' /><img src='b.png' /> ";

	exec("convert -size 100x100 gradient:black-red 3.png");
	exec("convert 3.png -colorspace hsl " .
			"-channel r -evaluate set $h% +channel " .
			"-channel g -evaluate set $hsl[1]% +channel " .
			"c.png");
	echo "<img src='3.png' /><img src='c.png' />";
?>
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to color a black-none png file?

Post by fmw42 »

Not quite sure I follow what you are doing? What is the goal of your processing?
exec("convert 3.png -colorspace hsl " .
"-channel r -evaluate set $h% +channel " .
"-channel g -evaluate set $hsl[1]% +channel " .
"c.png");
exec("convert 2.png -colorspace hsl " .
"-channel r -evaluate set $h% +channel " .
"-channel g -evaluate set $hsl[1]% +channel " .
"b.png");
These results are still in HSL space. Don't you need to convert back to RGB? Furthermore, your gradient:black-none has an alpha channel and it may be getting lost by the HSL conversion.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: how to color a black-none png file?

Post by anthony »

Saving to PNG automatically converts back to RGB colorspace.

However it should be pointed out that HSL is (mostly) linear, as is RGB. You will probably want to adjust the results to be sRGB. Adding a -gamma 2.2 after converting to RGB colorspace is probably the simplest way to do this in IMv6, and generally close enough to sRGB for most purposes.

See the new section on IM Examples... Human Color Perception
http://www.imagemagick.org/Usage/color_ ... perception

IMv7 would probably be more automatic, unless sRGB default handling is turned off (for mathematical image processing)


As to the actual problem, I think a clear explanation of what you are trying to do will be needed for us to help you.

However I can tell you why you don't get the full result. You set hue, and saturation, but your luminance is still set to zero which is black for all hues and saturations! As such you gradient remains black!

For an example of working with other colorspaces see.. Gradients in other Colorspaces
http://www.imagemagick.org/Usage/canvas ... colorspace
Also see...
http://www.imagemagick.org/Usage/color_ ... colorwheel
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: how to color a black-none png file?

Post by fmw42 »

Anthony wrote:Saving to PNG automatically converts back to RGB colorspace.
How do you know that? I would have expected to have a PNG image where red was hue, green was saturation and blue was lightness.

Testing:

convert rose: -colorspace HSL rose_hsl.png

does indeed show that the result is the same as rose:

Seems to be the case for .tiff and .jpg. This is something I would not have expected. I guess to get what I expect, I would have to use -separate and then -combine without setting the colorspace.

convert rose: -colorspace HSL -separate -combine rose_hsl2.png

Indeed, that does what I had expected from the -colorspace alone.

Learned something new today!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: how to color a black-none png file?

Post by anthony »

Basically if the format can not handle a specific color space IM converts it.

PNG, JPEG, and TIFF does not handle HSL colorspaces, unless a color profile is provided to make it so.
All file formats assume the colorspace is sRGB unless the profile is provided, so IM caters to that assumption.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
linjuming
Posts: 122
Joined: 2011-01-25T00:09:50-07:00
Authentication code: 8675308

Re: how to color a black-none png file?

Post by linjuming »

However I can tell you why you don't get the full result. You set hue, and saturation, but your luminance is still set to zero which is black for all hues and saturations! As such you gradient remains black!
thank you , this is the point.
Post Reply