Page 1 of 1

How to set an opacity for a color?

Posted: 2017-03-15T13:32:46-07:00
by Airon65
My test code is:

Code: Select all

Magick::Image img( "images/tree.png" );
img.strokeColor( "red" );
img.fillColor( Magick::Color( 0, 255, 0, 127 ) );
img.strokeWidth( 5 );
img.draw( Magick::DrawableCircle( 300, 300, 200, 200 ) );
img.write( "images/img.png" );
But it doesn't work as semi-transparent color in this way. What do I do wrong?

I compile it with the command:

Code: Select all

g++ -Wall first.cpp -o first `Magick++-config --cppflags --cxxflags --ldflags --libs`
where:

Code: Select all

$ Magick++-config --cppflags --cxxflags --ldflags --libs
-DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=16 -DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=16 -DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=16 -I/usr/local/Cellar/imagemagick/7.0.5-2/include/ImageMagick-7
-DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=16 -DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=16 -DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=16 -I/usr/local/Cellar/imagemagick/7.0.5-2/include/ImageMagick-7
-L/usr/local/Cellar/imagemagick/7.0.5-2/lib -lMagick++-7.Q16HDRI -lMagickWand-7.Q16HDRI -lMagickCore-7.Q16HDRI
-L/usr/local/Cellar/imagemagick/7.0.5-2/lib -lMagick++-7.Q16HDRI -lMagickWand-7.Q16HDRI -lMagickCore-7.Q16HDRI

Re: How to set an opacity for a color?

Posted: 2017-03-15T14:18:42-07:00
by snibgo
That should work fine, if your IM is Q8. But writing software that depends on a particular Q-number is unwise. (And I suppose your IM isn't Q8.)

I suggest you try:

Code: Select all

image.fillColor( Magick::Color( 0, QuantumRange, 0, QuantumRange/2 ) );
EDIT: Dividing by "2.0" is probably better, so HDRI can give a genuine 50%. In addition, the documentation http://www.imagemagick.org/Magick++/Color.html#Color suggests we should use MaxRGB rather than QuantumRange, so we have:

Code: Select all

image.fillColor( Magick::Color( 0, MaxRGB, 0, MaxRGB/2.0 ) );

Re: How to set an opacity for a color?

Posted: 2017-03-15T14:58:33-07:00
by Airon65
Thanks the first one is worked for me! In the second way I get error: "error: use of undeclared identifier 'MaxRGB'" but the first one works perfectly! :)

Re: How to set an opacity for a color?

Posted: 2017-03-15T15:05:23-07:00
by fmw42
Perhaps you need to specify a numeric value for MaxRGB?

Re: How to set an opacity for a color?

Posted: 2017-03-15T15:23:46-07:00
by snibgo
Specifying a number like 127 or 32767 is a bad idea because that will work as desired only for a particular Q-number.

I notice that v6.9.3-7 deprecate.h contains:

Code: Select all

#define MaxRGB  QuantumRange  /* deprecated */
Perhaps the OP's IM is newer than mine (and newer than the documentation), and MaxRGB was dropped after that version.

Re: How to set an opacity for a color?

Posted: 2017-03-15T15:27:57-07:00
by Airon65
Yeah, I use the latest IM 7.0.5. Thanks for the help!