Jump to content

Codelab Snippets Thread


Recommended Posts

I figure we need a place to put all of our small tidbits of code we use to simplify tasks.

Here are some of my codelab scripts:

Red channel:

void Render(Surface dst, Surface src, Rectangle rect)
{
   for(int y = rect.Top; y < rect.Bottom; y++)
   {
       for (int x = rect.Left; x < rect.Right; x++)
       {
           ColorBgra color = src[x, y];
           color.B = 0;
           color.G = 0;
           dst[x, y] = color;
       }
   }
}

Blue channel:

void Render(Surface dst, Surface src, Rectangle rect)
{
   for(int y = rect.Top; y < rect.Bottom; y++)
   {
       for (int x = rect.Left; x < rect.Right; x++)
       {
           ColorBgra color = src[x, y];
           color.R = 0;
           color.G = 0;
           dst[x, y] = color;
       }
   }
}

Green channel:

void Render(Surface dst, Surface src, Rectangle rect)
{
   for(int y = rect.Top; y < rect.Bottom; y++)
   {
       for (int x = rect.Left; x < rect.Right; x++)
       {
           ColorBgra color = src[x, y];
           color.B = 0;
           color.R = 0;
           dst[x, y] = color;
       }
   }
}

Invert alpha channel:

void Render(Surface dst, Surface src, Rectangle rect)
{
   for(int y = rect.Top; y < rect.Bottom; y++)
   {
       for (int x = rect.Left; x < rect.Right; x++)
       {
           ColorBgra color = src[x, y];
           color.A = (byte)(255-color.A);
           dst[x, y] = color;
       }
   }
}

Pixel value to alpha:

void Render(Surface dst, Surface src, Rectangle rect)
{
   for(int y = rect.Top; y < rect.Bottom; y++)
   {

       for (int x = rect.Left; x < rect.Right; x++)
       {
           byte full = (byte)(255);
           ColorBgra c = src[x, y];
           int luma = (int)(c.R*0.3 + c.G*0.59+ c.B*0.11);
           c.A = (byte)(255-luma);
           dst[x, y] = c; 
       }
   }
}

I have several others.. :)

You are encouraged to post your scripts and constructively criticize others' scripts.

P.S. put the channels in three layers (R, B, and G), stting each layer's mode to additive if you want to edit in channels...

Sources EDITED for less stupidity...

~~

Link to comment
Share on other sites

I must ask why you are declaring the "color" variable outside of the Render function ... that instantly makes it not thread safe. Just put it inside the Render function and you're good.

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

forumSig_bmwE60.jpg

Link to comment
Share on other sites

Pixels do not contain HSV values. They are always stored as BGRA. You must convert to HSV, do manipulation in that form, and then convert back to BGRA.

The Paint.NET Blog: https://blog.getpaint.net/

Donations are always appreciated! https://www.getpaint.net/donate.html

forumSig_bmwE60.jpg

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