/opengles/src/FractionalColor.h

http://ftk.googlecode.com/ · C Header · 189 lines · 119 code · 32 blank · 38 comment · 0 complexity · 74af123d9a593fe037a63cf348240df0 MD5 · raw file

  1. #ifndef EGL_FRACTIONAL_COLOR_H
  2. #define EGL_FRACTIONAL_COLOR_H 1
  3. // ==========================================================================
  4. //
  5. // FractionalColor Fixed point representation of an RGBA color
  6. //
  7. // --------------------------------------------------------------------------
  8. //
  9. // 10-05-2003 Hans-Martin Will initial version
  10. //
  11. // --------------------------------------------------------------------------
  12. //
  13. // Copyright (c) 2004, Hans-Martin Will. All rights reserved.
  14. //
  15. // Redistribution and use in source and binary forms, with or without
  16. // modification, are permitted provided that the following conditions are
  17. // met:
  18. //
  19. // * Redistributions of source code must retain the above copyright
  20. // notice, this list of conditions and the following disclaimer.
  21. // * Redistributions in binary form must reproduce the above copyright
  22. // notice, this list of conditions and the following disclaimer in the
  23. // documentation and/or other materials provided with the distribution.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  30. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. // THE POSSIBILITY OF SUCH DAMAGE.
  36. //
  37. // ==========================================================================
  38. #include "OGLES.h"
  39. #include "fixed.h"
  40. #include "Color.h"
  41. namespace EGL {
  42. class FractionalColor {
  43. public:
  44. EGL_Fixed r, g, b, a; // rgba components
  45. public:
  46. FractionalColor() {
  47. r = g = b = a = 0;
  48. }
  49. FractionalColor(const Color & color) {
  50. r = (color.R() << 8 | color.R()) + (color.R() >> 7);
  51. g = (color.G() << 8 | color.G()) + (color.G() >> 7);
  52. b = (color.B() << 8 | color.B()) + (color.B() >> 7);
  53. a = (color.A() << 8 | color.A()) + (color.A() >> 7);
  54. }
  55. FractionalColor(EGL_Fixed R, EGL_Fixed G, EGL_Fixed B, EGL_Fixed A) {
  56. r = R;
  57. g = G;
  58. b = B;
  59. a = A;
  60. }
  61. FractionalColor(const EGL_Fixed * rgba) {
  62. r = rgba[0];
  63. g = rgba[1];
  64. b = rgba[2];
  65. a = rgba[3];
  66. }
  67. static FractionalColor Clamp(const EGL_Fixed * rgba) {
  68. return FractionalColor(
  69. EGL_CLAMP(rgba[0], 0, EGL_ONE),
  70. EGL_CLAMP(rgba[1], 0, EGL_ONE),
  71. EGL_CLAMP(rgba[2], 0, EGL_ONE),
  72. EGL_CLAMP(rgba[3], 0, EGL_ONE));
  73. }
  74. FractionalColor(const FractionalColor& other) {
  75. r = other.r;
  76. b = other.b;
  77. g = other.g;
  78. a = other.a;
  79. }
  80. FractionalColor& operator=(const FractionalColor& other) {
  81. r = other.r;
  82. b = other.b;
  83. g = other.g;
  84. a = other.a;
  85. return *this;
  86. }
  87. // TODO; Decide: Is Fatcional color 8.16 or (1).16 format
  88. inline U16 ConvertTo565() const {
  89. return
  90. EGL_IntFromFixed(b * 0xFF) >> 3 |
  91. (EGL_IntFromFixed(g * 0xFF) & 0xFC) << 3 |
  92. (EGL_IntFromFixed(r * 0xFF) & 0xF8) << 8;
  93. }
  94. inline U16 ConvertTo5551() const {
  95. return
  96. EGL_IntFromFixed(b * 0xFF) >> 3 |
  97. (EGL_IntFromFixed(g * 0xFF) & 0xF8) << 2 |
  98. (EGL_IntFromFixed(r * 0xFF) & 0xF8) << 7 |
  99. (EGL_IntFromFixed(a * 0xFF) & 0x80) << 8;
  100. }
  101. // convert fixed point to byte format
  102. inline operator Color() const {
  103. return Color(
  104. EGL_IntFromFixed((0x1FF * EGL_CLAMP(r, 0, EGL_ONE)) >> 1),
  105. EGL_IntFromFixed((0x1FF * EGL_CLAMP(g, 0, EGL_ONE)) >> 1),
  106. EGL_IntFromFixed((0x1FF * EGL_CLAMP(b, 0, EGL_ONE)) >> 1),
  107. EGL_IntFromFixed((0x1FF * EGL_CLAMP(a, 0, EGL_ONE)) >> 1));
  108. }
  109. inline void Clamp() {
  110. r = EGL_CLAMP(r, 0, EGL_ONE);
  111. g = EGL_CLAMP(g, 0, EGL_ONE);
  112. b = EGL_CLAMP(b, 0, EGL_ONE);
  113. a = EGL_CLAMP(a, 0, EGL_ONE);
  114. }
  115. inline FractionalColor operator*(const FractionalColor& other) const {
  116. return FractionalColor(EGL_Mul(r, other.r),
  117. EGL_Mul(g, other.g), EGL_Mul(b, other.b), a);
  118. }
  119. inline FractionalColor& operator*=(const FractionalColor& other) {
  120. r = EGL_Mul(r, other.r);
  121. g = EGL_Mul(g, other.g);
  122. b = EGL_Mul(b, other.b);
  123. return *this;
  124. }
  125. inline FractionalColor operator*(EGL_Fixed scale) const {
  126. return FractionalColor(EGL_Mul(r, scale),
  127. EGL_Mul(g, scale), EGL_Mul(b, scale), EGL_Mul(a, scale));
  128. }
  129. inline FractionalColor& operator*=(EGL_Fixed scale) {
  130. r = EGL_Mul(r, scale);
  131. g = EGL_Mul(g, scale);
  132. b = EGL_Mul(b, scale);
  133. a = EGL_Mul(a, scale);
  134. return *this;
  135. }
  136. inline FractionalColor operator-(const FractionalColor& other) const {
  137. return FractionalColor(r - other.r, g - other.g, b - other.b, a - other.a);
  138. }
  139. inline FractionalColor operator+(const FractionalColor& other) const {
  140. return FractionalColor(r + other.r, g + other.g, b + other.b, a + other.a);
  141. }
  142. inline void Accumulate(const FractionalColor& color,
  143. EGL_Fixed scale) {
  144. r += EGL_Mul(color.r, scale);
  145. g += EGL_Mul(color.g, scale);
  146. b += EGL_Mul(color.b, scale);
  147. }
  148. inline FractionalColor& operator+=(const FractionalColor& color) {
  149. r += color.r;
  150. g += color.g;
  151. b += color.b;
  152. //a += color.a;
  153. return *this;
  154. }
  155. };
  156. }
  157. #endif //ndef EGL_FRACTIONAL_COLOR_H