From ed434975e5de00e91718b075f04492f69c1bb04a Mon Sep 17 00:00:00 2001 From: Neil Date: Tue, 20 Apr 2021 09:11:22 +1000 Subject: Feature [feature-requests:#1402]. Unify colour type with ColourAlpha. Change ColourDesired to ColourAlpha in XPM and RGBAImage. --- src/Indicator.cxx | 18 ++++++++++-------- src/XPM.cxx | 37 ++++++++++++++----------------------- src/XPM.h | 8 ++++---- 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/Indicator.cxx b/src/Indicator.cxx index 1ebc18b7f..f65b461ef 100644 --- a/src/Indicator.cxx +++ b/src/Indicator.cxx @@ -74,17 +74,19 @@ void Indicator::Draw(Surface *surface, const PRectangle &rc, const PRectangle &r const int width = std::min(4000, static_cast(rcSquiggle.Width())); RGBAImage image(width, 3, 1.0, nullptr); - enum { alphaFull = 0xff, alphaSide = 0x2f, alphaSide2=0x5f }; + constexpr unsigned int alphaFull = 0xff; + constexpr unsigned int alphaSide = 0x2f; + constexpr unsigned int alphaSide2 = 0x5f; for (int x = 0; x < width; x++) { if (x%2) { // Two halfway columns have a full pixel in middle flanked by light pixels - image.SetPixel(x, 0, sacDraw.fore, alphaSide); - image.SetPixel(x, 1, sacDraw.fore, alphaFull); - image.SetPixel(x, 2, sacDraw.fore, alphaSide); + image.SetPixel(x, 0, ColourAlpha(sacDraw.fore, alphaSide)); + image.SetPixel(x, 1, ColourAlpha(sacDraw.fore, alphaFull)); + image.SetPixel(x, 2, ColourAlpha(sacDraw.fore, alphaSide)); } else { // Extreme columns have a full pixel at bottom or top and a mid-tone pixel in centre - image.SetPixel(x, (x % 4) ? 0 : 2, sacDraw.fore, alphaFull); - image.SetPixel(x, 1, sacDraw.fore, alphaSide2); + image.SetPixel(x, (x % 4) ? 0 : 2, ColourAlpha(sacDraw.fore, alphaFull)); + image.SetPixel(x, 1, ColourAlpha(sacDraw.fore, alphaSide2)); } } surface->DrawRGBAImage(rcSquiggle, image.GetWidth(), image.GetHeight(), image.Pixels()); @@ -207,13 +209,13 @@ void Indicator::Draw(Surface *surface, const PRectangle &rc, const PRectangle &r // Draw horizontal lines top and bottom for (int x=0; xDrawRGBAImage(rcBox, image.GetWidth(), image.GetHeight(), image.Pixels()); diff --git a/src/XPM.cxx b/src/XPM.cxx index c310911f6..ef2ba961a 100644 --- a/src/XPM.cxx +++ b/src/XPM.cxx @@ -60,17 +60,17 @@ unsigned int ValueOfHex(const char ch) noexcept { return 0; } -ColourDesired ColourFromHex(const char *val) noexcept { +ColourAlpha ColourFromHex(const char *val) noexcept { const unsigned int r = ValueOfHex(val[0]) * 16 + ValueOfHex(val[1]); const unsigned int g = ValueOfHex(val[2]) * 16 + ValueOfHex(val[3]); const unsigned int b = ValueOfHex(val[4]) * 16 + ValueOfHex(val[5]); - return ColourDesired(r, g, b); + return ColourAlpha(r, g, b); } } -ColourDesired XPM::ColourFromCode(int ch) const noexcept { +ColourAlpha XPM::ColourFromCode(int ch) const noexcept { return colourCodeTable[ch]; } @@ -116,7 +116,7 @@ void XPM::Init(const char *const *linesForm) { if (!linesForm) return; - std::fill(colourCodeTable, std::end(colourCodeTable), ColourDesired(0)); + std::fill(colourCodeTable, std::end(colourCodeTable), ColourAlpha(0, 0, 0)); const char *line0 = linesForm[0]; width = atoi(line0); line0 = NextField(line0); @@ -134,7 +134,7 @@ void XPM::Init(const char *const *linesForm) { const char *colourDef = linesForm[c+1]; const char code = colourDef[0]; colourDef += 4; - ColourDesired colour(0xff, 0xff, 0xff); + ColourAlpha colour(0, 0, 0, 0); if (*colourDef == '#') { colour = ColourFromHex(colourDef+1); } else { @@ -173,19 +173,13 @@ void XPM::Draw(Surface *surface, const PRectangle &rc) { } } -void XPM::PixelAt(int x, int y, ColourDesired &colour, bool &transparent) const noexcept { - if (pixels.empty() || (x<0) || (x >= width) || (y<0) || (y >= height)) { - colour = ColourDesired(0); - transparent = true; - return; +ColourAlpha XPM::PixelAt(int x, int y) const noexcept { + if (pixels.empty() || (x < 0) || (x >= width) || (y < 0) || (y >= height)) { + // Out of bounds -> transparent black + return ColourAlpha(0, 0, 0, 0); } const int code = pixels[y * width + x]; - transparent = code == codeTransparent; - if (transparent) { - colour = ColourDesired(0); - } else { - colour = ColourFromCode(code); - } + return ColourFromCode(code); } std::vector XPM::LinesFormFromTextForm(const char *textForm) { @@ -239,10 +233,7 @@ RGBAImage::RGBAImage(const XPM &xpm) { pixelBytes.resize(CountBytes()); for (int y=0; y(alpha); + pixel[3] = colour.GetAlpha(); } // Transform a block of pixels from RGBA to BGRA with premultiplied alpha. diff --git a/src/XPM.h b/src/XPM.h index cacae9b8a..3f7b66b9a 100644 --- a/src/XPM.h +++ b/src/XPM.h @@ -18,9 +18,9 @@ class XPM { int width=1; int nColours=1; std::vector pixels; - ColourDesired colourCodeTable[256]; + ColourAlpha colourCodeTable[256]; char codeTransparent=' '; - ColourDesired ColourFromCode(int ch) const noexcept; + ColourAlpha ColourFromCode(int ch) const noexcept; void FillRun(Surface *surface, int code, int startX, int y, int x) const; public: explicit XPM(const char *textForm); @@ -36,7 +36,7 @@ public: void Draw(Surface *surface, const PRectangle &rc); int GetHeight() const noexcept { return height; } int GetWidth() const noexcept { return width; } - void PixelAt(int x, int y, ColourDesired &colour, bool &transparent) const noexcept; + ColourAlpha PixelAt(int x, int y) const noexcept; private: static std::vectorLinesFormFromTextForm(const char *textForm); }; @@ -65,7 +65,7 @@ public: float GetScaledWidth() const noexcept { return width / scale; } int CountBytes() const noexcept; const unsigned char *Pixels() const noexcept; - void SetPixel(int x, int y, ColourDesired colour, int alpha) noexcept; + void SetPixel(int x, int y, ColourAlpha colour) noexcept; static void BGRAFromRGBA(unsigned char *pixelsBGRA, const unsigned char *pixelsRGBA, size_t count) noexcept; }; -- cgit v1.2.3