CCSprite 颜色混合类似于 UIImage 上的 kCGBlendModeColor
我正在尝试在精灵图像上叠加颜色,并且对如何将颜色与精灵混合有疑问。我使用以下 updateImageTint
例程在 UIImage
上叠加颜色,输出正是我所希望的。图像保持白色,但灰色阴影饱和或被指定的覆盖颜色烧毁。参数 kCGBlendModeColor 似乎可以解决标准 UIImage 颜色混合的问题。
然而,我试图使用精灵获得相同的效果,但颜色看起来就像是在整个图像、白色区域等之上着色。我假设这是一个混合问题。是否有一组参数可以传递给 setBlendFunc 以使输出与下面的 updateImageTint 方法相匹配?
我已经尝试过这个...但没有运气
[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 coloredImg;
}
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 coloredImg;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论