diff options
author | Neil <nyamatongwe@gmail.com> | 2020-06-06 16:13:43 +1000 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2020-06-06 16:13:43 +1000 |
commit | 34b22f7165d91eae9d720f629196f9b4e8f7a9b2 (patch) | |
tree | 390850c452feeb54e6d3ada38371c855e07f6eef | |
parent | d94ed9026036e6b98001ef2adbf5ce605fbb8a29 (diff) | |
download | scintilla-mirror-34b22f7165d91eae9d720f629196f9b4e8f7a9b2.tar.gz |
Avoid type-pun union when converting from RGBA colour to DWORD as this may be
undefined or implementation defined behaviour.
Drop some casts by hoisting out part of dwordMultiplied.
-rw-r--r-- | win32/PlatWin.cxx | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/win32/PlatWin.cxx b/win32/PlatWin.cxx index da2671260..b2b90fa5e 100644 --- a/win32/PlatWin.cxx +++ b/win32/PlatWin.cxx @@ -738,23 +738,19 @@ void AllFour(DWORD *pixels, int width, int height, int x, int y, DWORD val) noex pixels[(height-1-y)*width+width-1-x] = val; } -DWORD dwordFromBGRA(byte b, byte g, byte r, byte a) noexcept { - union { - byte pixVal[4]; - DWORD val; - } converter; - converter.pixVal[0] = b; - converter.pixVal[1] = g; - converter.pixVal[2] = r; - converter.pixVal[3] = a; - return converter.val; +constexpr DWORD dwordFromBGRA(byte b, byte g, byte r, byte a) noexcept { + return (a << 24) | (r << 16) | (g << 8) | b; +} + +constexpr byte AlphaScaled(unsigned char component, unsigned int alpha) noexcept { + return static_cast<byte>(component * alpha / 255); } DWORD dwordMultiplied(ColourDesired colour, unsigned int alpha) noexcept { return dwordFromBGRA( - static_cast<byte>(colour.GetBlue() * alpha / 255), - static_cast<byte>(colour.GetGreen() * alpha / 255), - static_cast<byte>(colour.GetRed() * alpha / 255), + AlphaScaled(colour.GetBlue(), alpha), + AlphaScaled(colour.GetGreen(), alpha), + AlphaScaled(colour.GetRed(), alpha), static_cast<byte>(alpha)); } |