diff options
author | falkTX <falktx@gmail.com> | 2014-11-18 05:13:37 +0000 |
---|---|---|
committer | falkTX <falktx@gmail.com> | 2014-11-18 05:13:37 +0000 |
commit | f609f1643c09a90aab4d4481a689a6ee8e9f697a (patch) | |
tree | d850003704e47ca00a2e09316e6def3b358a548d /dgl/src/Color.cpp | |
parent | ac9c0067fbb52fa5bb35388baf56a081377d3c4a (diff) |
Add Color::fromHTML function
Diffstat (limited to 'dgl/src/Color.cpp')
-rw-r--r-- | dgl/src/Color.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/dgl/src/Color.cpp b/dgl/src/Color.cpp index 78bbd488..91c03ec0 100644 --- 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); |