~sil2100/nux/precise_sru-1

« back to all changes in this revision

Viewing changes to tests/gtest-nuxcore-color.cpp

  • Committer: Didier Roche
  • Date: 2012-02-17 10:28:35 UTC
  • mfrom: (159.3.34)
  • Revision ID: didier.roche@canonical.com-20120217102835-7fyqcabz0vad239t
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2012 Inalogic Inc.
 
3
 *
 
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.
 
7
 *
 
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.
 
13
 *
 
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/>
 
17
 *
 
18
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
 
19
 *
 
20
 */
 
21
 
 
22
#include <gmock/gmock.h>
 
23
 
 
24
#include "Nux/Nux.h"
 
25
 
 
26
using namespace testing;
 
27
 
 
28
static const float epsilon = 0.005f;
 
29
 
 
30
namespace {
 
31
 
 
32
  TEST(TestColor, TestColorConstructor0)
 
33
  {
 
34
    nux::Color c;
 
35
 
 
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);
 
41
  }
 
42
 
 
43
  TEST(TestColor, TestColorConstructor1)
 
44
  {
 
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);
 
52
 
 
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);
 
60
 
 
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);
 
68
  }
 
69
 
 
70
  TEST(TestColor, TestColorConstructor2)
 
71
  {
 
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);
 
78
 
 
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);
 
85
  }
 
86
 
 
87
  TEST(TestColor, TestColorConstructor3)
 
88
  {
 
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);
 
95
    
 
96
    //             A         R         G       B
 
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);
 
103
  }
 
104
 
 
105
 
 
106
  TEST(TestColor, TestColorPremultiplied0)
 
107
  {
 
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);
 
114
 
 
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);
 
122
  }
 
123
 
 
124
  TEST(TestColor, TestColorRGBToHSV0)
 
125
  {
 
126
    nux::color::RedGreenBlue rgb(255/255.0f, 128/255.0f, 169/255.0f);
 
127
    nux::color::HueSaturationValue hsv(rgb);
 
128
 
 
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);
 
132
  }
 
133
 
 
134
  TEST(TestColor, TestColorHSVToRGB0)
 
135
  {
 
136
    nux::color::HueSaturationValue hsv(341/360.0f, 50/100.0f, 100/100.0f);
 
137
    nux::color::RedGreenBlue rgb(hsv);
 
138
 
 
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);
 
142
  }
 
143
 
 
144
  TEST(TestColor, TestColorRGBToHSV1)
 
145
  {
 
146
    nux::color::RedGreenBlue rgb(65/255.0f, 28/255.0f, 6/255.0f);
 
147
    nux::color::HueSaturationValue hsv(rgb);
 
148
 
 
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);
 
152
  }
 
153
 
 
154
  TEST(TestColor, TestColorHSVToRGB1)
 
155
  {
 
156
    nux::color::HueSaturationValue hsv(22/360.0f, 91/100.0f, 25/100.0f);
 
157
    nux::color::RedGreenBlue rgb(hsv);
 
158
 
 
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);
 
162
  }
 
163
 
 
164
  TEST(TestColor, TestColorRGBToHSV2)
 
165
  {
 
166
    nux::color::RedGreenBlue rgb(90/255.0f, 65/255.0f, 158/255.0f);
 
167
    nux::color::HueSaturationValue hsv(rgb);
 
168
 
 
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);
 
172
  }
 
173
 
 
174
 
 
175
  TEST(TestColor, TestColorHSVToRGB2)
 
176
  {
 
177
    nux::color::HueSaturationValue hsv(256/360.0f, 59/100.0f, 62/100.0f);
 
178
    nux::color::RedGreenBlue rgb(hsv);
 
179
 
 
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);
 
183
  }
 
184
 
 
185
  TEST(TestColor, TestColorRGBToHLS0)
 
186
  {
 
187
    nux::color::RedGreenBlue rgb(127/255.0f, 0/255.0f, 255/255.0f);
 
188
    nux::color::HueLightnessSaturation hls(rgb);
 
189
 
 
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);
 
193
  }
 
194
 
 
195
  TEST(TestColor, TestColorHLSToRGB0)
 
196
  {
 
197
    nux::color::HueLightnessSaturation hls(270/360.0f, 50/100.0f, 100/100.0f);
 
198
    nux::color::RedGreenBlue rgb(hls);
 
199
 
 
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);
 
203
  }
 
204
 
 
205
  TEST(TestColor, TestColorRGBToHLS1)
 
206
  {
 
207
    nux::color::RedGreenBlue rgb(50/255.0f, 84/255.0f, 13/255.0f);
 
208
    nux::color::HueLightnessSaturation hls(rgb);
 
209
 
 
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);
 
213
  }
 
214
 
 
215
  TEST(TestColor, TestColorHLSToRGB1)
 
216
  {
 
217
    nux::color::HueLightnessSaturation hls(89/360.0f, 19/100.0f, 73/100.0f);
 
218
    nux::color::RedGreenBlue rgb(hls);
 
219
 
 
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);
 
223
  }
 
224
 
 
225
  TEST(TestColor, TestColorRGBToHLS2)
 
226
  {
 
227
    nux::color::RedGreenBlue rgb(201/255.0f, 200/255.0f, 239/255.0f);
 
228
    nux::color::HueLightnessSaturation hls(rgb);
 
229
 
 
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);
 
233
  }
 
234
 
 
235
  TEST(TestColor, TestColorHLSToRGB2)
 
236
  {
 
237
    nux::color::HueLightnessSaturation hls(242/360.0f, 86/100.0f, 55/100.0f);
 
238
    nux::color::RedGreenBlue rgb(hls);
 
239
 
 
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);
 
243
  }
 
244
}
 
245