Back to index

nux  3.0.0
gtest-nuxcore-color.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2012 Inalogic Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License version 3, as
00006  * published by the  Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranties of
00010  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00012  * License for more details.
00013  *
00014  * You should have received a copy of both the GNU Lesser General Public
00015  * License version 3 along with this program.  If not, see
00016  * <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 #include <gmock/gmock.h>
00023 
00024 #include "Nux/Nux.h"
00025 
00026 using namespace testing;
00027 
00028 static const float epsilon = 0.005f;
00029 
00030 namespace {
00031 
00032   TEST(TestColor, TestColorConstructor0)
00033   {
00034     nux::Color c;
00035 
00036     EXPECT_EQ(c.red,    0.0f);
00037     EXPECT_EQ(c.green,  0.0f);
00038     EXPECT_EQ(c.blue,   0.0f);
00039     EXPECT_EQ(c.alpha,  1.0);
00040     EXPECT_EQ(c.IsPremultiplied(), false);
00041   }
00042 
00043   TEST(TestColor, TestColorConstructor1)
00044   {
00045     // testing float inputs
00046     nux::Color c0(0.1f, 0.2f, 0.3f);
00047     EXPECT_EQ(c0.red,    0.1f);
00048     EXPECT_EQ(c0.green,  0.2f);
00049     EXPECT_EQ(c0.blue,   0.3f);
00050     EXPECT_EQ(c0.alpha,  1.0f);
00051     EXPECT_EQ(c0.IsPremultiplied(), false);
00052 
00053     // testing float inputs
00054     nux::Color c1(0.1f, 0.2f, 0.3f, 0.4f);
00055     EXPECT_EQ(c1.red,    0.1f);
00056     EXPECT_EQ(c1.green,  0.2f);
00057     EXPECT_EQ(c1.blue,   0.3f);
00058     EXPECT_EQ(c1.alpha,  0.4f);
00059     EXPECT_EQ(c1.IsPremultiplied(), false);
00060 
00061     // testing double inputs
00062     nux::Color c2(0.1, 0.2, 0.3, 0.4);
00063     EXPECT_EQ(c2.red,    0.1f);
00064     EXPECT_EQ(c2.green,  0.2f);
00065     EXPECT_EQ(c2.blue,   0.3f);
00066     EXPECT_EQ(c2.alpha,  0.4f);
00067     EXPECT_EQ(c2.IsPremultiplied(), false);
00068   }
00069 
00070   TEST(TestColor, TestColorConstructor2)
00071   {
00072     nux::Color c0(255, 255, 255);
00073     EXPECT_EQ(c0.red,    1.0f);
00074     EXPECT_EQ(c0.green,  1.0f);
00075     EXPECT_EQ(c0.blue,   1.0f);
00076     EXPECT_EQ(c0.alpha,  1.0f);
00077     EXPECT_EQ(c0.IsPremultiplied(), false);
00078 
00079     nux::Color c1(10, 128, 192);
00080     EXPECT_EQ(c1.red,    10/255.0f);
00081     EXPECT_EQ(c1.green,  128/255.0f);
00082     EXPECT_EQ(c1.blue,   192/255.0f);
00083     EXPECT_EQ(c1.alpha,  1.0f);
00084     EXPECT_EQ(c1.IsPremultiplied(), false);
00085   }
00086 
00087   TEST(TestColor, TestColorConstructor3)
00088   {
00089     nux::Color c0((255<<24)|(255<<16)|(255<<8)|255);
00090     EXPECT_EQ(c0.red,    1.0f);
00091     EXPECT_EQ(c0.green,  1.0f);
00092     EXPECT_EQ(c0.blue,   1.0f);
00093     EXPECT_EQ(c0.alpha,  1.0f);
00094     EXPECT_EQ(c0.IsPremultiplied(), false);
00095     
00096     //             A         R         G       B
00097     nux::Color c1((128<<24)|(192<<16)|(128<<8)|10);
00098     EXPECT_EQ(c1.red,    192/255.0f);
00099     EXPECT_EQ(c1.green,  128/255.0f);
00100     EXPECT_EQ(c1.blue,   10/255.0f);
00101     EXPECT_EQ(c1.alpha,  128/255.0f);
00102     EXPECT_EQ(c1.IsPremultiplied(), false);
00103   }
00104 
00105   
00106   TEST(TestColor, TestColorConstructorHex)
00107   {
00108     nux::Color c0("#abc");
00109     EXPECT_EQ(c0.red,    0xaa / 255.0f);
00110     EXPECT_EQ(c0.green,  0xbb / 255.0f);
00111     EXPECT_EQ(c0.blue,   0xcc / 255.0f);
00112     EXPECT_EQ(c0.alpha,  1.0f);
00113     EXPECT_EQ(c0.IsPremultiplied(), false);
00114 
00115     nux::Color c1("#abcd");
00116     EXPECT_EQ(c1.red,    0xaa / 255.0f);
00117     EXPECT_EQ(c1.green,  0xbb / 255.0f);
00118     EXPECT_EQ(c1.blue,   0xcc / 255.0f);
00119     EXPECT_EQ(c1.alpha,  0xdd / 255.0f);
00120     EXPECT_EQ(c1.IsPremultiplied(), false);
00121 
00122     nux::Color c2("#aabbcc");
00123     EXPECT_EQ(c2.red,    0xaa / 255.0f);
00124     EXPECT_EQ(c2.green,  0xbb / 255.0f);
00125     EXPECT_EQ(c2.blue,   0xcc / 255.0f);
00126     EXPECT_EQ(c2.alpha,  1.0f);
00127     EXPECT_EQ(c2.IsPremultiplied(), false);
00128 
00129 
00130     nux::Color c3("#aabbccdd");
00131     EXPECT_EQ(c3.red,    0xaa / 255.0f);
00132     EXPECT_EQ(c3.green,  0xbb / 255.0f);
00133     EXPECT_EQ(c3.blue,   0xcc / 255.0f);
00134     EXPECT_EQ(c3.alpha,  0xdd / 255.0f);
00135     EXPECT_EQ(c3.IsPremultiplied(), false);
00136     
00137     nux::Color c4("abcdef");
00138     EXPECT_EQ(c4.red,    0xab / 255.0f );
00139     EXPECT_EQ(c4.green,  0xcd / 255.0f);
00140     EXPECT_EQ(c4.blue,   0xef / 255.0f);
00141     EXPECT_EQ(c4.alpha,  1.0f);
00142     EXPECT_EQ(c4.IsPremultiplied(), false);
00143   }
00144 
00145   TEST(TestColor, TestColorConstructorHexInvalid)
00146   {
00147     nux::Color c0("");
00148     EXPECT_EQ(c0.red,    0.0f);
00149     EXPECT_EQ(c0.green,  0.0f);
00150     EXPECT_EQ(c0.blue,   0.0f);
00151     EXPECT_EQ(c0.alpha,  1.0f);
00152     EXPECT_EQ(c0.IsPremultiplied(), false);
00153     
00154     nux::Color c1("#abcdeG");
00155     EXPECT_EQ(c0.red,    0.0f);
00156     EXPECT_EQ(c0.green,  0.0f);
00157     EXPECT_EQ(c0.blue,   0.0f);
00158     EXPECT_EQ(c0.alpha,  1.0f);
00159     EXPECT_EQ(c1.IsPremultiplied(), false);
00160   }
00161 
00162 
00163   TEST(TestColor, TestColorPremultiplied0)
00164   {
00165     nux::Color c0(0.81f, 0.24f, 0.53f, 0.79f);
00166     EXPECT_EQ(c0.red,    0.81f);
00167     EXPECT_EQ(c0.green,  0.24f);
00168     EXPECT_EQ(c0.blue,   0.53f);
00169     EXPECT_EQ(c0.alpha,  0.79f);
00170     EXPECT_EQ(c0.IsPremultiplied(), false);
00171 
00172     nux::Color c1 = c0.GetPremultiplied();
00173     EXPECT_EQ(c1.red,    0.81f * 0.79f);
00174     EXPECT_EQ(c1.green,  0.24f * 0.79f);
00175     EXPECT_EQ(c1.blue,   0.53f * 0.79f);
00176     EXPECT_EQ(c1.alpha,  0.79f);
00177     EXPECT_EQ(c1.IsPremultiplied(), true);
00178     EXPECT_EQ(c0.IsPremultiplied(), false);
00179   }
00180 
00181   TEST(TestColor, TestColorRGBToHSV0)
00182   {
00183     nux::color::RedGreenBlue rgb(255/255.0f, 128/255.0f, 169/255.0f);
00184     nux::color::HueSaturationValue hsv(rgb);
00185 
00186     EXPECT_NEAR(hsv.hue,          341/360.0f, epsilon);
00187     EXPECT_NEAR(hsv.saturation,   50/100.0f, epsilon);
00188     EXPECT_NEAR(hsv.value,        100/100.0f, epsilon);
00189   }
00190 
00191   TEST(TestColor, TestColorHSVToRGB0)
00192   {
00193     nux::color::HueSaturationValue hsv(341/360.0f, 50/100.0f, 100/100.0f);
00194     nux::color::RedGreenBlue rgb(hsv);
00195 
00196     EXPECT_NEAR(rgb.red,        255/255.0f, epsilon);
00197     EXPECT_NEAR(rgb.green,      128/255.0f, epsilon);
00198     EXPECT_NEAR(rgb.blue,       169/255.0f, epsilon);
00199   }
00200 
00201   TEST(TestColor, TestColorRGBToHSV1)
00202   {
00203     nux::color::RedGreenBlue rgb(65/255.0f, 28/255.0f, 6/255.0f);
00204     nux::color::HueSaturationValue hsv(rgb);
00205 
00206     EXPECT_NEAR(hsv.hue,          22/360.0f, epsilon);
00207     EXPECT_NEAR(hsv.saturation,   91/100.0f, epsilon);
00208     EXPECT_NEAR(hsv.value,        25/100.0f, epsilon);
00209   }
00210 
00211   TEST(TestColor, TestColorHSVToRGB1)
00212   {
00213     nux::color::HueSaturationValue hsv(22/360.0f, 91/100.0f, 25/100.0f);
00214     nux::color::RedGreenBlue rgb(hsv);
00215 
00216     EXPECT_NEAR(rgb.red,        65/255.0f, epsilon);
00217     EXPECT_NEAR(rgb.green,      28/255.0f, epsilon);
00218     EXPECT_NEAR(rgb.blue,       6/255.0f, epsilon);
00219   }
00220 
00221   TEST(TestColor, TestColorRGBToHSV2)
00222   {
00223     nux::color::RedGreenBlue rgb(90/255.0f, 65/255.0f, 158/255.0f);
00224     nux::color::HueSaturationValue hsv(rgb);
00225 
00226     EXPECT_NEAR(hsv.hue,          256/360.0f, epsilon);
00227     EXPECT_NEAR(hsv.saturation,   59/100.0f, epsilon);
00228     EXPECT_NEAR(hsv.value,        62/100.0f, epsilon);
00229   }
00230 
00231 
00232   TEST(TestColor, TestColorHSVToRGB2)
00233   {
00234     nux::color::HueSaturationValue hsv(256/360.0f, 59/100.0f, 62/100.0f);
00235     nux::color::RedGreenBlue rgb(hsv);
00236 
00237     EXPECT_NEAR(rgb.red,        90/255.0f, epsilon);
00238     EXPECT_NEAR(rgb.green,      65/255.0f, epsilon);
00239     EXPECT_NEAR(rgb.blue,       158/255.0f, epsilon);
00240   }
00241 
00242   TEST(TestColor, TestColorRGBToHLS0)
00243   {
00244     nux::color::RedGreenBlue rgb(127/255.0f, 0/255.0f, 255/255.0f);
00245     nux::color::HueLightnessSaturation hls(rgb);
00246 
00247     EXPECT_NEAR(hls.hue,          270/360.0f, epsilon);
00248     EXPECT_NEAR(hls.lightness,    50/100.0f, epsilon);
00249     EXPECT_NEAR(hls.saturation,   100/100.0f,  epsilon);
00250   }
00251 
00252   TEST(TestColor, TestColorHLSToRGB0)
00253   {
00254     nux::color::HueLightnessSaturation hls(270/360.0f, 50/100.0f, 100/100.0f);
00255     nux::color::RedGreenBlue rgb(hls);
00256 
00257     EXPECT_NEAR(rgb.red,        127/255.0f, epsilon);
00258     EXPECT_NEAR(rgb.green,      0/255.0f, epsilon);
00259     EXPECT_NEAR(rgb.blue,       255/255.0f, epsilon);
00260   }
00261 
00262   TEST(TestColor, TestColorRGBToHLS1)
00263   {
00264     nux::color::RedGreenBlue rgb(50/255.0f, 84/255.0f, 13/255.0f);
00265     nux::color::HueLightnessSaturation hls(rgb);
00266 
00267     EXPECT_NEAR(hls.hue,          89/360.0f, epsilon);
00268     EXPECT_NEAR(hls.lightness,    19/100.0f, epsilon);
00269     EXPECT_NEAR(hls.saturation,   73/100.0f, epsilon);
00270   }
00271 
00272   TEST(TestColor, TestColorHLSToRGB1)
00273   {
00274     nux::color::HueLightnessSaturation hls(89/360.0f, 19/100.0f, 73/100.0f);
00275     nux::color::RedGreenBlue rgb(hls);
00276 
00277     EXPECT_NEAR(rgb.red,        50/255.0f, epsilon);
00278     EXPECT_NEAR(rgb.green,      84/255.0f, epsilon);
00279     EXPECT_NEAR(rgb.blue,       13/255.0f, epsilon);
00280   }
00281 
00282   TEST(TestColor, TestColorRGBToHLS2)
00283   {
00284     nux::color::RedGreenBlue rgb(201/255.0f, 200/255.0f, 239/255.0f);
00285     nux::color::HueLightnessSaturation hls(rgb);
00286 
00287     EXPECT_NEAR(hls.hue,          242/360.0f, epsilon);
00288     EXPECT_NEAR(hls.lightness,    86/100.0f,  epsilon);
00289     EXPECT_NEAR(hls.saturation,   55/100.0f,  epsilon);
00290   }
00291 
00292   TEST(TestColor, TestColorHLSToRGB2)
00293   {
00294     nux::color::HueLightnessSaturation hls(242/360.0f, 86/100.0f, 55/100.0f);
00295     nux::color::RedGreenBlue rgb(hls);
00296 
00297     EXPECT_NEAR(rgb.red,        201/255.0f, epsilon);
00298     EXPECT_NEAR(rgb.green,      200/255.0f, epsilon);
00299     EXPECT_NEAR(rgb.blue,       239/255.0f, epsilon);
00300   }
00301 }
00302