Browse Source

Fix Color::fromHTML, cleanup

Signed-off-by: falkTX <falktx@falktx.com>
pull/272/head
falkTX 4 years ago
parent
commit
277a5dfe18
Signed by: falkTX <falktx@falktx.com> GPG Key ID: CDBAA37ABC74FBA0
3 changed files with 45 additions and 48 deletions
  1. +10
    -10
      dgl/Color.hpp
  2. +1
    -1
      dgl/src/ApplicationPrivateData.hpp
  3. +34
    -37
      dgl/src/Color.cpp

+ 10
- 10
dgl/Color.hpp View File

@@ -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



+ 1
- 1
dgl/src/ApplicationPrivateData.hpp View File

@@ -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;



+ 34
- 37
dgl/src/Color.cpp View File

@@ -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]);


Loading…
Cancel
Save