[Share knowledge] More easy use of CCLayerColor

Hello there again ! :slight_smile:

I would like to share with you another code snippet for more easy managing colors.
previously EventBus

Every time we need to think which color struct we should pick it is time consuming. So I wan’t share with you how i skip this :slight_smile:

Eg. usage:

auto strap = cocos2d::CCLayerColor::create(Acme::Color::convert(color));
//...
auto strap = cocos2d::CCLayerColor::create(Acme::Color::convert(0xff00ff00));
//...
label->setColor(Acme::Color::convert(Theme::Color::black));

Simply you don’t have to think is it a ccColor3B or ccColor4F or ccColor4B

You only need those 2 files or connect them as you want :wink:

Raw color handling:

Cocos2d-x extension (wrapper)


2 Likes

Nice. Thanks. Another tip from my utilities is to use a small square texture Sprite. Create a helper factor to ::create, ->setTextureRect(winRect), and choose default anchor/position (either center,center or bottom_left, bottom_left, and return where the user can then just use ->setColor(..) on it (or obviously you could pass in color to factor createFullScreenColorLayerAsSprite(..., color) or whatever).

The purpose here is that you can then batch along with all your other sprites. Not sure if it saves anything (perf, etc), but my reasoning was to simplify the API since LayerColor is kinda an oddball.

2c

2 Likes

I think we should have this in cocos2d-x!
It is very convenient.

@zhangxm
What do you think?
I mean adding a new format for a color, the format that can take values like 0xff0000ff.

1 Like

Did you mean cocos2d::Color3B(0xff00ff00)?

I think structs like Color3B etc are very good and we shouldn’t change them. We can add simple layer obove them because 0xff00ff00 don’t have information about is it ARGB or RGBA etc.

We can introduce something like:

0xff00ff00_argb (i will prepare sample soon)