Fix initialization order fiasco with colors

The legacy color refs are needed by the COLOR4D constructor
when constructing a static variable, so they can't be static
themselves.
This commit is contained in:
Ian McInerney 2020-06-19 17:05:18 +01:00
parent 5649558cff
commit 5b3d947b7e
3 changed files with 46 additions and 40 deletions

View File

@ -237,7 +237,7 @@ void DIALOG_COLOR_PICKER::initDefinedColors( CUSTOM_COLORS_LIST* aPredefinedColo
grid_row++; grid_row++;
} }
int ii = grid_row + (grid_col*table_row_count); // The index in g_ColorRefs int ii = grid_row + (grid_col*table_row_count); // The index in colorRefs()
int butt_ID = ID_COLOR_BLACK + ii; int butt_ID = ID_COLOR_BLACK + ii;
wxMemoryDC iconDC; wxMemoryDC iconDC;
@ -246,7 +246,7 @@ void DIALOG_COLOR_PICKER::initDefinedColors( CUSTOM_COLORS_LIST* aPredefinedColo
iconDC.SelectObject( ButtBitmap ); iconDC.SelectObject( ButtBitmap );
KIGFX::COLOR4D buttcolor = KIGFX::COLOR4D( g_ColorRefs[ii].m_Numcolor ); KIGFX::COLOR4D buttcolor = KIGFX::COLOR4D( colorRefs()[ii].m_Numcolor );
m_Color4DList[ butt_ID - ID_COLOR_BLACK ] = buttcolor; m_Color4DList[ butt_ID - ID_COLOR_BLACK ] = buttcolor;
iconDC.SetPen( *wxBLACK_PEN ); iconDC.SetPen( *wxBLACK_PEN );
@ -265,7 +265,7 @@ void DIALOG_COLOR_PICKER::initDefinedColors( CUSTOM_COLORS_LIST* aPredefinedColo
wxLEFT | wxBOTTOM, 5 ); wxLEFT | wxBOTTOM, 5 );
wxStaticText* label = new wxStaticText( m_panelDefinedColors, -1, wxStaticText* label = new wxStaticText( m_panelDefinedColors, -1,
wxGetTranslation( g_ColorRefs[ii].m_ColorName ), wxGetTranslation( colorRefs()[ii].m_ColorName ),
wxDefaultPosition, wxDefaultSize, 0 ); wxDefaultPosition, wxDefaultSize, 0 );
m_fgridColor->Add( label, 1, m_fgridColor->Add( label, 1,
wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL |

View File

@ -32,8 +32,12 @@ using namespace KIGFX;
#define TS( string ) wxString( _HKI( string ) ).ToStdString() #define TS( string ) wxString( _HKI( string ) ).ToStdString()
const StructColors g_ColorRefs[NBCOLORS] = // We can't have this as a plain static variable, because it is referenced during the initialization
// of other static variables, so we must initialize it explicitly on first use.
const StructColors* colorRefs()
{ {
static StructColors s_ColorRefs[NBCOLORS] =
{
{ 0, 0, 0, BLACK, TS( "Black" ), DARKDARKGRAY }, { 0, 0, 0, BLACK, TS( "Black" ), DARKDARKGRAY },
{ 72, 72, 72, DARKDARKGRAY, TS( "Gray 1" ), DARKGRAY }, { 72, 72, 72, DARKDARKGRAY, TS( "Gray 1" ), DARKGRAY },
{ 132, 132, 132, DARKGRAY, TS( "Gray 2" ), LIGHTGRAY }, { 132, 132, 132, DARKGRAY, TS( "Gray 2" ), LIGHTGRAY },
@ -64,7 +68,9 @@ const StructColors g_ColorRefs[NBCOLORS] =
{ 0, 0, 255, PURERED, TS( "Red 4" ), WHITE }, { 0, 0, 255, PURERED, TS( "Red 4" ), WHITE },
{ 255, 0, 255, PUREMAGENTA, TS( "Magenta 4" ), WHITE }, { 255, 0, 255, PUREMAGENTA, TS( "Magenta 4" ), WHITE },
{ 0, 255, 255, PUREYELLOW, TS( "Yellow 4" ), WHITE }, { 0, 255, 255, PUREYELLOW, TS( "Yellow 4" ), WHITE },
}; };
return s_ColorRefs;
}
COLOR4D::COLOR4D( EDA_COLOR_T aColor ) COLOR4D::COLOR4D( EDA_COLOR_T aColor )
@ -75,9 +81,9 @@ COLOR4D::COLOR4D( EDA_COLOR_T aColor )
return; return;
} }
r = g_ColorRefs[aColor].m_Red / 255.0; r = colorRefs()[aColor].m_Red / 255.0;
g = g_ColorRefs[aColor].m_Green / 255.0; g = colorRefs()[aColor].m_Green / 255.0;
b = g_ColorRefs[aColor].m_Blue / 255.0; b = colorRefs()[aColor].m_Blue / 255.0;
a = 1.0; a = 1.0;
} }
@ -450,7 +456,7 @@ EDA_COLOR_T COLOR4D::FindNearestLegacyColor( int aR, int aG, int aB )
for( EDA_COLOR_T trying = EDA_COLOR_T::BLACK; trying < EDA_COLOR_T::NBCOLORS; for( EDA_COLOR_T trying = EDA_COLOR_T::BLACK; trying < EDA_COLOR_T::NBCOLORS;
trying = static_cast<EDA_COLOR_T>( int( trying ) + 1 ) ) trying = static_cast<EDA_COLOR_T>( int( trying ) + 1 ) )
{ {
const StructColors &c = g_ColorRefs[trying]; const StructColors &c = colorRefs()[trying];
int distance = (aR - c.m_Red) * (aR - c.m_Red) + int distance = (aR - c.m_Red) * (aR - c.m_Red) +
(aG - c.m_Green) * (aG - c.m_Green) + (aG - c.m_Green) * (aG - c.m_Green) +
(aB - c.m_Blue) * (aB - c.m_Blue); (aB - c.m_Blue) * (aB - c.m_Blue);

View File

@ -87,7 +87,7 @@ struct StructColors
}; };
/// Global list of legacy color names, still used all over the place for constructing COLOR4D's /// Global list of legacy color names, still used all over the place for constructing COLOR4D's
extern const StructColors g_ColorRefs[NBCOLORS]; const StructColors* colorRefs();
namespace KIGFX namespace KIGFX