Jump to content
How to Install Plugins ×

Diagonal Lines Plugin [v2.5, September 13th, 2009]


Recommended Posts

I've been searching for a plugin that makes diagonal lines for a long time...
Since I couldn't, I decided that maybe I should make one myself!
It turned out to be a lot easier than I thought, and here it is!

The plugin will be placed under Effects -> Render

 

Download

 

Source:

Spoiler

#region UICode
int Amount1 = 60; // [0,500] Interval
int Amount2 = 1; // [0,500] Brush Width
int Amount3 = 6; // [0,10] Anti-alias Level
int Amount4 = 45; // [0,360] Angle
bool Amount5 = true; // [0,1] Use Primary Color
ColorBgra Amount6 = ColorBgra.FromBgr(0,0,0); // Color
int Amount7 = 255; // [0,255] Alpha
#endregion

ColorBgra multiplyRGB(ColorBgra CurrentPixel, ColorBgra PrimaryColor,
                      double mul, double div)
{
    double inv_mul = div - mul;   
    if (CurrentPixel.A == 0 || Math.Round(255 * mul/div) == 255)
    {
        CurrentPixel.R = PrimaryColor.R;
        CurrentPixel.G = PrimaryColor.G;
        CurrentPixel.B = PrimaryColor.B;                               
    }
    else
    {   
        CurrentPixel.R = (byte)(((int)(PrimaryColor.R) * mul + (int)(CurrentPixel.R) * inv_mul) / div);
        CurrentPixel.G = (byte)(((int)(PrimaryColor.G) * mul + (int)(CurrentPixel.G) * inv_mul) / div);
        CurrentPixel.B = (byte)(((int)(PrimaryColor.B) * mul + (int)(CurrentPixel.B) * inv_mul) / div);
    }
    return CurrentPixel;
}   

int multiplyAlpha(int currentA, int newA)
{
    //if no alpha needed to add or if totally opaque
    if (newA == 0 || currentA == 255)
    {
        return currentA;
    }
    //if current alpha is zero, new alpha would not need adjustment   
    else if (currentA == 0 || newA == 255)
    {
        return newA;
    }
    else if (currentA < 255)
    {
        currentA += newA * (255 - currentA) / 255;
        return currentA;       
    }
    return 0;   
}   

