How to invert color of sprite?

How to invert color of sprite?
0.0 0


I’m not sure if it has to do with the setBlendFunc() method, but I want to change the colors of a sprite to be the opposite of the original color, so that the reds turn to blues and such. Please help and thank you.


using this site, it seems that if i create a white texture and place it over my sprite and then set the blend function of the texture to:

then i will get the effect I want. but is there a way to do this without an extra sprite?

when the sprite is first created, cocos2d sets the blend function to:
src = GL_ONE


I could be wrong but this might require the use of shaders. The test.cpp shows how to use them but it’s not too clear. I am currently reading up on shaders in opengl es 2.0 but I can’t find a reference on how to “call” them in cocos2d-x, other than the test.cpp.


24 hours later.
This site, although for the original cocos2d really helped me understand shaders. This along with the shaders test in TestCPP served this purpose even better than the OpenGl es 2.0 programming guide textbook, at least for a simple case like this. I still have some questions as I am not entirely sure what is going on here, even though it works.

Sprite.cpp (excerpt)
//here I have a sprite, where at a certain point should change to a negative color
CCGLProgram shader = new CCGLProgram;
//a vertex and a fragment shader , both used to modify pixels
//stored in Resources/shaders
//OPENGL VARIABLES, referred to as a_position, a_texcoords and a_color in the shaders
//all three may not be necessary in this case
//i suppose that this makes sure that the variables in the vertex shader transfer to the pixel shader
//gets called for each vertex, being a 2d sprite it is only used for the corners of the texture
//automatically passed in from the program or buffer or whatever
//some of these variables/attributes may not be needed
attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;
//varying variables get sent to the pixel shader
//mediump for precision i guess, there is also high and low based on quality/performance
#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
varying mediump vec2 v_texCoord;
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
void main
//CCMVP is the cocos2d transform
gl_Position = CC_MVPMatrix
//send to the pixel shader in this variable
v_fragmentColor = a_color;
v_texCoord = a_texCoord;

//the pixel shader, gets called for each pixel
//low precision
#ifdef GL_ES
precision lowp float;

//note that its the same name in the vertex shader
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;

//i dont even “bind” a texture so this is either NULL
//or its the sprite that I am using it for
uniform sampler2D CC_Texture0;

void main()
vec4 originalColor = texture2D(CC_Texture0, v_texCoord);

//i am confused as to why I need to do this check and discard
//without this part, the shader will turn the alpha portions of my texture to a nontransparent white
if(originalColor.a == 0.0)

//negate the rgb values, gl_FragColor is the color of the pixel
gl_FragColor = vec4(1.0 - originalColor.r, 1.0 - originalColor.g, 1.0 - originalColor.b, originalColor.a);
also note that these were created using the files ccShader_PositionTextureColor_frag.h/vert.h found in cocos2d-x-2.1.4/cocos2dx/shaders
If anyone has some input on anything in this thread I would really appreciate it.


I might be wrong but since its premultiplied alpha the alpha doesn’t change the transparency of the color. It is used afterwards in blending.

With an alpha of 0 you will have 0,0,0,0 as an original color, because its premultiplied.
That means you will be making your color 1,1,1,0 and that would translate to turn every pixel white.