Jump to content

Please help with "Kill Color Keeper" plugin.


Recommended Posts

Hi Everyone,

 

Being a beginner I know next to nothing about programming and I am trying to achieve something.

 

Here, I wanted to make amendment in UI for Kill color Keeper like this. This was to include dropdown from Jotaf's original plugin.

 

94pqGO3.png

Previous UI was.

Ke1os7u.png

 

In UI region I Added,

#region UICode
DoubleSliderControl Amount1 = 1; // [0,20] Color tolerance:
ColorWheelControl Amount2 = ColorBgra.FromBgr(0,0,0); // [PrimaryColor] {!Amount3} Select Color
CheckboxControl Amount3 = false; // [0,1] Use specific Values to Select Colors
ListBoxControl Amount4 = 0; // {Amount3} What Color?|What color?|Primary Color|Secondary Color|White|Gray|Black
RadioButtonControl Amount5 = 0; // [1] Functions|Keep Color|Kill Color
IntSliderControl Amount6 = 0; // [0,255] Consider transparent any alpha smaller than:
IntSliderControl Amount7 = 255; // [0,255] {Amount5} Consider transparent any alpha greater than:
#endregion

And for assigning input color value I updated this line

ColorBgra color = Amount2;

into this block of codes.

ColorBgra color;
    if(Amount3)
        color = Amount2;
    else
      //selecting things from dropdown
        switch(Amount4)
        {   
            case 0: // Primary color
          
                color = (ColorBgra)EnvironmentParameters.PrimaryColor;
                break;
            case 1: // Secondary color
                color = (ColorBgra)EnvironmentParameters.SecondaryColor;
                break;
            case 2: // White
                color = ColorBgra.White;
                break;
            case 3: // Gray
                color = ColorBgra.Gray;
                break;
            default: // Black
                color = ColorBgra.Black;
                break;
        }   

 

So, In UI output is as expected, for the drop-down & Color-Wheel are enabled and disabled and working as expected. But somehow the selection of color is not working at all.

 

i.e. The behavior is I am seeing.

  • When checkbox for "specific values" is checked, Dropdown is enabled as expected, I can choose color from dropdown but Output for the selected Color from dropdown doesn't reflect on Canvas.
  • When checkbox for "specific values" is uncheckedColorWheel is  enabled as expected, I can change value for color but Again output for the selected Color from ColorWheel doesn't reflect on Canvas.

 

UI is eternally struck on Primary Color which is default value. If I comment the whole "if else and switch" block of new code,  and replace it with simply "ColorBgra color = Amount2;" every thing starts working again magically. How to make the it work?  I am at my wit's end and I can't understand what is happening here. :cry::cry::cry:.

 

Here is the full code.:arrow-down:


Code:

 

Spoiler

// Name:Kill Color Keeper
// Submenu: Color
// Author: Pratyush
// Title: KillColorKeeper
// Version: 1.2.0
// Desc: Removes or Preserves a color while maintaining the alpha information
// Keywords: Color|Transparency|Alpha|Retention|Background
// URL:
// Help:
#region UICode
DoubleSliderControl Amount1 = 1; // [0,20] Color tolerance:
ColorWheelControl Amount2 = ColorBgra.FromBgr(0,0,0); // [PrimaryColor] {!Amount3} Select Color
CheckboxControl Amount3 = false; // [0,1] Use specific Values to Select Colors
ListBoxControl Amount4 = 0; // {Amount3} What Color?|What color?|Primary Color|Secondary Color|White|Gray|Black
RadioButtonControl Amount5 = 0; // [1] Functions|Keep Color|Kill Color
IntSliderControl Amount6 = 0; // [0,255] Consider transparent any alpha smaller than:
IntSliderControl Amount7 = 255; // [0,255] {Amount5} Consider transparent any alpha greater than:
#endregion

//Kill Color Keeper by Pratyush
//Based on Original Grim Color Reaper by Jotaf

