Fade text (label) on transparent background to transparent.

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
Bert
Posts: 12
Joined: 2018-06-29T05:04:42-07:00
Authentication code: 1152

Fade text (label) on transparent background to transparent.

Post by Bert »

Hello

I'm trying to create the following:

Text (in any color) on a transparent background which in itself starts fading into nothing (transparency) starting from a certain width.

This is an example that I've made in Photoshop (using a Layer Mask) and what it should be:

Image

This is the code I'm using at the moment (might be a bit convoluted, I used it mainly to test things out)

Code: Select all

using System.Drawing;
using ImageMagick;

namespace TextToPicture
{
    class Slideshow
    {
        static void Main(string[] args)
        {
            using (var completeMaskText = new MagickImage(MagickColors.Transparent, 310, 30))
            {
                var gradientSettings = new MagickReadSettings()
                {
                    Width = 270,
                    Height = 30
                };

                gradientSettings.SetDefine("gradient:direction", "east");

                Color r = Color.FromArgb(255, 0, 0);

                using (var filledMaskText = new MagickImage(MagickColors.White, 40, 30))
                using (var gradientMaskText = new MagickImage($"gradient:white-none", gradientSettings))
                {
                    completeMaskText.Composite(filledMaskText, 0, 0, CompositeOperator.Over);
                    completeMaskText.Composite(gradientMaskText, 40, 0, CompositeOperator.Over);
                }

                using (var mText = new MagickImage(MagickColors.Transparent, 310, 30))
                {
                    var textSettings = new MagickReadSettings
                    {
                        BackgroundColor = MagickColors.Transparent,
                        FillColor = r,
                        Width = 310,
                        Height = 30,
                    };

                    mText.Read($"label:Hello World hello world hello world!", textSettings);

                    using (var completeTextBox = new MagickImage(MagickColors.Transparent, 310, 30))
                    {
                        using (var text = new MagickImage(MagickColors.Transparent, 310, 30))
                        {
                            text.Density = new Density(300);
                            text.Format = MagickFormat.Png;

                            text.Composite(mText, 0, 0, CompositeOperator.Over);
                            text.Composite(completeMaskText, 0, 0, CompositeOperator.Multiply);
                            text.Write(@"D:\result.png");
                        }
                    }
                }
            }
        }
    }
}
This code puts a gradient (from white to transparent) over the text, but this makes the text's anti-aliasing 'lighter' and it looks bad.

Image

So I thought: I'll change the gradient from red to transparent instead, which makes the red darker (which is logical, because it puts red on red, but this is also not what I want).

Image

Then I tried creating an actual Layer Mask (like I've done in Photoshop) but if I use CopyAlpha the Alpha channel has to be turned off, which removes the initial transparency...

I'm kind of lost at the moment, if you guys can help me out with this I'd be grateful.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Fade text (label) on transparent background to transparent.

Post by snibgo »

Bert wrote:So I thought: I'll change the gradient from red to transparent instead, which makes the red darker (which is logical, because it puts red on red, but this is also not what I want).
By "transparent" I assume you mean transparent-black. So you fade from both opaque to transparent, and from red to black. So you might try transparent-red instead.
snibgo's IM pages: im.snibgo.com
Bert
Posts: 12
Joined: 2018-06-29T05:04:42-07:00
Authentication code: 1152

Re: Fade text (label) on transparent background to transparent.

Post by Bert »

alas, that's what I've also tried (with the second example). It looks good, but the red is more 'redder'.

so I've tried white-transparent and red-transparent (and just the CopyAlpha with a white-black)

The original should have RGB(255,0,0), but if I multiply a transparent-red gradient over it the red becomes RGB(255,63,63)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Fade text (label) on transparent background to transparent.

Post by snibgo »

Your filledMaskText is white. Why not red? You have "white" twice in your code, but I can't see that you want any white in the result. You want red, with varying degrees of opacity. Don't you?
snibgo's IM pages: im.snibgo.com
Bert
Posts: 12
Joined: 2018-06-29T05:04:42-07:00
Authentication code: 1152

Re: Fade text (label) on transparent background to transparent.

Post by Bert »

The code I've provided is just what I use for my examples. I've changed filledMaskText to MagickColors.Red and GradientMaskText to gradient:red-none but when I composite multiply them I get the 'redder red' version, whereas I just want actual red (255 0 0) fading to transparent.
Bert
Posts: 12
Joined: 2018-06-29T05:04:42-07:00
Authentication code: 1152

Re: Fade text (label) on transparent background to transparent.

Post by Bert »

Fixed it. Using CopyAlpha was the right direction, but I had to make sure the Alpha was the entire 'text', not just the gradient.

So the following
Image

with the following Alpha (using CopyAlpha on the image above)
Image

Comes to this:
Image

Maybe it'll help someone out in the future.
Post Reply