Grid Sage Forums

Grid Sage Forums

  • November 21, 2024, 11:07:40 AM
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

News:

LINKS: Website | Steam | Wiki

Author Topic: Selected layer's RGB fg/bg snapping to nearest color in current palette  (Read 5672 times)

gumix

  • Cyborg
  • ***
  • Posts: 134
    • View Profile

Hey, is there command to replace foreground and/or background colors (as enabled by apply flags) of all cells on current layer with nearest colors in selected palette?
I would have my head cut off there was such thing but I cannot find it in manual now.
Logged

Kyzrati

  • Administrator
  • True Cogmind
  • *****
  • Posts: 4477
    • View Profile
    • Cogmind
Re: Selected layer's RGB fg/bg snapping to nearest color in current palette
« Reply #1 on: September 02, 2021, 07:30:46 PM »

There indeed is not, and in fact I'm not even sure what "nearest colors" would mean in this case! There being multiple ways to define that...

You can replace specific colors in an image using various means via the palette. Reminder:
Shift-LMB x2      Swap Occurences of Color 1 with Color 2
Alt-LMB (x2)      Edit and Replace Foreground Color in Image
Alt-RMB (x2)      Edit and Replace Background Color in Image


Also:
Quote
Shift-clicking will also open the color picker, but once the new color is confirmed will also replace all matching colors in the image (applied to foreground and/or background, depending on which apply modes are active at the time, NOT whether LMB/RMB used).

Could these be something you're looking for?
Logged
Josh Ge, Developer - Dev Blog | @GridSageGames | Patreon

gumix

  • Cyborg
  • ***
  • Posts: 134
    • View Profile
Re: Selected layer's RGB fg/bg snapping to nearest color in current palette
« Reply #2 on: September 03, 2021, 04:28:46 AM »

Thanks Kyzrati,
That is what I've used in the past and probably I've messed it up with another program... ahh yes with my own xp automation :X

Quote
There being multiple ways to define that...
Yep I'm aware of that, simple RGB least square diff would be a thing! :)

Is this 2.0 backlog applicable?
« Last Edit: September 03, 2021, 04:30:51 AM by gumix »
Logged

Kyzrati

  • Administrator
  • True Cogmind
  • *****
  • Posts: 4477
    • View Profile
    • Cogmind
Re: Selected layer's RGB fg/bg snapping to nearest color in current palette
« Reply #3 on: September 05, 2021, 10:22:08 PM »

So in this case "replace with nearest colors" seems like you're trying to convert an image from one entire palette to another different palette, eh? Makes more sense when I think of it like that (I was originally just thinking of one color at a time), although seems pretty niche :P

Could certainly add it to the list for the future, but that's kinda far away, I think... Might be kind of a complicated feature to do a patch for now.

How does "RGB least square diff " work?
Logged
Josh Ge, Developer - Dev Blog | @GridSageGames | Patreon

gumix

  • Cyborg
  • ***
  • Posts: 134
    • View Profile
Re: Selected layer's RGB fg/bg snapping to nearest color in current palette
« Reply #4 on: September 06, 2021, 11:36:00 AM »

Yea, it's 100% niche - 1 stupid user (me), I've painted many new sprites using arbitrary rgb colors "just for now, to see how it look".
Then it turned to look really good but sprites were using out of production palette colors :(

Quote
How does "RGB least square diff " work?
I meant, for every x,y cell on active layer, separately for background and foreground, we're looking for such palette index (0-255) on active palette which results in smallest color error given by:

Code: [Select]
int ColorDiff(uint8 rgb1[3], uint8 rgb2[3])
{
    static const int w[3] = {3,4,1}; // human is less sensitive to blue changes, this could be configurable
    int dr = rgb1[0] - rgb2[0]; // per channel difference
    int dg = rgb1[1] - rgb2[1];
    int db = rgb1[2] - rgb2[2];
    return w[0]*dr*dr + w[1]*dg*dg + w[2]*db*db; // weighted sum of square diffs
}

Of course I've fixed my mistake outside of RP, so it is not must have feature in RP.
Logged

Kyzrati

  • Administrator
  • True Cogmind
  • *****
  • Posts: 4477
    • View Profile
    • Cogmind
Re: Selected layer's RGB fg/bg snapping to nearest color in current palette
« Reply #5 on: September 10, 2021, 12:38:42 AM »

Gotcha. Well I could probably find a way to slip it into a little patch if you or others have a repeated need for something like this, especially you since you're such a big RP user ;)
Logged
Josh Ge, Developer - Dev Blog | @GridSageGames | Patreon

gumix

  • Cyborg
  • ***
  • Posts: 134
    • View Profile
Re: Selected layer's RGB fg/bg snapping to nearest color in current palette
« Reply #6 on: September 11, 2021, 02:06:37 PM »

That's awesome, having it working with apply flags on all  unlocked and visible layers would make me the happiest RP user, but probably I'm already :)
Logged

Kyzrati

  • Administrator
  • True Cogmind
  • *****
  • Posts: 4477
    • View Profile
    • Cogmind
Re: Selected layer's RGB fg/bg snapping to nearest color in current palette
« Reply #7 on: September 16, 2021, 06:23:18 AM »

Alrighty gumix, your special delivery has finally arrived!

Remapping a layer's colors to the current palette can now be done via Shift-Alt-p, or use Ctrl-Shift-Alt-p to remap all unlocked layers.

Demos:





You can download the patch here, with files for overwriting the latest v1.60. Edit: Patch removed, is now included in REXPaint 1.7.

Let me know if there are any issues, but based on my tests it currently seems to be working as intended...
« Last Edit: January 09, 2024, 09:03:48 PM by Kyzrati »
Logged
Josh Ge, Developer - Dev Blog | @GridSageGames | Patreon

gumix

  • Cyborg
  • ***
  • Posts: 134
    • View Profile
Re: Selected layer's RGB fg/bg snapping to nearest color in current palette
« Reply #8 on: September 17, 2021, 03:45:07 AM »

Works great :)

Previously I've posted a false bug report, I've deleted it immediately after figuring out I'm using wrong key stroke (ctrl_alt_shift_w instead of ctrl_alt_shift_p). I'm sorry, if you had a chance to read it :)

And showing number of colors changed / unique colors is a great addition!

Thank you!

« Last Edit: September 17, 2021, 03:47:54 AM by gumix »
Logged

Kyzrati

  • Administrator
  • True Cogmind
  • *****
  • Posts: 4477
    • View Profile
    • Cogmind
Re: Selected layer's RGB fg/bg snapping to nearest color in current palette
« Reply #9 on: September 17, 2021, 03:47:36 AM »

Ha, yes I did see that at first since I got it via email and thought it sounded kinda weird ("wait what I reversed the foreground/background in the code or something???"). Happy painting :)
Logged
Josh Ge, Developer - Dev Blog | @GridSageGames | Patreon

gumix

  • Cyborg
  • ***
  • Posts: 134
    • View Profile
Re: Selected layer's RGB fg/bg snapping to nearest color in current palette
« Reply #10 on: September 17, 2021, 06:16:57 AM »

Hey, may I ask what actual color difference is implemented?
Works very well, I'm just curious.
Logged

Kyzrati

  • Administrator
  • True Cogmind
  • *****
  • Posts: 4477
    • View Profile
    • Cogmind
Re: Selected layer's RGB fg/bg snapping to nearest color in current palette
« Reply #11 on: September 17, 2021, 06:33:51 AM »

Yeah I think the results turned out pretty well. I indeed went with Euclidean RGB distance, as per your suggestion. I spent some time researching other options before finalizing that bit, but this approach ended up being both the easiest and fastest, so went with that.

I was kinda surprised it worked as well as it did, since in the past other RGB-related comparison operations I've worked with didn't seem to have nearly as nice an effect as in HSV space. I'd be interested in comparing RGB vs HSV in terms of color matching for this purpose, too (weighted, naturally), but I don't really have a good enough collection of sample material to work with, so that would take too long...
Logged
Josh Ge, Developer - Dev Blog | @GridSageGames | Patreon

gumix

  • Cyborg
  • ***
  • Posts: 134
    • View Profile
Re: Selected layer's RGB fg/bg snapping to nearest color in current palette
« Reply #12 on: September 17, 2021, 07:49:56 AM »

Great, thanks. Yes, HSV may be better if snapping to particular hue is more important than global luminance match.
Logged