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 /** 32 * An RGBA color class, its value present as percent 33 * @param {Number} r 34 * @param {Number} g 35 * @param {Number} b 36 * @param {Number} a 37 * @constructor 38 */ 39 cc.RGBA = function(r,g,b,a){ 40 this.r = r ; // percent 41 this.g = g ; // percent 42 this.b = b ; // percent 43 this.a = a ; // percent 44 }; 45 46 cc.HSV = function(h,s,v){ 47 this.h = h ; // angle in degrees 48 this.s = s ; // percent 49 this.v = v ; // percent 50 }; 51 52 cc.ControlUtils = {}; 53 54 cc.ControlUtils.addSpriteToTargetWithPosAndAnchor = function(spriteName,target,pos,anchor){ 55 var sprite =cc.Sprite.create("#" + spriteName); 56 57 if (!sprite) 58 return null; 59 60 sprite.setPosition(pos); 61 sprite.setAnchorPoint(anchor); 62 target.addChild(sprite); 63 return sprite; 64 }; 65 66 cc.ControlUtils.HSVfromRGB = function(rgbaValue){ 67 var out = new cc.HSV(); 68 var min, max, delta; 69 70 min = rgbaValue.r < rgbaValue.g ? rgbaValue.r : rgbaValue.g; 71 min = min < rgbaValue.b ? min : rgbaValue.b; 72 73 max = rgbaValue.r > rgbaValue.g ? rgbaValue.r : rgbaValue.g; 74 max = max > rgbaValue.b ? max : rgbaValue.b; 75 76 out.v = max; // v 77 delta = max - min; 78 if( max > 0.0 ){ 79 out.s = (delta / max); // s 80 } else { 81 // r = g = b = 0 // s = 0, v is undefined 82 out.s = 0.0; 83 out.h = -1; // its now undefined (don't know if setting to NAN is a good idea) 84 return out; 85 } 86 87 if( rgbaValue.r >= max ){ // > is bogus, just keeps compilor happy 88 out.h = ( rgbaValue.g - rgbaValue.b ) / delta; // between yellow & magenta 89 } else { 90 if( rgbaValue.g >= max ) 91 out.h = 2.0 + ( rgbaValue.b - rgbaValue.r ) / delta; // between cyan & yellow 92 else 93 out.h = 4.0 + ( rgbaValue.r - rgbaValue.g ) / delta; // between magenta & cyan 94 } 95 96 out.h *= 60.0; // degrees 97 98 if( out.h < 0.0 ) 99 out.h += 360.0; 100 101 return out; 102 }; 103 104 cc.ControlUtils.RGBfromHSV = function(hsvValue){ 105 var hh, p, q, t, ff; 106 var i; 107 var out = new cc.RGBA(); 108 out.a = 1; 109 110 if (hsvValue.s <= 0.0){ // < is bogus, just shuts up warnings 111 112 if (!hsvValue.h){ // value.h == NAN 113 out.r = hsvValue.v; 114 out.g = hsvValue.v; 115 out.b = hsvValue.v; 116 return out; 117 } 118 119 // error - should never happen 120 out.r = 0.0; 121 out.g = 0.0; 122 out.b = 0.0; 123 return out; 124 } 125 126 hh = hsvValue.h; 127 if(hh >= 360.0) 128 hh = 0.0; 129 hh /= 60.0; 130 131 i = 0 | hh; 132 ff = hh - i; 133 p = hsvValue.v * (1.0 - hsvValue.s); 134 q = hsvValue.v * (1.0 - (hsvValue.s * ff)); 135 t = hsvValue.v * (1.0 - (hsvValue.s * (1.0 - ff))); 136 137 switch(i) { 138 case 0: 139 out.r = hsvValue.v; 140 out.g = t; 141 out.b = p; 142 break; 143 case 1: 144 out.r = q; 145 out.g = hsvValue.v; 146 out.b = p; 147 break; 148 case 2: 149 out.r = p; 150 out.g = hsvValue.v; 151 out.b = t; 152 break; 153 154 case 3: 155 out.r = p; 156 out.g = q; 157 out.b = hsvValue.v; 158 break; 159 case 4: 160 out.r = t; 161 out.g = p; 162 out.b = hsvValue.v; 163 break; 164 default: 165 out.r = hsvValue.v; 166 out.g = p; 167 out.b = q; 168 break; 169 } 170 return out; 171 }; 172 173 cc.ControlUtils.CCRectUnion = function(rect1, rect2){ 174 return cc.rectUnion(rect1,rect2); 175 };