Sign in to follow this  
FanofSMBX

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

Share this post


Link to post
Share on other sites

Can mod please change the title to reversed "Turn Selection to the Most Used Color in it (Plugin Request)" since it is hard to see from the forum homepage. Thank you

Share this post


Link to post
Share on other sites

You can change it by editing your initial post.

Share this post


Link to post
Share on other sites

Is there some limitations on how it would be used. A selection could contain a million colors or more.

 

If you don't mind me asking, what is the purpose of this proposed plugin? Why do to need a plugin to do this?

Share this post


Link to post
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/

 

Share this post


Link to post
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.

Sign in to follow this