From e23a80838f760f301ed2189e10c20d4c4d067406 Mon Sep 17 00:00:00 2001 From: Daniel Brooks Date: Sun, 1 Sep 2024 13:59:50 -0700 Subject: [PATCH] properly compute colors for palette indexes above 15 in curses builds Fixes #72973, #76092 --- src/cata_imgui.cpp | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/src/cata_imgui.cpp b/src/cata_imgui.cpp index d4e18ec1d2b25..dd91172235aa2 100644 --- a/src/cata_imgui.cpp +++ b/src/cata_imgui.cpp @@ -36,8 +36,39 @@ struct pairs { short BG; }; +ImVec4 impalette[256] = {}; std::array::COLOR_NAMES_COUNT> rgbPalette; -std::array colorpairs; //storage for pair'ed colored +std::array colorpairs; //storage for paired colors + +static ImVec4 compute_color( uint8_t index ) +{ + if( index < 16 ) { + RGBTuple &rgbCol = rgbPalette[index]; + return { static_cast( rgbCol.Red ) / 255.0f, + static_cast( rgbCol.Green ) / 255.0f, + static_cast( rgbCol.Blue ) / 255.0f, + 1.0f }; + } else if( index < 232 ) { + static uint8_t colors[6] = {0, 95, 135, 175, 215, 255}; + int i = index - 16; + int r = i / 36; + i -= 36 * r; + int g = i / 6; + i -= 6 * g; + int b = i; + return { static_cast( colors[r] ) / 255.0f, + static_cast( colors[g] ) / 255.0f, + static_cast( colors[b] ) / 255.0f, + 1.0f }; + } else { + static uint8_t gray[24] = {8, 18, 28, 38, 48, 58, 68, 78, 88, 98, 108, 118, + 128, 138, 148, 158, 168, 178, 188, 198, 208, 218, + 228, 238 + }; + float level = static_cast( gray[index - 232] ) / 255.0f; + return { level, level, level, 1.0f }; + } +} ImVec4 cataimgui::imvec4_from_color( nc_color &color ) { @@ -48,11 +79,7 @@ ImVec4 cataimgui::imvec4_from_color( nc_color &color ) if( color.is_bold() ) { palette_index += color_loader::COLOR_NAMES_COUNT / 2; } - RGBTuple &rgbCol = rgbPalette[palette_index]; - return { static_cast( rgbCol.Red / 255. ), - static_cast( rgbCol.Green / 255. ), - static_cast( rgbCol.Blue / 255. ), - static_cast( 255. ) }; + return impalette[palette_index]; } std::vector> imtui_events; @@ -177,8 +204,10 @@ void cataimgui::client::process_input( void *input ) void cataimgui::load_colors() { - color_loader().load( rgbPalette ); + for( int i = 0; i < 256; i++ ) { + impalette[i] = compute_color( i ); + } } void cataimgui::init_pair( int p, int f, int b )