WColorTest.C 3.2 KB

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