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