Jump to content

Help with Stripe Plugin


Pratyush

Recommended Posts

Hi @BoltBait @toe_head2001 , I am trying to write a plugin in codelab. When I run the code in Codelab, it works perfectly. but problem is When I try see see preview some error happens.

eOhfKtC.png

 

And I am also not able to build dll. 

23Vm3Qu.png

 

PFA below the my script and icon file for dll. 

 

The effect is not complete but work in progress. I will be happy If you can find out what went wrong and how to build dll. I need this effect for some important work. ( I need to draw stripes on  canvas with any specified number.)

 

// Name: Gradual Stripes
// Submenu: Render
// Author: Pratyush
// Title: Gradual Stripes
// Version: 1.0.0
// Desc: Draws stripes with gradual change in shades
// Keywords: Stripes|Grayscale|Lines
// URL:
// Help:
#region UICode
IntSliderControl Amount1 = 9; // [0,64] Slider 1 Description
#endregion

void Render(Surface dst, Surface src, Rectangle rect)
{
    // Delete any of these lines you don't need
    Rectangle selection = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();
    int CenterX = ((selection.Right - selection.Left) / 2) + selection.Left;
    int CenterY = ((selection.Bottom - selection.Top) / 2) + selection.Top;
    ColorBgra PrimaryColor = EnvironmentParameters.PrimaryColor;
    ColorBgra SecondaryColor = EnvironmentParameters.SecondaryColor;
    int BrushWidth = (int)EnvironmentParameters.BrushWidth;

    ColorBgra CurrentPixel;
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        int n = Amount1;
        int[] A = new int[n+1];
        int i;
        /* initialize elements of array n */
        A[0] = rect.Left;
        if (IsCancelRequested) return;
        for ( i = 1; i <= n; i++ )
        {
            A[ i ] = (rect.Left*(n - i) + i*rect.Right)/n;
            for (int x = A[i-1]; x < A[i]; x++)
            {
                CurrentPixel = src[x,y];
                //int p = PrimaryColor.R;
                int g1,g2,g3,g4;
                g1 = (PrimaryColor.R *(n - i) + (i -1)*SecondaryColor.R)/n;
                g2 = (PrimaryColor.G *(n - i) + (i -1)*SecondaryColor.G)/n;
                g3 = (PrimaryColor.B *(n - i) + (i -1)*SecondaryColor.B)/n;
                g4 = (PrimaryColor.A *(n - i) + (i -1)*SecondaryColor.A)/n;

                CurrentPixel.R = (byte)g1;
                CurrentPixel.G = (byte)g2;
                CurrentPixel.B = (byte)g3;
                CurrentPixel.A = (byte)g4;
                dst[x,y] = CurrentPixel;
			}
        }
    }
}

 

gradual stripes.png

MyScript_2.cs

Edited by Pratyush

Rl7un0O.png

Link to comment
Share on other sites

@MadJik Thanks for your help, my code is done and it run and build successfully. ( You solved it. :biggrin:)

 

But I want the stripes left start from zero and right less 

This value makes the last right shade equal to secondary color, while first left shade not equal to primary color. 

g1 = (PrimaryColor.R *(n - i) + (i)*SecondaryColor.R)/

But I prefer that first left shade to be equal to primary color, (RHS can be different) so I had used this code.

g1 = (PrimaryColor.R *(n - i) + (i -1)*SecondaryColor.R)/n 

That should I need to do to achieve that

Edited by Pratyush

Rl7un0O.png

Link to comment
Share on other sites

4 hours ago, Pratyush said:

g1 = (PrimaryColor.R *(n - i) + (i -1)*SecondaryColor.R)/n 

 

 

I don't think that's quite correct. Your scaling factors are (n - i) and (i - 1), and your divisor is n. But in situations like this, the sum of the scaling factors should equal the divisor, which it doesn't. I assume i runs from 0 to (n - 1), in which case you want (n - 1 - i) and i as the scaling factors and (n - 1) as the divisor.

Link to comment
Share on other sites

I think @MJW is right but I'm not skilled enough to explain it.

Maths are useful to create a plugin. You don't need the third loop, just y and x.

Stripe nr zero(0) is the first step and is equal to the primary color. The number of steps to calculate is Amount1 - 1.

