[core] Add a few conveniences to Color

This commit is contained in:
John Firebaugh
2016-06-15 13:22:00 -07:00
parent d48333f26d
commit a020e535ca
10 changed files with 29 additions and 24 deletions

View File

@@ -30,14 +30,14 @@ public:
ShapeAnnotationGeometry geometry;
float opacity = 1;
float width = 1;
Color color = { 0, 0, 0, 1 };
Color color = Color::black();
};
class FillAnnotation {
public:
ShapeAnnotationGeometry geometry;
float opacity = 1;
Color color = { 0, 0, 0, 1 };
Color color = Color::black();
Color outlineColor = { 0, 0, 0, -1 };
};

View File

@@ -5,6 +5,7 @@
#include <array>
#include <mbgl/gl/gl.hpp>
#include <mbgl/util/color.hpp>
namespace mbgl {
namespace gl {
@@ -23,7 +24,7 @@ struct ClearDepth {
};
struct ClearColor {
struct Type { GLfloat r, g, b, a; };
using Type = Color;
static const Type Default;
inline static void Set(const Type& value) {
MBGL_CHECK_ERROR(glClearColor(value.r, value.g, value.b, value.a));
@@ -35,10 +36,6 @@ struct ClearColor {
}
};
inline bool operator!=(const ClearColor::Type& a, const ClearColor::Type& b) {
return a.r != b.r || a.g != b.g || a.b != b.b || a.a != b.a;
}
struct ClearStencil {
using Type = GLint;
static const Type Default;

View File

@@ -11,14 +11,26 @@ public:
float g = 0.0f;
float b = 0.0f;
float a = 0.0f;
static constexpr Color black() { return { 0.0f, 0.0f, 0.0f, 1.0f }; };
static constexpr Color white() { return { 1.0f, 1.0f, 1.0f, 1.0f }; };
};
inline bool operator== (const Color& colorA, const Color& colorB) {
inline bool operator==(const Color& colorA, const Color& colorB) {
return colorA.r == colorB.r && colorA.g == colorB.g && colorA.b == colorB.b && colorA.a == colorB.a;
}
inline bool operator!= (const Color& colorA, const Color& colorB) {
return !(colorA.r == colorB.r && colorA.g == colorB.g && colorA.b == colorB.b && colorA.a == colorB.a);
inline bool operator!=(const Color& colorA, const Color& colorB) {
return !(colorA == colorB);
}
inline Color operator*(const Color& color, float alpha) {
return {
color.r * alpha,
color.g * alpha,
color.b * alpha,
color.a * alpha
};
}
} // namespace mbgl

View File

@@ -144,9 +144,9 @@ void Painter::render(const Style& style, const FrameData& frame_, SpriteAtlas& a
config.depthMask = GL_TRUE;
config.colorMask = { GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE };
if (frame.debugOptions & MapDebugOptions::Wireframe) {
config.clearColor = { 0.0f, 0.0f, 0.0f, 1.0f };
config.clearColor = Color::black();
} else {
config.clearColor = { background.r, background.g, background.b, background.a };
config.clearColor = background;
}
config.clearStencil = 0;
config.clearDepth = 1;

View File

@@ -45,7 +45,7 @@ void Painter::renderBackground(const BackgroundLayer& layer) {
} else {
if (wireframe) {
plainShader->u_color = { 0.0f, 0.0f, 0.0f, 1.0f };
plainShader->u_color = Color::black();
plainShader->u_opacity = 1.0f;
} else {
plainShader->u_color = properties.backgroundColor;

View File

@@ -44,7 +44,7 @@ void Painter::renderDebugText(Tile& tile, const mat4 &matrix) {
plainShader->u_matrix = matrix;
// Draw white outline
plainShader->u_color = { 1.0f, 1.0f, 1.0f, 1.0f };
plainShader->u_color = Color::white();
config.lineWidth = 4.0f * frame.pixelRatio;
tile.debugBucket->drawLines(*plainShader, store);
@@ -55,7 +55,7 @@ void Painter::renderDebugText(Tile& tile, const mat4 &matrix) {
#endif
// Draw black text.
plainShader->u_color = { 0.0f, 0.0f, 0.0f, 1.0f };
plainShader->u_color = Color::black();
config.lineWidth = 2.0f * frame.pixelRatio;
tile.debugBucket->drawLines(*plainShader, store);

View File

@@ -38,8 +38,8 @@ void Painter::renderFill(FillBucket& bucket,
bool wireframe = frame.debugOptions & MapDebugOptions::Wireframe;
if (wireframe) {
fillColor = { 1.0f, 1.0f, 1.0f, 1.0f };
strokeColor = { 1.0f, 1.0f, 1.0f, 1.0f };
fillColor = Color::white();
strokeColor = Color::white();
opacity = 1.0f;
pattern = false;
outline = true;

View File

@@ -37,7 +37,7 @@ void Painter::renderLine(LineBucket& bucket,
float blur = properties.lineBlur + antialiasing;
Color color { 1.0f, 1.0f, 1.0f, 1.0f };
Color color = Color::white();
float opacity = 1.0f;
if (!wireframe) {
color = properties.lineColor;

View File

@@ -30,7 +30,7 @@ public:
class RenderData {
public:
Color backgroundColor = { 0, 0, 0, 0 };
Color backgroundColor;
std::set<style::Source*> sources;
std::vector<RenderItem> order;
};

View File

@@ -266,11 +266,7 @@ RenderData Style::getRenderData() const {
const BackgroundPaintProperties& paint = background->impl->paint;
if (layer.get() == layers[0].get() && paint.backgroundPattern.value.from.empty()) {
// This is a solid background. We can use glClear().
result.backgroundColor = paint.backgroundColor;
result.backgroundColor.r *= paint.backgroundOpacity;
result.backgroundColor.g *= paint.backgroundOpacity;
result.backgroundColor.b *= paint.backgroundOpacity;
result.backgroundColor.a *= paint.backgroundOpacity;
result.backgroundColor = paint.backgroundColor * paint.backgroundOpacity;
} else {
// This is a textured background, or not the bottommost layer. We need to render it with a quad.
result.order.emplace_back(*layer);