void Render(Surface dst, Surface src, Rectangle rect)
{
    ColorBgra PrimaryColor = (ColorBgra)EnvironmentParameters.PrimaryColor;
    ColorBgra CurrentPixel;
    int interval = Amount1;
    int bwidth   = Amount2;
    int aalevel  = Amount3;
    int angle    = Amount4;
   
    if (Amount5 == false)
    {
        PrimaryColor = Amount6;
        PrimaryColor.A = (byte)(Amount7);
    }                       
   
    //modifying my (x + y) % interval Algorithm, we multiply the x and y by certain value
    double xfactor = 1;
    double yfactor = 1;
   
    //if y is longer than x, we modify y factor, and vice versa
    if (angle <= 45 || (angle > 135 && angle < 225) || angle > 315)
    {
        yfactor = Math.Tan(angle * (3.14159 / 180));
    }
    else
    {
        xfactor = 1 / Math.Tan(angle * (3.14159 / 180));
    }
   
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        for (int x = rect.Left; x < rect.Right; x++)
        {
            CurrentPixel = src[x, y];
            double testnum = (x * xfactor + y * yfactor);
            if (testnum < 0)
            {
                //make the number a positive before doing the modular (%) operation
                testnum = testnum + interval * 1000000;
            }
            double modular = testnum % interval;
       
            //if modular < thickness
            if (modular < bwidth)
            {
                if ((int)(PrimaryColor.A) == 255)
                {
                    CurrentPixel.R = (byte)PrimaryColor.R;
                    CurrentPixel.G = (byte)PrimaryColor.G;
                    CurrentPixel.B = (byte)PrimaryColor.B;
                    CurrentPixel.A = (byte)PrimaryColor.A;
                }
                else
                {
                    double alpha = (double)(PrimaryColor.A);
                    CurrentPixel = multiplyRGB(CurrentPixel, PrimaryColor, alpha, 255); //if PrimaryColor.A < 255, multiply colors
                    CurrentPixel.A = (byte)(multiplyAlpha((int)(CurrentPixel.A), (int)(alpha))); //if CurrentPixel.A < 255 and PrimaryColor.A < 255
                }
            }
            else if (aalevel > 0)
            {
                //if pixel is beside the line, we do our antialias operation
                if (modular <= bwidth + 1 ||
                    modular >= interval - 1)
                {
                    double factor = 0;
                    if (modular >= interval - 1)
                    {
                        factor = 1 - (modular - interval + 1);
                    }
                    else
                    {
                        factor = 1 - (bwidth - modular + 1);
                    }
       
                    //hacks to get around special angles
                    if (angle == 45)
                    {
                        factor += 0.8;
                    }
                    else if (angle == 90)
                    {
                        factor = 0;
                    }
                    else if (angle == 135)
                    {
                        factor -= 0.7;
                    }
       
                    //our antialias routine
                    double mul = (double)((10 - aalevel) * factor);
                    double div = mul + 1;
                    double alpha = (double)(PrimaryColor.A);   
                    ColorBgra tempPixel = new ColorBgra();
                                       
                    tempPixel = multiplyRGB(CurrentPixel, PrimaryColor, 1, div); //Antialias pixel by factor
                    CurrentPixel = multiplyRGB(CurrentPixel, tempPixel, (double)(PrimaryColor.A), 255); //if PrimaryColor.A < 255, multiply colors
                    CurrentPixel.A = (byte)(multiplyAlpha((int)(CurrentPixel.A), (int)(alpha/div))); //if CurrentPixel.A < 255, multiply alpha
                }
            }                           
       
   
            //and we are done
            dst[x, y] = CurrentPixel;
        }
    }
}

 

 


Enjoy!

Update1: added brushwidth slide bar.
Update2: added antialias level & menu icon.
Update3: fixed transparency bug.
Update4: added angle slide bar.
Update5: added colorwheel, alpha slide bar, fixed more transparency issues

 

 

Edited by toe_head2001
Formatting

siggiecj5.png

Some links: | Personal Website | Alien Attack |

Try out my plugins: | Antialias | Diagonal Lines |

Link to comment
Share on other sites

Thank you! I have wanted this for quite a while!

v An excellent open–source strategy game—highly recommended.

 

"I wish I had never been born," she said. "What are we born for?"

"For infinite happiness," said the Spirit. "You can step out into it at any moment..."

Link to comment
Share on other sites

Nooooez! Another plug-in in the "Effect" section! *Faints*

Please put it under Render.

Done

Did you use BoltBait's scanline source code for it?

Nope, I wrote the code from scratch.

-------------------------------------------------------------------------------

Update: fixed transparency bug. You can now apply the plugin using a semi-transparent brush color and it will multiply the color to the background just like what it does when you use the line/curve tool.

siggiecj5.png

Some links: | Personal Website | Alien Attack |

Try out my plugins: | Antialias | Diagonal Lines |

Link to comment
Share on other sites

I really wish I had something like this a week ago. Instead, I had to do all the lines by hand with the line tool, which took about 5 minutes, and way not accurate at all with spacing.

Ill certanly be using this a ton in the future. Thanks!

I'm still alive!

Link to comment
Share on other sites

snospmiS's userbars (in his signature) and my userbar (in my signature). Then there's LiquidCore's signature which uses diagonal lines, whether with this plugin or not is beside the point. Diagonal lines in general can be used for anything just like Clouds, Gaussian blur or even your Centre Lines plugin, it's all a case of application.

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