summaryrefslogtreecommitdiff
path: root/include/tools/color.hxx
diff options
context:
space:
mode:
authorNoel <noel.grandin@collabora.co.uk>2021-01-15 14:49:12 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2021-01-16 10:07:07 +0100
commit63a68064bb33f180b8a231f7524d99405d910226 (patch)
tree7ecf05b057c5ca4d80a48af045998a4b34484561 /include/tools/color.hxx
parentd534a4c7b45ff254b339e806c6a11f13d9ff0043 (diff)
make the Color constructors explicitly specify transparency
to reduce the churn, we leave the existing constructor in place, and add a clang plugin to detect when the value passed to the existing constructor may contain transparency/alpha data. i.e. we leave expressions like Color(0xffffff) alone, but warn about any non-constant expression, and any expression like Color(0xff000000) Change-Id: Id2ce58e08882d9b7bd0b9f88eca97359dcdbcc8c Reviewed-on: https://gerrit.libreoffice.org/c/core/+/109362 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'include/tools/color.hxx')
-rw-r--r--include/tools/color.hxx38
1 files changed, 29 insertions, 9 deletions
diff --git a/include/tools/color.hxx b/include/tools/color.hxx
index aca6b0ed79b7..3226184ca117 100644
--- a/include/tools/color.hxx
+++ b/include/tools/color.hxx
@@ -40,6 +40,10 @@ constexpr sal_uInt8 ColorChannelMerge(sal_uInt8 nDst, sal_uInt8 nSrc, sal_uInt8
}
+/** used to deliberately select the right constructor */
+enum ColorTransparencyTag { ColorTransparency = 0 };
+enum ColorAlphaTag { ColorAlpha = 0 };
+
// Color
class SAL_WARN_UNUSED TOOLS_DLLPUBLIC Color
@@ -70,21 +74,37 @@ public:
: mValue(0) // black
{}
- constexpr Color(sal_uInt32 nColor)
+ constexpr Color(const sal_uInt32 nColor)
: mValue(nColor)
- {}
+ {
+ assert(nColor <= 0xffffff && "don't pass transparency to this constructor, use the Color(ColorTransparencyTag,...) or Color(ColorAlphaTag,...) constructor to make it explicit");
+ }
- constexpr Color(sal_uInt8 nTransparency, sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue)
+ constexpr Color(enum ColorTransparencyTag, sal_uInt32 nColor)
+ : mValue(nColor)
+ {
+ }
+
+ constexpr Color(enum ColorAlphaTag, sal_uInt32 nColor)
+ : mValue((nColor & 0xffffff) | (255 - (nColor >> 24)))
+ {
+ }
+
+ constexpr Color(enum ColorTransparencyTag, sal_uInt8 nTransparency, sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue)
: mValue(sal_uInt32(nBlue) | (sal_uInt32(nGreen) << 8) | (sal_uInt32(nRed) << 16) | (sal_uInt32(nTransparency) << 24))
{}
+ constexpr Color(enum ColorAlphaTag, sal_uInt8 nAlpha, sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue)
+ : Color(ColorTransparency, 255 - nAlpha, nRed, nGreen, nBlue)
+ {}
+
constexpr Color(sal_uInt8 nRed, sal_uInt8 nGreen, sal_uInt8 nBlue)
- : Color(0, nRed, nGreen, nBlue)
+ : Color(ColorTransparency, 0, nRed, nGreen, nBlue)
{}
// constructor to create a tools-Color from ::basegfx::BColor
explicit Color(const basegfx::BColor& rBColor)
- : Color(0,
+ : Color(ColorTransparency, 0,
sal_uInt8(std::lround(rBColor.getRed() * 255.0)),
sal_uInt8(std::lround(rBColor.getGreen() * 255.0)),
sal_uInt8(std::lround(rBColor.getBlue() * 255.0)))
@@ -394,7 +414,7 @@ inline bool operator >>=( const css::uno::Any & rAny, Color & value )
sal_Int32 nTmp = {}; // spurious -Werror=maybe-uninitialized
if (!(rAny >>= nTmp))
return false;
- value = Color(nTmp);
+ value = Color(ColorTransparency, nTmp);
return true;
}
@@ -413,7 +433,7 @@ namespace com::sun::star::uno {
// Test compile time conversion of Color to sal_uInt32
-static_assert (sal_uInt32(Color(0x00, 0x12, 0x34, 0x56)) == 0x00123456);
+static_assert (sal_uInt32(Color(ColorTransparency, 0x00, 0x12, 0x34, 0x56)) == 0x00123456);
static_assert (sal_uInt32(Color(0x12, 0x34, 0x56)) == 0x00123456);
// Color types
@@ -437,8 +457,8 @@ constexpr ::Color COL_LIGHTMAGENTA ( 0xFF, 0x00, 0xFF );
constexpr ::Color COL_LIGHTGRAYBLUE ( 0xE0, 0xE0, 0xFF );
constexpr ::Color COL_YELLOW ( 0xFF, 0xFF, 0x00 );
constexpr ::Color COL_WHITE ( 0xFF, 0xFF, 0xFF );
-constexpr ::Color COL_TRANSPARENT ( 0xFF, 0xFF, 0xFF, 0xFF );
-constexpr ::Color COL_AUTO ( 0xFF, 0xFF, 0xFF, 0xFF );
+constexpr ::Color COL_TRANSPARENT ( ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF );
+constexpr ::Color COL_AUTO ( ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF );
constexpr ::Color COL_AUTHOR1_DARK ( 198, 146, 0 );
constexpr ::Color COL_AUTHOR1_NORMAL ( 255, 255, 158 );
constexpr ::Color COL_AUTHOR1_LIGHT ( 255, 255, 195 );