Amount1 must be 2 or more (avoid the "divided by 0 error" if Amount1 = 1).

Each stripe step size per chanel RGB is (Secondary - Primary) / (Amount1 - 1) //MJW is right!

Calculate for each x in the width the stripe number (group).

 

etc...

 

The final code...

Spoiler

// Name: Gradual Stripes
// Submenu: Render
// Author: Pratyush
// Title: Gradual Stripes
// Version: 1.0.0
// Desc: Draws stripes with gradual change in shades
// Keywords: Stripes|Grayscale|Lines
// URL:
// Help:
#region UICode
IntSliderControl Amount1 = 5; // [2,64] Quantity of Gradual Stripes
ColorWheelControl Amount2 = ColorBgra.FromBgr(0,0,0); // [PrimaryColor] Quantity of Gradual Stripes
ColorWheelControl Amount3 = ColorBgra.FromBgr(255,255,255); // [SecondaryColor] Quantity of Gradual Stripes
#endregion

void Render(Surface dst, Surface src, Rectangle rect)
{
    ColorBgra PC = Amount2;
    ColorBgra SC = Amount3;

    // Step 0 is the first step, how much left to do: Amount - 1
    int Maxgroup = Amount1 - 1;

        
    // Define the size of step per Channel
    // assuming PC is the start and SC is the end
    int stepR = (SC.R - PC.R) / Maxgroup;
    int stepG = (SC.G - PC.G) / Maxgroup;
    int stepB = (SC.B - PC.B) / Maxgroup;
    int stepA = 0; //(PC.A + SC.A) / Maxgroup;
    

    ColorBgra CurrentPixel;
    
    // qunatity of shade to cover the canvas
    float divide = (float)rect.Width / Amount1;
    
    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 group = (int)(x / divide);
            CurrentPixel.R = (byte)(PC.R + stepR * group);
            CurrentPixel.G = (byte)(PC.G + stepG * group);
            CurrentPixel.B = (byte)(PC.B + stepB * group);
            CurrentPixel.A = (byte)(PC.A + stepA * group);
            dst[x,y] = CurrentPixel;
        }
    }
}

 

 

edit:

extra features (ideas) to add:

- invert colors (checkbox)

- make it vertical (checkbox)

- repeat it

- start on the center

 

  • Like 1
  • Upvote 1
Link to comment
Share on other sites

@Pratyush, you know you can make the code editor Dark, right?

 

Yeah, so in both cases it's failing to compile, because CodeLab is generating UI code for Amount2 and Amount3, but the fields backing that code are commented out.  We'll correct this for future versions of CodeLab.

Edited by toe_head2001

(September 25th, 2023)  Sorry about any broken images in my posts. I am aware of the issue.

bp-sig.png
My Gallery  |  My Plugin Pack

Layman's Guide to CodeLab

Link to comment
Share on other sites

7 hours ago, MadJik said:

Each stripe step size per chanel RGB is (Secondary - Primary) / (Amount1 - 1) //MJW is right

 

Yup, you were right. after changing its value now stripes start from Primary Color and end with Secondary Color. I was feeling something is miss because both side doesn't matching to primary and secondary color.

Rl7un0O.png

Link to comment
Share on other sites

5 hours ago, toe_head2001 said:

in both cases it's failing to compile, because CodeLab is generating UI code for Amount2 and Amount3, but the fields backing that code are commented out.  We'll correct this for future versions of CodeLab.

 

I coded a fix for this.  It will be in the next release of CodeLab.

 

Basically, it will ignore any commented out lines.  If you use the UIBuilder, when you click OK, those lines will be deleted.

  • Like 1
  • Upvote 3
Link to comment
Share on other sites

Hi,

I added feature for repeating stripes. I was trying to add choice of vertical and horizontal stripes.

In case of vertical stripes it's working fine. But when I try to create horizontal stripes it gets some problem.

Here is code.

 

