Colour.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCORECOLOUR_H
  28. #define ROCKETCORECOLOUR_H
  29. #include "Header.h"
  30. namespace Rocket {
  31. namespace Core {
  32. /**
  33. Templated class for a four-component RGBA colour.
  34. @author Peter Curry
  35. */
  36. template < typename ColourType, int AlphaDefault >
  37. class Colour
  38. {
  39. public:
  40. /// Lightweight, non-initialising constructor.
  41. inline Colour();
  42. /// Initialising constructor.
  43. /// @param[in] red Initial red value of the colour.
  44. /// @param[in] green Initial green value of the colour.
  45. /// @param[in] blue Initial blue value of the colour.
  46. /// @param[in] alpha Initial alpha value of the colour.
  47. inline Colour(ColourType red, ColourType green, ColourType blue, ColourType alpha = AlphaDefault);
  48. /// Returns the sum of this colour and another. This does not saturate the channels.
  49. /// @param[in] rhs The colour to add this to.
  50. /// @return The sum of the two colours.
  51. inline Colour operator+(const Colour& rhs) const;
  52. /// Returns the result of subtracting another colour from this colour.
  53. /// @param[in] rhs The colour to subtract from this colour.
  54. /// @return The result of the subtraction.
  55. inline Colour operator-(const Colour& rhs) const;
  56. /// Returns the result of multiplying this colour by another.
  57. /// @param[in] rhs The colour to multiply by.
  58. /// @return The result of the multiplication.
  59. Colour operator*(const Colour& rhs) const;
  60. /// Returns the result of multiplying this colour component-wise by a scalar.
  61. /// @param[in] rhs The scalar value to multiply by.
  62. /// @return The result of the scale.
  63. inline Colour operator*(float rhs) const;
  64. /// Returns the result of dividing this colour component-wise by a scalar.
  65. /// @param[in] rhs The scalar value to divide by.
  66. /// @return The result of the scale.
  67. inline Colour operator/(float rhs) const;
  68. /// Adds another colour to this in-place. This does not saturate the channels.
  69. /// @param[in] rhs The colour to add.
  70. inline void operator+=(const Colour& rhs);
  71. /// Subtracts another colour from this in-place.
  72. /// @param[in] rhs The colour to subtract.
  73. inline void operator-=(const Colour& rhs);
  74. /// Multiplies this colour component-wise with another in-place.
  75. /// @param[in] rhs The colour to multiply by.
  76. /// @return This colour, post-operation.
  77. void operator*=(const Colour& rhs);
  78. /// Scales this colour component-wise in-place.
  79. /// @param[in] rhs The value to scale this colours's components by.
  80. inline void operator*=(float rhs);
  81. /// Scales this colour component-wise in-place by the inverse of a value.
  82. /// @param[in] rhs The value to divide this colour's components by.
  83. inline void operator/=(float rhs);
  84. /// Equality operator.
  85. /// @param[in] rhs The colour to compare this against.
  86. /// @return True if the two colours are equal, false otherwise.
  87. inline bool operator==(const Colour& rhs) { return red == rhs.red && green == rhs.green && blue == rhs.blue && alpha == rhs.alpha; }
  88. /// Inequality operator.
  89. /// @param[in] rhs The colour to compare this against.
  90. /// @return True if the two colours are not equal, false otherwise.
  91. inline bool operator!=(const Colour& rhs) { return red != rhs.red || green != rhs.green || blue != rhs.blue || alpha != rhs.alpha; }
  92. /// Auto-cast operator.
  93. /// @return A pointer to the first value.
  94. inline operator const ColourType*() const { return &red; }
  95. /// Constant auto-cast operator.
  96. /// @return A constant pointer to the first value.
  97. inline operator ColourType*() { return &red; }
  98. ColourType red, green, blue, alpha;
  99. };
  100. }
  101. }
  102. namespace Rocket {
  103. namespace Core {
  104. #include "Colour.inl"
  105. }
  106. }
  107. #endif