From 3d2d57cd30aae8d2811f679fcb30fd0e8f2ab2e9 Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Mon, 28 Nov 2016 13:00:53 -0800 Subject: [core] Assert valid range for color components --- include/mbgl/util/color.hpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'include/mbgl/util/color.hpp') diff --git a/include/mbgl/util/color.hpp b/include/mbgl/util/color.hpp index 7693ce636d..178d0dc758 100644 --- a/include/mbgl/util/color.hpp +++ b/include/mbgl/util/color.hpp @@ -2,6 +2,7 @@ #include +#include #include 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, -- cgit v1.2.1