color.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include "video/color.hpp"
  17. #include <assert.h>
  18. const Color Color::BLACK(0.0, 0.0, 0.0);
  19. const Color Color::RED(1.0, 0.0, 0.0);
  20. const Color Color::GREEN(0.0, 1.0, 0.0);
  21. const Color Color::BLUE(0.0, 0.0, 1.0);
  22. const Color Color::CYAN(0.0, 1.0, 1.0);
  23. const Color Color::MAGENTA(1.0, 0.0, 1.0);
  24. const Color Color::YELLOW(1.0, 1.0, 0.0);
  25. const Color Color::WHITE(1.0, 1.0, 1.0);
  26. Color::Color() :
  27. red(0),
  28. green(0),
  29. blue(0),
  30. alpha(1.0f)
  31. {}
  32. Color::Color(float red_, float green_, float blue_, float alpha_) :
  33. red(red_),
  34. green(green_),
  35. blue(blue_),
  36. alpha(alpha_)
  37. {
  38. assert(0 <= red && red <= 1.0f);
  39. assert(0 <= green && green <= 1.0f);
  40. assert(0 <= blue && blue <= 1.0f);
  41. }
  42. Color::Color(const std::vector<float>& vals) :
  43. red(),
  44. green(),
  45. blue(),
  46. alpha()
  47. {
  48. if (vals.size() < 3) {
  49. red = 0;
  50. green = 0;
  51. blue = 0;
  52. alpha = 0;
  53. return;
  54. }
  55. red = vals[0];
  56. green = vals[1];
  57. blue = vals[2];
  58. if (vals.size() > 3)
  59. alpha = vals[3];
  60. else
  61. alpha = 1.0;
  62. assert(0 <= red && red <= 1.0f);
  63. assert(0 <= green && green <= 1.0f);
  64. assert(0 <= blue && blue <= 1.0f);
  65. }
  66. bool
  67. Color::operator==(const Color& other) const
  68. {
  69. return red == other.red && green == other.green && blue == other.blue
  70. && alpha == other.alpha;
  71. }
  72. bool
  73. Color::operator!=(const Color& other) const
  74. {
  75. return !(operator==(other));
  76. }
  77. float
  78. Color::greyscale() const
  79. {
  80. return red * 0.30f + green * 0.59f + blue * 0.11f;
  81. }
  82. Color
  83. Color::multiply_linearly(float v) const
  84. {
  85. // For the approximate sRGB conversion, it is sufficient to apply the exponent
  86. // to v.
  87. v = add_gamma(v);
  88. return Color(red * v, green * v, blue * v, alpha);
  89. }
  90. bool
  91. Color::operator < (const Color& other) const
  92. {
  93. return greyscale() < other.greyscale();
  94. }
  95. std::vector<float>
  96. Color::toVector()
  97. {
  98. std::vector<float> result;
  99. result.clear();
  100. result.push_back(red);
  101. result.push_back(green);
  102. result.push_back(blue);
  103. result.push_back(alpha);
  104. return result;
  105. }
  106. /* EOF */