commit f609f1643c09a90aab4d4481a689a6ee8e9f697a
parent ac9c0067fbb52fa5bb35388baf56a081377d3c4a
Author: falkTX <falktx@gmail.com>
Date: Tue, 18 Nov 2014 05:13:12 +0000
Add Color::fromHTML function
Diffstat:
2 files changed, 49 insertions(+), 1 deletion(-)
diff --git a/dgl/Color.hpp b/dgl/Color.hpp
@@ -28,7 +28,7 @@ START_NAMESPACE_DGL
// 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 {
/**
@@ -74,6 +74,11 @@ struct Color {
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.
*/
void interpolate(const Color& other, float u) noexcept;
diff --git a/dgl/src/Color.cpp b/dgl/src/Color.cpp
@@ -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));
}
+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
{
fixRange(u);