How to set an opacity for a color?

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

How to set an opacity for a color?

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to set an opacity for a color?

Post 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 ) );
snibgo's IM pages: im.snibgo.com
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

Re: How to set an opacity for a color?

Post 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! :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to set an opacity for a color?

Post by fmw42 »

Perhaps you need to specify a numeric value for MaxRGB?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to set an opacity for a color?

Post 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.
snibgo's IM pages: im.snibgo.com
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

Re: How to set an opacity for a color?

Post by Airon65 »

Yeah, I use the latest IM 7.0.5. Thanks for the help!
Post Reply