【问题标题】:how to calculate Brightness of RGB color?如何计算RGB颜色的亮度?
【发布时间】:2012-02-27 18:58:14
【问题描述】:

我有我的功能:

-(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);
}

它不适合我。

例如:r:84 g:67 b:73。函数返回 72.760002。在 Photoshop 中,此颜色的亮度为 33%。怎么了?

谢谢。

【问题讨论】:

  • 如果你将结果除以255.0f,它会起作用。
  • 我在调用前除以 255。这是工作

标签: objective-c rgb brightness hsb


【解决方案1】:

使用UIColorNSColor

-(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
    }
}

【讨论】:

  • 只是一个免责声明,[color getHue:&h saturation:&s brightness:&b alpha:NULL] 在 iOS 5.0 或更高版本中可用。
【解决方案2】:

您的 RGB 值范围是 0 到 255,而不是 0 到 99 - 如果您想得到一个百分比,则需要先将它们除以:

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

此外,“亮度”和 RGB 值之间没有单一的转换——Photoshop 可能使用与您不同的公式。如果您想了解更多信息,我推荐 Charles Poynton 的 "Gamma FAQ""Color FAQ"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 2020-06-09
    • 2011-09-20
    • 2010-11-07
    相关资源
    最近更新 更多