← Back to team overview

kicad-developers team mailing list archive

[PATCH] Add WithAlpha method to COLOR4D

 

Hi,

Here's a patch to add a new method to COLOR4D: WithAlpha. This means
you can do this:

    const COLOR4D color = getSomeColor(); //could be an argument or static const

    gal.SetStrokeColor( color.WithAlpha( 0.5 ) );

Rather than:

    const COLOR4D color = getSomeColor(); //could be an argument or static const

    COLOR4D temp = color; //don't modify base color
    color.a = 0.5;
    gal.SetStrokeColor( temp );

or the shorter but less readable:

    gal.SetStrokeColor( { color.r, color.g, color.b, 0.5 } );

It also uses transparent sanity checking in debug builds.

Cheers,

John
From 25566fab5dbd0ef185b995383eae1ca5b97f6c12 Mon Sep 17 00:00:00 2001
From: John Beard <john.j.beard@xxxxxxxxx>
Date: Wed, 1 Mar 2017 09:59:25 +0800
Subject: [PATCH] Add WithAlpha method to COLOR4D

This is useful for taking a "base" color and making derived colours with
the given alpha, but the same RGB value.
---
 include/gal/color4d.h | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/include/gal/color4d.h b/include/gal/color4d.h
index 4e0c0f4ae..ebcf333bc 100644
--- a/include/gal/color4d.h
+++ b/include/gal/color4d.h
@@ -224,6 +224,19 @@ public:
     }
 
     /**
+     * Function WithAlpha
+     * Returns a colour with the same colour, but the given alpha
+     * @param aAlpha specifies the alpha of the new color
+     * @return COLOR4D color with that alpha
+     */
+     COLOR4D WithAlpha( double aAlpha ) const
+     {
+         assert( aAlpha >= 0.0 && aAlpha <= 1.0 );
+
+         return COLOR4D( r, g, b, aAlpha );
+     }
+
+    /**
      * Function Inverted
      * Returns an inverted color, alpha remains the same.
      * @return COLOR4D& Inverted color.
-- 
2.12.0


Follow ups