2012-01-23 3 views
1

CCSprite에 대해 "색상 오버레이"를 적용하고 싶습니다. 기본적으로 완전히 희석됩니다.CCSprite의 흰색 오버레이

나는

mySprite.color = ccc3(0,0,0); 

을 사용할 수 있습니다 그리고 내 스프라이트 검은 색 오버레이를 가져옵니다. 그러나

mySprite.color = ccc3(255,255,255); 

자연스럽게 내 스프라이트에 영향을 미치지 않습니다. 대신, 내 스프라이트는 원래 색상을 유지합니다.

이렇게 다른 방법으로 달성 할 수 있습니까? HTTP : // 난 정말 내 모든 애니메이션/스프라이트 등의 화이트 버전은 흰색 또는 색상을이 그레이 스케일 코드를 변환 할 수 있습니다

+1

보기이 할 여유가 없다 www.cocos2d-iphone.org/forum/topic/13597#post-79944 – eazimmerman

답변

1

http://www.cocos2d-iphone.org/forum/topic/10227#post-58662

UIImage *convertToColor(UIImage *source, ccColor3B color) 
{ 
    CGSize size = [source size]; 
    int width = size.width; 
    int height = size.height; 

    // the pixels will be painted to this array 
    uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t)); 

    // clear the pixels so any transparency is preserved 
    memset(pixels, 0, width * height * sizeof(uint32_t)); 

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    // create a context with RGBA pixels 
    CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast); 

    // paint the bitmap to our context which will fill in the pixels array 
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), source.CGImage); 

    for(int y = 0; y < height; y++) { 
     for(int x = 0; x < width; x++) { 
      uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x]; 

      // convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale 
      uint32_t gray = 0.3 * rgbaPixel[rgbaRED] + 0.59 * rgbaPixel[rgbaGREEN] + 0.11 * rgbaPixel[rgbaBLUE]; 

      // set the pixels to white 
      if (gray > 0) 
      { 
       rgbaPixel[rgbaRED] = color.r; 
       rgbaPixel[rgbaGREEN] = color.g; 
       rgbaPixel[rgbaBLUE] = color.b; 
      } 
     } 
    } 

    // create a new CGImageRef from our context with the modified pixels 
    CGImageRef image = CGBitmapContextCreateImage(context); 

    // we're done with the context, color space, and pixels 
    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 
    free(pixels); 

    // make a new UIImage to return 
    UIImage *resultUIImage = [UIImage imageWithCGImage:image]; 

    // we're done with image now too 
    CGImageRelease(image); 

    return resultUIImage; 
} 
CCSprite *colorSprite(CCSprite *sprite, ccColor3B color) 
{ 
    UIImage *image = convertSpriteToImage(sprite); 
    UIImage *white = convertToColor(image, color); 
    CCTexture2D *texture = [[CCTexture2D alloc] initWithImage:white]; 
    CCSprite *newSprite = [CCSprite spriteWithTexture:texture]; 
    return newSprite; 
} 
UIImage *convertSpriteToImage(CCSprite *sprite) 
{ 
    sprite.scale = 1.0f; 
    sprite.position = ccp(sprite.contentSize.width/2, sprite.contentSize.height/2); 

    CCRenderTexture *renderer = [CCRenderTexture renderTextureWithWidth:sprite.contentSize.width height:sprite.contentSize.height]; 
    [renderer begin]; 
    [sprite visit]; 
    [renderer end]; 

    return [UIImage imageWithData:[renderer getUIImageAsDataFromBuffer:kCCImageFormatPNG]]; 
} 
관련 문제