GoldenCrystal

Make Transparent plugin

21 posts in this topic

I don't think this effect already existed (maybe I din't search enough), so I wrote this little effect today.

Basically, this takes an opaque image along with a background/transparent color, and makes the image transparent in respect of the chosen color.

The closer a pixel's color is to the chosen color, the more transparent it will become. This can be used, for example, to change the background color of an image.

I took this image as an example:

mt-sample_before.png

Then I applied my filter choosing some random blue color I could find in the sky of the picture:

mt-sample_after.png

Here the image is really transparent (you might want to open it in Paint.NET to see better) and can blend really well on any background.

Now I applied a dark background (just by adding a layer under the image, and filling it with the color) and the image looks like if it was the night :)

mt-sample_modified.png

This is still really simple, and you don't have any other parameter than the color yet.

I hosted the plugin on my ftp here: [Download]

If you have any questions or comment... ;)

4

Share this post


Link to post
Share on other sites

Tanel made a colour to Alpha plugin which is what that seems to be doing...

0

Share this post


Link to post
Share on other sites

I looked at those, and it looks these are absolutely not the same thing, though the one by Tanel effectively looks a lot like my effect. ;)

My intent was not to remove some parts of the image, but to do something more or less like an "evolved color-keying".

My effect preserves the whole image, but just decompose it as a background color + transparent image.

I just tried to maximize the transparency of every pixel, and calculated then calculated rgb values in order to have something like this:

"original color" = blend("background color", "filtered color")

I'll try to find a better image to show this

0

Share this post


Link to post
Share on other sites

Thank you so much for this wonderful plug-in! You have saved me hours and hours of work! :D

0

Share this post


Link to post
Share on other sites

This is a very good plugin to use, because it appears to use the correct algorithm for maximizing transparency while making the new color blend to produce the original color when alpha-blended to erased color. Not every plugin that does something similar does it as well. (Must have been a tough crowd, since the plugin's author got no Reputation points for this useful plugin.)

0

Share this post


Link to post
Share on other sites

Actually, the author got no rep points because this plugin was posted back when we were still on forum software that didn't have any rep points.  :-)

0

Share this post


Link to post
Share on other sites

One major weakness of this otherwise excellent plugin is that it doesn't handle non-opaque pixels correctly.

0

Share this post


Link to post
Share on other sites

FYI: VS code for a plugin

 

Hidden Content:
 
using System;
using System.Text;
using System.Windows;
using System.Reflection;
using System.Windows.Forms;
using PaintDotNet;
using PaintDotNet.Effects;
using PaintDotNet.IndirectUI;
using PaintDotNet.PropertySystem;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: AssemblyTitle("Make Transparent Plugin")]
[assembly: AssemblyDescription("Make Transparent Paint.NET Effect")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("GoldenCrystal")]
[assembly: AssemblyProduct("MakeTransparent")]
[assembly: AssemblyCopyright("Copyright © GoldenCrystal 2008")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: AssemblyVersion("1.0.0.0")]

namespace MakeTransparentEffect
{
    public class PluginSupportInfo : IPluginSupportInfo
    {
        public string Author
        {
            get
            {
                return "GoldenCrystal";
            }
        }
        public string Copyright
        {
            get
            {
                return ((AssemblyCopyrightAttribute)base.GetType().Assembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false)[0]).Copyright;
            }
        }

