From 4c355c32d3e70d1fe141e3e8d9a0b6b4dd96050c Mon Sep 17 00:00:00 2001
From: John Beard <john.j.beard@gmail.com>
Date: Thu, 8 Nov 2018 13:28:42 +0000
Subject: [PATCH] Fix rounding in COLOR4D::ToColour

Due to the implicit floor of the cast from double to unsigned char,
there was a small rounding error in COLOUR4D's WX conversion function.

This fixes the failing tests.

Also make the cast to unsigned char explicit.
---
 common/gal/color4d.cpp     | 14 ++++++++++++++
 include/gal/color4d.h      |  6 +-----
 qa/common/test_color4d.cpp |  5 +----
 3 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/common/gal/color4d.cpp b/common/gal/color4d.cpp
index b7653f3ccb..d4abcf5c01 100644
--- a/common/gal/color4d.cpp
+++ b/common/gal/color4d.cpp
@@ -79,6 +79,20 @@ COLOR4D::COLOR4D( EDA_COLOR_T aColor )
     }
 
 
+    wxColour COLOR4D::ToColour() const
+    {
+        using CHAN_T = wxColourBase::ChannelType;
+
+        const wxColour colour(
+            static_cast<CHAN_T>( r * 255 + 0.5 ),
+            static_cast<CHAN_T>( g * 255 + 0.5 ),
+            static_cast<CHAN_T>( b * 255 + 0.5 ),
+            static_cast<CHAN_T>( a * 255 + 0.5 )
+        );
+        return colour;
+    }
+
+
     COLOR4D COLOR4D::LegacyMix( COLOR4D aColor ) const
     {
         COLOR4D candidate;
diff --git a/include/gal/color4d.h b/include/gal/color4d.h
index c5ff3ccc5c..a5bf00c865 100644
--- a/include/gal/color4d.h
+++ b/include/gal/color4d.h
@@ -89,11 +89,7 @@ public:
 
     wxString ToWxString( long flags ) const;
 
-    wxColour ToColour() const
-    {
-        wxColour colour( r * 255, g * 255, b * 255, a * 255 );
-        return colour;
-    }
+    wxColour ToColour() const;
 
     /**
      * Function LegacyMix()
diff --git a/qa/common/test_color4d.cpp b/qa/common/test_color4d.cpp
index 940fb0ad1d..9d9354c4a2 100644
--- a/qa/common/test_color4d.cpp
+++ b/qa/common/test_color4d.cpp
@@ -247,12 +247,11 @@ static std::vector<WX_CONV_CASE> wx_conv_cases = {
     { { 0xFF, 0x00, 0x00, 0xFF }, { 0.999, 0.001, 0.0, 1.0 } },
 };
 
-#ifdef HAVE_EXPECTED_FAILURES
 
 /**
  * Check conversion to WxColour
  */
-BOOST_AUTO_TEST_CASE( ToWx, *boost::unit_test::expected_failures( 3 ) )
+BOOST_AUTO_TEST_CASE( ToWx )
 {
     for( const auto& c : wx_conv_cases )
     {
@@ -266,8 +265,6 @@ BOOST_AUTO_TEST_CASE( ToWx, *boost::unit_test::expected_failures( 3 ) )
     }
 }
 
-#endif // HAVE_EXPECTED_FAILURES
-
 
 /**
  * Check conversion from WxColour