1 // This may look like C code, but it's really -*- C++ -*-
2 /*
3  * Copyright (C) 2010 Emweb bv, Herent, Belgium.
4  *
5  * See the LICENSE file for terms of use.
6  */
7 #include <boost/test/unit_test.hpp>
8 
9 #include <Wt/WException.h>
10 #include <Wt/WColor.h>
11 
BOOST_AUTO_TEST_CASE(color_test_constructors)12 BOOST_AUTO_TEST_CASE( color_test_constructors )
13 {
14   //string constructors
15   {
16     Wt::WColor c("#f80");
17     BOOST_REQUIRE(c.red() == 255);
18     BOOST_REQUIRE(c.green() == 136);
19     BOOST_REQUIRE(c.blue() == 0);
20     BOOST_REQUIRE(c.alpha() == 255);
21     BOOST_REQUIRE(c.cssText() == "#f80");
22   }
23 
24   {
25     Wt::WColor c("#12a0cf");
26     BOOST_REQUIRE(c.red() == 18);
27     BOOST_REQUIRE(c.green() == 160);
28     BOOST_REQUIRE(c.blue() == 207);
29     BOOST_REQUIRE(c.alpha() == 255);
30   }
31 
32   {
33     Wt::WColor c("#FF0000");
34     BOOST_REQUIRE(c.red() == 255);
35     BOOST_REQUIRE(c.green() == 0);
36     BOOST_REQUIRE(c.blue() == 0);
37     BOOST_REQUIRE(c.alpha() == 255);
38   }
39 
40   {
41     Wt::WColor c("rgb(18,160,207)");
42     BOOST_REQUIRE(c.red() == 18);
43     BOOST_REQUIRE(c.green() == 160);
44     BOOST_REQUIRE(c.blue() == 207);
45     BOOST_REQUIRE(c.alpha() == 255);
46   }
47 
48   {
49     Wt::WColor c("rgb( 18  , 160  ,  207 )");
50     BOOST_REQUIRE(c.red() == 18);
51     BOOST_REQUIRE(c.green() == 160);
52     BOOST_REQUIRE(c.blue() == 207);
53     BOOST_REQUIRE(c.alpha() == 255);
54   }
55 
56   {
57     Wt::WColor c("rgb( 50%  , 25%  ,  0%)");
58     BOOST_REQUIRE(c.red() == 127);
59     BOOST_REQUIRE(c.green() == 63);
60     BOOST_REQUIRE(c.blue() == 0);
61     BOOST_REQUIRE(c.alpha() == 255);
62   }
63 
64   {
65     Wt::WColor c("rgba(18,160,207,0.2)");
66     BOOST_REQUIRE(c.red() == 18);
67     BOOST_REQUIRE(c.green() == 160);
68     BOOST_REQUIRE(c.blue() == 207);
69     BOOST_REQUIRE(c.alpha() == 51);
70   }
71 
72   {
73     Wt::WColor c("rgba(18 , 160  ,  207 , 0.2)");
74     BOOST_REQUIRE(c.red() == 18);
75     BOOST_REQUIRE(c.green() == 160);
76     BOOST_REQUIRE(c.blue() == 207);
77     BOOST_REQUIRE(c.alpha() == 51);
78   }
79 
80   {
81     Wt::WColor c("rgba( 50%  , 25%  ,  0%, 0.22)");
82     BOOST_REQUIRE(c.red() == 127);
83     BOOST_REQUIRE(c.green() == 63);
84     BOOST_REQUIRE(c.blue() == 0);
85     BOOST_REQUIRE(c.alpha() == 56);
86   }
87 
88   {
89     Wt::WColor c("#ff80");
90     BOOST_REQUIRE(c.red() == 255 && c.green() == 255 && c.blue() == 0x88 && c.alpha() == 0);
91   }
92 
93   //try to mess things up
94   {
95     Wt::WColor c("#f8 0");
96     BOOST_REQUIRE(c.red() == 0 && c.green() == 0 && c.blue() == 0);
97   }
98 
99   {
100     Wt::WColor c("rgb(30%,20%)");
101     BOOST_REQUIRE(c.red() == 0 && c.green() == 0 && c.blue() == 0);
102   }
103 
104   {
105     Wt::WColor c("rgb(30%,20%,50%,0.2)");
106     BOOST_REQUIRE(c.red() == 0 && c.green() == 0 && c.blue() == 0);
107   }
108 
109   {
110     Wt::WColor c("rgb30%,20%,50%)");
111     BOOST_REQUIRE(c.red() == 0 && c.green() == 0 && c.blue() == 0);
112   }
113 
114   {
115     Wt::WColor c("rgb(30%,20%,50%");
116     BOOST_REQUIRE(c.red() == 0 && c.green() == 0 && c.blue() == 0);
117   }
118 
119   {
120     Wt::WColor c("rgba(30%,20%,50%,a)");
121     BOOST_REQUIRE(c.alpha() == 255);
122   }
123 
124   {
125     Wt::WColor c("rgba(30%,20%,50%)");
126     BOOST_REQUIRE(c.red() == 0 && c.green() == 0 && c.blue() == 0);
127   }
128 
129   {
130     Wt::WColor c("rgba(30%,20%,50%,0.2,x)");
131     BOOST_REQUIRE(c.red() == 0 && c.green() == 0 && c.blue() == 0);
132   }
133 
134   {
135     Wt::WColor c("gold");
136     BOOST_REQUIRE(c.red() == 0 && c.green() == 0 && c.blue() == 0);
137     BOOST_REQUIRE(c.cssText() == "gold");
138   }
139 }
140