diff --git a/src/brush.c b/src/brush.c index 63014847..d17d761d 100644 --- a/src/brush.c +++ b/src/brush.c @@ -806,7 +806,7 @@ void Remap_brush(void) // ne seront pas utilisées par Remap_brush_LOWLEVEL. for (color=0;color<=255;color++) if (used[color] != 0) - used[color]=Best_color(Spare_palette[color].R,Spare_palette[color].G,Spare_palette[color].B); + used[color]=Best_color_perceptual(Spare_palette[color].R,Spare_palette[color].G,Spare_palette[color].B); // Il reste une couleur non calculée dans la table qu'il faut mettre à // jour: c'est la couleur de fond. On l'avait inhibée pour éviter son diff --git a/src/graph.c b/src/graph.c index a3b44978..59bc3138 100644 --- a/src/graph.c +++ b/src/graph.c @@ -673,13 +673,16 @@ void Remap_spare(void) // ne seront pas utilisées par Remap_general_lowlevel. for (color=0;color<=255;color++) if (used[color]) - used[color]=Best_color(Spare_palette[color].R,Spare_palette[color].G,Spare_palette[color].B); + used[color]=Best_color_perceptual(Spare_palette[color].R,Spare_palette[color].G,Spare_palette[color].B); // Maintenant qu'on a une super table de conversion qui n'a que le nom // qui craint un peu, on peut faire l'échange dans la brosse de toutes les // teintes. for (layer=0; layerPages->Nb_layers; layer++) Remap_general_lowlevel(used,Spare_backups->Pages->Image[layer],Spare_image_width,Spare_image_height,Spare_image_width); + + // Change transparent color index + Spare_backups->Pages->Transparent_color=used[Spare_backups->Pages->Transparent_color]; } diff --git a/src/windows.c b/src/windows.c index 4254f54a..7a0805f8 100644 --- a/src/windows.c +++ b/src/windows.c @@ -2584,22 +2584,14 @@ void Display_all_screen(void) byte Best_color(byte r,byte g,byte b) { - // 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 col; int delta_r,delta_g,delta_b; int dist; int best_dist=0x7FFFFFFF; int rmean; byte best_color=0; - end_color=col; - do + for (col=0; col<256; col++) { if (!Exclude_color[col]) { @@ -2619,9 +2611,7 @@ byte Best_color(byte r,byte g,byte b) best_color=col; } } - // Loop - col++; - } while(col!=end_color); + } return best_color; } @@ -2658,6 +2648,51 @@ byte Best_color_nonexcluded(byte red,byte green,byte blue) } + +byte Best_color_perceptual(byte r,byte g,byte b) +{ + + int col; + float best_diff=255.0*1.56905; + byte best_color=0; + float target_bri; + float bri; + float diff_b, diff_c, diff; + + // Similar to Perceptual_lightness(); + target_bri = sqrt(0.26*r*0.26*r + 0.55*g*0.55*g + 0.19*b*0.19*b); + + for (col=0; col<256; col++) + { + if (Exclude_color[col]) + continue; + + diff_c = sqrt( + (0.26*(Main_palette[col].R-r))* + (0.26*(Main_palette[col].R-r))+ + (0.55*(Main_palette[col].G-g))* + (0.55*(Main_palette[col].G-g))+ + (0.19*(Main_palette[col].B-b))* + (0.19*(Main_palette[col].B-b))); + // Exact match + if (diff_c==0) + return col; + + bri = sqrt(0.26*Main_palette[col].R*0.26*Main_palette[col].R + 0.55*Main_palette[col].G*0.55*Main_palette[col].G + 0.19*Main_palette[col].B*0.19*Main_palette[col].B); + diff_b = abs(target_bri-bri); + + diff=0.25*(diff_b-diff_c)+diff_c; + if (diff