2
* Copyright 2012 Inalogic Inc.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3, as
6
* published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranties of
10
* MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11
* PURPOSE. See the applicable version of the GNU Lesser General Public
12
* License for more details.
14
* You should have received a copy of both the GNU Lesser General Public
15
* License version 3 along with this program. If not, see
16
* <http://www.gnu.org/licenses/>
18
* Authored by: Jay Taoko <jaytaoko@inalogic.com>
22
#include <gmock/gmock.h>
26
using namespace testing;
28
static const float epsilon = 0.005f;
32
TEST(TestColor, TestColorConstructor0)
36
EXPECT_EQ(c.red, 0.0f);
37
EXPECT_EQ(c.green, 0.0f);
38
EXPECT_EQ(c.blue, 0.0f);
39
EXPECT_EQ(c.alpha, 1.0);
40
EXPECT_EQ(c.IsPremultiplied(), false);
43
TEST(TestColor, TestColorConstructor1)
45
// testing float inputs
46
nux::Color c0(0.1f, 0.2f, 0.3f);
47
EXPECT_EQ(c0.red, 0.1f);
48
EXPECT_EQ(c0.green, 0.2f);
49
EXPECT_EQ(c0.blue, 0.3f);
50
EXPECT_EQ(c0.alpha, 1.0f);
51
EXPECT_EQ(c0.IsPremultiplied(), false);
53
// testing float inputs
54
nux::Color c1(0.1f, 0.2f, 0.3f, 0.4f);
55
EXPECT_EQ(c1.red, 0.1f);
56
EXPECT_EQ(c1.green, 0.2f);
57
EXPECT_EQ(c1.blue, 0.3f);
58
EXPECT_EQ(c1.alpha, 0.4f);
59
EXPECT_EQ(c1.IsPremultiplied(), false);
61
// testing double inputs
62
nux::Color c2(0.1, 0.2, 0.3, 0.4);
63
EXPECT_EQ(c2.red, 0.1f);
64
EXPECT_EQ(c2.green, 0.2f);
65
EXPECT_EQ(c2.blue, 0.3f);
66
EXPECT_EQ(c2.alpha, 0.4f);
67
EXPECT_EQ(c2.IsPremultiplied(), false);
70
TEST(TestColor, TestColorConstructor2)
72
nux::Color c0(255, 255, 255);
73
EXPECT_EQ(c0.red, 1.0f);
74
EXPECT_EQ(c0.green, 1.0f);
75
EXPECT_EQ(c0.blue, 1.0f);
76
EXPECT_EQ(c0.alpha, 1.0f);
77
EXPECT_EQ(c0.IsPremultiplied(), false);
79
nux::Color c1(10, 128, 192);
80
EXPECT_EQ(c1.red, 10/255.0f);
81
EXPECT_EQ(c1.green, 128/255.0f);
82
EXPECT_EQ(c1.blue, 192/255.0f);
83
EXPECT_EQ(c1.alpha, 1.0f);
84
EXPECT_EQ(c1.IsPremultiplied(), false);
87
TEST(TestColor, TestColorConstructor3)
89
nux::Color c0((255<<24)|(255<<16)|(255<<8)|255);
90
EXPECT_EQ(c0.red, 1.0f);
91
EXPECT_EQ(c0.green, 1.0f);
92
EXPECT_EQ(c0.blue, 1.0f);
93
EXPECT_EQ(c0.alpha, 1.0f);
94
EXPECT_EQ(c0.IsPremultiplied(), false);
97
nux::Color c1((128<<24)|(192<<16)|(128<<8)|10);
98
EXPECT_EQ(c1.red, 192/255.0f);
99
EXPECT_EQ(c1.green, 128/255.0f);
100
EXPECT_EQ(c1.blue, 10/255.0f);
101
EXPECT_EQ(c1.alpha, 128/255.0f);
102
EXPECT_EQ(c1.IsPremultiplied(), false);
106
TEST(TestColor, TestColorPremultiplied0)
108
nux::Color c0(0.81f, 0.24f, 0.53f, 0.79f);
109
EXPECT_EQ(c0.red, 0.81f);
110
EXPECT_EQ(c0.green, 0.24f);
111
EXPECT_EQ(c0.blue, 0.53f);
112
EXPECT_EQ(c0.alpha, 0.79f);
113
EXPECT_EQ(c0.IsPremultiplied(), false);
115
nux::Color c1 = c0.GetPremultiplied();
116
EXPECT_EQ(c1.red, 0.81f * 0.79f);
117
EXPECT_EQ(c1.green, 0.24f * 0.79f);
118
EXPECT_EQ(c1.blue, 0.53f * 0.79f);
119
EXPECT_EQ(c1.alpha, 0.79f);
120
EXPECT_EQ(c1.IsPremultiplied(), true);
121
EXPECT_EQ(c0.IsPremultiplied(), false);
124
TEST(TestColor, TestColorRGBToHSV0)
126
nux::color::RedGreenBlue rgb(255/255.0f, 128/255.0f, 169/255.0f);
127
nux::color::HueSaturationValue hsv(rgb);
129
EXPECT_NEAR(hsv.hue, 341/360.0f, epsilon);
130
EXPECT_NEAR(hsv.saturation, 50/100.0f, epsilon);
131
EXPECT_NEAR(hsv.value, 100/100.0f, epsilon);
134
TEST(TestColor, TestColorHSVToRGB0)
136
nux::color::HueSaturationValue hsv(341/360.0f, 50/100.0f, 100/100.0f);
137
nux::color::RedGreenBlue rgb(hsv);
139
EXPECT_NEAR(rgb.red, 255/255.0f, epsilon);
140
EXPECT_NEAR(rgb.green, 128/255.0f, epsilon);
141
EXPECT_NEAR(rgb.blue, 169/255.0f, epsilon);
144
TEST(TestColor, TestColorRGBToHSV1)
146
nux::color::RedGreenBlue rgb(65/255.0f, 28/255.0f, 6/255.0f);
147
nux::color::HueSaturationValue hsv(rgb);
149
EXPECT_NEAR(hsv.hue, 22/360.0f, epsilon);
150
EXPECT_NEAR(hsv.saturation, 91/100.0f, epsilon);
151
EXPECT_NEAR(hsv.value, 25/100.0f, epsilon);
154
TEST(TestColor, TestColorHSVToRGB1)
156
nux::color::HueSaturationValue hsv(22/360.0f, 91/100.0f, 25/100.0f);
157
nux::color::RedGreenBlue rgb(hsv);
159
EXPECT_NEAR(rgb.red, 65/255.0f, epsilon);
160
EXPECT_NEAR(rgb.green, 28/255.0f, epsilon);
161
EXPECT_NEAR(rgb.blue, 6/255.0f, epsilon);
164
TEST(TestColor, TestColorRGBToHSV2)
166
nux::color::RedGreenBlue rgb(90/255.0f, 65/255.0f, 158/255.0f);
167
nux::color::HueSaturationValue hsv(rgb);
169
EXPECT_NEAR(hsv.hue, 256/360.0f, epsilon);
170
EXPECT_NEAR(hsv.saturation, 59/100.0f, epsilon);
171
EXPECT_NEAR(hsv.value, 62/100.0f, epsilon);
175
TEST(TestColor, TestColorHSVToRGB2)
177
nux::color::HueSaturationValue hsv(256/360.0f, 59/100.0f, 62/100.0f);
178
nux::color::RedGreenBlue rgb(hsv);
180
EXPECT_NEAR(rgb.red, 90/255.0f, epsilon);
181
EXPECT_NEAR(rgb.green, 65/255.0f, epsilon);
182
EXPECT_NEAR(rgb.blue, 158/255.0f, epsilon);
185
TEST(TestColor, TestColorRGBToHLS0)
187
nux::color::RedGreenBlue rgb(127/255.0f, 0/255.0f, 255/255.0f);
188
nux::color::HueLightnessSaturation hls(rgb);
190
EXPECT_NEAR(hls.hue, 270/360.0f, epsilon);
191
EXPECT_NEAR(hls.lightness, 50/100.0f, epsilon);
192
EXPECT_NEAR(hls.saturation, 100/100.0f, epsilon);
195
TEST(TestColor, TestColorHLSToRGB0)
197
nux::color::HueLightnessSaturation hls(270/360.0f, 50/100.0f, 100/100.0f);
198
nux::color::RedGreenBlue rgb(hls);
200
EXPECT_NEAR(rgb.red, 127/255.0f, epsilon);
201
EXPECT_NEAR(rgb.green, 0/255.0f, epsilon);
202
EXPECT_NEAR(rgb.blue, 255/255.0f, epsilon);
205
TEST(TestColor, TestColorRGBToHLS1)
207
nux::color::RedGreenBlue rgb(50/255.0f, 84/255.0f, 13/255.0f);
208
nux::color::HueLightnessSaturation hls(rgb);
210
EXPECT_NEAR(hls.hue, 89/360.0f, epsilon);
211
EXPECT_NEAR(hls.lightness, 19/100.0f, epsilon);
212
EXPECT_NEAR(hls.saturation, 73/100.0f, epsilon);
215
TEST(TestColor, TestColorHLSToRGB1)
217
nux::color::HueLightnessSaturation hls(89/360.0f, 19/100.0f, 73/100.0f);
218
nux::color::RedGreenBlue rgb(hls);
220
EXPECT_NEAR(rgb.red, 50/255.0f, epsilon);
221
EXPECT_NEAR(rgb.green, 84/255.0f, epsilon);
222
EXPECT_NEAR(rgb.blue, 13/255.0f, epsilon);
225
TEST(TestColor, TestColorRGBToHLS2)
227
nux::color::RedGreenBlue rgb(201/255.0f, 200/255.0f, 239/255.0f);
228
nux::color::HueLightnessSaturation hls(rgb);
230
EXPECT_NEAR(hls.hue, 242/360.0f, epsilon);
231
EXPECT_NEAR(hls.lightness, 86/100.0f, epsilon);
232
EXPECT_NEAR(hls.saturation, 55/100.0f, epsilon);
235
TEST(TestColor, TestColorHLSToRGB2)
237
nux::color::HueLightnessSaturation hls(242/360.0f, 86/100.0f, 55/100.0f);
238
nux::color::RedGreenBlue rgb(hls);
240
EXPECT_NEAR(rgb.red, 201/255.0f, epsilon);
241
EXPECT_NEAR(rgb.green, 200/255.0f, epsilon);
242
EXPECT_NEAR(rgb.blue, 239/255.0f, epsilon);