diff --git a/src/windows.c b/src/windows.c index f3d28505..a528d327 100644 --- a/src/windows.c +++ b/src/windows.c @@ -2629,14 +2629,22 @@ void Display_all_screen(void) byte Best_color(byte r,byte g,byte b) { - short col; + // This "static" allows the loop to start on the last successful match. + // If the same color is requested again (and it happens often) and the match + // was perfect, it allows an early exit that avoids + // 255 computations of color distance. + // This system still works with no bad effects when the palette changes. + static byte col=0; + + byte end_color; int delta_r,delta_g,delta_b; int dist; int best_dist=0x7FFFFFFF; int rmean; byte best_color=0; - for (col=0; col<256; col++) + end_color=col; + do { if (!Exclude_color[col]) { @@ -2656,21 +2664,31 @@ byte Best_color(byte r,byte g,byte b) best_color=col; } } - } + // Loop + col++; + } while(col!=end_color); return best_color; } byte Best_color_nonexcluded(byte red,byte green,byte blue) { - short col; + // This "static" allows the loop to start on the last successful match. + // If the same color is requested again (and it happens often) and the match + // was perfect, it allows an early exit that avoids + // 255 computations of color distance. + // This system still works with no bad effects when the palette changes. + static byte col=0; + + byte end_color; int delta_r,delta_g,delta_b; int dist; int best_dist=0x7FFFFFFF; int rmean; byte best_color=0; - for (col=0; col<256; col++) + end_color=col; + do { delta_r=(int)Main_palette[col].R-red; delta_g=(int)Main_palette[col].G-green; @@ -2688,7 +2706,11 @@ byte Best_color_nonexcluded(byte red,byte green,byte blue) best_dist=dist; best_color=col; } - } + + // Loop + col++; + } while(col!=end_color); + return best_color; }