Skip to content

refactor: replace nearest_color duplicate with LbPaletteFindColour#4587

Draft
cerwym wants to merge 1 commit intodkfans:masterfrom
cerwym:fix/palette-nearest-colour
Draft

refactor: replace nearest_color duplicate with LbPaletteFindColour#4587
cerwym wants to merge 1 commit intodkfans:masterfrom
cerwym:fix/palette-nearest-colour

Conversation

@cerwym
Copy link
Contributor

@cerwym cerwym commented Mar 1, 2026

Remove the local nearest_color() from custom_sprites.c — LbPaletteFindColour() in bflib_video already provides the same palette-search. Using the existing function eliminates duplication across three sites.

Also fix OverlayEffect RGB alpha blending: was incorrectly blending palette indices as integers; now unpacks RGB from palette, blends in colour space, then maps back via LbPaletteFindColour.

Copilot AI review requested due to automatic review settings March 1, 2026 17:19
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors palette nearest-colour lookup to use the shared LbPaletteFindColour() helper and updates the overlay lens effect to perform alpha blending in RGB space (instead of blending palette indices), then remapping back to the nearest palette entry.

Changes:

  • Replaced custom_sprites.c’s local nearest_color() with LbPaletteFindColour() when building the RGB→palette lookup table.
  • Fixed OverlayEffect blending by unpacking palette RGB, blending channels, then mapping the blended RGB back to the nearest palette index via LbPaletteFindColour().
  • Added required bflib_video.h includes for access to LbPaletteFindColour() / palette globals.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/kfx/lense/OverlayEffect.cpp Switches overlay compositing to RGB-space alpha blend + palette remap using LbPaletteFindColour().
src/custom_sprites.c Removes duplicated nearest-colour implementation and uses LbPaletteFindColour() for RGB→palette table generation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 977 to +981
for (uint32_t i = 0; i < table_size; ++i) {
rgb_to_pal_table[i] = nearest_color(i, palette);
rgb_to_pal_table[i] = LbPaletteFindColour(palette,
i & (MAX_COLOR_VALUE - 1),
(i >> 6) & (MAX_COLOR_VALUE - 1),
(i >> 12) & (MAX_COLOR_VALUE - 1));
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This replacement changes the nearest-colour metric: the removed nearest_color() used a simple sum of absolute channel deltas, while LbPaletteFindColour() uses squared-distance plus multiple tie-break passes. If the intent is to keep sprite conversion identical, this will produce different palette indices for some RGB inputs; consider either keeping the prior distance metric (e.g., via a dedicated helper) or updating expectations/docs to reflect the intentional behavior change.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to see the difference on this

Comment on lines 977 to 982
for (uint32_t i = 0; i < table_size; ++i) {
rgb_to_pal_table[i] = nearest_color(i, palette);
rgb_to_pal_table[i] = LbPaletteFindColour(palette,
i & (MAX_COLOR_VALUE - 1),
(i >> 6) & (MAX_COLOR_VALUE - 1),
(i >> 12) & (MAX_COLOR_VALUE - 1));
}
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

load_rgb_to_pal_table() calls LbPaletteFindColour() 64^3 times; because LbPaletteFindColour() does multiple 256-entry passes, this may significantly increase one-time startup/mod load time compared to the prior single-pass implementation. If this becomes noticeable, consider caching/precomputing the table to disk or using a single-pass search tailored to the 6-bit palette format for this bulk build step.

Copilot uses AI. Check for mistakes.
Remove the local nearest_color() from custom_sprites.c — LbPaletteFindColour()
in bflib_video already provides the same palette-search. Using the existing
function eliminates duplication across three sites.

Also fix OverlayEffect RGB alpha blending: was incorrectly blending palette
indices as integers; now pre-builds a 256x256 blend LUT per render call
(65280 palette lookups once) instead of calling LbPaletteFindColour per pixel.
@cerwym cerwym force-pushed the fix/palette-nearest-colour branch from 14a5285 to f6b9ecc Compare March 1, 2026 17:36
@cerwym cerwym requested review from PieterVdc and xtremeqg March 1, 2026 17:43
@cerwym
Copy link
Contributor Author

cerwym commented Mar 2, 2026

Leaving this open to perf issues resolved

@PieterVdc PieterVdc marked this pull request as draft March 2, 2026 11:09
@PieterVdc
Copy link
Member

Leaving this open to perf issues resolved

I assume you meant to Convert to draft then

@cerwym
Copy link
Contributor Author

cerwym commented Mar 2, 2026

Tomato potato

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants