From 3ed9d0d652dcb2fe1637ae9f83146044d48b0289 Mon Sep 17 00:00:00 2001 From: Yves Rizoud Date: Sun, 22 Aug 2010 16:48:19 +0000 Subject: [PATCH] Improve auto-detection of menu colors when using very small palettes git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1584 416bcca6-2ee7-4201-b75f-2eb2f807beb1 --- src/engine.c | 4 +-- src/global.h | 6 ++++ src/init.c | 5 +++ src/windows.c | 94 ++++++++++++++++++++++++++++++++++++++------------- 4 files changed, 84 insertions(+), 25 deletions(-) diff --git a/src/engine.c b/src/engine.c index 4a6a6d4b..fbaa6dc0 100644 --- a/src/engine.c +++ b/src/engine.c @@ -1550,7 +1550,7 @@ void Open_window(word width,word height, const char * title) Save_background(&(Window_background[Windows_open-1]), Window_pos_X, Window_pos_Y, width, height); // Fenêtre grise - Block(Window_pos_X+(Menu_factor_X<<1),Window_pos_Y+(Menu_factor_Y<<1),(width-4)*Menu_factor_X,(height-4)*Menu_factor_Y,MC_Light); + Block(Window_pos_X+(Menu_factor_X<<1),Window_pos_Y+(Menu_factor_Y<<1),(width-4)*Menu_factor_X,(height-4)*Menu_factor_Y,MC_Window); // -- Frame de la fenêtre ----- --- -- - - @@ -1852,7 +1852,7 @@ void Window_draw_slider(T_Scroller_button * button) Block(Window_pos_X+(button->Pos_X*Menu_factor_X), Window_pos_Y+(slider_position*Menu_factor_Y), - 11*Menu_factor_X,button->Cursor_height*Menu_factor_Y,MC_Dark/*MC_White*/); + 11*Menu_factor_X,button->Cursor_height*Menu_factor_Y,MC_OnBlack/*MC_White*/); Update_rect(Window_pos_X+(button->Pos_X*Menu_factor_X), Window_pos_Y+button->Pos_Y*Menu_factor_Y, diff --git a/src/global.h b/src/global.h index ce469e90..060f4f3a 100644 --- a/src/global.h +++ b/src/global.h @@ -73,6 +73,12 @@ GFX2_GLOBAL byte MC_Light; ///< Index of color to use as "light grey" in the GUI GFX2_GLOBAL byte MC_White; ///< Index of color to use as "white" in the GUI menus. GFX2_GLOBAL byte MC_Trans; ///< Index of color to use as "transparent" while loading the GUI file. +GFX2_GLOBAL byte MC_OnBlack; ///< Index of color immediately lighter than "black" in the GUI menus. +GFX2_GLOBAL byte MC_Window; ///< Index of color to use as window background in the GUI menus. +GFX2_GLOBAL byte MC_Lighter; ///< Index of color lighter than window in the GUI menus. +GFX2_GLOBAL byte MC_Darker; ///< Index of color darker than window in the GUI menus. + + // Input state GFX2_GLOBAL word Mouse_X; ///< Current mouse cursor position. GFX2_GLOBAL word Mouse_Y; ///< Current mouse cursor position. diff --git a/src/init.c b/src/init.c index 57b355ee..e5dcc200 100644 --- a/src/init.c +++ b/src/init.c @@ -2682,6 +2682,11 @@ void Set_current_skin(const char *skinfile, T_Gui_skin *gfx) MC_Light = gfx->Color[2]; MC_White = gfx->Color[3]; MC_Trans = gfx->Color_trans; + MC_OnBlack=MC_Dark; + MC_Window=MC_Light; + MC_Lighter=MC_White; + MC_Darker=MC_Dark; + // Set menubars to point to the new data for (i=0; i<3; i++) diff --git a/src/windows.c b/src/windows.c index 31f44273..4f1b4c29 100644 --- a/src/windows.c +++ b/src/windows.c @@ -2738,7 +2738,14 @@ void Remap_screen_after_menu_colors_change(void) } - +int Same_color(T_Components * palette, byte c1, byte c2) +{ + if (palette[c1].R==palette[c2].R && + palette[c1].G==palette[c2].G && + palette[c1].B==palette[c2].B) + return 1; + return 0; +} void Compute_optimal_menu_colors(T_Components * palette) { @@ -2746,9 +2753,10 @@ void Compute_optimal_menu_colors(T_Components * palette) byte l[256]; byte s[256]; byte h; - byte max_l = 0, min_l = 255; - byte low_l, hi_l; - byte delta_low = 255, delta_hi = 255; + int max_l = -1, min_l = 256; + int low_l, hi_l; + int delta_low = 999999; + int delta_high = 999999; Old_black =MC_Black; Old_dark = MC_Dark; @@ -2790,7 +2798,11 @@ void Compute_optimal_menu_colors(T_Components * palette) // On cherche une couleur de transparence différente des 4 autres. for (MC_Trans=0; ((MC_Trans==MC_Black) || (MC_Trans==MC_Dark) || (MC_Trans==MC_Light) || (MC_Trans==MC_White)); MC_Trans++); - + // Easy case + MC_OnBlack=MC_Dark; + MC_Window=MC_Light; + MC_Lighter=MC_White; + MC_Darker=MC_Dark; Remap_menu_sprites(); return; } @@ -2805,50 +2817,86 @@ void Compute_optimal_menu_colors(T_Components * palette) // Compute luminance for whole palette // Take the darkest as black, the brightest white - for(i = 0; i < 256; i++) { + for(i = 0; i < 256; i++) + { RGB_to_HSL(palette[i].R, palette[i].G, palette[i].B, &h, &s[i], &l[i]); - if (l[i] > max_l) { + if (l[i] > max_l) + { max_l = l[i]; MC_White = i; } - - if (l[i] < min_l) { + } + for(i = 0; i < 256; i++) + { + if (l[i] < min_l && i!=MC_White) + { min_l = l[i]; MC_Black = i; } } - // Find colors nearest to min+(max-min)/3 and min+2(max-min)/3 + // Find color nearest to min+2(max-min)/3 // but at the same time we try to minimize the saturation so that the menu // still looks grey - low_l = min_l + (max_l - min_l)/3; hi_l = min_l + 2*(max_l - min_l)/3; - for (i = 0; i < 256; i++) { - if ( abs(l[i] - low_l) + s[i]/2 < delta_low ) { - delta_low = abs(l[i] - low_l); - MC_Dark = i; - } - - if ( abs(l[i] - hi_l) + s[i]/3 < delta_hi ) { - delta_hi = abs(l[i] - hi_l); + for (i = 0; i < 256; i++) + { + if ( abs(l[i] - hi_l) + s[i]/4 < delta_high && i!=MC_White && i!=MC_Black) + { + delta_high = abs(l[i] - hi_l) + s[i]/4; MC_Light = i; } } + + // Target "Dark color" is halfway between Light and Black + low_l = ((int)l[MC_Light]+l[MC_Black])/2; + for (i = 0; i < 256; i++) + { + if ( abs((int)l[i] - low_l) + s[i]/4 < delta_low && i!=MC_White && i!=MC_Black && i!=MC_Light) + { + delta_low = abs((int)l[i] - low_l)+ s[i]/4; + MC_Dark = i; + } + } + + + //if (l[MC_Light]