programing

UIColor에서 RGB 추출

instargram 2023. 9. 9. 08:52
반응형

UIColor에서 RGB 추출

전에 물어봤던 걸 봤는데 내 예제가 작동하지 않는 것 같습니다.

const CGFloat *toCol = CGColorGetComponents([[UIColor greenColor] CGColor]);

GDB로 보기엔 배열이 비어있습니다. 힌트라도 있나요?

당신이 제공한 코드 샘플이 효과가 있을 것입니다.

시도해 보기:

UIColor uicolor = [[UIColor greenColor] retain];
CGColorRef color = [uicolor CGColor];

int numComponents = CGColorGetNumberOfComponents(color);

if (numComponents == 4)
{
  const CGFloat *components = CGColorGetComponents(color);
  CGFloat red = components[0];
  CGFloat green = components[1];
  CGFloat blue = components[2];
  CGFloat alpha = components[3];
}

[uicolor release];

iOS 5에서는 다음을 사용할 수 있습니다.

UIColor *color = [UIColor orangeColor];
CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;

if ([color respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
    [color getRed:&red green:&green blue:&blue alpha:&alpha];
} else {
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    red = components[0];
    green = components[1];
    blue = components[2];
    alpha = components[3];
}

RGB 이외의 색상도 고려한 만능 솔루션(예: [UIColor blackColor])

UIColor *color = [UIColor blackColor];
CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;
// iOS 5
if ([color respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
     [color getRed:&red green:&green blue:&blue alpha:&alpha];
} else {
     // < iOS 5
     const CGFloat *components = CGColorGetComponents(color.CGColor);
     red = components[0];
     green = components[1];
     blue = components[2];
     alpha = components[3];
}

// This is a non-RGB color
if(CGColorGetNumberOfComponents(color.CGColor) == 2) {
    CGFloat hue;
    CGFloat saturation;
    CGFloat brightness;
    [color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];

}

그냥 memcpy를 사용합니다.

CGColorRef tmpColor = [[currentColorView backgroundColor] CGColor];
CGFloat newComponents[4] = {};
memcpy(newComponents, CGColorGetComponents(tmpColor), sizeof(newComponents));
// now newComponents is filled with tmpColor rgba data

윌스터의 지시에 감사드립니다.그레이스케일 색상을 사용하는 모든 사용자에게colorWithWhite:alpha:), 아래 코드 샘플을 통해 화이트 값을 알 수 있습니다(이 방법으로 생성된 색상은 HSV 방법이 작동하지 않습니다).

CGFloat red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0, white = 0.0;

// This is a non-RGB color
if(CGColorGetNumberOfComponents(self.color.CGColor) == 2) {
    [self.color getWhite:&white alpha:&alpha];
}
else {
    // iOS 5
    if ([self.color respondsToSelector:@selector(getRed:green:blue:alpha:)]) {
        [self.color getRed:&red green:&green blue:&blue alpha:&alpha];
    } else {
        // < iOS 5
        const CGFloat *components = CGColorGetComponents(self.color.CGColor);
        red = components[0];
        green = components[1];
        blue = components[2];
        alpha = components[3];
    }
}

유색 유틸리티를 확인합니다.UIColor로 이 작업 및 기타 여러 유용한 작업을 수행하기에 매우 좋은 라이브러리인 것 같습니다.예를 들어, 이 라이브러리를 사용하면 다음과 같이 쓸 수 있습니다.

CGFloat red = [myColor red];

언급URL : https://stackoverflow.com/questions/816828/extracting-rgb-from-uicolor

반응형