ccTypes.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2016 Chukong Technologies Inc.
  6. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  7. http://www.cocos2d-x.org
  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. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. ****************************************************************************/
  24. #include "base/ccTypes.h"
  25. NS_CC_BEGIN
  26. const std::string STD_STRING_EMPTY("");
  27. const ssize_t CC_INVALID_INDEX = -1;
  28. /**
  29. * Color3B
  30. */
  31. Color3B::Color3B()
  32. : r(0)
  33. , g(0)
  34. , b(0)
  35. {}
  36. Color3B::Color3B(GLubyte _r, GLubyte _g, GLubyte _b)
  37. : r(_r)
  38. , g(_g)
  39. , b(_b)
  40. {}
  41. Color3B::Color3B(const Color4B& color)
  42. : r(color.r)
  43. , g(color.g)
  44. , b(color.b)
  45. {}
  46. Color3B::Color3B(const Color4F& color)
  47. : r(color.r * 255.0f)
  48. , g(color.g * 255.0f)
  49. , b(color.b * 255.0f)
  50. {}
  51. bool Color3B::operator==(const Color3B& right) const
  52. {
  53. return (r == right.r && g == right.g && b == right.b);
  54. }
  55. bool Color3B::operator==(const Color4B& right) const
  56. {
  57. return (r == right.r && g == right.g && b == right.b && 255 == right.a);
  58. }
  59. bool Color3B::operator==(const Color4F& right) const
  60. {
  61. return (right.a == 1.0f && Color4F(*this) == right);
  62. }
  63. bool Color3B::operator!=(const Color3B& right) const
  64. {
  65. return !(*this == right);
  66. }
  67. bool Color3B::operator!=(const Color4B& right) const
  68. {
  69. return !(*this == right);
  70. }
  71. bool Color3B::operator!=(const Color4F& right) const
  72. {
  73. return !(*this == right);
  74. }
  75. /**
  76. * Color4B
  77. */
  78. Color4B::Color4B()
  79. : r(0)
  80. , g(0)
  81. , b(0)
  82. , a(0)
  83. {}
  84. Color4B::Color4B(GLubyte _r, GLubyte _g, GLubyte _b, GLubyte _a)
  85. : r(_r)
  86. , g(_g)
  87. , b(_b)
  88. , a(_a)
  89. {}
  90. Color4B::Color4B(const Color3B& color, GLubyte _a)
  91. : r(color.r)
  92. , g(color.g)
  93. , b(color.b)
  94. , a(_a)
  95. {}
  96. Color4B::Color4B(const Color4F& color)
  97. : r(color.r * 255)
  98. , g(color.g * 255)
  99. , b(color.b * 255)
  100. , a(color.a * 255)
  101. {}
  102. bool Color4B::operator==(const Color4B& right) const
  103. {
  104. return (r == right.r && g == right.g && b == right.b && a == right.a);
  105. }
  106. bool Color4B::operator==(const Color3B& right) const
  107. {
  108. return (r == right.r && g == right.g && b == right.b && a == 255);
  109. }
  110. bool Color4B::operator==(const Color4F& right) const
  111. {
  112. return (*this == Color4B(right));
  113. }
  114. bool Color4B::operator!=(const Color4B& right) const
  115. {
  116. return !(*this == right);
  117. }
  118. bool Color4B::operator!=(const Color3B& right) const
  119. {
  120. return !(*this == right);
  121. }
  122. bool Color4B::operator!=(const Color4F& right) const
  123. {
  124. return !(*this == right);
  125. }
  126. /**
  127. * Color4F
  128. */
  129. Color4F::Color4F()
  130. : r(0.0f)
  131. , g(0.0f)
  132. , b(0.0f)
  133. , a(0.0f)
  134. {}
  135. Color4F::Color4F(float _r, float _g, float _b, float _a)
  136. : r(_r)
  137. , g(_g)
  138. , b(_b)
  139. , a(_a)
  140. {}
  141. Color4F::Color4F(const Color3B& color, float _a)
  142. : r(color.r / 255.0f)
  143. , g(color.g / 255.0f)
  144. , b(color.b / 255.0f)
  145. , a(_a)
  146. {}
  147. Color4F::Color4F(const Color4B& color)
  148. : r(color.r / 255.0f)
  149. , g(color.g / 255.0f)
  150. , b(color.b / 255.0f)
  151. , a(color.a / 255.0f)
  152. {}
  153. bool Color4F::operator==(const Color4F& right) const
  154. {
  155. return (r == right.r && g == right.g && b == right.b && a == right.a);
  156. }
  157. bool Color4F::operator==(const Color3B& right) const
  158. {
  159. return (a == 1.0f && Color3B(*this) == right);
  160. }
  161. bool Color4F::operator==(const Color4B& right) const
  162. {
  163. return (*this == Color4F(right));
  164. }
  165. bool Color4F::operator!=(const Color4F& right) const
  166. {
  167. return !(*this == right);
  168. }
  169. bool Color4F::operator!=(const Color3B& right) const
  170. {
  171. return !(*this == right);
  172. }
  173. bool Color4F::operator!=(const Color4B& right) const
  174. {
  175. return !(*this == right);
  176. }
  177. Color4F& operator+=(Color4F& lhs, const Color4F& rhs) {
  178. lhs.r += rhs.r;
  179. lhs.g += rhs.g;
  180. lhs.b += rhs.b;
  181. lhs.a += rhs.a;
  182. return lhs;
  183. }
  184. Color4F operator+(Color4F lhs, const Color4F& rhs) {
  185. return lhs += rhs;
  186. }
  187. Color4F& operator-=(Color4F& lhs, const Color4F& rhs) {
  188. lhs.r -= rhs.r;
  189. lhs.g -= rhs.g;
  190. lhs.b -= rhs.b;
  191. lhs.a -= rhs.a;
  192. return lhs;
  193. }
  194. Color4F operator-(Color4F lhs, const Color4F& rhs) {
  195. return lhs -= rhs;
  196. }
  197. Color4F& operator*=(Color4F& lhs, const Color4F& rhs) {
  198. lhs.r *= rhs.r;
  199. lhs.g *= rhs.g;
  200. lhs.b *= rhs.b;
  201. lhs.a *= rhs.a;
  202. return lhs;
  203. }
  204. Color4F& operator*=(Color4F& lhs, float rhs) {
  205. lhs.r *= rhs;
  206. lhs.g *= rhs;
  207. lhs.b *= rhs;
  208. lhs.a *= rhs;
  209. return lhs;
  210. }
  211. Color4F operator*(Color4F lhs, const Color4F& rhs) {
  212. return lhs *= rhs;
  213. }
  214. Color4F operator*(Color4F lhs, float rhs) {
  215. return lhs *= rhs;
  216. }
  217. Color4F& operator/=(Color4F& lhs, const Color4F& rhs) {
  218. lhs.r /= rhs.r;
  219. lhs.g /= rhs.g;
  220. lhs.b /= rhs.b;
  221. lhs.a /= rhs.a;
  222. return lhs;
  223. }
  224. Color4F& operator/=(Color4F& lhs, float rhs) {
  225. lhs.r /= rhs;
  226. lhs.g /= rhs;
  227. lhs.b /= rhs;
  228. lhs.a /= rhs;
  229. return lhs;
  230. }
  231. Color4F operator/(Color4F lhs, const Color4F& rhs) {
  232. return lhs /= rhs;
  233. }
  234. Color4F operator/(Color4F lhs, float rhs) {
  235. return lhs /= rhs;
  236. }
  237. /**
  238. * Color constants
  239. */
  240. const Color3B Color3B::WHITE (255, 255, 255);
  241. const Color3B Color3B::YELLOW (255, 255, 0);
  242. const Color3B Color3B::GREEN ( 0, 255, 0);
  243. const Color3B Color3B::BLUE ( 0, 0, 255);
  244. const Color3B Color3B::RED (255, 0, 0);
  245. const Color3B Color3B::MAGENTA(255, 0, 255);
  246. const Color3B Color3B::BLACK ( 0, 0, 0);
  247. const Color3B Color3B::ORANGE (255, 127, 0);
  248. const Color3B Color3B::GRAY (166, 166, 166);
  249. const Color4B Color4B::WHITE (255, 255, 255, 255);
  250. const Color4B Color4B::YELLOW (255, 255, 0, 255);
  251. const Color4B Color4B::GREEN ( 0, 255, 0, 255);
  252. const Color4B Color4B::BLUE ( 0, 0, 255, 255);
  253. const Color4B Color4B::RED (255, 0, 0, 255);
  254. const Color4B Color4B::MAGENTA(255, 0, 255, 255);
  255. const Color4B Color4B::BLACK ( 0, 0, 0, 255);
  256. const Color4B Color4B::ORANGE (255, 127, 0, 255);
  257. const Color4B Color4B::GRAY (166, 166, 166, 255);
  258. const Color4F Color4F::WHITE ( 1, 1, 1, 1);
  259. const Color4F Color4F::YELLOW ( 1, 1, 0, 1);
  260. const Color4F Color4F::GREEN ( 0, 1, 0, 1);
  261. const Color4F Color4F::BLUE ( 0, 0, 1, 1);
  262. const Color4F Color4F::RED ( 1, 0, 0, 1);
  263. const Color4F Color4F::MAGENTA( 1, 0, 1, 1);
  264. const Color4F Color4F::BLACK ( 0, 0, 0, 1);
  265. const Color4F Color4F::ORANGE ( 1, 0.5f, 0, 1);
  266. const Color4F Color4F::GRAY (0.65f, 0.65f, 0.65f, 1);
  267. const BlendFunc BlendFunc::DISABLE = {GL_ONE, GL_ZERO};
  268. const BlendFunc BlendFunc::ALPHA_PREMULTIPLIED = {GL_ONE, GL_ONE_MINUS_SRC_ALPHA};
  269. const BlendFunc BlendFunc::ALPHA_NON_PREMULTIPLIED = {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA};
  270. const BlendFunc BlendFunc::ADDITIVE = {GL_SRC_ALPHA, GL_ONE};
  271. NS_CC_END