CCSprite Color Blend Similar to kCGBlendModeColor on UIImage
I am trying to overlay color on a sprite image and have questions about how to blend the color with the sprite. I've used the following updateImageTint
routine to overlay color on a UIImage
and the output is what I am hoping for. The image maintains whites but shades of gray are saturated or burned with the specified overlay color. The param kCGBlendModeColor
seems to do the trick for standard UIImage
color blending.
However, I am trying to get the same effect using a sprite, but the color looks simply like it's coloring on top of the entire image, white areas and all. I'm assuming this is a blend issue. Is there a set of parameters I can pass to the setBlendFunc to get the output to match the updateImageTint
method below?
I've tried this...but with no luck
[spriteToAdjust setBlendFunc: (ccBlendFunc) { GL_ONE, GL_ONE }];
[spriteToAdjust setColor:ccc3(red*255.0,green*255.0,blue*255.0)];
-(UIImage*) updateImageTint:(UIImage*) img toColor:(UIColor*) toColor
{
// begin a new image context, to draw our colored image onto
UIGraphicsBeginImageContext(img.size);
// get a reference to that context we created
CGContextRef context = UIGraphicsGetCurrentContext();
// set the fill color
UIColor *color = toColor;
[color setFill];
// translate/flip the graphics context (for transforming from CG* coords to UI* coords
CGContextTranslateCTM(context, 0, img.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
// set the blend mode to color burn, and the original image
CGContextSetBlendMode(context, kCGBlendModeColor);
CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
CGContextDrawImage(context, rect, img.CGImage);
// set a mask that matches the shape of the image, then draw (color burn) a colored rectangle
CGContextClipToMask(context, rect, img.CGImage);
CGContextAddRect(context, rect);
CGContextDrawPath(context,kCGPathFill);
// generate a new UIImage from the graphics context we drew onto
UIImage *coloredImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//return the color-burned image
return colored开发者_如何转开发Img;
}
精彩评论