From c266b38e05b6fccc7d1f8390b9977ae112cf5ac2 Mon Sep 17 00:00:00 2001 From: Yves Rizoud Date: Wed, 13 Apr 2011 21:40:47 +0000 Subject: [PATCH] Fix part of issue 433: Safety colors (after image load) are now only added when not already present in palette, and in reverse order so that in the very common case of 0=black, the white is added in slot 255 (Now it's more consistent with 'backspace' in palette which forcibly adds these colors in unused slots at the end) git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1775 416bcca6-2ee7-4201-b75f-2eb2f807beb1 --- src/loadsave.c | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/loadsave.c b/src/loadsave.c index 5f135c10..d2a06de4 100644 --- a/src/loadsave.c +++ b/src/loadsave.c @@ -685,14 +685,35 @@ void Load_image(T_IO_Context *context) memset(color_usage,0,sizeof(color_usage)); if (Count_used_colors(color_usage)<252) { - int gui_index=0; - int c; - for (c=255; c>=0 && gui_index<4; c--) + int gui_index; + // From white to black + for (gui_index=3; gui_index>=0; gui_index--) { - if (color_usage[c]==0) + int c; + T_Components gui_color; + + gui_color=*Favorite_GUI_color(gui_index); + // Try find a very close match (ignore last 2 bits) + for (c=255; c>=0; c--) { - context->Palette[c]=*Favorite_GUI_color(gui_index); - gui_index++; + if ((context->Palette[c].R|3) == (gui_color.R|3) + && (context->Palette[c].G|3) == (gui_color.G|3) + && (context->Palette[c].B|3) == (gui_color.B|3) ) + break; + } + if (c<0) // Not found + { + // Find an unused slot at end of palette + for (c=255; c>=0; c--) + { + if (color_usage[c]==0) + { + context->Palette[c]=gui_color; + // Tag as a used color + color_usage[c]=1; + break; + } + } } } }