如何从UIColor获得反色?

时间:2011-05-05 05:56:30

标签: objective-c ios uicolor inverse

e.g。黑色的反色应为白色。

10 个答案:

答案 0 :(得分:28)

的iOS5 +

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

答案 1 :(得分:23)

这应该有效:

// oldColor is the UIColor to invert
const CGFloat *componentColors = CGColorGetComponents(oldColor.CGColor);

UIColor *newColor = [[UIColor alloc] initWithRed:(1.0 - componentColors[0])
                                           green:(1.0 - componentColors[1])
                                            blue:(1.0 - componentColors[2])
                                           alpha:componentColors[3]];

来源:Check if UIColor is dark or bright?

答案 2 :(得分:23)

----编辑----

基于@ amleszk的回答,我用这种方法更新了UIColor扩展/类别:

// Swift
func inverseColor() -> UIColor {
    var alpha: CGFloat = 1.0

    var white: CGFloat = 0.0
    if self.getWhite(&white, alpha: &alpha) {
        return UIColor(white: 1.0 - white, alpha: alpha)
    }

    var hue: CGFloat = 0.0, saturation: CGFloat = 0.0, brightness: CGFloat = 0.0
    if self.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: &alpha) {
        return UIColor(hue: 1.0 - hue, saturation: 1.0 - saturation, brightness: 1.0 - brightness, alpha: alpha)
    }

    var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0
    if self.getRed(&red, green: &green, blue: &blue, alpha: &alpha) {
        return UIColor(red: 1.0 - red, green: 1.0 - green, blue: 1.0 - blue, alpha: alpha)
    }

    return self
}

// Objective-C
- (UIColor *)inverseColor {
    CGFloat alpha;

    CGFloat white;
    if ([self getWhite:&white alpha:&alpha]) {
        return [UIColor colorWithWhite:1.0 - white alpha:alpha];
    }

    CGFloat hue, saturation, brightness;
    if ([self getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha]) {
        return [UIColor colorWithHue:1.0 - hue saturation:1.0 - saturation brightness:1.0 - brightness alpha:alpha];
    }

    CGFloat red, green, blue;
    if ([self getRed:&red green:&green blue:&blue alpha:&alpha]) {
        return [UIColor colorWithRed:1.0 - red green:1.0 - green blue:1.0 - blue alpha:alpha];
    }

    return nil;
}

----已弃用----

基于@ grc的回答,我使用这种方法创建了一个UIColor类别:

- (UIColor *)inverseColor {

    CGColorRef oldCGColor = self.CGColor;

    int numberOfComponents = CGColorGetNumberOfComponents(oldCGColor);

    // can not invert - the only component is the alpha
    // e.g. self == [UIColor groupTableViewBackgroundColor]
    if (numberOfComponents == 1) {
        return [UIColor colorWithCGColor:oldCGColor];
    }

    const CGFloat *oldComponentColors = CGColorGetComponents(oldCGColor);
    CGFloat newComponentColors[numberOfComponents];

    int i = numberOfComponents - 1;
    newComponentColors[i] = oldComponentColors[i]; // alpha
    while (--i >= 0) {
        newComponentColors[i] = 1 - oldComponentColors[i];
    }

    CGColorRef newCGColor = CGColorCreate(CGColorGetColorSpace(oldCGColor), newComponentColors);
    UIColor *newColor = [UIColor colorWithCGColor:newCGColor];
    CGColorRelease(newCGColor);

    return newColor;
}

答案 3 :(得分:11)

Swift方式是扩展UIColor:

extension UIColor {
    func inverse () -> UIColor {
        var r:CGFloat = 0.0; var g:CGFloat = 0.0; var b:CGFloat = 0.0; var a:CGFloat = 0.0;
        if self.getRed(&r, green: &g, blue: &b, alpha: &a) {
            return UIColor(red: 1.0-r, green: 1.0 - g, blue: 1.0 - b, alpha: a)
        }
        return .black // Return a default colour
    }
}

答案 4 :(得分:7)

GRC的解决方案存在一个问题:CGColorGetComponents以0.0-1.0的比例返回,而不是2-255。所以你应该使用

UIColor *newColor = [[UIColor alloc] initWithRed:(1.0 - componentColors[0])
                                           green:(1.0 - componentColors[1])
                                            blue:(1.0 - componentColors[2])
                                           alpha:componentColors[3]];

