← Back to team overview

kicad-developers team mailing list archive

[PATCH] Preserve color scheme when switching to legacy (Fixes lp:1670669)

 

Hi all,

This patch changes the color scheme behavior when switching between GAL and
legacy canvases.  Previously, the switch would cause all colors to be
mutated to "legacy safe" versions.  Now, switching to legacy just sets a
flag that causes the colors to be returned from the COLORS_DESIGN_SETTINGS
as "legacy safe" while preserving the underlying colors.

-Jon
From f5ce890d7538aeaafb3bfde19eae71e933ed7616 Mon Sep 17 00:00:00 2001
From: Jon Evans <jon@xxxxxxxxxxxxx>
Date: Mon, 27 Nov 2017 22:12:31 -0500
Subject: [PATCH] Preserve color scheme when switching to legacy (Fixes
 lp:1670669)

Instead of modifying the colors when switching to legacy canvas,
they will now be preserved and only returned as the "legacy" colors.
---
 common/class_colors_design_settings.cpp |  7 +++++--
 common/gal/color4d.cpp                  |  2 +-
 gerbview/gerbview_frame.cpp             | 17 ++++-------------
 gerbview/gerbview_frame.h               |  3 ---
 include/class_colors_design_settings.h  | 16 ++++++++++++++++
 include/draw_frame.h                    |  6 ------
 include/gal/color4d.h                   |  7 ++++++-
 pcbnew/pcbframe.cpp                     | 23 ++++++-----------------
 pcbnew/wxPcbStruct.h                    |  3 ---
 9 files changed, 38 insertions(+), 46 deletions(-)

