[core] Assert valid range for color components

This commit is contained in:
John Firebaugh
2016-11-28 13:00:53 -08:00
parent 99c2bf2580
commit 3d2d57cd30
2 changed files with 32 additions and 16 deletions

View File

@@ -2,6 +2,7 @@
#include <mbgl/util/optional.hpp>
#include <cassert>
#include <string>
namespace mbgl {
@@ -9,6 +10,19 @@ namespace mbgl {
// Stores a premultiplied color, with all four channels ranging from 0..1
class Color {
public:
constexpr Color() = default;
constexpr Color(float r_, float g_, float b_, float a_)
: r(r_), g(g_), b(b_), a(a_) {
assert(r_ >= 0.0f);
assert(r_ <= 1.0f);
assert(g_ >= 0.0f);
assert(g_ <= 1.0f);
assert(b_ >= 0.0f);
assert(b_ <= 1.0f);
assert(a_ >= 0.0f);
assert(a_ <= 1.0f);
}
float r = 0.0f;
float g = 0.0f;
float b = 0.0f;
@@ -33,6 +47,8 @@ constexpr bool operator!=(const Color& colorA, const Color& colorB) {
}
constexpr Color operator*(const Color& color, float alpha) {
assert(alpha >= 0.0f);
assert(alpha <= 1.0f);
return {
color.r * alpha,
color.g * alpha,