// Name: Gradual Stripes
// Submenu: Render
// Author: Pratyush
// Title: Gradual Stripes
// Version: 1.2.0
// Desc: Draws stripes with gradual change in shades
// Keywords: Stripes|Grayscale|Lines
// URL:
// Help:
#region UICode
IntSliderControl Amount1 = 5; // [2,64] Quantity of Gradual Stripes
IntSliderControl Amount2 = 1; // [1,10] No. of repeatation
ColorWheelControl Amount3 = ColorBgra.FromBgr(0,0,0); // [PrimaryColor] Primary Color
ColorWheelControl Amount4 = ColorBgra.FromBgr(255,255,255); // [SecondaryColor] Secondary Color
CheckboxControl Amount5 = false; // [0,1] Vertical
#endregion

void Render(Surface dst, Surface src, Rectangle rect)
{
    ColorBgra PC = Amount3;
    ColorBgra SC = Amount4;

    // Step 0 is the first step, how much left to do: Amount - 1
    int Maxgroup = Amount1 - 1;
    //Maxgroup = Maxgroup;


    // Define the size of step per Channel
    // assuming PC is the start and SC is the end
    int stepR = (SC.R - PC.R) / Maxgroup;
    int stepG = (SC.G - PC.G) / Maxgroup;
    int stepB = (SC.B - PC.B) / Maxgroup;
    int stepA = 0; //(PC.A + SC.A) / Maxgroup;


    ColorBgra CurrentPixel;
    float divide;
    // qunatity of shade to cover the canvas
    if (Amount5 == false)
    {

        divide = (float)rect.Width / Amount1;
        divide = divide/Amount2;
        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 group = (int)(x / divide);
                group = group%Amount1;
                CurrentPixel.R = (byte)(PC.R + stepR * group);
                CurrentPixel.G = (byte)(PC.G + stepG * group);
                CurrentPixel.B = (byte)(PC.B + stepB * group);
                CurrentPixel.A = (byte)(PC.A + stepA * group);
                dst[x,y] = CurrentPixel;
            }
        }
    }
    else
    {
        divide = (float)rect.Height / Amount1;
        divide = divide/Amount2;

        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 group = (int)(y / divide);
                group = group%Amount1;
                CurrentPixel.R = (byte)(PC.R + stepR * group);
                CurrentPixel.G = (byte)(PC.G + stepG * group);
                CurrentPixel.B = (byte)(PC.B + stepB * group);
                CurrentPixel.A = (byte)(PC.A + stepA * group);
                dst[x,y] = CurrentPixel;
            }
        }
    }

}

 

78c8WBF.jpg

But in case of horizontal i get following. 

6KYBHJz.jpg

 

What should we need to do get output.

Rl7un0O.png

Link to comment
Share on other sites

Spoiler

// Name: Gradual Stripes
// Submenu: Render
// Author: Pratyush
// Title: Gradual Stripes
// Version: 1.2.0
// Desc: Draws stripes with gradual change in shades
// Keywords: Stripes|Grayscale|Lines
// URL:
// Help:

#region UICode
IntSliderControl Amount1 = 5; // [2,64] Stripes
IntSliderControl Amount2 = 1; // [1,10] Repetitions
ColorWheelControl Amount3 = ColorBgra.FromBgr(0,0,0); // [PrimaryColor] Primary Color
ColorWheelControl Amount4 = ColorBgra.FromBgr(255,255,255); // [SecondaryColor] Secondary Color
CheckboxControl Amount5 = false; // [0,1] Vertical
#endregion

void Render(Surface dst, Surface src, Rectangle rect)
{
    ColorBgra PC = Amount3;
    ColorBgra SC = Amount4;
    Rectangle sel = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();
    // Step 0 is the first step, how much left to do: Amount - 1
    int Maxgroup = Amount1 - 1;
    //Maxgroup = Maxgroup;


    // Define the size of step per Channel
    // assuming PC is the start and SC is the end
    int stepR = (SC.R - PC.R) / Maxgroup;
    int stepG = (SC.G - PC.G) / Maxgroup;
    int stepB = (SC.B - PC.B) / Maxgroup;
    int stepA = 0; //(PC.A + SC.A) / Maxgroup;
    int group = 0;

    ColorBgra CurrentPixel;
    int divide;
    // quantity of shade to cover the canvas

    if (!Amount5)
    {
        divide = (sel.Height / Amount1)/Amount2;
    }
    else
    {
        divide = (sel.Width / Amount1)/Amount2;
    }

    for (int y = sel.Top; y < sel.Bottom; y++)
    {
        if (IsCancelRequested) return;

        for (int x = sel.Left; x < sel.Right; x++)
        {
            CurrentPixel = src[x,y];
            if (!Amount5)
            {
                group = y / divide;
            }
            else
            {
                group = x / divide;
            }
            group = group%Amount1;
            CurrentPixel.R = (byte)(PC.R + stepR * group);
            CurrentPixel.G = (byte)(PC.G + stepG * group);
            CurrentPixel.B = (byte)(PC.B + stepB * group);
            CurrentPixel.A = (byte)(PC.A + stepA * group);
            dst[x,y] = CurrentPixel;
        }
    }
}

 

 

