i've got my function:

-(void)rgbToHSBWithR:(float)red G:(float)green B:(float)blue {

float brightness = red * 0.3 + green * 0.59 + blue * 0.11; // found in stackoverflow
NSLog(@"%f",brightness);
}

and it isn't work for me.

for example: r:84 g:67 b:73. function return 72.760002. In Photoshop brightness for this color is 33%. What's wrong?

Thanks.

有帮助吗?

解决方案

Use UIColor or NSColor:

-(void)rgbToHSBWithR:(float)red G:(float)green B:(float)blue {
    // assuming values are in 0 - 1 range, if they are byte representations, divide them by 255
    UIColor *color = [UIColor colorWithRed:red green:green blue:blue alpha:1];

    float h, s, b;
    if ([color getHue:&h saturation:&s brightness:&b alpha:NULL])
    {
        // h, s, & b now contain what you need
    }
}

其他提示

Your RGB values range from 0 to 255, not 0 to 99 -- you need to divide them first if you want to end up with a percentage:

float brightness = (red / 255.0) * 0.3 + (green / 255.0) * 0.59 + (blue / 255.0) * 0.11;

Also, there is no single conversion between "brightness" and RGB values -- Photoshop may be using a different formula than you are. If you want to know more, I recommend the "Gamma FAQ" and "Color FAQ" by Charles Poynton.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top