2011-05-05 27 views

回答

21

---- ----編輯

基於@ amleszk的回答,我更新了的UIColor類別使用此方法:

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

---- DEPRECATED ----

基於@ 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; 
} 
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?

+0

謝謝你,你的答案是非常好的。但如果oldColor是[UIColor colorWithWhite:1 alpha:1],newColor將是透明的。 – iwill 2011-05-05 16:35:10

+0

完美答案。先生,謝謝你。 – Sid 2013-04-30 14:56:06

+2

這隻適用於RGB顏色。以下的答案是更好的。 – 2013-08-27 07:45:48

27

的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]; 
} 
+0

什麼是「1.」含義? – iwill 2013-01-09 09:23:10

+0

它與「1.0」(double)或「1f」(float)相同。它只是通知編譯器我們將使用float而不是整數。 – carlossless 2013-01-23 09:49:23

7

從GRC解決方案有一個問題:在0.0-1.0的比例CGColorGetComponents回報,而不是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.顏色,而不是255.順便說一句,1.代表float 1.0,你應該在1.0中輸入1.0 1.,代替以避免混淆

1

你沒有得到逆顏色灰色.. 因此,當你使用灰色回地面和它的反色文本顏色效果

這個工程即使是灰色,我只是在@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; 
} 
11

斯威夫特的方法是延長的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 
    } 
} 
+0

這不處理非rgb色彩空間,所以我會回答應該是首選。 – steipete 2015-11-29 11:18:09

+1

以灰色顯示失敗 – 2016-02-20 08:59:51

1

所以對於所有swifters有益來到這裏尋找答案 - 這是應該看起來像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); 
} 
1

我使用了Dade的答案,並調整了一下,因爲我正在尋找計算文本前景顏色給定背景顏色的好方法。

所以,如果你想給一個給定的背景顏色一個很好的文字顏色,我會建議你這樣做。它給你你給的背景顏色的明色:

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

它就像你滑動滑塊RGB直到亮分量命中最大。

例子:

titleLable.backgroundColor = UIColor.blackColor() 
titleLabel.textColor = titleLabel.backgroundColor?.maxBright() 

會給你的黑色標籤白色。嘗試其他顏色,你會看到有趣的結果:)

這可能不是你要找的,但它確實給文本前/後顏色有趣的結果。

公正地分享