utest.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 #include <gtest/gtest.h>
35 #include <color_util/convert.h>
36 
38 using color_util::toInt;
40 
41 TEST(color_util, full_color)
42 {
43  // use unsigned ints because unsigned char would overflow
44  unsigned int inc = 1; // can increase this to speed up tests
45  for (unsigned int i = 0; i < 256; i+=inc)
46  {
47  for (unsigned int j = 0; j < 256; j+=inc)
48  {
49  for (unsigned int k = 0; k < 256; k+=inc)
50  {
51  for (unsigned int a = 0; a < 256; a+=50) // don't test all alpha values
52  {
53  color_util::ColorRGBA24 rgba24(i, j, k, a);
54  color_util::ColorHSVA24 hsva24(i, j, k, a);
55 
56  color_util::ColorRGBA rgba = toFloat(rgba24);
57  color_util::ColorHSVA hsva = toFloat(hsva24);
58 
59  ASSERT_EQ(rgba24, toInt(rgba));
60  ASSERT_EQ(hsva24, toInt(hsva));
61  }
62  }
63  }
64  }
65 }
66 
67 void checkConversion(double r, double g, double b, double h, double s, double v, double epsilon = 0.001)
68 {
69  double a = 0.9; // arbitrary value
70 
71  color_util::ColorRGBA rgba(r, g, b, a);
72  color_util::ColorHSVA hsva(h, s, v, a);
73 
74  color_util::ColorRGBA converted_rgba = changeColorspace(hsva);
75 
76  ASSERT_NEAR(converted_rgba.r, r, epsilon);
77  ASSERT_NEAR(converted_rgba.g, g, epsilon);
78  ASSERT_NEAR(converted_rgba.b, b, epsilon);
79  ASSERT_NEAR(converted_rgba.a, a, epsilon);
80 
81  color_util::ColorHSVA converted_hsva = changeColorspace(rgba);
82  ASSERT_NEAR(converted_hsva.h, h, epsilon);
83  ASSERT_NEAR(converted_hsva.s, s, epsilon);
84  ASSERT_NEAR(converted_hsva.v, v, epsilon);
85  ASSERT_NEAR(converted_hsva.a, a, epsilon);
86 }
87 
88 TEST(color_util, random_color_check)
89 {
90  // black
91  checkConversion(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
92 
93  // white
94  checkConversion(1.0, 1.0, 1.0, 0.0, 0.0, 1.0);
95 
96  // red
97  checkConversion(1.0, 0.0, 0.0, 0.0, 1.0, 1.0);
98 
99  // green
100  checkConversion(0.0, 1.0, 0.0, 0.33333, 1.0, 1.0);
101 
102  // blue
103  checkConversion(0.0, 0.0, 1.0, 0.66667, 1.0, 1.0);
104 
105  // cyan
106  checkConversion(0.0, 1.0, 1.0, 0.5, 1.0, 1.0);
107 
108  // magenta
109  checkConversion(1.0, 0.0, 1.0, 0.83333, 1.0, 1.0);
110 
111  // yellow
112  checkConversion(1.0, 1.0, 0.0, 0.16667, 1.0, 1.0);
113 
114  // gray
115  checkConversion(0.8, 0.8, 0.8, 0.0, 0.0, 0.8);
116 }
117 
118 int main(int argc, char** argv)
119 {
120  testing::InitGoogleTest(&argc, argv);
121  return RUN_ALL_TESTS();
122 }
double epsilon
color_util::ColorHSVA changeColorspace(const color_util::ColorRGBA &rgba)
Definition: convert.cpp:92
void checkConversion(double r, double g, double b, double h, double s, double v, double epsilon=0.001)
Definition: utest.cpp:67
int main(int argc, char **argv)
Definition: utest.cpp:118
TEST(color_util, full_color)
Definition: utest.cpp:41
color_util::ColorRGBA24 toInt(const color_util::ColorRGBA &float_color)
Definition: convert.cpp:70
color_util::ColorRGBA toFloat(const color_util::ColorRGBA24 &int_color)
Definition: convert.cpp:45


color_util
Author(s):
autogenerated on Sun Jan 10 2021 04:08:24