/opengles/src/Color.h

http://ftk.googlecode.com/ · C Header · 261 lines · 171 code · 52 blank · 38 comment · 2 complexity · 020aba49beb278e651deffb7af99ccaf MD5 · raw file

  1. #ifndef EGL_COLOR_H
  2. #define EGL_COLOR_H 1
  3. // ==========================================================================
  4. //
  5. // Color Integer 8-8-8-8 representation of an RGBA color
  6. //
  7. // --------------------------------------------------------------------------
  8. //
  9. // 10-09-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. namespace EGL {
  41. class Color {
  42. public:
  43. enum {
  44. MAX = 0xff
  45. };
  46. public:
  47. U8 r, g, b, a; // rgba components
  48. public:
  49. Color() {
  50. r = g = b = a = 0;
  51. }
  52. Color(U32 rgba) {
  53. r = (rgba >> 24) & 0xff;
  54. g = (rgba >> 16) & 0xff;
  55. b = (rgba >> 8) & 0xff;
  56. a = rgba & 0xff;
  57. }
  58. Color(U8 R, U8 G, U8 B, U8 A) {
  59. r = R;
  60. g = G;
  61. b = B;
  62. a = A;
  63. }
  64. Color(const Color& other) {
  65. r = other.r;
  66. g = other.g;
  67. b = other.b;
  68. a = other.a;
  69. }
  70. Color& operator=(const Color& other) {
  71. r = other.r;
  72. g = other.g;
  73. b = other.b;
  74. a = other.a;
  75. return *this;
  76. }
  77. bool operator==(const Color& other) const {
  78. return ConvertToRGBA() == other.ConvertToRGBA();
  79. }
  80. inline U8 A() const {
  81. return a;
  82. }
  83. inline U8 R() const {
  84. return r;
  85. }
  86. inline U8 G() const {
  87. return g;
  88. }
  89. inline U8 B() const {
  90. return b;
  91. }
  92. inline Color Mask(bool maskR, bool maskG, bool maskB, bool maskA) const {
  93. return Color(maskR ? r : 0, maskG ? g : 0, maskB ? b : 0, maskA ? a : 0);
  94. }
  95. inline U16 ConvertTo565() const {
  96. return (b & 0xF8) >> 3 | (g & 0xFC) << 3 | (r & 0xF8) << 8;
  97. }
  98. inline U32 ConvertToRGBA() const {
  99. return r << 24 | g << 16 | b << 8 | a;
  100. }
  101. inline U16 ConvertTo5551() const {
  102. return (b & 0xF8) >> 2 | (g & 0xF8) << 3 | (r & 0xF8) << 8 | (a & 0x80) >> 7;
  103. }
  104. inline U16 ConvertTo4444() const {
  105. return (r & 0xf0) << 8 |
  106. (g & 0xf0) << 4 |
  107. (b & 0xf0) |
  108. a >> 4;
  109. }
  110. static inline Color FromRGBA(U32 rgba) {
  111. U8 r = (rgba & 0xFF000000) >> 24;
  112. U8 g = (rgba & 0x00FF0000) >> 16;
  113. U8 b = (rgba & 0x0000FF00) >> 8;
  114. U8 a = (rgba & 0x000000FF);
  115. return Color(r, g, b, a);
  116. }
  117. static inline Color From4444(U16 u4444) {
  118. U8 r = (u4444 & 0xF000u) >> 8;
  119. U8 g = (u4444 & 0x0F00u) >> 4;
  120. U8 b = (u4444 & 0x00F0u);
  121. U8 a = (u4444 & 0x000Fu) << 4;
  122. r |= r >> 4;
  123. g |= g >> 4;
  124. b |= b >> 4;
  125. a |= a >> 4;
  126. return Color(r, g, b, a);
  127. }
  128. static inline Color From565(U16 u565) {
  129. U8 b = (u565 & 0x001Fu) << 3;
  130. U8 g = (u565 & 0x07E0u) >> 3;
  131. U8 r = (u565 & 0xF800u) >> 8;
  132. r |= r >> 5;
  133. g |= g >> 6;
  134. b |= b >> 5;
  135. return Color(r, g, b, 0xFF);
  136. }
  137. static inline Color From565A(U16 u565, U8 alpha) {
  138. U8 b = (u565 & 0x001Fu) << 3;
  139. U8 g = (u565 & 0x07E0u) >> 3;
  140. U8 r = (u565 & 0xF800u) >> 8;
  141. r |= r >> 5;
  142. g |= g >> 6;
  143. b |= b >> 5;
  144. return Color(r, g, b, alpha);
  145. }
  146. static inline Color From5551(U16 u5551) {
  147. U8 b = (u5551 & 0x003Eu) << 2;
  148. U8 g = (u5551 & 0x07C0u) >> 3;
  149. U8 r = (u5551 & 0xF800u) >> 8;
  150. U8 a = (u5551 & 0x0001u) << 7;
  151. r |= r >> 5;
  152. g |= g >> 5;
  153. b |= b >> 5;
  154. if (a) a |= 0x7f;
  155. return Color(r, g, b, a);
  156. }
  157. static inline Color FromLuminanceAlpha(U16 la) {
  158. U8 l = (la & 0xff);
  159. U8 a = (la & 0xff00) >> 8;
  160. return Color(l, l, l, a);
  161. }
  162. Color operator+(const Color& other) const {
  163. return Color(clamp(r + other.r), clamp(g + other.g),
  164. clamp(b + other.b), clamp(a + other.a));
  165. }
  166. Color operator*(const Color& factor) const {
  167. return Color(mul(r, factor.r), mul(g, factor.g), mul(b, factor.b), mul(a, factor.a));
  168. }
  169. // -------------------------------------------------------------------------
  170. // return the blend of src and dst, where alpha is a value between 0 and 0x100
  171. // -------------------------------------------------------------------------
  172. static inline Color Blend(const Color& src, const Color& dst, U32 alpha) {
  173. U32 oneMinusAlpha = 0x100 - alpha;
  174. return Color((src.R() * alpha + dst.R() * oneMinusAlpha) >> 8,
  175. (src.G() * alpha + dst.G() * oneMinusAlpha) >> 8,
  176. (src.B() * alpha + dst.B() * oneMinusAlpha) >> 8,
  177. src.A());
  178. }
  179. static inline Color BlendAlpha(const Color& src, const Color& dst, U32 alpha) {
  180. U32 oneMinusAlpha = 0x100 - alpha;
  181. return Color((src.R() * alpha + dst.R() * oneMinusAlpha) >> 8,
  182. (src.G() * alpha + dst.G() * oneMinusAlpha) >> 8,
  183. (src.B() * alpha + dst.B() * oneMinusAlpha) >> 8,
  184. (src.A() * alpha + dst.A() * oneMinusAlpha) >> 8);
  185. }
  186. static inline Color Average(const Color & a, const Color & b) {
  187. return Color((a.R() + b.R()) / 2,
  188. (a.G() + b.G()) / 2,
  189. (a.B() + b.B()) / 2,
  190. (a.A() + b.A()) / 2);
  191. }
  192. static inline Color Average(const Color & a, const Color & b,
  193. const Color & c, const Color & d) {
  194. return Color((a.R() + b.R() + c.R() + d.R()) / 4,
  195. (a.G() + b.G() + c.G() + d.G()) / 4,
  196. (a.B() + b.B() + c.B() + d.B()) / 4,
  197. (a.A() + b.A() + c.A() + d.A()) / 4);
  198. }
  199. private:
  200. static inline U8 clamp(U16 value) {
  201. return (value > MAX) ? (U8) MAX : (U8) value;
  202. }
  203. static inline U8 mul(U8 color, U8 factor) {
  204. U16 prod = color * factor;
  205. return (prod + (prod >> 7)) >> 8;
  206. }
  207. };
  208. }
  209. #endif //ndef EGL_COLOR_H