Colour.inl 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // Lightweight, non-initialising constructor.
  28. template < typename ColourType, int AlphaDefault >
  29. Colour< ColourType, AlphaDefault >::Colour()
  30. {
  31. }
  32. // Initialising constructor.
  33. template < typename ColourType, int AlphaDefault >
  34. Colour< ColourType, AlphaDefault >::Colour(ColourType _red, ColourType _green, ColourType _blue, ColourType _alpha)
  35. {
  36. red = _red;
  37. green = _green;
  38. blue = _blue;
  39. alpha = _alpha;
  40. }
  41. // Returns the sum of this colour and another. This does not saturate the channels.
  42. template < typename ColourType, int AlphaDefault >
  43. Colour< ColourType, AlphaDefault > Colour< ColourType, AlphaDefault >::operator+(const Colour< ColourType, AlphaDefault >& rhs) const
  44. {
  45. return Colour< ColourType, AlphaDefault >(red + rhs.red, green + rhs.green, blue + rhs.blue, alpha + rhs.alpha);
  46. }
  47. // Returns the result of subtracting another colour from this colour.
  48. template < typename ColourType, int AlphaDefault >
  49. Colour< ColourType, AlphaDefault > Colour< ColourType, AlphaDefault >::operator-(const Colour< ColourType, AlphaDefault >& rhs) const
  50. {
  51. return Colour< ColourType, AlphaDefault >(red - rhs.red, green - rhs.green, blue - rhs.blue, alpha - rhs.alpha);
  52. }
  53. // Returns the result of multiplying this colour component-wise by a scalar.
  54. template < typename ColourType, int AlphaDefault >
  55. Colour< ColourType, AlphaDefault > Colour< ColourType, AlphaDefault >::operator*(float rhs) const
  56. {
  57. return Colour((ColourType) (red * rhs), (ColourType) (green * rhs), (ColourType) (blue * rhs), (ColourType) (alpha * rhs));
  58. }
  59. // Returns the result of dividing this colour component-wise by a scalar.
  60. template < typename ColourType, int AlphaDefault >
  61. Colour< ColourType, AlphaDefault > Colour< ColourType, AlphaDefault >::operator/(float rhs) const
  62. {
  63. return Colour((ColourType) (red / rhs), (ColourType) (green / rhs), (ColourType) (blue / rhs), (ColourType) (alpha / rhs));
  64. }
  65. // Adds another colour to this in-place. This does not saturate the channels.
  66. template < typename ColourType, int AlphaDefault >
  67. void Colour< ColourType, AlphaDefault >::operator+=(const Colour& rhs)
  68. {
  69. red += rhs.red;
  70. green += rhs.green;
  71. blue += rhs.blue;
  72. alpha += rhs.alpha;
  73. }
  74. // Subtracts another colour from this in-place.
  75. template < typename ColourType, int AlphaDefault >
  76. void Colour< ColourType, AlphaDefault >::operator-=(const Colour& rhs)
  77. {
  78. red -= rhs.red;
  79. green -= rhs.green;
  80. blue -= rhs.blue;
  81. alpha -= rhs.alpha;
  82. }
  83. // Scales this colour component-wise in-place.
  84. template < typename ColourType, int AlphaDefault >
  85. void Colour< ColourType, AlphaDefault >::operator*=(float rhs)
  86. {
  87. red = (ColourType)(red * rhs);
  88. green = (ColourType)(green * rhs);
  89. blue = (ColourType)(blue * rhs);
  90. alpha = (ColourType)(alpha * rhs);
  91. }
  92. // Scales this colour component-wise in-place by the inverse of a value.
  93. template < typename ColourType, int AlphaDefault >
  94. void Colour< ColourType, AlphaDefault >::operator/=(float rhs)
  95. {
  96. *this *= (1.0f / rhs);
  97. }
  98. template < >
  99. Colour< float, 1 > ROCKETCORE_API Colour< float, 1 >::operator*(const Colour< float, 1 >& rhs) const;
  100. template < >
  101. Colour< byte, 255 > ROCKETCORE_API Colour< byte, 255 >::operator*(const Colour< byte, 255 >& rhs) const;
  102. template < >
  103. void ROCKETCORE_API Colour< float, 1 >::operator*=(const Colour& rhs);
  104. template < >
  105. void ROCKETCORE_API Colour< byte, 255 >::operator*=(const Colour& rhs);