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
This commit is contained in:
Yves Rizoud 2011-02-16 17:03:45 +00:00
parent 2378d8cedc
commit 80c37034f9
8 changed files with 95 additions and 15 deletions

View File

@ -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;
}

View File

@ -59,6 +59,7 @@ typedef struct {
int CharBegin[256];
int CharWidth[256];
int Space;
unsigned char Transparent;
} SFont_Font;
///

View File

@ -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 à

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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<best_diff)
{
best_diff=diff;
best_color=col;
}
}
return best_color;
}
byte Old_black;
byte Old_dark;

View File

@ -100,6 +100,7 @@ void Window_display_icon_sprite(word x_pos,word y_pos,byte type);
byte Best_color(byte red,byte green,byte blue);
byte Best_color_nonexcluded(byte red,byte green,byte blue);
byte Best_color_perceptual(byte r,byte g,byte b);
byte Best_color_perceptual_except(byte r,byte g,byte b, byte except);
void Horizontal_XOR_line_zoom(short x_pos, short y_pos, short width);
void Vertical_XOR_line_zoom(short x_pos, short y_pos, short height);