Browse Source

Add Color::fromHTML function

gh-pages
falkTX 10 years ago
parent
commit
f609f1643c
2 changed files with 49 additions and 1 deletions
  1. +6
    -1
      dgl/Color.hpp
  2. +43
    -0
      dgl/src/Color.cpp

+ 6
- 1
dgl/Color.hpp View File

@@ -28,7 +28,7 @@ START_NAMESPACE_DGL
// TODO: create color from "#333" and "#112233" like strings // TODO: create color from "#333" and "#112233" like strings


/** /**
A color made from red, green, blue and alpha floating-point values in [0..1] range.
A color made from red, green, blue and alpha floating-point values in [0..1] range.
*/ */
struct Color { struct Color {
/** /**
@@ -73,6 +73,11 @@ struct Color {
*/ */
static Color fromHSL(float hue, float saturation, float lightness, float alpha = 1.0f); static Color fromHSL(float hue, float saturation, float lightness, float alpha = 1.0f);


/**
Create a color from a HTML string like "#333" or "#112233".
*/
static Color fromHTML(const char* rgb, float alpha = 1.0f);

/** /**
Linearly interpolate this color against another. Linearly interpolate this color against another.
*/ */


+ 43
- 0
dgl/src/Color.cpp View File

@@ -108,6 +108,49 @@ Color Color::fromHSL(float hue, float saturation, float lightness, float alpha)
return nvgHSLA(hue, saturation, lightness, static_cast<uchar>(getFixedRange(alpha)*255.0f)); return nvgHSLA(hue, saturation, lightness, static_cast<uchar>(getFixedRange(alpha)*255.0f));
} }


Color Color::fromHTML(const char* rgb, float alpha)
{
Color fallback;
DISTRHO_SAFE_ASSERT_RETURN(rgb != nullptr && rgb[0] != '\0', fallback);

if (rgb[0] == '#') ++rgb;
DISTRHO_SAFE_ASSERT_RETURN(rgb[0] != '\0', fallback);

std::size_t rgblen(std::strlen(rgb));
DISTRHO_SAFE_ASSERT_RETURN(rgblen == 3 || rgblen == 6, fallback);

char rgbtmp[3] = { '\0', '\0', '\0' };
int r, g, b;

if (rgblen == 3)
{
rgbtmp[0] = rgb[0];
r = std::strtol(rgbtmp, nullptr, 16);

rgbtmp[0] = rgb[1];
g = std::strtol(rgbtmp, nullptr, 16);

rgbtmp[0] = rgb[2];
b = std::strtol(rgbtmp, nullptr, 16);
}
else
{
rgbtmp[0] = rgb[0];
rgbtmp[1] = rgb[1];
r = std::strtol(rgbtmp, nullptr, 16);

rgbtmp[0] = rgb[2];
rgbtmp[1] = rgb[3];
g = std::strtol(rgbtmp, nullptr, 16);

rgbtmp[0] = rgb[4];
rgbtmp[1] = rgb[5];
b = std::strtol(rgbtmp, nullptr, 16);
}

return Color(r, g, b, static_cast<int>(getFixedRange(alpha)*255.0f));
}

void Color::interpolate(const Color& other, float u) noexcept void Color::interpolate(const Color& other, float u) noexcept
{ {
fixRange(u); fixRange(u);


Loading…
Cancel
Save