Edited by xod
  • Upvote 1
Link to comment
Share on other sites

Hi @xod there is something happening, 

dGCVCrT.png

 

In the end there are some more stripes are getting rendered (no. of extra stripes is (quantity of gradual stripes)/(No. of repetition)-1, if both are divisible).

Rl7un0O.png

Link to comment
Share on other sites

On 1/20/2018 at 5:03 AM, toe_head2001 said:

you know you can make the code editor Dark, right?

 

Thanks for reminding that I thought that its Dark theme automatically activates when PDN is dark. Its dark theme is very pleasing.

Edited by Pratyush

Rl7un0O.png

Link to comment
Share on other sites

50 minutes ago, Pratyush said:

... I thought that its Dark theme automatically activates when PDN is dark...

Perhaps in a future version. Rick has not given us an official way to detect PDN's dark theme. That is to say, CodeLab doesn't know which PDN theme is active; it just inherits the colors.

(September 25th, 2023)  Sorry about any broken images in my posts. I am aware of the issue.

bp-sig.png
My Gallery  |  My Plugin Pack

Layman's Guide to CodeLab

Link to comment
Share on other sites

13 hours ago, Pratyush said:

int group = (int)(y / divide);

 

I had forced the group number to be an integer, but in XOD's suggestion that was missing. I will try including that and test.

Rl7un0O.png

Link to comment
Share on other sites

if (group > Maxgroup) group = Maxgroup;

 

The last stripe could be longer than the others... due to the reminder of the division.

 

edit:

Doesn't work with repetition.

 

Solution:

Spoiler

// Name: Gradual Stripes
// Submenu: Render
// Author: Pratyush
// Title: Gradual Stripes
// Version: 1.2.0
// Desc: Draws stripes with gradual change in shades
// Keywords: Stripes|Grayscale|Lines
// URL:
// Help:

#region UICode
IntSliderControl Amount1 = 9; // [2,64] Stripes
IntSliderControl Amount2 = 7; // [1,10] Repetitions
ColorWheelControl Amount3 = ColorBgra.FromBgr(0,0,0); // [PrimaryColor] Primary Color
ColorWheelControl Amount4 = ColorBgra.FromBgr(255,255,255); // [SecondaryColor] Secondary Color
CheckboxControl Amount5 = false; // [0,1] Vertical
#endregion

void Render(Surface dst, Surface src, Rectangle rect)
{
    ColorBgra PC = Amount3;
    ColorBgra SC = Amount4;
    Rectangle sel = EnvironmentParameters.GetSelection(src.Bounds).GetBoundsInt();
    // Step 0 is the first step, how much left to do: Amount - 1
    int Maxgroup = Amount1 - 1;


    // Define the size of step per Channel
    // assuming PC is the start and SC is the end
    int stepR = (SC.R - PC.R) / Maxgroup;
    int stepG = (SC.G - PC.G) / Maxgroup;
    int stepB = (SC.B - PC.B) / Maxgroup;
    int stepA = (SC.A - PC.A) / Maxgroup;
    int group = 0;
    float grheight=(float)(Amount1 * Amount2 / (float)sel.Height);
    float grwidth = (float)(Amount1 * Amount2 / (float)sel.Width);
    ColorBgra CurrentPixel;

    for (int y = sel.Top; y < sel.Bottom; y++)
    {
        if (IsCancelRequested) return;
        if (!Amount5) group = (int)(y * grheight);
        for (int x = sel.Left; x < sel.Right; x++)
        {
            CurrentPixel = src[x,y];
            if (Amount5) group = (int)(x * grwidth);
            group = group % Amount1;
            CurrentPixel.R = (byte)(PC.R + stepR * group);
            CurrentPixel.G = (byte)(PC.G + stepG * group);
            CurrentPixel.B = (byte)(PC.B + stepB * group);
            CurrentPixel.A = (byte)(PC.A + stepA * group);
            dst[x,y] = CurrentPixel;
        }
    }
}

 

 

  • Like 1
