From 33da9b2327a098df445c4ebd924237adda1cdd8b Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Wed, 5 Apr 2023 14:07:01 +0100 Subject: [PATCH] Reintroduce constexpr to COLOR4D This fixes the initialization-order fiasco in the color initialization sequence, which was originally fixed by making COLOR4D constexpr, but was then reintroduced when the assert was changed to wxASSERT (wxASSERT is not compatible with constexpr). --- common/gal/color4d.cpp | 11 ++++++----- include/gal/color4d.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/common/gal/color4d.cpp b/common/gal/color4d.cpp index 937d4a428c..150e2008c8 100644 --- a/common/gal/color4d.cpp +++ b/common/gal/color4d.cpp @@ -521,11 +521,12 @@ COLOR4D& COLOR4D::Desaturate() return *this; } - -const COLOR4D COLOR4D::UNSPECIFIED( 0, 0, 0, 0 ); -const COLOR4D COLOR4D::WHITE( 1, 1, 1, 1 ); -const COLOR4D COLOR4D::BLACK( 0, 0, 0, 1 ); -const COLOR4D COLOR4D::CLEAR( 1, 0, 1, 0 ); +// These call the 5-argument constructor to get a constexpr COLOR4D object. +// The 5th argument isn't actually used at all, so its bool value doesn't matter. +constexpr COLOR4D COLOR4D::UNSPECIFIED( 0, 0, 0, 0, true ); +constexpr COLOR4D COLOR4D::WHITE( 1, 1, 1, 1, true ); +constexpr COLOR4D COLOR4D::BLACK( 0, 0, 0, 1, true ); +constexpr COLOR4D COLOR4D::CLEAR( 1, 0, 1, 0, true ); double COLOR4D::Distance( const COLOR4D& other ) const diff --git a/include/gal/color4d.h b/include/gal/color4d.h index 5616d8b104..f1fa893503 100644 --- a/include/gal/color4d.h +++ b/include/gal/color4d.h @@ -129,6 +129,37 @@ public: wxASSERT( a >= 0.0 && a <= 1.0 ); } + /** + * A COLOR4D constructor that is constexpr-capable. + * + * This constructor simply adds a 5th unused argument to differentiate it from + * the normal 4-argument constructor, so any COLOR4D instances that should be compile-time + * created should call this 5-argument version, while everyother instance should use the + * 4-argument version. + * + * @param aRed is the red component [0.0 .. 1.0]. + * @param aGreen is the green component [0.0 .. 1.0]. + * @param aBlue is the blue component [0.0 .. 1.0]. + * @param aAlpha is the alpha value [0.0 .. 1.0]. + * @param aIsConst is just an argument to differentiate this constructor from the 4-argument one, + * actual value doesn't matter. + */ + constexpr COLOR4D( double aRed, double aGreen, double aBlue, double aAlpha, bool aIsConst ) : + r( aRed ), + g( aGreen ), + b( aBlue ), + a( aAlpha ) + { + /*!!!!!!!!!!!!!!!!!!!!!! + * DO NOT change these to wxASSERT or collapse this into the other constructor, + * it must remain separate to ensure COLOR4D objects can be compile-time constructed + */ + assert( r >= 0.0 && r <= 1.0 ); + assert( g >= 0.0 && g <= 1.0 ); + assert( b >= 0.0 && b <= 1.0 ); + assert( a >= 0.0 && a <= 1.0 ); + } + /** * @param aColor is one of KiCad's palette colors. * @see EDA_COLOR_T