commit 17393403bf03049a8178cd217f691c149017ec80
parent 815d01277152c0067b6d309a92aa367651e6cd18
Author: falkTX <falktx@gmail.com>
Date: Sun, 17 Aug 2014 15:39:21 +0100
Color using int; NanoVG extended color funcs and in-frame test
Diffstat:
4 files changed, 68 insertions(+), 6 deletions(-)
diff --git a/dgl/Color.hpp b/dgl/Color.hpp
@@ -48,7 +48,7 @@ struct Color {
Create a color from red, green, blue and alpha numeric values.
Values must be in [0..255] range.
*/
- Color(const uchar red, const uchar green, const uchar blue, const uchar alpha = 255) noexcept;
+ Color(const int red, const int green, const int blue, const int alpha = 255) noexcept;
/**
Create a color from red, green, blue and alpha floating-point values.
@@ -71,7 +71,7 @@ struct Color {
Create a color specified by hue, saturation, lightness and alpha.
HSL values are all in [0..1] range, alpha in [0..255] range.
*/
- static Color HSL(const float hue, const float saturation, const float lightness, const uchar alpha = 255);
+ static Color HSL(const float hue, const float saturation, const float lightness, const int alpha = 255);
/**
Linearly interpolate this color against another.
diff --git a/dgl/NanoVG.hpp b/dgl/NanoVG.hpp
@@ -313,6 +313,18 @@ public:
void strokeColor(const Color& color);
/**
+ Sets current stroke style to a solid color, made from red, green, blue and alpha numeric values.
+ Values must be in [0..255] range.
+ */
+ void strokeColor(const int red, const int green, const int blue, const int alpha = 255);
+
+ /**
+ Sets current stroke style to a solid color, made from red, green, blue and alpha numeric values.
+ Values must in [0..1] range.
+ */
+ void strokeColor(const float red, const float green, const float blue, const float alpha = 1.0f);
+
+ /**
Sets current stroke style to a paint, which can be a one of the gradients or a pattern.
*/
void strokePaint(const Paint& paint);
@@ -323,6 +335,18 @@ public:
void fillColor(const Color& color);
/**
+ Sets current fill style to a solid color, made from red, green, blue and alpha numeric values.
+ Values must be in [0..255] range.
+ */
+ void fillColor(const int red, const int green, const int blue, const int alpha = 255);
+
+ /**
+ Sets current fill style to a solid color, made from red, green, blue and alpha numeric values.
+ Values must in [0..1] range.
+ */
+ void fillColor(const float red, const float green, const float blue, const float alpha = 1.0f);
+
+ /**
Sets current fill style to a paint, which can be a one of the gradients or a pattern.
*/
void fillPaint(const Paint& paint);
@@ -713,6 +737,7 @@ public:
private:
NVGcontext* const fContext;
+ bool fInFrame;
DISTRHO_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NanoVG)
};
diff --git a/dgl/src/Color.cpp b/dgl/src/Color.cpp
@@ -25,7 +25,7 @@ START_NAMESPACE_DGL
Color::Color() noexcept
: red(1.0f), green(1.0f), blue(1.0f), alpha(1.0f) {}
-Color::Color(const uchar r, const uchar g, const uchar b, const uchar a) noexcept
+Color::Color(const int r, const int g, const int b, const int 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) {}
Color::Color(const float r, const float g, const float b, const float a) noexcept
@@ -51,7 +51,7 @@ void Color::interpolate(const Color& other, const float u) noexcept
alpha = alpha * oneMinusU + other.alpha * u2;
}
-Color Color::HSL(const float hue, const float saturation, const float lightness, const uchar alpha)
+Color Color::HSL(const float hue, const float saturation, const float lightness, const int alpha)
{
return nvgHSLA(hue, saturation, lightness, alpha);
}
diff --git a/dgl/src/NanoVG.cpp b/dgl/src/NanoVG.cpp
@@ -116,19 +116,23 @@ void NanoImage::_updateSize()
// NanoVG
NanoVG::NanoVG()
- : fContext(nvgCreateGL(512, 512, NVG_ANTIALIAS))
+ : fContext(nvgCreateGL(512, 512, NVG_ANTIALIAS)),
+ fInFrame(false)
{
DISTRHO_SAFE_ASSERT_RETURN(fContext != nullptr,);
}
NanoVG::NanoVG(const int textAtlasWidth, const int textAtlasHeight)
- : fContext(nvgCreateGL(textAtlasWidth, textAtlasHeight, NVG_ANTIALIAS))
+ : fContext(nvgCreateGL(textAtlasWidth, textAtlasHeight, NVG_ANTIALIAS)),
+ fInFrame(false)
{
DISTRHO_SAFE_ASSERT_RETURN(fContext != nullptr,);
}
NanoVG::~NanoVG()
{
+ DISTRHO_SAFE_ASSERT(! fInFrame);
+
if (fContext != nullptr)
nvgDeleteGL(fContext);
}
@@ -141,7 +145,9 @@ void NanoVG::beginFrame(const int width, const int height, const float scaleFact
DISTRHO_SAFE_ASSERT_RETURN(width > 0,);
DISTRHO_SAFE_ASSERT_RETURN(height > 0,);
DISTRHO_SAFE_ASSERT_RETURN(scaleFactor > 0.0f,);
+ DISTRHO_SAFE_ASSERT_RETURN(! fInFrame,);
+ fInFrame = true;
nvgBeginFrame(fContext, width, height, scaleFactor, static_cast<NVGalpha>(alpha));
}
@@ -149,15 +155,22 @@ void NanoVG::beginFrame(Widget* const widget)
{
if (fContext == nullptr) return;
DISTRHO_SAFE_ASSERT_RETURN(widget != nullptr,);
+ DISTRHO_SAFE_ASSERT_RETURN(! fInFrame,);
Window& window(widget->getParentWindow());
+
+ fInFrame = true;
nvgBeginFrame(fContext, window.getWidth(), window.getHeight(), 1.0f, NVG_PREMULTIPLIED_ALPHA);
}
void NanoVG::endFrame()
{
+ DISTRHO_SAFE_ASSERT_RETURN(fInFrame,);
+
if (fContext != nullptr)
nvgEndFrame(fContext);
+
+ fInFrame = false;
}
// -----------------------------------------------------------------------
@@ -190,6 +203,18 @@ void NanoVG::strokeColor(const Color& color)
nvgStrokeColor(fContext, color);
}
+void NanoVG::strokeColor(const int red, const int green, const int blue, const int alpha)
+{
+ if (fContext != nullptr)
+ nvgStrokeColor(fContext, nvgRGBA(red, green, blue, alpha));
+}
+
+void NanoVG::strokeColor(const float red, const float green, const float blue, const float alpha)
+{
+ if (fContext != nullptr)
+ nvgStrokeColor(fContext, nvgRGBAf(red, green, blue, alpha));
+}
+
void NanoVG::strokePaint(const Paint& paint)
{
if (fContext != nullptr)
@@ -202,6 +227,18 @@ void NanoVG::fillColor(const Color& color)
nvgFillColor(fContext, color);
}
+void NanoVG::fillColor(const int red, const int green, const int blue, const int alpha)
+{
+ if (fContext != nullptr)
+ nvgFillColor(fContext, nvgRGBA(red, green, blue, alpha));
+}
+
+void NanoVG::fillColor(const float red, const float green, const float blue, const float alpha)
+{
+ if (fContext != nullptr)
+ nvgFillColor(fContext, nvgRGBAf(red, green, blue, alpha));
+}
+
void NanoVG::fillPaint(const Paint& paint)
{
if (fContext != nullptr)