From 5b3d947b7ec2a9a2817def008b8ad201a0a18d77 Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Fri, 19 Jun 2020 17:05:18 +0100 Subject: [PATCH] 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. --- common/dialogs/dialog_color_picker.cpp | 6 +- common/gal/color4d.cpp | 78 ++++++++++++++------------ include/gal/color4d.h | 2 +- 3 files changed, 46 insertions(+), 40 deletions(-) diff --git a/common/dialogs/dialog_color_picker.cpp b/common/dialogs/dialog_color_picker.cpp index 33f8d29973..466d3753cb 100644 --- a/common/dialogs/dialog_color_picker.cpp +++ b/common/dialogs/dialog_color_picker.cpp @@ -237,7 +237,7 @@ void DIALOG_COLOR_PICKER::initDefinedColors( CUSTOM_COLORS_LIST* aPredefinedColo 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; wxMemoryDC iconDC; @@ -246,7 +246,7 @@ void DIALOG_COLOR_PICKER::initDefinedColors( CUSTOM_COLORS_LIST* aPredefinedColo 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; iconDC.SetPen( *wxBLACK_PEN ); @@ -265,7 +265,7 @@ void DIALOG_COLOR_PICKER::initDefinedColors( CUSTOM_COLORS_LIST* aPredefinedColo wxLEFT | wxBOTTOM, 5 ); wxStaticText* label = new wxStaticText( m_panelDefinedColors, -1, - wxGetTranslation( g_ColorRefs[ii].m_ColorName ), + wxGetTranslation( colorRefs()[ii].m_ColorName ), wxDefaultPosition, wxDefaultSize, 0 ); m_fgridColor->Add( label, 1, wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | diff --git a/common/gal/color4d.cpp b/common/gal/color4d.cpp index 14a7e14e1e..78f1bec822 100644 --- a/common/gal/color4d.cpp +++ b/common/gal/color4d.cpp @@ -32,39 +32,45 @@ using namespace KIGFX; #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() { - { 0, 0, 0, BLACK, TS( "Black" ), DARKDARKGRAY }, - { 72, 72, 72, DARKDARKGRAY, TS( "Gray 1" ), DARKGRAY }, - { 132, 132, 132, DARKGRAY, TS( "Gray 2" ), LIGHTGRAY }, - { 194, 194, 194, LIGHTGRAY, TS( "Gray 3" ), WHITE }, - { 255, 255, 255, WHITE, TS( "White" ), WHITE }, - { 194, 255, 255, LIGHTYELLOW, TS( "L.Yellow" ), WHITE }, - { 72, 0, 0, DARKBLUE, TS( "Blue 1" ), BLUE }, - { 0, 72, 0, DARKGREEN, TS( "Green 1" ), GREEN }, - { 72, 72, 0, DARKCYAN, TS( "Cyan 1" ), CYAN }, - { 0, 0, 72, DARKRED, TS( "Red 1" ), RED }, - { 72, 0, 72, DARKMAGENTA, TS( "Magenta 1" ), MAGENTA }, - { 0, 72, 72, DARKBROWN, TS( "Brown 1" ), BROWN }, - { 132, 0, 0, BLUE, TS( "Blue 2" ), LIGHTBLUE }, - { 0, 132, 0, GREEN, TS( "Green 2" ), LIGHTGREEN }, - { 132, 132, 0, CYAN, TS( "Cyan 2" ), LIGHTCYAN }, - { 0, 0, 132, RED, TS( "Red 2" ), LIGHTRED }, - { 132, 0, 132, MAGENTA, TS( "Magenta 2" ), LIGHTMAGENTA }, - { 0, 132, 132, BROWN, TS( "Brown 2" ), YELLOW }, - { 194, 0, 0, LIGHTBLUE, TS( "Blue 3" ), PUREBLUE, }, - { 0, 194, 0, LIGHTGREEN, TS( "Green 3" ), PUREGREEN }, - { 194, 194, 0, LIGHTCYAN, TS( "Cyan 3" ), PURECYAN }, - { 0, 0, 194, LIGHTRED, TS( "Red 3" ), PURERED }, - { 194, 0, 194, LIGHTMAGENTA, TS( "Magenta 3" ), PUREMAGENTA }, - { 0, 194, 194, YELLOW, TS( "Yellow 3" ), PUREYELLOW }, - { 255, 0, 0, PUREBLUE, TS( "Blue 4" ), WHITE }, - { 0, 255, 0, PUREGREEN, TS( "Green 4" ), WHITE }, - { 255, 255, 0, PURECYAN, TS( "Cyan 4" ), WHITE }, - { 0, 0, 255, PURERED, TS( "Red 4" ), WHITE }, - { 255, 0, 255, PUREMAGENTA, TS( "Magenta 4" ), WHITE }, - { 0, 255, 255, PUREYELLOW, TS( "Yellow 4" ), WHITE }, -}; + static StructColors s_ColorRefs[NBCOLORS] = + { + { 0, 0, 0, BLACK, TS( "Black" ), DARKDARKGRAY }, + { 72, 72, 72, DARKDARKGRAY, TS( "Gray 1" ), DARKGRAY }, + { 132, 132, 132, DARKGRAY, TS( "Gray 2" ), LIGHTGRAY }, + { 194, 194, 194, LIGHTGRAY, TS( "Gray 3" ), WHITE }, + { 255, 255, 255, WHITE, TS( "White" ), WHITE }, + { 194, 255, 255, LIGHTYELLOW, TS( "L.Yellow" ), WHITE }, + { 72, 0, 0, DARKBLUE, TS( "Blue 1" ), BLUE }, + { 0, 72, 0, DARKGREEN, TS( "Green 1" ), GREEN }, + { 72, 72, 0, DARKCYAN, TS( "Cyan 1" ), CYAN }, + { 0, 0, 72, DARKRED, TS( "Red 1" ), RED }, + { 72, 0, 72, DARKMAGENTA, TS( "Magenta 1" ), MAGENTA }, + { 0, 72, 72, DARKBROWN, TS( "Brown 1" ), BROWN }, + { 132, 0, 0, BLUE, TS( "Blue 2" ), LIGHTBLUE }, + { 0, 132, 0, GREEN, TS( "Green 2" ), LIGHTGREEN }, + { 132, 132, 0, CYAN, TS( "Cyan 2" ), LIGHTCYAN }, + { 0, 0, 132, RED, TS( "Red 2" ), LIGHTRED }, + { 132, 0, 132, MAGENTA, TS( "Magenta 2" ), LIGHTMAGENTA }, + { 0, 132, 132, BROWN, TS( "Brown 2" ), YELLOW }, + { 194, 0, 0, LIGHTBLUE, TS( "Blue 3" ), PUREBLUE, }, + { 0, 194, 0, LIGHTGREEN, TS( "Green 3" ), PUREGREEN }, + { 194, 194, 0, LIGHTCYAN, TS( "Cyan 3" ), PURECYAN }, + { 0, 0, 194, LIGHTRED, TS( "Red 3" ), PURERED }, + { 194, 0, 194, LIGHTMAGENTA, TS( "Magenta 3" ), PUREMAGENTA }, + { 0, 194, 194, YELLOW, TS( "Yellow 3" ), PUREYELLOW }, + { 255, 0, 0, PUREBLUE, TS( "Blue 4" ), WHITE }, + { 0, 255, 0, PUREGREEN, TS( "Green 4" ), WHITE }, + { 255, 255, 0, PURECYAN, TS( "Cyan 4" ), WHITE }, + { 0, 0, 255, PURERED, TS( "Red 4" ), WHITE }, + { 255, 0, 255, PUREMAGENTA, TS( "Magenta 4" ), WHITE }, + { 0, 255, 255, PUREYELLOW, TS( "Yellow 4" ), WHITE }, + }; + return s_ColorRefs; +} COLOR4D::COLOR4D( EDA_COLOR_T aColor ) @@ -75,9 +81,9 @@ COLOR4D::COLOR4D( EDA_COLOR_T aColor ) return; } - r = g_ColorRefs[aColor].m_Red / 255.0; - g = g_ColorRefs[aColor].m_Green / 255.0; - b = g_ColorRefs[aColor].m_Blue / 255.0; + r = colorRefs()[aColor].m_Red / 255.0; + g = colorRefs()[aColor].m_Green / 255.0; + b = colorRefs()[aColor].m_Blue / 255.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; trying = static_cast( int( trying ) + 1 ) ) { - const StructColors &c = g_ColorRefs[trying]; + const StructColors &c = colorRefs()[trying]; int distance = (aR - c.m_Red) * (aR - c.m_Red) + (aG - c.m_Green) * (aG - c.m_Green) + (aB - c.m_Blue) * (aB - c.m_Blue); diff --git a/include/gal/color4d.h b/include/gal/color4d.h index 97e4850ca0..a38e0d6c49 100644 --- a/include/gal/color4d.h +++ b/include/gal/color4d.h @@ -87,7 +87,7 @@ struct StructColors }; /// 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