存档

2011年1月 的存档

Cocos2d How to use CCC4?

2011年1月17日 没有评论

Any one have any ideas on how to convert ccc3 to ccc4 or even just a macro!
I am currently using CCMotion streak and it requires me to use ccc4 but i dont know what combination makes what color!
Please any help would be appreciated!
Thanks

from ccTypes.h

typedef struct _ccColor4B
{
 GLubyte r;
 GLubyte g;
 GLubyte b;
 GLubyte a;
} ccColor4B;
//! helper macro that creates an ccColor4B type
static inline ccColor4B
ccc4(const GLubyte r, const GLubyte g, const GLubyte b, const GLubyte o)
{
 ccColor4B c = {r, g, b, o};
 return c;
}

For exampe:

ccColor4b myColor = ccc4(255, 0, 0, 255); creates a solid red color

Again from ccTypes.h

/** Returns a ccColor4F from a ccColor3B. Alpha will be 1.
 @since v0.99.1
 */
static inline ccColor4F ccc4FFromccc3B(ccColor3B c)
{ 
 return (ccColor4F){c.r/255.f, c.g/255.f, c.b/255.f, 1.f};
}

If it’s not enough for your write your own converter like the last one

EDIT:

If you have ccColor3B myColor3B and you want to have ccColor4F myColor4F simply use the converter:

myColor4F c = ccc4FFromccc3B(myColor3B);

If you want to have ccColor4B write your own converter:

static inline ccColor4B ccc4BFromccc3B(ccColor3B c)
{ 
 return (ccColor4F){c.r, c.g, c.b, 255};
}

and use it like this:

ccColor4B c = ccc4BFromccc3B(myColor);

For anyone having the same issue with CocosSharp (the Xamarin C# port of Cocos2D):

To convert a CCColor3B to a CCColor4F, you can pass in the CCColor3B as a argument to the constructor of CCColor4F. For example:

var color = new CCColor4F (CCColor3B.Red);

CCColor4B doesn’t have a constructor with CCColor3B as an argument, so you have to pass in the individual values for Red, Green, and Blue, as follows:

var sourceColor = CCColor3B.Red;    
var color = new CCColor4B (sourceColor.R. sourceColor.G, sourceColor.B);

I like to create extension methods to encapsulate the above, as follows:

public static class ColorExtensions
{
    public static CCColor4F ToColor4F(this CCColor3B sourceColor)
    {
        return new CCColor4F (sourceColor);
    }

    public static CCColor4B ToColor4B(this CCColor3B sourceColor)
    {
        return new CCColor4B (sourceColor.R, sourceColor.G, sourceColor.B);
    }
}

So you can then perform conversions using the following:

var bColor = CCColor3B.ToColor4B();
var fColor = CCColor3B.ToColor4F();
分类: stackoverflow精选, unity3d 标签: