减轻UIColor

时间:2012-10-03 19:52:45

标签: objective-c cocoa-touch uicolor

昨天我遇到了一个需要减轻UIColor的情况,所以我创建了一个类别并添加了lighten方法。我认为直接将颜色乘以颜色的每个组成部分,但我的绿色开始变黄,所以我知道它必须更复杂。

我想出的解决方案是将sRGB转换为线性,将颜色相乘,然后转换回来。这似乎有效,但我不确定它是否“正确”。我在文档中找不到任何说明UIColor在sRGB空间中的内容。我也不是色彩科学家所以我只对所涉及的数学有基本的了解。

无论如何,这是我的代码,我要求进行一些同行评审,看看是否有人对修改UIColors有了更好的理解。

CGFloat sRGB2Linear(CGFloat x){
    CGFloat a = 0.055;
    if(x <= 0.04045){
        return x * (1.0 / 12.92);
    }else{
        return pow((x + a) * (1.0 / (1 + a)), 2.4);
    }
}

CGFloat linear2sRGB(CGFloat x){
    CGFloat a = 0.055;
    if(x <= 0.0031308){
        return x * 12.92;
    }else{
        return (1 + a) * pow(x, 1 / 2.4) - a;
    }
}

- (UIColor *)lighten:(CGFloat)value{
    const CGFloat *components = CGColorGetComponents([self CGColor]);
    CGFloat newR = (sRGB2Linear(components[0])+1)*value;
    CGFloat newG = (sRGB2Linear(components[1])+1)*value;
    CGFloat newB = (sRGB2Linear(components[2])+1)*value;
    newR = MAX(0, MIN(1, linear2sRGB(newR)));
    newG = MAX(0, MIN(1, linear2sRGB(newG)));
    newB = MAX(0, MIN(1, linear2sRGB(newB)));
    return [UIColor colorWithRed:newR green:newG blue:newB alpha:components[3]];
}

7 个答案:

答案 0 :(得分:24)

这是我的iOS5 +解决方案,可轻松改变相对亮度(使一体机变亮和变暗):

+ (UIColor*)changeBrightness:(UIColor*)color amount:(CGFloat)amount
{

    CGFloat hue, saturation, brightness, alpha;
    if ([color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]) {
        brightness += (amount-1.0);
        brightness = MAX(MIN(brightness, 1.0), 0.0);
        return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:alpha];
    }

    CGFloat white;
    if ([color getWhite:&white alpha:&alpha]) {
        white += (amount-1.0);
        white = MAX(MIN(white, 1.0), 0.0);
        return [UIColor colorWithWhite:white alpha:alpha];
    }

    return nil;
}

它被调用如下:

[self changeBrightness:someUiColor amount:1.1]

使用1.1它会使亮度增加10%; 0.9会使亮度降低10%。请注意,10%相对于纯白色(即,10%的亮度总是增加。)如果您希望百分比增加一致地使颜色变亮或变暗,则无论初始亮度如何,这都是预期的行为。< / p>

答案 1 :(得分:4)

您可以使用色调,饱和度和亮度。

使用

获取旧值
- (BOOL)getHue:(CGFloat *)hue saturation:(CGFloat *)saturation brightness:(CGFloat *)brightness alpha:(CGFloat *)alpha

然后调整亮度,并构建一个新颜色:

- (UIColor *)initWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha

请记住,您需要将指针传递给第一个函数。

答案 2 :(得分:2)

Kof在Swift的回答:

extension UIColor {
    public func changeBrightness(b: CGFloat, saturation s: CGFloat = 1.0, hue h: CGFloat = 1.0) -> UIColor? {
        var hue : CGFloat = 0.0
        var saturation : CGFloat = 0.0
        var brightness : CGFloat = 0.0
        var alpha : CGFloat = 0.0

        if self.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha) {
            brightness *= b
            brightness = max(min(brightness, 1.0), 0.0)
            saturation *= s
            saturation = max(min(saturation, 1.0), 0.0)
            hue *= h
            hue = max(min(hue, 1.0), 0.0)

            return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: alpha)
        }

        var white: CGFloat = 0.0
        if self.getWhite(&white, alpha: &alpha) {
            white += (b - 1.0)
            white = max(min(b, 1.0), 0.0)

            return UIColor(white: white, alpha: alpha)
        }

        return nil
    }
}

答案 3 :(得分:1)

与白色混合是我发现的唯一不会扭曲原始颜色的方法,例如以下Swift扩展:

extension UIColor {

    @objc func lighten(_ alpha: CGFloat) -> UIColor {
        var r: CGFloat = 0
        var g: CGFloat = 0
        var b: CGFloat = 0
        var a: CGFloat = 0
        guard self.getRed(&r, green: &g, blue: &b, alpha: &a) else { return self }
        return UIColor(red:r + (1.0-r) * alpha, green:g + (1.0-g) * alpha, blue:b + (1.0-b) * alpha, alpha: a)
    }
}

答案 4 :(得分:0)

我已经改进了ank的方法,允许修改饱和度和色调。

输入被视为百分比,因此1.0的b等于100%(无变化)。要将亮度降低30%,请将b设置为0.7。

+ (UIColor*)changeColor:(UIColor*)color brightness:(CGFloat)b saturation:(CGFloat)s hue:(CGFloat)h
{

    CGFloat hue, saturation, brightness, alpha;
    if ([color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]) {
        brightness *= b;
        brightness = MAX(MIN(brightness, 1.0), 0.0);
        saturation *= s;
        saturation = MAX(MIN(saturation, 1.0), 0.0);
        hue *= h;
        hue = MAX(MIN(hue, 1.0), 0.0);
        return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:alpha];
    }

    CGFloat white;
    if ([color getWhite:&white alpha:&alpha]) {
        white += (b-1.0);
        white = MAX(MIN(b, 1.0), 0.0);
        return [UIColor colorWithWhite:white alpha:alpha];
    }

    return nil;
}

答案 5 :(得分:0)

- (UIColor *)mixColorWithWhiteAndAlpha:(CGFloat)alpha {
    CGFloat r, g, b, a;
    if ([self getRed:&r green:&g blue:&b alpha:&a])
        return [UIColor colorWithRed:r + (1 -r) * alpha
                               green:g + (1 -g) * alpha
                                blue:b + (1 -b) * alpha
                               alpha:a];
    return nil;
}

它应该返回带有颜色的混合白色

所以你可以将alpha设置为0.2,我认为它会没问题

答案 6 :(得分:0)

- (UIColor *)changeBrightnessToPercentage:(CGFloat)percentage {
    CGFloat hue, saturation, brightness, alpha;
    if ([self getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]) {
        brightness += (percentage - 1.0);
        brightness = MAX(MIN(brightness, 1.0), 0.0);
        return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:alpha];
    }

    CGFloat white;
    if ([self getWhite:&white alpha:&alpha]) {
        white += (percentage - 1.0);
        white = MAX(MIN(white, 1.0), 0.0);
        return [UIColor colorWithWhite:white alpha:alpha];
    }

    return nil;
}

== 1.0是提供的颜色,&gt; 1.0是较浅的颜色,而&lt; 1.0是较暗的颜色。