        public string DisplayName
        {
            get
            {
                return ((AssemblyProductAttribute)base.GetType().Assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0]).Product;
            }
        }

        public Version Version
        {
            get
            {
                return base.GetType().Assembly.GetName().Version;
            }
        }

        public Uri WebsiteUri
        {
            get
            {
                return new Uri("http://www.getpaint.net/redirect/plugins.html");
            }
        }
    }

    [PluginSupportInfo(typeof(PluginSupportInfo), DisplayName = "MakeTransparent")]
    public class MakeTransparentEffectPlugin : PropertyBasedEffect
    {
        public static string StaticName
        {
            get
            {
                return "Make Transparent";
            }
        }

        public static Image StaticIcon
        {
            get
            {
                return new Bitmap(typeof(MakeTransparentEffectPlugin), "EffectPluginIcon.png");
            }
        }

        public MakeTransparentEffectPlugin()
            : base(StaticName, StaticIcon, "Color", EffectFlags.Configurable)
        {
        }

        public enum PropertyNames
        {
            Amount1
        }


        protected override PropertyCollection OnCreatePropertyCollection()
        {
            List<Property> props = new List<Property>();

            props.Add(new Int32Property(PropertyNames.Amount1, ColorBgra.ToOpaqueInt32(ColorBgra.FromBgra(EnvironmentParameters.PrimaryColor.B, EnvironmentParameters.PrimaryColor.G, EnvironmentParameters.PrimaryColor.R, 255)), 0, 0xffffff));

            return new PropertyCollection(props);
        }

        protected override ControlInfo OnCreateConfigUI(PropertyCollection props)
        {
            ControlInfo configUI = CreateDefaultConfigUI(props);

            configUI.SetPropertyControlValue(PropertyNames.Amount1, ControlInfoPropertyNames.DisplayName, "Background Color");
            configUI.SetPropertyControlValue(PropertyNames.Amount1, ControlInfoPropertyNames.Description, "Color that will be made 100% transparent");
            configUI.SetPropertyControlType(PropertyNames.Amount1, PropertyControlType.ColorWheel);

            return configUI;
        }

        protected override void OnSetRenderInfo(PropertyBasedEffectConfigToken newToken, RenderArgs dstArgs, RenderArgs srcArgs)
        {
            this.Amount1 = ColorBgra.FromOpaqueInt32(newToken.GetProperty<Int32Property>(PropertyNames.Amount1).Value);

            base.OnSetRenderInfo(newToken, dstArgs, srcArgs);
        }


        protected override void OnCustomizeConfigUIWindowProperties(PropertyCollection props)
        {
            // Change the effect's window title
            props[ControlInfoPropertyNames.WindowTitle].Value = "Make Transparent";
            base.OnCustomizeConfigUIWindowProperties(props);
        }

        protected override unsafe void OnRender(Rectangle[] rois, int startIndex, int length)
        {
            if (length == 0) return;
            for (int i = startIndex; i < startIndex + length; ++i)
            {
                Render(DstArgs.Surface, SrcArgs.Surface, rois[i]);
            }
        }

        #region User Entered Code
        #region UICode
        ColorBgra Amount1 = ColorBgra.FromBgr(0, 0, 0); // Background Color
        #endregion

        private ColorBgra TransformColor(ColorBgra color)
        {
            int R = Amount1.R - color.R;
            int G = Amount1.G - color.G;
            int B = Amount1.B - color.B;
            int maxvalue = (R > G) ? ((R >  ? R :  : ((G >  ? G : ;
            int minvalue = (R < G) ? ((R <  ? R :  : ((G <  ? G : ;
            int max = Math.Max(Math.Abs(maxvalue), Math.Abs(minvalue));
            if (max > 0)
            {
                return ColorBgra.FromBgraClamped(Amount1.B + 255 * (color.B - Amount1. / max, Amount1.G + 255 * (color.G - Amount1.G) / max, Amount1.R + 255 * (color.R - Amount1.R) / max, max);
            }
            return ColorBgra.FromBgra(255, 255, 255, 0);
        }

        void Render(Surface dst, Surface src, Rectangle rect)
        {
            Rectangle selection = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();

            ColorBgra CurrentPixel;
            for (int y = rect.Top; y < rect.Bottom; y++)
            {
                for (int x = rect.Left; x < rect.Right; x++)
                {
                    CurrentPixel = src[x, y];

                    dst[x, y] = TransformColor(CurrentPixel);
                }
            }
        }

        #endregion
    }
}

1

Share this post


Link to post
Share on other sites

Thank you, ReMake. I'd like to modify it to correctly deal with pixel alpha. I'm not certain about the propriety of doing so without the original programmer's permission, I think it's probably okay.

 

(I'm impressed with the simplicity of GoldenCrystal's implementation; my version of the same idea was somewhat less straight-forward.)

0

Share this post


Link to post
Share on other sites

I just tried this plugin. I think it works great. I removed a dark orange color and made a layer of purple and lavender rays with Red's Gradients Galore. 

 

I then started to play around with the blend modes and had some very pleasant surprises!

 

th_Make%20Transparent%20Original_zpsi4hv      th_Make%20Transparent%20Example_zpskdz7q      th_Make%20Transparent%20Example_01_zpsuo

 

 

This image was on my work computer and labeled 'Welcome Scan'.  Could have came with the OS or been a part of some HP software from my printer/scanner.     :)

 

 

@MJW...if you can update this plugin to work as you see as 'correctly' that would be fantastic! Thank you.

Edited by lynxster4
0

Share this post


Link to post
Share on other sites

... I'm not certain about the propriety of doing so without the original programmer's permission, I think it's probably okay.

 

It is unlikely that you will be able to contact the author. GoldenCrystal was at a forum one day only. If you make this plugin better - it will be good.

1

Share this post


Link to post
Share on other sites

I forgot I had this plugin already. Not a bad little plugin either.....

 

TEPtVj7.gif

2

Share this post


Link to post
Share on other sites

Perhaps I'm confused, but I don't think Make Transparent always works correctly. If I place a (200, 200, 200) gray layer over a (100, 100, 100) gray layer, then apply Make Transparent to the top layer with the color set to (100, 100, 100), the apparent color changes. I don't think it should.

 

I thought it might be because I made some experimental changes, but I re-downloaded the original DLL, and believe I'm using that version. Still, I thought I'd previously tested the plugin and found it worked as it should. If someone else could run the little test I described above, I'd appreciate it.

 

If it doesn't work as it ought to, I'll probably make a new plugin.

 

The test:

Starting with two layers,

Fill the upper layer with (200, 200, 200)

Fill the lower layer with (100, 100, 100)

Set the Primary Color to (100, 100, 100)

Select a region in the upper layer

Apply Make Transparent

 

The appearance of the selected region shouldn't change.

 

(ANOTHER) EDIT: I'm pretty sure that Make Transparent doesn't do what I'd like it to do, which is to change the transparency and color of each pixel in a manner that minimizes alpha while producing the original color when alpha-blended to the specified background color.  I know how to do that, but I'll have to experiment to find the best way to minimize the annoying off-by-one errors that occur due to rounding differences. Accounting for the pixel alpha is easy: it's just a matter of scaling the computed alpha by the pixel alpha. I'd always thought that was the case, but I finally went through the math, and (believe I) confirmed it.

 

ONE MORE EDIT: What I believed I confirmed is not true. The actual solution is to compute the new color and alpha relieve to the image color alpha-blended to the selected color. I've made a new plugin, Color Clearer, that's intended to do what I think it ought to do.

Edited by MJW
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now