Jump to content

Make Palette Plugin v1.0 (February 15, 2021)


geist_

Recommended Posts

VERSION: Only checked for paint.net version 4.2.15.

 

Adjustments > Make Palette(.dll)

 

As the name suggests, from the current selection the plugin/adjustment will make an "UntitledPalette.txt" file in your \Documents\paint.net User Files\Palettes with each color taken only once.
It also displays how many unique colors are in the palette.

 

 

 

MakePalette.zip

Edited by geist_
3/11/2021 updated to toe_head's code.
  • Upvote 1

the usual

 

0054-0133.gif

Link to comment
Share on other sites

@geist_ thanks for sharing your plugin 🙂. I tried it on two different images and it created a separate palette for both but it only created the palette with the color most used in the image. I do have 2 requests if they are not too much trouble. Could the plugin save the palette to Documents\paint.net App Files\Palettes instead? That is where the other palettes appear. And lastly, is there a way to allow the user to give the palette a name before it saves? I have included the 2 images I tried the plugin on and provided a link to the corresponding palette that was created. Thanks again for your time to make the plugin.

 

Image 1 and link to corresponding palette:

UntitledPalette1.txt

 

jK4IjP.png

 

Image 2 and link to corresponding palette:

UntitledPalette2.txt

 

jK4mtM.png

  • Like 1

Click HERE to see my Pictorium of images I have worked on.

 

lMbNWe.png

You were once wild here. Don't let them tame you.

 ~ Isadora Duncan

Link to comment
Share on other sites

Crashed. Twice.

 

