summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Firebaugh <john.firebaugh@gmail.com>2016-11-28 13:00:53 -0800
committerJohn Firebaugh <john.firebaugh@gmail.com>2016-11-29 06:52:24 -0800
commit3d2d57cd30aae8d2811f679fcb30fd0e8f2ab2e9 (patch)
tree5a2700d943cdb190e7cb73c8683d08e186806263
parent99c2bf2580f57542ce2a473dba2587604d10f6ac (diff)
downloadqtlocation-mapboxgl-3d2d57cd30aae8d2811f679fcb30fd0e8f2ab2e9.tar.gz
[core] Assert valid range for color components
-rw-r--r--include/mbgl/util/color.hpp16
-rw-r--r--test/api/annotations.test.cpp32
2 files changed, 32 insertions, 16 deletions
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 <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,
diff --git a/test/api/annotations.test.cpp b/test/api/annotations.test.cpp
index 31277718da..72a2d62bde 100644
--- a/test/api/annotations.test.cpp
+++ b/test/api/annotations.test.cpp
@@ -59,7 +59,7 @@ TEST(Annotations, LineAnnotation) {
LineString<double> line = {{ { 0, 0 }, { 45, 45 }, { 30, 0 } }};
LineAnnotation annotation { line };
- annotation.color = { { 255, 0, 0, 1 } };
+ annotation.color = Color::red();
annotation.width = { 5 };
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
@@ -75,7 +75,7 @@ TEST(Annotations, FillAnnotation) {
Polygon<double> polygon = {{ {{ { 0, 0 }, { 0, 45 }, { 45, 45 }, { 45, 0 } }} }};
FillAnnotation annotation { polygon };
- annotation.color = { { 255, 0, 0, 1 } };
+ annotation.color = Color::red();
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
test.map.addAnnotation(annotation);
@@ -94,13 +94,13 @@ TEST(Annotations, AntimeridianAnnotationSmall) {
LineString<double> line = {{ { antimeridian, 20 }, { antimeridian, -20 } }};
LineAnnotation lineAnnotation { line };
- lineAnnotation.color = { { 255, 0, 0, 1 } };
+ lineAnnotation.color = Color::red();
lineAnnotation.width = { 2 };
test.map.addAnnotation(lineAnnotation);
Polygon<double> polygon = {{ {{ { antimeridian+10, 0 }, { antimeridian - 10, 10 }, { antimeridian-10, -10 } }} }};
FillAnnotation polygonAnnotation { polygon };
- polygonAnnotation.color = { { 0, 0, 255, 1 } };
+ polygonAnnotation.color = Color::blue();
test.map.addAnnotation(polygonAnnotation);
test.checkRendering("antimeridian_annotation_small");
@@ -115,13 +115,13 @@ TEST(Annotations, AntimeridianAnnotationLarge) {
LineString<double> line = {{ { antimeridian, 20 }, { antimeridian, -20 } }};
LineAnnotation lineAnnotation { line };
- lineAnnotation.color = { { 255, 0, 0, 1 } };
+ lineAnnotation.color = Color::red();
lineAnnotation.width = { 2 };
test.map.addAnnotation(lineAnnotation);
Polygon<double> polygon = {{ {{ { antimeridian-10, 0 }, { -antimeridian+10, 10 }, { -antimeridian+10, -10 } }} }};
FillAnnotation polygonAnnotation { polygon };
- polygonAnnotation.color = { { 0, 0, 255, 1 } };
+ polygonAnnotation.color = Color::blue();
test.map.addAnnotation(polygonAnnotation);
test.checkRendering("antimeridian_annotation_large");
@@ -132,9 +132,9 @@ TEST(Annotations, OverlappingFillAnnotation) {
Polygon<double> polygon = {{ {{ { 0, 0 }, { 0, 45 }, { 45, 45 }, { 45, 0 } }} }};
FillAnnotation underlaidAnnotation { polygon };
- underlaidAnnotation.color = { { 0, 255, 0, 1 } };
+ underlaidAnnotation.color = Color::green();
FillAnnotation overlaidAnnotation { polygon };
- overlaidAnnotation.color = { { 255, 0, 0, 1 } };
+ overlaidAnnotation.color = Color::red();
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
test.map.addAnnotation(underlaidAnnotation);
@@ -173,7 +173,7 @@ TEST(Annotations, NonImmediateAdd) {
Polygon<double> polygon = {{ {{ { 0, 0 }, { 0, 45 }, { 45, 45 }, { 45, 0 } }} }};
FillAnnotation annotation { polygon };
- annotation.color = { { 255, 0, 0, 1 } };
+ annotation.color = Color::red();
test.map.addAnnotation(annotation);
test.checkRendering("non_immediate_add");
@@ -211,7 +211,7 @@ TEST(Annotations, UpdateLineAnnotationGeometry) {
AnnotationTest test;
LineAnnotation annotation { LineString<double> {{ { 0, 0 }, { 45, 45 }, { 30, 0 } }} };
- annotation.color = { { 255, 0, 0, 1 } };
+ annotation.color = Color::red();
annotation.width = { 5 };
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
@@ -228,7 +228,7 @@ TEST(Annotations, UpdateLineAnnotationStyle) {
AnnotationTest test;
LineAnnotation annotation { LineString<double> {{ { 0, 0 }, { 45, 45 }, { 30, 0 } }} };
- annotation.color = { { 255, 0, 0, 1 } };
+ annotation.color = Color::red();
annotation.width = { 5 };
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
@@ -236,7 +236,7 @@ TEST(Annotations, UpdateLineAnnotationStyle) {
test::render(test.map, test.view);
- annotation.color = { { 0, 255, 0, 1 } };
+ annotation.color = Color::green();
annotation.width = { 2 };
test.map.updateAnnotation(line, annotation);
test.checkRendering("update_line_style");
@@ -246,7 +246,7 @@ TEST(Annotations, UpdateFillAnnotationGeometry) {
AnnotationTest test;
FillAnnotation annotation { Polygon<double> {{ {{ { 0, 0 }, { 0, 45 }, { 45, 45 }, { 45, 0 } }} }} };
- annotation.color = { { 255, 0, 0, 1 } };
+ annotation.color = Color::red();
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
AnnotationID fill = test.map.addAnnotation(annotation);
@@ -263,14 +263,14 @@ TEST(Annotations, UpdateFillAnnotationStyle) {
Polygon<double> polygon = {{ {{ { 0, 0 }, { 0, 45 }, { 45, 45 }, { 45, 0 } }} }};
FillAnnotation annotation { polygon };
- annotation.color = { { 255, 0, 0, 1 } };
+ annotation.color = Color::red();
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));
AnnotationID fill = test.map.addAnnotation(annotation);
test::render(test.map, test.view);
- annotation.color = { { 0, 255, 0, 1 } };
+ annotation.color = Color::green();
test.map.updateAnnotation(fill, annotation);
test.checkRendering("update_fill_style");
}
@@ -293,7 +293,7 @@ TEST(Annotations, RemoveShape) {
LineString<double> line = {{ { 0, 0 }, { 45, 45 } }};
LineAnnotation annotation { line };
- annotation.color = { { 255, 0, 0, 1 } };
+ annotation.color = Color::red();
annotation.width = { 5 };
test.map.setStyleJSON(util::read_file("test/fixtures/api/empty.json"));