CCControlUtils.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /****************************************************************************
  2. Copyright (c) 2012 cocos2d-x.org
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "CCControlUtils.h"
  21. NS_CC_EXT_BEGIN
  22. Sprite* ControlUtils::addSpriteToTargetWithPosAndAnchor(const char* spriteName, Node * target, Vec2 pos, Vec2 anchor)
  23. {
  24. Sprite *sprite =Sprite::createWithSpriteFrameName(spriteName);
  25. if (!sprite)
  26. return nullptr;
  27. sprite->setPosition(pos);
  28. sprite->setAnchorPoint(anchor);
  29. target->addChild(sprite);
  30. return sprite;
  31. }
  32. HSV ControlUtils::HSVfromRGB(RGBA value)
  33. {
  34. HSV out;
  35. double min, max, delta;
  36. min = value.r < value.g ? value.r : value.g;
  37. min = min < value.b ? min : value.b;
  38. max = value.r > value.g ? value.r : value.g;
  39. max = max > value.b ? max : value.b;
  40. out.v = max; // v
  41. delta = max - min;
  42. if( max > 0.0 )
  43. {
  44. out.s = (delta / max); // s
  45. } else
  46. {
  47. // r = g = b = 0 // s = 0, v is undefined
  48. out.s = 0.0;
  49. out.h = -1; // its now undefined (don't know if setting to NAN is a good idea)
  50. return out;
  51. }
  52. if( value.r >= max ) // > is bogus, just keeps compilor happy
  53. {
  54. out.h = ( value.g - value.b ) / delta; // between yellow & magenta
  55. } else
  56. {
  57. if( value.g >= max )
  58. out.h = 2.0 + ( value.b - value.r ) / delta; // between cyan & yellow
  59. else
  60. out.h = 4.0 + ( value.r - value.g ) / delta; // between magenta & cyan
  61. }
  62. out.h *= 60.0; // degrees
  63. if( out.h < 0.0 )
  64. out.h += 360.0;
  65. return out;
  66. }
  67. RGBA ControlUtils::RGBfromHSV(HSV value)
  68. {
  69. double hh, p, q, t, ff;
  70. long i;
  71. RGBA out;
  72. out.a = 1;
  73. if (value.s <= 0.0) // < is bogus, just shuts up warnings
  74. {
  75. if (std::isnan(value.h)) // value.h == NAN
  76. {
  77. out.r = value.v;
  78. out.g = value.v;
  79. out.b = value.v;
  80. return out;
  81. }
  82. // error - should never happen
  83. out.r = 0.0;
  84. out.g = 0.0;
  85. out.b = 0.0;
  86. return out;
  87. }
  88. hh = value.h;
  89. if(hh >= 360.0) hh = 0.0;
  90. hh /= 60.0;
  91. i = (long)hh;
  92. ff = hh - i;
  93. p = value.v * (1.0 - value.s);
  94. q = value.v * (1.0 - (value.s * ff));
  95. t = value.v * (1.0 - (value.s * (1.0 - ff)));
  96. switch(i)
  97. {
  98. case 0:
  99. out.r = value.v;
  100. out.g = t;
  101. out.b = p;
  102. break;
  103. case 1:
  104. out.r = q;
  105. out.g = value.v;
  106. out.b = p;
  107. break;
  108. case 2:
  109. out.r = p;
  110. out.g = value.v;
  111. out.b = t;
  112. break;
  113. case 3:
  114. out.r = p;
  115. out.g = q;
  116. out.b = value.v;
  117. break;
  118. case 4:
  119. out.r = t;
  120. out.g = p;
  121. out.b = value.v;
  122. break;
  123. case 5:
  124. default:
  125. out.r = value.v;
  126. out.g = p;
  127. out.b = q;
  128. break;
  129. }
  130. return out;
  131. }
  132. Rect ControlUtils::RectUnion(const Rect& src1, const Rect& src2)
  133. {
  134. Rect result;
  135. float x1 = MIN(src1.getMinX(), src2.getMinX());
  136. float y1 = MIN(src1.getMinY(), src2.getMinY());
  137. float x2 = MAX(src1.getMaxX(), src2.getMaxX());
  138. float y2 = MAX(src1.getMaxY(), src2.getMaxY());
  139. result.origin=Vec2(x1,y1);
  140. result.size=Size(x2-x1, y2-y1);
  141. return result;
  142. }
  143. NS_CC_EXT_END