代替。否则一切都将是白色的(1.0和更大)

与amleszk使用的有点相同,那里它也是1.-color,而不是255.顺便说一下1.表示浮点数1.0,你应该宁可输入1.0而不是1.,以避免混淆

答案 5 :(得分:2)

因此,为了帮助所有快捷方式来到这里寻找答案 - 这就是它应该在swift中的样子:

func inverseColor(color: UIColor) -> UIColor{
    var a: CGFloat = 0.0; var r: CGFloat = 0.0; var g: CGFloat = 0.0; var b: CGFloat = 0.0;
    color.getRed(&r, green: &g, blue: &b, alpha: &a);
    return UIColor(red: -r, green: -g, blue: -b, alpha: a);
}

答案 6 :(得分:1)

你没有得到Gray的反色。 因此,当您使用灰色背景及其反色作为文本颜色时效果

这甚至适用于灰色,我只是在@iWills代码中添加了一些额外的代码。

//====== TO GET THE OPPOSIT COLORS =====
-(UIColor *)reverseColorOf :(UIColor *)oldColor
{
    CGColorRef oldCGColor = oldColor.CGColor;

    int numberOfComponents = CGColorGetNumberOfComponents(oldCGColor);
    // can not invert - the only component is the alpha
    if (numberOfComponents == 1) {
        return [UIColor colorWithCGColor:oldCGColor];
    }

    const CGFloat *oldComponentColors = CGColorGetComponents(oldCGColor);
    CGFloat newComponentColors[numberOfComponents];

    int i = numberOfComponents - 1;
    newComponentColors[i] = oldComponentColors[i]; // alpha
    while (--i >= 0) {
        newComponentColors[i] = 1 - oldComponentColors[i];
    }

    CGColorRef newCGColor = CGColorCreate(CGColorGetColorSpace(oldCGColor), newComponentColors);
    UIColor *newColor = [UIColor colorWithCGColor:newCGColor];
    CGColorRelease(newCGColor);

    //=====For the GRAY colors 'Middle level colors'
    CGFloat white = 0;
    [oldColor getWhite:&white alpha:nil];

    if(white>0.3 && white < 0.67)
    {
        if(white >= 0.5)
            newColor = [UIColor darkGrayColor];
        else if (white < 0.5)
            newColor = [UIColor blackColor];

    }
    return newColor;
}

答案 7 :(得分:1)

我使用Dade的答案并稍微调整一下,因为我正在寻找一种很好的方法来计算给定背景颜色的文本前景色。

因此,如果您想为给定的背景颜色获得漂亮的文本颜色,我建议您这样做。它为您提供给定背景颜色的最亮颜色:

intA - intB

就像滑动RGB滑块一样,直到最亮的组件达到最大值。

示例:

extension UIColor {
    func maxBright() -> UIColor {
        var r:CGFloat = 0.0; var g:CGFloat = 0.0; var b:CGFloat = 0.0; var a:CGFloat = 0.0;
        if self.getRed(&r, green: &g, blue: &b, alpha: &a) {
            let d:CGFloat = 1.0 - max(r,g,b)
            return UIColor(red: r + d, green: g + d , blue: b + d, alpha: 1.0)

        }
        return self
    }
}

会给你一个白色的黑色标签。尝试其他颜色,你会看到有趣的结果:))

这可能不是你想要的,但它确实为文本前/后颜色提供了有趣的结果。

只是分享

答案 8 :(得分:0)

Swift 解决方案扩展了UIColor以添加计算属性inverted

extension UIColor {
    var inverted: UIColor {
        var a: CGFloat = 0.0, r: CGFloat = 0.0, g: CGFloat = 0.0, b: CGFloat = 0.0
        return getRed(&r, green: &g, blue: &b, alpha: &a) ? UIColor(red: 1.0-r, green: 1.0-g, blue: 1.0-b, alpha: a) : .black
    }
}

在任何UIColor实例(.red.blue.white等)上使用,例如:

view.backgroundColor = UIColor.blue.inverted //Results in yellow background
view.backgroundColor = UIColor.black.inverted //Results in white background

答案 9 :(得分:0)

当您使用 SwiftUI 时,您可以使用 View 的 colorInvert

.colorInvert()