Link to comment
Share on other sites

13 hours ago, Pratyush said:

Hi @xod , How did it worked?

Normally when we go for horizontal lines , it gets divided into ROI's due to multi threading. 

 

 

// Name: 
// Submenu: 
// Author: 
// Title: 
// Version: 
// Desc: 
// Keywords: 
// URL:
// Help:

#region UICode
IntSliderControl Amount1 = 1; // [0,10] Test
#endregion

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

    dst.CopySurface(src, rect.Location, rect);
    RenderArgs ra = new RenderArgs(dst);
    Graphics g = ra.Graphics;
    g.Clip = new Region(rect);

    g.SmoothingMode = SmoothingMode.AntiAlias;
    g.TextRenderingHint = TextRenderingHint.AntiAlias;

    using (SolidBrush textBrush = new SolidBrush(Color.Black))
    using (Font textFont = new Font("Tahoma", 14, FontStyle.Regular))
    {
        g.DrawString("rect.Width: " + rect.Width.ToString(), textFont, textBrush, 10, 0);
        
        g.DrawString("rect.Height: " + rect.Height.ToString(), textFont, textBrush, 10, 30);
        //rect.Height return a value 100 times smaller than the height of the canvas
        //you can try with another size of the canvas
        
        g.DrawString("sel.Width: " + sel.Width.ToString(), textFont, textBrush, 10, 60);
        
        g.DrawString("sel.Height: " + sel.Height.ToString(), textFont, textBrush, 10, 90);        
    }
}

 

  • Like 1
Link to comment
Share on other sites

9 hours ago, toe_head2001 said:

Perhaps in a future version. Rick has not given us an official way to detect PDN's dark theme. That is to say, CodeLab doesn't know which PDN theme is active; it just inherits the colors.

 

In my plugin Align object I used this method:

RegistryKey regkey = Registry.CurrentUser.OpenSubKey("Software\\paint.net");
Object getValueData = regkey.GetValue("UI/AeroColorScheme"); //read the value from registry

I do not know it is allowed or not.

 

7crVqxZ.png

 

Link to comment
Share on other sites

8 minutes ago, xod said:

I do not know it is allowed or not.

That's a very bad idea for a number of reasons. Not to mention it will give you false positives.

Your plugin thinks the Dark theme is active, but it's not.  It looks like your plugin is very confused. :P

badDarkDetect.png

 

You should be using the UseAppThemeColors property.

(September 25th, 2023)  Sorry about any broken images in my posts. I am aware of the issue.

bp-sig.png
My Gallery  |  My Plugin Pack

Layman's Guide to CodeLab

Link to comment
Share on other sites

On 1/21/2018 at 11:22 AM, Pratyush said:

Hi,

I added feature for repeating stripes. I was trying to add choice of vertical and horizontal stripes.

In case of vertical stripes it's working fine. But when I try to create horizontal stripes it gets some problem.

 

Consider one of the lines:

divide = (float)rect.Width / Amount1;

That's wrong. "rect" is the rectangle for the ROI. You can't have values that are used in the calculations depend on the dimensions of the ROI. It may work sometimes, especially when there's no selection, but it won't work in general. Values like that can only depend on the surface dimensions or the selection dimensions, never on the ROI dimensions. The  choice between the surface dimensions or the selection dimensions depends on what you're trying to do. (Instead of "selection dimensions," it would be more precise to say the dimensions of the selection's  bounding rectangle.)

 

I believe using the surface height and width will fix your problem. You might consider putting the setup in PreRender. If you want do some setup that can't be done in PreRender, you should ask yourself if you're doing it right. The only additional information you have in Render is about the ROI, and it's a rare plugin, indeed, that needs that for anything other than controlling which destination pixels to process.

 

On a more minor point. If you're going to do things in floating point, you should compute the reciprocal of "divide" so you can replace divisions with multiplications. Multiplies are considerably faster.

  • Upvote 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.

×
×
  • Create New...