Jump to content

Alpha Transform (continued, updated January 22, 2022)


ReMake

Recommended Posts

This is an update of the original and slightly forgotten Alpha Transform plugin by @DW-dev. See changelog below.

 

This effect can be useful for creating an alpha mask of an image.

 

Compatible with paint.net 4.3.2+ only

 

Download from my PluginPack

 

You can find it under Adjustments -> Alpha Transform

 

This effect allows you to operate with the alpha value of a pixel based on an estimate of the maximum value of the R, G or B components (RGB color model) or its perceived brightness (YCbCr or YUV color model).

 

The pixel brightness range is determined by two thresholds. Pixels with brightness less the lower limit get alpha set to 0 (fully transparent). Pixels with brightness more the upper limit get alpha set to 255 (fully opaque). Pixels with brightness in the range between the lower and upper limits get alpha set to a value between 0 and 255 depending on their brightness.

 

Before:

Before.jpg

After:

Result.png

Color Model - a way to estimate the brightness of a pixel:
RGB - by the maximum value of R, G, or B.
YCbCr, YUV - by perceived pixel brightness.
Set Alpha = 0 ... and Set Alpha = 255 ... is define the thresholds for changing the Alpha value.
Invert Alpha values - switch between alpha and color.
Replace RGB with Color - color selection:
Source - without color replacement
Primary / Secondary / Grayscale Custom - colors that replace the source color.

 

CodeLab Source

Spoiler
// Name:Alpha Transform
// Submenu:
// Author:David Wilson & ReMake
// Title:Alpha Transform
// Version:1.3
// Desc:Alpha Transform Plugin for Paint.NET
// Keywords:paint.net|effect|alpha|transform
// URL:https://www.getpaint.net/redirect/plugins.html
#region UICode
ListBoxControl<model> Model = model.RGB; // Color Model|RGB|YCbCr|YUV
IntSliderControl aLess = 0; // [0,100] Set Alpha = 0, if pixel brightness <=
IntSliderControl aMore = 100; // [0,100] Set Alpha = 255, if pixel brightness >
CheckboxControl Invert = false; // Invert Alpha values
ListBoxControl<mode> Mode = 0; // Replace RGB with color|Source|Primary|Secondary|Grayscale|Custom
ColorWheelControl Custom = ColorBgra.FromBgr(20, 50, 140); // [!] {!Mode} 
#endregion

enum model
{
    RGB,
    YCbCr,
    YUV
}

enum mode
{
    Source,
    Primary,
    Secondary,
    Grayscale,
    Custom
}

void Render(Surface dst, Surface src, Rectangle rect)
{
    Rectangle selection = EnvironmentParameters.SelectionBounds;
    ColorBgra PrimaryColor = EnvironmentParameters.PrimaryColor;
    ColorBgra SecondaryColor = EnvironmentParameters.SecondaryColor;

    ColorBgra CurrentPixel;

    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        if (IsCancelRequested) return;
        for (int x = rect.Left; x < rect.Right; x++)
        {
            CurrentPixel = src[x,y];
            int BR = 0;
            switch (Model)
            {
                case model.RGB:
                    BR = (byte)((double)Math.Max(CurrentPixel.R, Math.Max(CurrentPixel.G, CurrentPixel.B)) / 2.55);
                    break;
                case model.YCbCr:
                    BR = (byte)(((4731 * CurrentPixel.B + 46869 * CurrentPixel.G + 13936 * CurrentPixel.R) >> 16) / 2.55);
                    break;
                case model.YUV:
                    BR = (byte)(CurrentPixel.GetIntensityByte() / 2.55);
                    break;
            }
            BR = (BR <= aLess) ? CurrentPixel.A = 0 : ((BR > aMore) ? CurrentPixel.A = 255 : CurrentPixel.A = (byte)((BR - aLess) * 255 / (aMore - aLess)));
            if (Invert)
            {
                CurrentPixel.A = (byte)(255 - CurrentPixel.A);
            }
            switch (Mode)
            {
                case mode.Source:
                    break;
                case mode.Primary:
                    CurrentPixel.R = PrimaryColor.R;
                    CurrentPixel.G = PrimaryColor.G;
                    CurrentPixel.B = PrimaryColor.B;
                    break;
                case mode.Secondary:
                    CurrentPixel.R = SecondaryColor.R;
                    CurrentPixel.G = SecondaryColor.G;
                    CurrentPixel.B = SecondaryColor.B;
                    break;
                case mode.Grayscale:
                    CurrentPixel.R = CurrentPixel.A;
                    CurrentPixel.G = CurrentPixel.A;
                    CurrentPixel.B = CurrentPixel.A;
                    break;
                case mode.Custom:
                    CurrentPixel.R = Custom.R;
                    CurrentPixel.G = Custom.G;
                    CurrentPixel.B = Custom.B;
                    break;
            }
            dst[x,y] = CurrentPixel;
        }
    }
}

 

Thanks to everyone who helped me create the interface of this effect.

 

Changelog

v1.3 (January 22, 2022)

  • Added the Grayscale item to the color selection drop-down list.
  • The icon has been changed (fixed).

 

v1.2 (October 14, 2021)

  • Added a drop-down list of color models.
  • Changed behavior of Set Alpha... sliders(MinMax dependency).
  • Added a color selection drop-down list.
  • Changed the behavior of the Grayscale Alpha checkbox.

 

v1.1 by @DW-dev (September 15, 2009)

  • Initial release
Edited by ReMake
Updated
  • Like 2
  • Upvote 1
Link to comment
Share on other sites

Thank you for the update @ReMake.  Yes, I'd forgotten about this plugin.  😊

 

Link to comment
Share on other sites

I definitely would like to see it included in your pack @ReMake.  There is so much control over what you can achieve!  ☺️

 

alphatransform_01.png

 

  • Upvote 1
Link to comment
Share on other sites

  • ReMake changed the title to Alpha Transform (continued, updated January 22, 2022)

The effect has been updated to version 1.3.

 

The Grayscale Alpha checkbox has been removed in the new version, its function has been moved to the Replace RGB with Color drop-down list. This made it possible to simplify the user interface.

 

The effect icon has been changed (fixed) according to the plan (in the previous version it was accidentally rotated 180° vertically).

  • Upvote 3
  • Hooray 2
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...