DPF

DISTRHO Plugin Framework
Log | Files | Refs | Submodules | README | LICENSE

commit 277a5dfe18d8ca479947eea1e6d106955934536c
parent 717c641f3225c9d29345f3855c1f9428e77f1ae5
Author: falkTX <falktx@falktx.com>
Date:   Sat,  1 May 2021 16:22:38 +0100

Fix Color::fromHTML, cleanup

Signed-off-by: falkTX <falktx@falktx.com>

Diffstat:
Mdgl/Color.hpp | 20++++++++++----------
Mdgl/src/ApplicationPrivateData.hpp | 2+-
Mdgl/src/Color.cpp | 71++++++++++++++++++++++++++++++++++-------------------------------------
3 files changed, 45 insertions(+), 48 deletions(-)

diff --git a/dgl/Color.hpp b/dgl/Color.hpp @@ -1,6 +1,6 @@ /* * DISTRHO Plugin Framework (DPF) - * Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com> + * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com> * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this @@ -19,11 +19,11 @@ #include "Base.hpp" -struct NVGcolor; - START_NAMESPACE_DGL -// ----------------------------------------------------------------------- +struct NVGcolor; + +// -------------------------------------------------------------------------------------------------------------------- /** A color made from red, green, blue and alpha floating-point values in [0..1] range. @@ -44,13 +44,13 @@ struct Color { /** Create a color from red, green, blue and alpha numeric values. - Values must be in [0..255] range. + All values except alpha must be in [0..255] range, with alpha in [0..1] range. */ - Color(int red, int green, int blue, int alpha = 255) noexcept; + Color(int red, int green, int blue, float alpha = 1.0f) noexcept; /** Create a color from red, green, blue and alpha floating-point values. - Values must in [0..1] range. + All values must in [0..1] range. */ Color(float red, float green, float blue, float alpha = 1.0f) noexcept; @@ -74,7 +74,7 @@ struct Color { /** Create a color from a HTML string like "#333" or "#112233". */ - static Color fromHTML(const char* rgb, float alpha = 1.0f); + static Color fromHTML(const char* rgb, float alpha = 1.0f) noexcept; /** Linearly interpolate this color against another. @@ -83,7 +83,7 @@ struct Color { /** Check if this color matches another. - @note Comparison is forced within 8-bit color values. + @note Comparison is done within 8-bit color space. */ bool isEqual(const Color& color, bool withAlpha = true) noexcept; bool isNotEqual(const Color& color, bool withAlpha = true) noexcept; @@ -103,7 +103,7 @@ struct Color { operator NVGcolor() const noexcept; }; -// ----------------------------------------------------------------------- +// -------------------------------------------------------------------------------------------------------------------- END_NAMESPACE_DGL diff --git a/dgl/src/ApplicationPrivateData.hpp b/dgl/src/ApplicationPrivateData.hpp @@ -57,7 +57,7 @@ struct Application::PrivateData { /** Flag one window shown or hidden status, which modifies @a visibleWindows. For standalone mode only. - Modifies @a isStarting and @a isQuitting under certain conditions */ + Modifies @a isQuitting under certain conditions */ void oneWindowShown() noexcept; void oneWindowHidden() noexcept; diff --git a/dgl/src/Color.cpp b/dgl/src/Color.cpp @@ -1,6 +1,6 @@ /* * DISTRHO Plugin Framework (DPF) - * Copyright (C) 2012-2016 Filipe Coelho <falktx@falktx.com> + * Copyright (C) 2012-2021 Filipe Coelho <falktx@falktx.com> * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this @@ -16,10 +16,6 @@ #include "../Color.hpp" -#ifndef HAVE_DCAIRO -#include "nanovg/nanovg.h" -#endif - START_NAMESPACE_DGL // ----------------------------------------------------------------------- @@ -61,27 +57,27 @@ static uchar getFixedRange2(const float& value) return 0; if (value2 >= 255.0f) return 255; - return static_cast<uchar>(value2); + return static_cast<uchar>(value2 + 0.5f); } // ----------------------------------------------------------------------- Color::Color() noexcept - : red(1.0f), - green(1.0f), - blue(1.0f), + : red(0.0f), + green(0.0f), + blue(0.0f), alpha(1.0f) {} -Color::Color(int r, int g, int b, int a) noexcept +Color::Color(const int r, const int g, const int b, const float a) noexcept : red(static_cast<float>(r)/255.0f), green(static_cast<float>(g)/255.0f), blue(static_cast<float>(b)/255.0f), - alpha(static_cast<float>(a)/255.0f) + alpha(a) { fixBounds(); } -Color::Color(float r, float g, float b, float a) noexcept +Color::Color(const float r, const float g, const float b, const float a) noexcept : red(r), green(g), blue(b), @@ -109,7 +105,7 @@ Color& Color::operator=(const Color& color) noexcept return *this; } -Color::Color(const Color& color1, const Color& color2, float u) noexcept +Color::Color(const Color& color1, const Color& color2, const float u) noexcept : red(color1.red), green(color1.green), blue(color1.blue), @@ -136,65 +132,66 @@ Color Color::fromHSL(float hue, float saturation, float lightness, float alpha) return col; } -Color Color::fromHTML(const char* rgb, float alpha) +Color Color::fromHTML(const char* rgb, const float alpha) noexcept { Color fallback; DISTRHO_SAFE_ASSERT_RETURN(rgb != nullptr && rgb[0] != '\0', fallback); - if (rgb[0] == '#') ++rgb; + if (rgb[0] == '#') + ++rgb; DISTRHO_SAFE_ASSERT_RETURN(rgb[0] != '\0', fallback); - std::size_t rgblen(std::strlen(rgb)); + std::size_t rgblen = std::strlen(rgb); DISTRHO_SAFE_ASSERT_RETURN(rgblen == 3 || rgblen == 6, fallback); - char rgbtmp[3] = { '\0', '\0', '\0' }; + char rgbtmp[5] = { '0', 'x', '\0', '\0', '\0' }; int r, g, b; if (rgblen == 3) { - rgbtmp[0] = rgb[0]; - r = static_cast<int>(std::strtol(rgbtmp, nullptr, 16)); + rgbtmp[2] = rgb[0]; + r = static_cast<int>(std::strtol(rgbtmp, nullptr, 16)) * 17; - rgbtmp[0] = rgb[1]; - g = static_cast<int>(std::strtol(rgbtmp, nullptr, 16)); + rgbtmp[2] = rgb[1]; + g = static_cast<int>(std::strtol(rgbtmp, nullptr, 16)) * 17; - rgbtmp[0] = rgb[2]; - b = static_cast<int>(std::strtol(rgbtmp, nullptr, 16)); + rgbtmp[2] = rgb[2]; + b = static_cast<int>(std::strtol(rgbtmp, nullptr, 16)) * 17; } else { - rgbtmp[0] = rgb[0]; - rgbtmp[1] = rgb[1]; + rgbtmp[2] = rgb[0]; + rgbtmp[3] = rgb[1]; r = static_cast<int>(std::strtol(rgbtmp, nullptr, 16)); - rgbtmp[0] = rgb[2]; - rgbtmp[1] = rgb[3]; + rgbtmp[2] = rgb[2]; + rgbtmp[3] = rgb[3]; g = static_cast<int>(std::strtol(rgbtmp, nullptr, 16)); - rgbtmp[0] = rgb[4]; - rgbtmp[1] = rgb[5]; + rgbtmp[2] = rgb[4]; + rgbtmp[3] = rgb[5]; b = static_cast<int>(std::strtol(rgbtmp, nullptr, 16)); } - return Color(r, g, b, static_cast<int>(getFixedRange(alpha)*255.0f)); + return Color(r, g, b, alpha); } void Color::interpolate(const Color& other, float u) noexcept { fixRange(u); - const float oneMinusU(1.0f - u); + const float oneMinusU = 1.0f - u; - red = red * oneMinusU + other.red * u; - green = green * oneMinusU + other.green * u; - blue = blue * oneMinusU + other.blue * u; - alpha = alpha * oneMinusU + other.alpha * u; + red = (red * oneMinusU) + (other.red * u); + green = (green * oneMinusU) + (other.green * u); + blue = (blue * oneMinusU) + (other.blue * u); + alpha = (alpha * oneMinusU) + (other.alpha * u); fixBounds(); } // ----------------------------------------------------------------------- -bool Color::isEqual(const Color& color, bool withAlpha) noexcept +bool Color::isEqual(const Color& color, const bool withAlpha) noexcept { const uchar r1 = getFixedRange2(rgba[0]); const uchar g1 = getFixedRange2(rgba[1]); @@ -212,7 +209,7 @@ bool Color::isEqual(const Color& color, bool withAlpha) noexcept return (r1 == r2 && g1 == g2 && b1 == b2); } -bool Color::isNotEqual(const Color& color, bool withAlpha) noexcept +bool Color::isNotEqual(const Color& color, const bool withAlpha) noexcept { const uchar r1 = getFixedRange2(rgba[0]); const uchar g1 = getFixedRange2(rgba[1]);