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