void Render(Surface dst, Surface src, Rectangle rect)
{
    ColorBgra color = Amount2;
    //ColorBgra color;
    //if(Amount3)
    //    color = Amount2;
    //else
    //  //selecting things from dropdown
    //    switch(Amount4)
    //    {   
    //        case 0: // Primary color
            
    //            color = (ColorBgra)EnvironmentParameters.PrimaryColor;
    //            break;
    //        case 1: // Secondary color
    //            color = (ColorBgra)EnvironmentParameters.SecondaryColor;
    //            break;
    //        case 2: // White
    //            color = ColorBgra.White;
    //            break;
    //        case 3: // Gray
    //            color = ColorBgra.Gray;
    //            break;
    //        default: // Black
    //            color = ColorBgra.Black;
    //            break;
    //    }   


    //color as double
    double R = color.R, G = color.G, B = color.B;

    ColorBgra pixel;
    double rdif, gdif, bdif;
    double alpha;

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

            //difference between this pixel's color and the color to erase
            rdif = pixel.R - R;
            gdif = pixel.G - G;
            bdif = pixel.B - B;

            //simple way of figuring out the alpha: the more distant the colors,
            //the larger the alpha.
            alpha = Amount1 * Math.Sqrt(rdif*rdif + gdif*gdif + bdif*bdif);

            if (Amount5 == 1)
            {
                if (alpha <= Amount6)  //alpha cut-off (fully transparent)
                    pixel.A = 0;

                else if (alpha < pixel.A)    //only apply change if it causes a pixel to become more
                {                            //transparent (to blend nicely with existing alphas)
                    pixel.A = (byte) alpha;

                    alpha = alpha / 255;  //normalize alpha to range 0..1

                    //we assume that each pixel is the result of linear interpolation between an
                    //unknown foreground color, and the user selected background color, which
                    //means it obeys the equation: (for each of Red, Green and Blue)
                    //  final = foreground*alpha + background*(1-alpha)
                    //we already figured out the alpha, so we just need to invert the equation
                    //to obtain the original foreground color:
                    //  foreground = (final - background*(1-alpha)) / alpha
                    pixel.R = Int32Util.ClampToByte((int) (((double) pixel.R - R*(1-alpha)) / alpha));
                    pixel.G = Int32Util.ClampToByte((int) (((double) pixel.G - G*(1-alpha)) / alpha));
                    pixel.B = Int32Util.ClampToByte((int) (((double) pixel.B - B*(1-alpha)) / alpha));
                }
            }
            else
            {

              	if (Amount6 >= pixel.A)  //Ignore transparent pixels
                    pixel.A = 0;

                else if (alpha >= Amount7)  //alpha cut-off (fully opaque)
                    pixel.A = 0;

                else if (alpha > pixel.A)    //only apply change if it causes a pixel to become more
                {
                    //transparent (to blend nicely with existing alphas)
                    pixel.A = (byte) alpha;

                    alpha = alpha / 255;  //normalize alpha to range 0..1

                    pixel.R = Int32Util.ClampToByte((int) (((double) pixel.R - R*(1-alpha)) / alpha));
                    pixel.G = Int32Util.ClampToByte((int) (((double) pixel.G - G*(1-alpha)) / alpha));
                    pixel.B = Int32Util.ClampToByte((int) (((double) pixel.B - B*(1-alpha)) / alpha));
                }
            }
            dst[x,y] = pixel;
        }
    }
}

 

Thanks and Regards.

Edited by Pratyush

Rl7un0O.png

Link to comment
Share on other sites

Look at this version of your code:

Spoiler

// Name:Kill Color Keeper
// Submenu: Color
// Author: Pratyush
// Title: KillColorKeeper
// Version: 1.2.0
// Desc: Removes or Preserves a color while maintaining the alpha information
// Keywords: Color|Transparency|Alpha|Retention|Background
// URL:
// Help:
#region UICode
DoubleSliderControl Amount1 = 1; // [0,20] Color tolerance:
ColorWheelControl Amount2 = ColorBgra.FromBgr(0,0,0); // [PrimaryColor] {Amount3} Select Color
ListBoxControl Amount3 = 0; // {Amount3} What Color?|Custom|Primary Color|Secondary Color|White|Gray|Black
RadioButtonControl Amount4 = 0; // [1] Functions|Keep Color|Kill Color
IntSliderControl Amount5 = 0; // [0,255] Consider transparent any alpha smaller than:
IntSliderControl Amount6 = 255; // [0,255] {Amount4} Consider transparent any alpha greater than:
#endregion

