Fix Lua function matchcolor2() inaccuracies caused by integer arithmetics
git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@2082 416bcca6-2ee7-4201-b75f-2eb2f807beb1
This commit is contained in:
parent
61c8dca353
commit
67d3636512
@ -19,7 +19,7 @@ $(OBJDIR)/factory.o: factory.c brush.h struct.h const.h buttons.h engine.h error
|
||||
filesel.h loadsave.h global.h graph.h io.h misc.h pages.h readline.h \
|
||||
sdlscreen.h windows.h palette.h input.h help.h realpath.h setup.h \
|
||||
tiles.h
|
||||
$(OBJDIR)/factory_gui.o: factory_gui.c brush.h struct.h const.h buttons.h engine.h \
|
||||
$(OBJDIR)/factory_m.o: factory_m.c brush.h struct.h const.h buttons.h engine.h \
|
||||
errors.h filesel.h loadsave.h global.h graph.h io.h misc.h pages.h \
|
||||
readline.h sdlscreen.h windows.h palette.h input.h help.h realpath.h \
|
||||
setup.h tiles.h
|
||||
@ -72,9 +72,6 @@ $(OBJDIR)/pages.o: pages.c global.h struct.h const.h pages.h errors.h loadsave.h
|
||||
$(OBJDIR)/palette.o: palette.c const.h struct.h global.h misc.h engine.h readline.h \
|
||||
buttons.h pages.h help.h sdlscreen.h errors.h op_c.h colorred.h \
|
||||
windows.h input.h palette.h shade.h
|
||||
$(OBJDIR)/palette_test.o: palette_test.c const.h struct.h global.h misc.h engine.h \
|
||||
readline.h buttons.h pages.h help.h sdlscreen.h errors.h op_c.h \
|
||||
colorred.h windows.h input.h palette.h shade.h
|
||||
$(OBJDIR)/pversion.o: pversion.c
|
||||
$(OBJDIR)/pxdouble.o: pxdouble.c global.h struct.h const.h sdlscreen.h misc.h \
|
||||
graph.h pxdouble.h pxwide.h
|
||||
|
||||
@ -860,7 +860,8 @@ int L_MatchColor(lua_State* L)
|
||||
int L_MatchColor2(lua_State* L)
|
||||
{
|
||||
double r, g, b;
|
||||
int c;
|
||||
double l_weight = 0.25;
|
||||
byte best_color = 0;
|
||||
int nb_args=lua_gettop(L);
|
||||
|
||||
if (nb_args < 3 || nb_args > 4)
|
||||
@ -870,17 +871,67 @@ int L_MatchColor2(lua_State* L)
|
||||
LUA_ARG_NUMBER(1, "matchcolor2", r, -DBL_MAX, DBL_MAX);
|
||||
LUA_ARG_NUMBER(2, "matchcolor2", g, -DBL_MAX, DBL_MAX);
|
||||
LUA_ARG_NUMBER(3, "matchcolor2", b, -DBL_MAX, DBL_MAX);
|
||||
if (nb_args == 3)
|
||||
if (nb_args > 3)
|
||||
{
|
||||
c = Best_color_perceptual(clamp_byte(r),clamp_byte(g),clamp_byte(b));
|
||||
LUA_ARG_NUMBER(4, "matchcolor2", l_weight, 0.0, 1.0);
|
||||
}
|
||||
else // nb_args == 4
|
||||
|
||||
// Similar to Best_color_perceptual(), but with floating point
|
||||
{
|
||||
float weight;
|
||||
LUA_ARG_NUMBER(4, "matchcolor2", weight, -DBL_MAX, DBL_MAX);
|
||||
c = Best_color_perceptual_weighted(clamp_byte(r),clamp_byte(g),clamp_byte(b),weight);
|
||||
}
|
||||
lua_pushinteger(L, c);
|
||||
int col;
|
||||
double best_diff=9e99;
|
||||
double target_bri;
|
||||
double bri;
|
||||
double diff_b, diff_c, diff;
|
||||
|
||||
if (r<0.0)
|
||||
r=0;
|
||||
else if (r>255.0)
|
||||
r=255.0;
|
||||
if (g<0.0)
|
||||
g=0;
|
||||
else if (g>255.0)
|
||||
g=255.0;
|
||||
if (b<0.0)
|
||||
b=0;
|
||||
else if (b>255.0)
|
||||
b=255.0;
|
||||
|
||||
// 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<1.0)
|
||||
{
|
||||
lua_pushinteger(L, col);
|
||||
return 1;
|
||||
}
|
||||
|
||||
bri = sqrt(0.26*0.26*(Main_palette[col].R*Main_palette[col].R) + 0.55*0.55*(Main_palette[col].G*Main_palette[col].G) + 0.19*0.19*(Main_palette[col].B*Main_palette[col].B));
|
||||
diff_b = fabs(target_bri-bri);
|
||||
|
||||
diff=l_weight*(diff_b-diff_c)+diff_c;
|
||||
if (diff<best_diff)
|
||||
{
|
||||
best_diff=diff;
|
||||
best_color=col;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lua_pushinteger(L, best_color);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
@ -2793,49 +2793,6 @@ byte Best_color_perceptual(byte r,byte g,byte b)
|
||||
return best_color;
|
||||
}
|
||||
|
||||
byte Best_color_perceptual_weighted(byte r,byte g,byte b,float l_weight)
|
||||
{
|
||||
|
||||
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=l_weight*(diff_b-diff_c)+diff_c;
|
||||
if (diff<best_diff)
|
||||
{
|
||||
best_diff=diff;
|
||||
best_color=col;
|
||||
}
|
||||
}
|
||||
|
||||
return best_color;
|
||||
}
|
||||
|
||||
byte Best_color_perceptual_except(byte r,byte g,byte b, byte except)
|
||||
{
|
||||
|
||||
|
||||
@ -102,7 +102,6 @@ byte Best_color_nonexcluded(byte red,byte green,byte blue);
|
||||
byte Best_color_range(byte red,byte green,byte blue,byte max);
|
||||
byte Best_color_perceptual(byte r,byte g,byte b);
|
||||
byte Best_color_perceptual_except(byte r,byte g,byte b, byte except);
|
||||
byte Best_color_perceptual_weighted(byte r,byte g,byte b, float weight);
|
||||
|
||||
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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user