iOS中的RGB转HSB



- (void)rgb:(UIColor *)_color
{
    CGFloat R, G, B;
    
    // 返回的字符串
    NSString *returnRGBStr;
    
    CGColorRef color = [_color CGColor];
    int numComponents = CGColorGetNumberOfComponents(color);
    
    const CGFloat *components = CGColorGetComponents(color);
    R = components[0];
    G = components[1];
    B = components[2];
    
    /*
     int r = R*255;
     int g = G*255;
     int b = B*255;
     */
    
    UIColor *testColor = [UIColor colorWithRed:R green:G blue:B alpha:1.00];
    
    CGFloat hue;
    CGFloat saturation;
    CGFloat brightness;
    CGFloat alpha;
    BOOL success = [testColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
    NSLog(@"success======>: %i hue: %0.2f, saturation: %0.2f, brightness: %0.2f, alpha: %0.2f", success, hue, saturation, brightness, alpha);
    
    returnRGBStr = [NSString stringWithFormat:@"%d,%d,%d",(int)(hue * 360),(int)(saturation * 100),(int)(brightness * 100)];

    NSLog(@"returnRGBStr:%@",returnRGBStr);
    
    if (self.delegate )
    {
        [self.delegate rgbValColor:returnRGBStr];
    }  
}

你可能感兴趣的:(iOS中的RGB转HSB)