2012-12-27 22 views
13

現在,我可以轉換一個十六進制字符串RGB顏色是這樣的:我怎樣才能得到一個十六進制字符串從的UIColor或RGB

// Input is without the # ie : white = FFFFFF 
+ (UIColor *)colorWithHexString:(NSString *)hexString 
{ 
    unsigned int hex; 
    [[NSScanner scannerWithString:hexString] scanHexInt:&hex]; 
    int r = (hex >> 16) & 0xFF; 
    int g = (hex >> 8) & 0xFF; 
    int b = (hex) & 0xFF; 

    return [UIColor colorWithRed:r/255.0f 
         green:g/255.0f 
         blue:b/255.0f 
         alpha:1.0f]; 
} 

BU我如何轉換RGB到十六進制字符串?

+0

看看這個,有一個很好的答案的http://計算器。 COM /問題/ 372 3846/convert-from-hex-color-to-rgb-struct-in-c –

回答

34

使用此方法:

- (NSString *)hexStringForColor:(UIColor *)color { 
     const CGFloat *components = CGColorGetComponents(color.CGColor); 
     CGFloat r = components[0]; 
     CGFloat g = components[1]; 
     CGFloat b = components[2]; 
     NSString *hexString=[NSString stringWithFormat:@"%02X%02X%02X", (int)(r * 255), (int)(g * 255), (int)(b * 255)]; 
     return hexString; 
} 
+3

+1,但'CGFloat * components'應該是'const CGFloat * components'並且不需要強制轉換。 – 2012-12-27 09:00:07

+0

@ H2CO3:好的。我學習可可時使用了3年的這段代碼。反正感謝'碳酸':第。我總是討厭化學,但現在我已經開始愛... –

+0

哈哈,這是一個成就:P – 2012-12-27 09:03:29

4

這是我在斯威夫特使用,請注意,這似乎很好地工作,當你把它與RGBA值創建的UIColor的代碼,但返回一些奇怪的發送結果時預先定義的顏色,如UIColor.darkGrayColor()

func hexFromUIColor(color: UIColor) -> String 
{ 
let hexString = String(format: "%02X%02X%02X", 
Int(CGColorGetComponents(color.CGColor)[0] * 255.0), 
Int(CGColorGetComponents(color.CGColor)[1] *255.0), 
Int(CGColorGetComponents(color.CGColor)[2] * 255.0)) 
return hexString 
} 
+4

您得到的奇怪結果是因爲系統定義的顏色不在RGB顏色空間中,而是在灰度顏色空間中。爲了防止崩潰或不正確的行爲,您應該檢查顏色空間匹配(CGColorGetColorSpace()),或至少如果顏色空間的組件數匹配(CGColorGetNumberOfComponents())。 – Alfonso

3

使用的UIColor的這種擴展從它那裏得到的hexstring。

extension: UIColor { 
     func toHexString() -> String { 
      var r:CGFloat = 0 
      var g:CGFloat = 0 
      var b:CGFloat = 0 
      var a:CGFloat = 0 

      getRed(&r, green: &g, blue: &b, alpha: &a) 

      let rgb:Int = (Int)(r*255)<<16 | (Int)(g*255)<<8 | (Int)(b*255)<<0 

      return String(format:"#%06x", rgb) 
     } 
    } 
+0

請在代碼中添加一些解釋,以便人們可以更好地理解它。 –

+1

您需要了解二進制操作的一些基本知識: - 他獲得0.0到1.0之間的紅色,綠色,藍色,alpha值,並乘以255(要使值介於0和255.0之間)。之後,'<<'運算符執行字節的移位操作以抵消顏色的Int值:您不需要移動藍色(因爲它已經是正確的Int值),對於需要移動的綠色因爲一個顏色值(0到255)在8個字節上,所以你需要移動8個。紅色是旁邊的g,所以你需要移動8 + 8(16)。 %06x打印十六進制值。 – Beninho85

3

Anoop答案是不正確的,如果你有[UIColor blackColor]嘗試它,它會返回綠顏色的十六進制字符串。
原因是系統是切割足以拯救存儲器它將設置

對於黑顏色
成分[0] = 0(R = 0,G = 0,B = 0),並
分量[1] = 1 (A = 1)。

對於白色
成分[0] = 1(R = 1,G = 1,B = 1),並
分量[1] = 1(A = 1)。以下類別的UIColor


使用十六進制
UIColor+Utility.h

@interface UIColor (Utility) 

/** 
Return representaiton in hex 
*/ 
-(NSString*)representInHex; 
@end 

UIColor+Utility.m

@implementation UIColor (Utility) 
-(NSString*)representInHex 
{ 
    const CGFloat *components = CGColorGetComponents(self.CGColor); 
    size_t count = CGColorGetNumberOfComponents(self.CGColor); 

    if(count == 2){ 
     return [NSString stringWithFormat:@"#%02lX%02lX%02lX", 
       lroundf(components[0] * 255.0), 
       lroundf(components[0] * 255.0), 
       lroundf(components[0] * 255.0)]; 
    }else{ 
     return [NSString stringWithFormat:@"#%02lX%02lX%02lX", 
       lroundf(components[0] * 255.0), 
       lroundf(components[1] * 255.0), 
       lroundf(components[2] * 255.0)]; 
    } 
} 
@end 

+0

你是對的(+1),Anoop只是處理基於RGBA的圖像,而你也覆蓋單色圖像。但實際上有更多的可能性:https://developer.apple.com/reference/coregraphics/cgcolorspacemodel?language=objc – vikingosegundo

相關問題