From 80c37034f926e14db0346066be3333e4849b33f8 Mon Sep 17 00:00:00 2001 From: Yves Rizoud Date: Wed, 16 Feb 2011 17:03:45 +0000 Subject: [PATCH] Fix a remap problem on brush, when modified by Lua script then transformed. Fix Brush remap to maintain transparent pixels. Fix Text tool , respects transparent pixels and preview is always visible (The black PF_ fonts are no longer shown on a black background) git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1726 416bcca6-2ee7-4201-b75f-2eb2f807beb1 --- src/SFont.c | 4 +++- src/SFont.h | 1 + src/brush.c | 2 +- src/buttons.c | 17 +++++++++++++++-- src/factory.c | 9 ++++++--- src/text.c | 34 ++++++++++++++++++++++++++-------- src/windows.c | 42 ++++++++++++++++++++++++++++++++++++++++++ src/windows.h | 1 + 8 files changed, 95 insertions(+), 15 deletions(-) diff --git a/src/SFont.c b/src/SFont.c index fef5b17f..cd44d850 100644 --- a/src/SFont.c +++ b/src/SFont.c @@ -117,7 +117,9 @@ SFont_Font* SFont_InitFont(SDL_Surface* Surface) pixel = GetPixel(Surface, 0, Surface->h-1); SDL_UnlockSurface(Surface); - SDL_SetColorKey(Surface, SDL_SRCCOLORKEY, pixel); + // No longer use SDL color keying + //SDL_SetColorKey(Surface, SDL_SRCCOLORKEY, pixel); + Font->Transparent=pixel; return Font; } diff --git a/src/SFont.h b/src/SFont.h index 27a54937..71b694b7 100644 --- a/src/SFont.h +++ b/src/SFont.h @@ -59,6 +59,7 @@ typedef struct { int CharBegin[256]; int CharWidth[256]; int Space; + unsigned char Transparent; } SFont_Font; /// diff --git a/src/brush.c b/src/brush.c index 4f210afd..f3001505 100644 --- a/src/brush.c +++ b/src/brush.c @@ -844,7 +844,7 @@ void Remap_brush(void) Brush_colormap[color]=color; else // Usual method: closest by r g b - Brush_colormap[color]=Best_color_perceptual(r,g,b); + Brush_colormap[color]=Best_color_perceptual_except(r,g,b,Back_color); } // Il reste une couleur non calculée dans la table qu'il faut mettre à diff --git a/src/buttons.c b/src/buttons.c index d483ea53..221c0b42 100644 --- a/src/buttons.c +++ b/src/buttons.c @@ -4867,9 +4867,22 @@ void Button_Text(void) if (str[0]) preview_string=str; is_truetype=TrueType_font(selected_font_index); - Window_rectangle(8, 106, 273, 50,(antialias&&is_truetype)?MC_Black:Back_color); free(new_brush); new_brush = Render_text(preview_string, selected_font_index, font_size, antialias, is_bold, is_italic, &new_width, &new_height, text_palette); + // Background: + if (antialias&&is_truetype) + // Solid + Window_rectangle(8, 106, 273, 50,MC_Black); + else if (is_truetype) + { + long l = text_palette[Fore_color].R+text_palette[Fore_color].G+text_palette[Fore_color].B; + Window_rectangle(8, 106, 273, 50,l>128*3? MC_Black:MC_Light); + } + else + { + long l = text_palette[Back_color].R+text_palette[Back_color].G+text_palette[Back_color].B; + Window_rectangle(8, 106, 273, 50,l>128*3? MC_Light:MC_Black); + } if (new_brush) { if (!is_truetype || (is_truetype&&antialias)) @@ -4906,7 +4919,7 @@ void Button_Text(void) //if (r==Main_palette[color].R && g==Main_palette[color].G && b==Main_palette[color].B) // colmap[color]=color; //else - colmap[color]=Best_color_perceptual(r,g,b); + colmap[color]=Best_color_perceptual_except(r,g,b,Back_color); } colmap[Back_color]=Back_color; diff --git a/src/factory.c b/src/factory.c index 62cdf692..94f466d3 100644 --- a/src/factory.c +++ b/src/factory.c @@ -153,6 +153,7 @@ void Update_colors_during_script(void) int L_SetBrushSize(lua_State* L) { + int i; int w; int h; int nb_args=lua_gettop(L); @@ -168,10 +169,12 @@ int L_SetBrushSize(lua_State* L) Brush_was_altered=1; // Fill with Back_color memset(Brush_original_pixels,Back_color,(long)Brush_width*Brush_height); - // Grab palette + memset(Brush,Back_color,(long)Brush_width*Brush_height); + // Adopt the current palette. memcpy(Brush_original_palette, Main_palette,sizeof(T_Palette)); - // Remap (no change) - Remap_brush(); + for (i=0; i<256; i++) + Brush_colormap[i]=i; + //-- // Center the handle Brush_offset_X=(Brush_width>>1); diff --git a/src/text.c b/src/text.c index 701ba06e..d5c70fa8 100644 --- a/src/text.c +++ b/src/text.c @@ -483,12 +483,7 @@ byte *Render_text_TTF(const char *str, int font_number, int size, int antialias, if (Fore_color==Back_color) { - if (Main_palette[Back_color].R+Main_palette[Back_color].G+Main_palette[Back_color].B > 128*3) - // Back color is rather light: - new_fore=MC_Black; - else - // Back color is rather dark: - new_fore=MC_White; + new_fore=Best_color_perceptual_except(Main_palette[Back_color].R, Main_palette[Back_color].G, Main_palette[Back_color].B, Back_color); } for (index=0; index < text_surface->w * text_surface->h; index++) @@ -578,12 +573,12 @@ byte *Render_text_SFont(const char *str, int font_number, int *width, int *heigh text_surface=SDL_CreateRGBSurface(SDL_SWSURFACE, *width, *height, 8, 0, 0, 0, 0); // Copy palette SDL_SetPalette(text_surface, SDL_LOGPAL, font_surface->format->palette->colors, 0, 256); - // Fill with backcolor + // Fill with transparent color rectangle.x=0; rectangle.y=0; rectangle.w=*width; rectangle.h=*height; - SDL_FillRect(text_surface, &rectangle, Back_color); + SDL_FillRect(text_surface, &rectangle, font->Transparent); // Rendu du texte SFont_Write(text_surface, font, 0, 0, str); if (!text_surface) @@ -604,6 +599,29 @@ byte *Render_text_SFont(const char *str, int font_number, int *width, int *heigh Get_SDL_Palette(font_surface->format->palette, palette); + // Swap current BG color with font's transparent color + if (font->Transparent != Back_color) + { + int c; + byte colmap[256]; + // Swap palette entries + + SWAP_BYTES(palette[font->Transparent].R, palette[Back_color].R) + SWAP_BYTES(palette[font->Transparent].G, palette[Back_color].G) + SWAP_BYTES(palette[font->Transparent].B, palette[Back_color].B) + + // Define a colormap + for (c=0; c<256; c++) + colmap[c]=c; + + // The swap + colmap[font->Transparent]=Back_color; + colmap[Back_color]=font->Transparent; + + Remap_general_lowlevel(colmap, new_brush, new_brush, text_surface->w,text_surface->h, text_surface->w); + + } + SDL_FreeSurface(text_surface); SFont_FreeFont(font); diff --git a/src/windows.c b/src/windows.c index 49c3d456..a0af70bb 100644 --- a/src/windows.c +++ b/src/windows.c @@ -2721,6 +2721,48 @@ byte Best_color_perceptual(byte r,byte g,byte b) return best_color; } +byte Best_color_perceptual_except(byte r,byte g,byte b, byte except) +{ + + 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 (col==except || 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