Jump to content

Plugin Request - Turn Selection to the Most Used Color in it


Recommended Posts

Hello, I have a plugin request, the attached image should explain it. I don't need color+transparencies to count as individual colors, just the color data. This will be helpful for my special method of resizing images which has better results than nearest neighbor but which it's very tedious to count the pixels in each region so I'm requesting just a ctrl+f that can recolor each region for me. Thank you.

 

request.png

Link to comment
Share on other sites

Indeed the performance could be quite lousy if the selection is large.

 

Try this code with CodeLab:

Spoiler

ColorBgra mode = ColorBgra.Zero;
bool collision = false;

void PreRender(Surface dst, Surface src)
{
    Dictionary<ColorBgra, int> foundColors = new Dictionary<ColorBgra, int>();
    Rectangle[] selRects = EnvironmentParameters.GetSelection(src.Bounds).GetRegionScansInt();
    ColorBgra currentPixel;
    foreach (Rectangle rect in selRects)
    {
        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].NewAlpha(byte.MaxValue);
                if (foundColors.ContainsKey(currentPixel))
                {
                    foundColors[currentPixel]++;
                }
                else
                {
                    foundColors.Add(currentPixel, 1);
                }
            }
        }
    }

    KeyValuePair<ColorBgra, int> kvpMode = new KeyValuePair<ColorBgra, int>(ColorBgra.Zero, 0);
    foreach (var item in foundColors)
    {
        if (item.Value > kvpMode.Value)
        {
            kvpMode = item;
        }
    }

    mode = kvpMode.Key;
    collision = foundColors.Values.Where(value => value == kvpMode.Value).Count() > 1;
    if (collision)
    {
        PaintDotNet.Threading.PdnSynchronizationContext.Instance.Send(new SendOrPostCallback(delegate(object state)
        {
            // This line runs on the UI thread and not on the Render thread
            MessageBox.Show("Color Collision");
        }), null);
    }
}

void Render(Surface dst, Surface src, Rectangle rect)
{
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        if (IsCancelRequested) return;
        for (int x = rect.Left; x < rect.Right; x++)
        {
            dst[x,y] = collision ? src[x,y] : mode;
        }
    }
}

 

 

https://forums.getpaint.net/topic/111233-codelab-for-average-users-a-laymans-guide/

 

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