//Kill Color Keeper by Pratyush
//Based on Original Grim Color Reaper by Jotaf

void Render(Surface dst, Surface src, Rectangle rect)
{


    ColorBgra pixel;
    double rdif, gdif, bdif;
    double alpha;

    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        for (int x = rect.Left; x < rect.Right; x++)
        {
            ColorBgra color;
            if(Amount3==0)
            {color = Amount2;}
            else
            //selecting things from dropdown
            switch(Amount3)
            {
                case 1: // Primary color
                    color = (ColorBgra)EnvironmentParameters.PrimaryColor;
                    break;
                case 2: // Secondary color
                    color = (ColorBgra)EnvironmentParameters.SecondaryColor;
                    break;
                case 3: // White
                    color = ColorBgra.White;
                    break;
                case 4: // Gray
                    color = ColorBgra.Gray;
                    break;
                case 5: // Black
                    color = ColorBgra.Black;
                    break;
                default: // Custom
                    color = Amount2;
                    break;
            }


            //color as double
            double R = color.R, G = color.G, B = color.B;

            pixel = src[x,y];

            //difference between this pixel's color and the color to erase
            rdif = pixel.R - R;
            gdif = pixel.G - G;
            bdif = pixel.B - B;

            //simple way of figuring out the alpha: the more distant the colors,
            //the larger the alpha.
            alpha = Amount1 * Math.Sqrt(rdif*rdif + gdif*gdif + bdif*bdif);

            if (Amount4 == 1)
            {
                if (alpha <= Amount5)  //alpha cut-off (fully transparent)
                    pixel.A = 0;

                else if (alpha < pixel.A)    //only apply change if it causes a pixel to become more
                {                            //transparent (to blend nicely with existing alphas)
                    pixel.A = (byte) alpha;

                    alpha = alpha / 255;  //normalize alpha to range 0..1

                    //we assume that each pixel is the result of linear interpolation between an
                    //unknown foreground color, and the user selected background color, which
                    //means it obeys the equation: (for each of Red, Green and Blue)
                    //  final = foreground*alpha + background*(1-alpha)
                    //we already figured out the alpha, so we just need to invert the equation
                    //to obtain the original foreground color:
                    //  foreground = (final - background*(1-alpha)) / alpha
                    pixel.R = Int32Util.ClampToByte((int) (((double) pixel.R - R*(1-alpha)) / alpha));
                    pixel.G = Int32Util.ClampToByte((int) (((double) pixel.G - G*(1-alpha)) / alpha));
                    pixel.B = Int32Util.ClampToByte((int) (((double) pixel.B - B*(1-alpha)) / alpha));
                }
            }
            else
            {

              	if (Amount5 >= pixel.A)  //Ignore transparent pixels
                    pixel.A = 0;

                else if (alpha >= Amount6)  //alpha cut-off (fully opaque)
                    pixel.A = 0;

                else if (alpha > pixel.A)    //only apply change if it causes a pixel to become more
                {
                    //transparent (to blend nicely with existing alphas)
                    pixel.A = (byte) alpha;

                    alpha = alpha / 255;  //normalize alpha to range 0..1

                    pixel.R = Int32Util.ClampToByte((int) (((double) pixel.R - R*(1-alpha)) / alpha));
                    pixel.G = Int32Util.ClampToByte((int) (((double) pixel.G - G*(1-alpha)) / alpha));
                    pixel.B = Int32Util.ClampToByte((int) (((double) pixel.B - B*(1-alpha)) / alpha));
                }
            }
            dst[x,y] = pixel;
        }
    }
}

 

This my code is non-optimal. You need to analyse it and then to optimize.

  • Like 1
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.

 Share

×
×
  • Create New...