File: C:\Program Files\Paint.NET\Effects\Make Palette.dll
      Name: MakePaletteEffect.MakePaletteEffectPlugin
      Version: 1.0.7716.40392
      Author: Copyright ©2021
      Copyright: Make Palette selected pixels
      Website: https://www.getpaint.net/redirect/plugins.html
      Full error message: PaintDotNet.WorkerThreadException: Worker thread threw an exception ---> System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\users\Ray\Documents\paint.net User Files\Palettes\UntitledPalette1.txt'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
   at MakePaletteEffect.MakePaletteEffectPlugin.MakePaletteFile(String _fileName, List`1 _colors, Int32 _colorsAmount)
   at MakePaletteEffect.MakePaletteEffectPlugin.Render(Surface dst, Surface src, Rectangle rect)
   at MakePaletteEffect.MakePaletteEffectPlugin.OnRender(Rectangle[] rois, Int32 startIndex, Int32 length)
   at PaintDotNet.Effects.Effect`1.Render(EffectConfigToken parameters, RenderArgs dstArgs, RenderArgs srcArgs, Rectangle[] rois, Int32 startIndex, Int32 length) in D:\src\pdn\src\Effects\Effect`1.cs:line 99
   at PaintDotNet.Effects.BackgroundEffectRenderer.RenderWithClipMask(Effect effect, EffectConfigToken token, RenderArgs dstArgs, RenderArgs srcArgs, RectInt32[] rois, Result`1 lazyClipMaskRenderer) in D:\src\pdn\src\PaintDotNet\Effects\BackgroundEffectRenderer.cs:line 192
   at PaintDotNet.Effects.BackgroundEffectRenderer.RendererContext.RenderTile(EffectConfigToken token, Int32 tileIndex) in D:\src\pdn\src\PaintDotNet\Effects\BackgroundEffectRenderer.cs:line 172
   at PaintDotNet.Effects.BackgroundEffectRenderer.RendererContext.RenderNextTile(EffectConfigToken token) in D:\src\pdn\src\PaintDotNet\Effects\BackgroundEffectRenderer.cs:line 164
   at PaintDotNet.Effects.BackgroundEffectRenderer.ThreadFunction() in D:\src\pdn\src\PaintDotNet\Effects\BackgroundEffectRenderer.cs:line 282
   --- End of inner exception stack trace ---
   at PaintDotNet.Effects.BackgroundEffectRenderer.DrainExceptions() in D:\src\pdn\src\PaintDotNet\Effects\BackgroundEffectRenderer.cs:line 451
   at PaintDotNet.Menus.EffectMenuBase.DoEffect(Effect effect, EffectConfigToken token, Result`1 lazySelectionScans, Result`1 lazyRegionToRenderScans, Result`1 lazyClipMaskRenderer, Surface originalSurface, Exception& exception) in D:\src\pdn\src\PaintDotNet\Menus\EffectMenuBase.cs:line 0

 

  • Like 1
Link to comment
Share on other sites

@toe_head2001 @ANONYMISS I think the reason it crashed is because it's supposed to store the file in C:\Users\name\Documents\paint.net User Files\Palettes\ 

which i thought was made by default. Also @Ladybug thanks for checking it out, it means a lot! The main reason i made this was for characters/item sprites, not really for landscapes, and yes, i will change it so the user can choose a name for the file before it saves it. Here's the source code:

MakePalette.zip

the usual

 

0054-0133.gif

Link to comment
Share on other sites

@geist_ I tried it again on one of my images and it did create more than one color but the colors looked washed out. Then I tried it on a sprite image and it worked beautifully. The image location still needs a bit of editing since it only saves it to Documents\paint.net App Files\. Just add the Palettes folder name. It did not give me the option to give it a name but perhaps you are still working on that. Thanks for sharing your coding abilities with some of us who have none, lol. Others on here are very well versed and can help you tweak the plugin if it needs to be.

  • Like 1

Click HERE to see my Pictorium of images I have worked on.

 

lMbNWe.png

You were once wild here. Don't let them tame you.

 ~ Isadora Duncan

Link to comment
Share on other sites

@ANONYMISS, not sure why. It is odd. I have the classic release of PDN. I put the plugins I download in Documents/Paint.net App Files/Effects and after the palette is rendered I move it to Documents/Paint.net App Files/Palettes. I hope that may be of help in comparing your setup and why it may be crashing.

Click HERE to see my Pictorium of images I have worked on.

 

lMbNWe.png

You were once wild here. Don't let them tame you.

 ~ Isadora Duncan

Link to comment
Share on other sites

10 minutes ago, Ladybug said:

@ANONYMISS, not sure why. It is odd. I have the classic release of PDN. I put the plugins I download in Documents/Paint.net App Files/Effects and after the palette is rendered I move it to Documents/Paint.net App Files/Palettes. I hope that may be of help in comparing your setup and why it may be crashing.

Thank you @Ladybug.

 

I use the classic release (in Windows 8.1  - cue booing from offstage) and placed the.dll in the Effects folder.

 

If it gets to be up-and-running I'd find it helpful.

 

P.s. But the present duplicitous behaviour is odd.

 

 

 

 

Link to comment
Share on other sites

@ANONYMISS, the only difference then is the version of Windows. I have Windows 10 (I will boo for that one but had to upgrade since it was a new computer).

Click HERE to see my Pictorium of images I have worked on.

 

lMbNWe.png

You were once wild here. Don't let them tame you.

 ~ Isadora Duncan

Link to comment
Share on other sites

@geist_, looks like you were trying to reinvent the wheel a bit.

 

Here's a simplified version. Let me know if you have any questions about it.

// Name:
// Submenu:
// Author:
// Title:
// Version:
// Desc:
// Keywords:
// URL:
// Help:
// Force Single Render Call

private static string ToHexString(ColorBgra c)
{
    return $"{c.A:X2}{c.R:X2}{c.G:X2}{c.B:X2}";
}

// creates file with colors
void MakePaletteFile(List<string> _colors)
{
    int fileCount = 0;
    string fileName = "UntitledPalette";
    string paletteDir = Path.Combine(this.Services.GetService<IUserFilesService>().UserFilesPath, "Palettes");
    string path = Path.Combine(paletteDir, fileName + ".txt");

    while (File.Exists(path))
    {
        fileCount++;
        path = Path.Combine(paletteDir, $"{fileName}{fileCount}.txt");
    }

    _colors = _colors.Distinct().ToList();
    _colors[0] = $"; {_colors.Count - 1} unique color(s)";


    File.WriteAllLines(path, _colors);
}


void Render(Surface dst, Surface src, Rectangle rect)
{
    List<string> colorCodes = new List<string>(){""};

    ColorBgra currentPixel;
    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];

            string colorAsHex = ToHexString(currentPixel);
            colorCodes.Add(colorAsHex);
        }
    }

    MakePaletteFile(colorCodes);
}

 

  • Like 2
  • Upvote 1

(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

  • 2 weeks later...

@geist_ is this still in developement or have you adopted Toe_head's suggestions?

Link to comment
Share on other sites

  • 8 months later...
On 2/17/2021 at 8:03 PM, toe_head2001 said:

Let me know if you have any questions about it.

When I bring the code into Codelab, it generates a error message and creates 25 different Palette files. All I wanted was to add it to a submenu (which is the easy part).

While we are talking about it, we never did get a dialogue box to name the palette file. Put it on my XMAS wish list. :)

 

 

PaintNetSignature.png.6bca4e07f5d738b2436f83d0ce1b876f.png

Link to comment
Share on other sites

3 minutes ago, AndrewDavid said:

When I bring the code into Codelab, it generates a error message and creates 25 different Palette files.

 

And now you know why there's 4 items in the menu for CodeLab.

image.png

Make sure to use the appropriate one.

  • Thanks 1

(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

  • 10 months later...

Hi, this is exactly what I was looking for so I made a couple tweaks. Now it sorts colors by most common found in the image and limits them to the palette limit which is 96. It shouldn't give I/O errors d crash but in case you're using non-english pdn this will make another, not localized, folder named "Palettes".

// Name:
// Submenu:
// Author:
// Title:
// Version:
// Desc:
// Keywords:
// URL:
// Help:
// Force Single Render Call

// creates file with colors
void MakePaletteFile(List<string> colors)
{
    int fileCount = 0;
    string fileName = "UntitledPalette";
    string paletteDir = Path.Combine(this.Services.GetService<IUserFilesService>().UserFilesPath, "Palettes");
    
    if (!Directory.Exists(paletteDir))
        Directory.CreateDirectory(paletteDir);
        
    string path = Path.Combine(paletteDir, fileName + ".txt");

    while (File.Exists(path))
    {
        fileCount++;
        path = Path.Combine(paletteDir, $"{fileName}{fileCount}.txt");
    }

    colors.Insert(0, $"; {colors.Count} unique color(s)");

    File.WriteAllLines(path, colors);
}

void Render(Surface dst, Surface src, Rectangle rect)
{
    List<ColorBgra> colors = new List<ColorBgra>();

    ColorBgra currentPixel;
    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];
            colors.Add(currentPixel);
        }
    }

    List<string> colorCodes = colors
        .GroupBy(color => color)
        .Select(g => new {Value = g.Key, Count = g.Count()})
        .OrderByDescending(color => color.Count)
        .Select(color => color.Value)
        .Take(96)
        .ToList()
        .ConvertAll<string>(c => c.ToHexString());

    MakePaletteFile(colorCodes);
}
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...