diff --git a/common/class_colors_design_settings.cpp b/common/class_colors_design_settings.cpp
index b030c072b..749ecfbc2 100644
--- a/common/class_colors_design_settings.cpp
+++ b/common/class_colors_design_settings.cpp
@@ -96,6 +96,7 @@ static const EDA_COLOR_T default_items_color[] = {
 COLORS_DESIGN_SETTINGS::COLORS_DESIGN_SETTINGS( FRAME_T aFrameType )
 {
     m_frameType = aFrameType;
+    m_legacyMode = false;
 
     for( unsigned src = 0, dst = 0; dst < DIM( m_LayersColors ); ++dst )
     {
@@ -124,7 +125,8 @@ COLOR4D COLORS_DESIGN_SETTINGS::GetLayerColor( LAYER_NUM aLayer ) const
 {
     if( (unsigned) aLayer < DIM( m_LayersColors ) )
     {
-        return m_LayersColors[aLayer];
+        return m_legacyMode ? m_LayersColors[aLayer].AsLegacyColor()
+                            : m_LayersColors[aLayer];
     }
     return COLOR4D::UNSPECIFIED;
 }
@@ -143,7 +145,8 @@ COLOR4D COLORS_DESIGN_SETTINGS::GetItemColor( int aItemIdx ) const
 {
     if( (unsigned) aItemIdx < DIM( m_LayersColors ) )
     {
-        return m_LayersColors[aItemIdx];
+        return m_legacyMode ? m_LayersColors[aItemIdx].AsLegacyColor()
+                            : m_LayersColors[aItemIdx];
     }
 
     return COLOR4D::UNSPECIFIED;
diff --git a/common/gal/color4d.cpp b/common/gal/color4d.cpp
index 4af7003fd..11b4366e3 100644
--- a/common/gal/color4d.cpp
+++ b/common/gal/color4d.cpp
@@ -144,7 +144,7 @@ COLOR4D::COLOR4D( EDA_COLOR_T aColor )
     }
 
 
-    EDA_COLOR_T COLOR4D::GetNearestLegacyColor( COLOR4D &aColor )
+    EDA_COLOR_T COLOR4D::GetNearestLegacyColor( const COLOR4D &aColor )
     {
         // Cache layer implemented here, because all callers are using wxColour
         static std::map< unsigned int, unsigned int > nearestCache;
diff --git a/gerbview/gerbview_frame.cpp b/gerbview/gerbview_frame.cpp
index d938237e0..a6abcc558 100644
--- a/gerbview/gerbview_frame.cpp
+++ b/gerbview/gerbview_frame.cpp
@@ -221,7 +221,7 @@ GERBVIEW_FRAME::GERBVIEW_FRAME( KIWAY* aKiway, wxWindow* aParent ):
     }
     else
     {
-        forceColorsToLegacy();
+        m_colorsSettings->SetLegacyMode( true );
         m_canvas->Refresh();
     }
 
@@ -1039,17 +1039,6 @@ void GERBVIEW_FRAME::unitsChangeRefresh()
 }
 
 
-void GERBVIEW_FRAME::forceColorsToLegacy()
-{
-    for( int i = 0; i < LAYER_ID_COUNT; i++ )
-    {
-        COLOR4D c = m_colorsSettings->GetLayerColor( i );
-        c.SetToNearestLegacyColor();
-        m_colorsSettings->SetLayerColor( i, c );
-    }
-}
-
-
 void GERBVIEW_FRAME::UseGalCanvas( bool aEnable )
 {
     EDA_DRAW_FRAME::UseGalCanvas( aEnable );
@@ -1065,6 +1054,8 @@ void GERBVIEW_FRAME::UseGalCanvas( bool aEnable )
         if( m_toolManager )
             m_toolManager->ResetTools( TOOL_BASE::GAL_SWITCH );
 
+        m_colorsSettings->SetLegacyMode( false );
+
         galCanvas->GetGAL()->SetGridColor( GetLayerColor( LAYER_GERBVIEW_GRID ) );
 
         galCanvas->GetView()->RecacheAllItems();
@@ -1081,7 +1072,7 @@ void GERBVIEW_FRAME::UseGalCanvas( bool aEnable )
         // Redirect all events to the legacy canvas
         galCanvas->SetEventDispatcher( NULL );
 
-        forceColorsToLegacy();
+        m_colorsSettings->SetLegacyMode( true );
         m_canvas->Refresh();
     }
 
diff --git a/gerbview/gerbview_frame.h b/gerbview/gerbview_frame.h
index d35ea8f1e..c0d8ecd0f 100644
--- a/gerbview/gerbview_frame.h
+++ b/gerbview/gerbview_frame.h
@@ -173,9 +173,6 @@ protected:
     /// The last filename chosen to be proposed to the user
     wxString                m_lastFileName;
 
-    ///> @copydoc EDA_DRAW_FRAME::forceColorsToLegacy()
-    virtual void forceColorsToLegacy() override;
-
 public:
     wxChoice* m_SelComponentBox;                // a choice box to display and highlight component graphic items
     wxChoice* m_SelNetnameBox;                  // a choice box to display and highlight netlist graphic items
diff --git a/include/class_colors_design_settings.h b/include/class_colors_design_settings.h
index e8e6fa22b..c91dcdfb4 100644
--- a/include/class_colors_design_settings.h
+++ b/include/class_colors_design_settings.h
@@ -92,9 +92,25 @@ public:
      */
     void SetAllColorsAs( COLOR4D aColor );
 
+    /**
+     * Enables or disables legacy color mode.  When enabled, all colors will be
+     * quantized to the legacy color palette when returned from GetItemColor and
+     * GetLayerColor (but the underlying color will not be changed, and can
+     * still be set to arbitrary colors).
+     */
+    void SetLegacyMode( bool aMode )
+    {
+        m_legacyMode = aMode;
+    }
+
 private:
     FRAME_T m_frameType;
 
+    /**
+     * @see SetLegacyMode()
+     */
+    bool m_legacyMode;
+
     void setupConfigParams();
 
 };
diff --git a/include/draw_frame.h b/include/draw_frame.h
index 536814e0b..e3e2cae89 100644
--- a/include/draw_frame.h
+++ b/include/draw_frame.h
@@ -149,12 +149,6 @@ protected:
      */
     virtual void unitsChangeRefresh();
 
-    /**
-     * Helper function to coerce all colors to legacy-compatible when
-     * switching from GAL to legacy canvas
-     */
-    virtual void forceColorsToLegacy() {}
-
     /**
      * Function GeneralControlKeyMovement
      * Handle the common part of GeneralControl dedicated to global
diff --git a/include/gal/color4d.h b/include/gal/color4d.h
index 0cde7651f..c5ff3ccc5 100644
--- a/include/gal/color4d.h
+++ b/include/gal/color4d.h
@@ -121,6 +121,11 @@ public:
      */
     COLOR4D& SetToNearestLegacyColor();
 
+    COLOR4D AsLegacyColor() const
+    {
+        return COLOR4D( COLOR4D::GetNearestLegacyColor( *this ) );
+    }
+
     /**
      * Packs the color into an unsigned int for compatibility with legacy canvas.
      * Note that this is a lossy downsampling and also that the alpha channel is lost.
@@ -135,7 +140,7 @@ public:
     /**
      * Determines the "nearest" EDA_COLOR_T according to ColorFindNearest
      */
-    static EDA_COLOR_T GetNearestLegacyColor( COLOR4D &aColor );
+    static EDA_COLOR_T GetNearestLegacyColor( const COLOR4D &aColor );
 #endif /* WX_COMPATIBLITY */
 
     /**
diff --git a/pcbnew/pcbframe.cpp b/pcbnew/pcbframe.cpp
index 4dd100559..28cd22831 100644
--- a/pcbnew/pcbframe.cpp
+++ b/pcbnew/pcbframe.cpp
@@ -713,11 +713,15 @@ void PCB_EDIT_FRAME::UseGalCanvas( bool aEnable )
         Compile_Ratsnest( NULL, true );
 
     PCB_BASE_EDIT_FRAME::UseGalCanvas( aEnable );
+    COLORS_DESIGN_SETTINGS& cds = Settings().Colors();
 
     if( aEnable )
     {
-        COLORS_DESIGN_SETTINGS& cds = Settings().Colors();
+        cds.SetLegacyMode( false );
         GetGalCanvas()->GetGAL()->SetGridColor( cds.GetLayerColor( LAYER_GRID ) );
+        auto view = GetGalCanvas()->GetView();
+        view->GetPainter()->GetSettings()->ImportLegacyColors( &cds );
+        GetGalCanvas()->Refresh();
     }
 
     enableGALSpecificMenus();
@@ -725,7 +729,7 @@ void PCB_EDIT_FRAME::UseGalCanvas( bool aEnable )
     // Force colors to be legacy-compatible in case they were changed in GAL
     if( !aEnable )
     {
-        forceColorsToLegacy();
+        cds.SetLegacyMode( true );
         Refresh();
     }
 
@@ -734,21 +738,6 @@ void PCB_EDIT_FRAME::UseGalCanvas( bool aEnable )
 }
 
 
-void PCB_EDIT_FRAME::forceColorsToLegacy()
-{
-    COLORS_DESIGN_SETTINGS& cds = Settings().Colors();
-
-    for( unsigned i = 0; i < DIM( cds.m_LayersColors ); i++ )
-    {
-        COLOR4D c = cds.GetLayerColor( i );
-        c.SetToNearestLegacyColor();
-        // Note the alpha chanel is not modified. Therefore the value
-        // is the previous value used in GAL canvas.
-        cds.SetLayerColor( i, c );
-    }
-}
-
-
 void PCB_EDIT_FRAME::enableGALSpecificMenus()
 {
     // some menus are active only in GAL mode and do nothing in legacy mode.
diff --git a/pcbnew/wxPcbStruct.h b/pcbnew/wxPcbStruct.h
index 46ec7263f..f6fd1a8fe 100644
--- a/pcbnew/wxPcbStruct.h
+++ b/pcbnew/wxPcbStruct.h
@@ -123,9 +123,6 @@ protected:
      */
     virtual void SwitchCanvas( wxCommandEvent& aEvent ) override;
 
-    ///> @copydoc EDA_DRAW_FRAME::forceColorsToLegacy()
-    virtual void forceColorsToLegacy() override;
-
 #if defined(KICAD_SCRIPTING) && defined(KICAD_SCRIPTING_ACTION_MENU)
     /**
      * Function RebuildActionPluginMenus
-- 
2.14.1


Follow ups