Advertisement
perjespersson

MME MEE

Mar 15th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. #include <stdexcept>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5.  
  6. class Color{
  7. public:
  8. Color(int r, int g, int b, int a = 255);
  9. Color(int g);
  10. int get_red();
  11. int get_green();
  12. int get_blue();
  13. int get_alpha();
  14.  
  15. Color operator+(Color const& rhs);
  16.  
  17. friend std::ostream& operator<<(std::ostream& lhs, Color const&);
  18. private:
  19. int red;
  20. int green;
  21. int blue;
  22. int alpha;
  23. };
  24.  
  25.  
  26.  
  27. //------------------------------------------------------------------//
  28.  
  29. Color::Color(int r, int g, int b, int a):
  30. red{r}, green{g}, blue{b}, alpha{a}
  31. {
  32. if(r > 255 || r<0 || g > 255 || g<0 ||b > 255 || b<0 || a > 255 || a<0){
  33. throw std::invalid_argument("Glöggen is lodium");
  34. }
  35. }
  36.  
  37. Color::Color(int g):
  38. red{g}, green{g}, blue{g}, alpha{255}
  39. {
  40. }
  41.  
  42. int Color::get_red(){
  43. return red;
  44. }
  45.  
  46. int Color::get_green(){
  47. return green;
  48. }
  49.  
  50. int Color::get_blue(){
  51. return blue;
  52. }
  53.  
  54. int Color::get_alpha(){
  55. return alpha;
  56. }
  57.  
  58. Color Color::operator+(Color const& rhs){
  59. Color tmp{1,2,5};
  60. std:: cout << this -> red;
  61. return tmp;
  62. }
  63.  
  64. std::ostream& operator<<(std::ostream& lhs, Color const& rhs) {
  65. lhs << "{" << rhs.red << ", " << rhs.green << ", " << rhs.blue << "}";
  66. return lhs;
  67. }
  68.  
  69.  
  70.  
  71. //------------------------------------------------------------------//
  72.  
  73. std::string str(Color const& c)
  74. {
  75. std::ostringstream oss;
  76. oss << c;
  77. return oss.str();
  78. }
  79.  
  80.  
  81.  
  82. // add your Color class here
  83. // you may but are not required to add a header file
  84. // do not modify anything else
  85.  
  86. #define CATCH_CONFIG_MAIN
  87. #include "catch.hpp"
  88. #include <sstream>
  89.  
  90. TEST_CASE("Constructor")
  91. {
  92. Color a{100,200,0,3};
  93.  
  94. CHECK( a.get_red() == 100 );
  95. CHECK( a.get_green() == 200 );
  96. CHECK( a.get_blue() == 0 );
  97. CHECK( a.get_alpha() == 3 );
  98.  
  99.  
  100.  
  101. Color b{100,200,0};
  102. CHECK( b.get_red() == 100 );
  103. CHECK( b.get_green() == 200 );
  104. CHECK( b.get_blue() == 0 );
  105. CHECK( b.get_alpha() == 255 );
  106.  
  107. a+b;
  108.  
  109. Color c{100};
  110. CHECK( c.get_red() == 100 );
  111. CHECK( c.get_green() == 100 );
  112. CHECK( c.get_blue() == 100 );
  113. CHECK( c.get_alpha() == 255 );
  114. }
  115.  
  116. TEST_CASE("construction")
  117. {
  118. CHECK_NOTHROW( (Color{1,255,0,255}) );
  119. CHECK_NOTHROW( (Color{100,200,0}) );
  120. CHECK_NOTHROW( (Color{75,140,60}) );
  121. CHECK_NOTHROW( (Color{128}) );
  122. CHECK_THROWS( (Color{256,-1,256,-1}) );
  123. CHECK_THROWS( (Color{-256,1,256,1}) );
  124. CHECK_THROWS( (Color{256,1,-256,1}) );
  125. }
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132. TEST_CASE("print")
  133. {
  134. CHECK( str(Color{50,80,20,128}) == "{50, 80, 20}");
  135. }
  136.  
  137. /*
  138. TEST_CASE("add color")
  139. {
  140. Color a{100,200,0};
  141. Color b{50,80,120};
  142. Color c{75,140,60};
  143.  
  144. REQUIRE( str(Color{50,80,20}) == "{50, 80, 20}");
  145.  
  146. CHECK( str(a+b) == str(c) );
  147. CHECK( str(a+c) != str(b) );
  148. CHECK( str(b+c) != str(a) );
  149. }
  150.  
  151. TEST_CASE("add grayscale")
  152. {
  153. Color a{100,200,0};
  154. Color b{100,150,50};
  155.  
  156. REQUIRE( str(Color{50,80,20}) == "{50, 80, 20}");
  157.  
  158. CHECK( str(a+100) == str(b) );
  159. CHECK( str(100+a) == str(b) );
  160. }
  161.  
  162. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement