2011-12-31 35 views
7

有沒有簡單的方法將UIColor轉換爲十六進制值?
或者我們必須得到與CGColorGetComponents的RGB組件,然後從那裏解決它?UIColor到十六進制(網絡色)

例如CGColorGetComponents(color.CGColor)[0] * 256

回答

7

我會考慮使用Erica Sadun的UIColor類別。它包含許多免費的功能,包括十六進制表示。使用起來非常簡單,只需將其添加到您使用的任何類頭中,或者將其添加到預編譯頭以獲得最大的靈活性。 如果您要添加到預編譯的頭,這樣做類似這樣的事情:

#ifdef __OBJC__ 
    #import <Foundation/Foundation.h> 
    #import <UIKit/UIKit.h> 
    #import "UIColor-Expanded.h" 
#endif 

那麼你可以使用它像這樣NSLog(@"%@", [myColor hexStringFromColor]);

GitHub的鏈接的UIColor類別:https://github.com/erica/uicolor-utilities

ArsTechnica有關它的文章:http://arstechnica.com/apple/guides/2009/02/iphone-development-accessing-uicolor-components.ars

+0

偉大的圖書館。感謝這一點。 – fes 2011-12-31 18:38:33

+0

沒問題。希望可以幫助你! – lewiguez 2011-12-31 19:48:43

1

據我所知,沒有任何內置解決方案。
但請注意,您應該乘以255而不是256

獲取十六進制表示法是一件容易的事情,所以您不會有很多麻煩來手動構建字符串。

NSLog(@"%X%X%X", redInteger, greenInteger, blueInteger); 
+0

我認爲這是一個聰明的做法。由於該組件返回一個浮點數,因此如何將其精確地轉換爲整數?例如對於紫色:UIDeviceRGBColorSpace 0.548638 0.554697 1 1 – fes 2011-12-31 17:40:12

+0

您將通過對結果進行投射或舍入來整數,測試RGB 0.548638 0.554697 1,截斷,舍入,每次給我*相同的*紫色。 – 2011-12-31 17:46:50

+0

你能給我一個截斷和舍入的例子嗎?也許我是四捨五入太高,但我總是得到1和%X%X%X出來作爲111 – fes 2011-12-31 18:07:11

9

我也必須將UIColor轉換爲它的十六進制組件。

正如lewiguez已經指出的那樣,在github有一個很好的類別,可以完成所有這些工作。

但是因爲我想了解它是如何完成的,我對RGB顏色做了自己的簡單實現。

+ (NSString*)colorToWeb:(UIColor*)color 
{ 
    NSString *webColor = nil; 

    // This method only works for RGB colors 
    if (color && 
     CGColorGetNumberOfComponents(color.CGColor) == 4) 
    { 
     // Get the red, green and blue components 
     const CGFloat *components = CGColorGetComponents(color.CGColor); 

     // These components range from 0.0 till 1.0 and need to be converted to 0 till 255 
     CGFloat red, green, blue; 
     red = roundf(components[0] * 255.0); 
     green = roundf(components[1] * 255.0); 
     blue = roundf(components[2] * 255.0); 

     // Convert with %02x (use 02 to always get two chars) 
     webColor = [[NSString alloc]initWithFormat:@"%02x%02x%02x", (int)red, (int)green, (int)blue]; 
    } 

    return webColor; 
} 

歡迎任何反饋!

+0

這種方法完美的工作,謝謝! – BFeher 2014-09-02 05:16:03

2

除了廣泛使用的基於字符串的解決方案之外,這裏是基於十六進制(整數)的解決方案。用法:

UIColor* color = lf_rgb(0x120aef); 
log(@"color %.6x", color.hex_rgb); 

然後你會得到「顏色120aef」。我會在https://github.com/superarts/LCategory這些代碼,也可以複製,粘貼到自己的代碼庫也:

#define lf_rgb(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] 

@interface UIColor (lc_rgb) 
- (NSInteger)hex_rgb; 
@end 

@implementation UIColor (lc_rgb) 
- (NSInteger)hex_rgb 
{ 
    CGFloat r, g, b, a; 
    BOOL result = [self getRed:&r green:&g blue:&b alpha:&a]; 
    // log(@"rgba: %f, %f, %f, %f", r * 255, g * 255, b * 255, a * 255); 
    if ((result == NO) || (a != 1.0f)) 
     return -1; 
    return 
     (NSInteger)(r * 255) * 256 * 256 + 
     (NSInteger)(g * 255) * 256 + 
     (NSInteger)(b * 255) * 1; 
} 
@end 
0

這是我做的如何與斯威夫特3做的:

extension UIColor { 
    var hex:String{ 
     get{ 
      var red:CGFloat = 0 
      var blue:CGFloat = 0 
      var green:CGFloat = 0 
      var alpha:CGFloat = 0 

      self.getRed(&red, green: &green, blue: &blue, alpha: &alpha) 
      let rgb:Int = (Int)(red*255)<<16 | (Int)(green*255)<<8 | (Int)(blue*255)<<0 
      return String.localizedStringWithFormat("#%06x", rgb) 
     } 
    } 
} 

或者你可以使其成爲一種方法。我只是喜歡color.hex外觀與color.hex()