2012-04-23 11 views
10

mam problem w przekształcaniu uicolor koloru hex, oto co znalazłemhex kolor z uicolor

CGColorRef colorref = [[Colorview_ backgroundColor] CGColor]; 

int numComponents = CGColorGetNumberOfComponents(colorref); 

if (numComponents == 4) { 
    const CGFloat *components = CGColorGetComponents(colorref); 

    int hexValue = 0xFF0000*components[0] + 0xFF00*components[1] + 0xFF*components[2]; 

    NSString *hexString = [NSString stringWithFormat:@"#%d", hexValue]; 
} 

ten kod daje mi # 5576149 (na przykład) na hexString, nas widać istnieje 7 cyfr nie 6, to nie jest szesnastkowy kolor, każda pomoc będzie doceniona, thx.

+0

Można korzystać z tej biblioteki https: // github .com/burhanuddin353/TFTColor –

Odpowiedz

2
NSString *hexString = [NSString stringWithFormat:@"#%d", hexValue]; 

Jesteś formatowania go jako cyfry z% d

chcesz sformatować go jako hex z% x% x lub - być może jako ciąg% s nie zrobił sprawdzić, co robi i funkcja co int hexValue trzyma

D lub podpisałem całkowitą dziesiętną 392

x liczba całkowita bez znaku szesnastkowy 7fa

X liczba całkowita bez znaku szesnastkowa (wielkimi literami) 7fa

+0

thx, które dają heksadecymalny kolor dla hexString, ale nie jest to ten sam kolor [Colorview_ backgroundColor], czegoś brakuje, może komponenty 0xFF * [3], Każdy pomysł jak dodać go do hexValue? –

+0

, jeśli żądany kolor to 5576149 = # 5515D5 po prostu przekształć int na heks. –

+0

NSString * hexString = [NSString stringWithFormat: @ "#% X", hexValue]; tutaj, co zrobiłem i daje mi wartość heksadecymalną (na przykład # 5515D5), ale mówię: kiedy używam tego koloru hex przez javascript, otrzymuję niewłaściwy kolor, to nie jest dokładnie to samo, co my wprowadź jeden. –

1

Próbowałem że wczoraj bo miałem dostać kolor hex na uicolor i uczynił to działa w javascript też, ale to nie działa, gdy składnik jest 0, ponieważ otrzymuje 0 zamiast 00. Tak więc czystym cyjanem byłoby RGB 0 255 255, a kod ten zwróciłby # 0ffff zamiast # 00ffff.

Zrobiłem ten kod od Ciebie, a to działa na moim app:

-(NSString*)colorToHex:(UIColor*)color{ 

    CGColorRef colorref = [color CGColor]; 

    const CGFloat *components = CGColorGetComponents(colorref); 

    NSString *hexString = @"#"; 
    int hexValue = 0; 

    for (int i=0; i<3; i++) { 
     if (components[i] == 0) { 
      hexString = [NSString stringWithFormat:@"%@00", hexString]; 
     }else{ 
      hexValue = 0xFF*components[i]; 
      hexString = [NSString stringWithFormat:@"%@%x", hexString, hexValue]; 
     } 
    } 

    return hexString; 
} 
12

Odpowiedź sylphos powyżej robi praca dla darkGrayColor.

To działa lepiej (zaczerpnięte z http://softteco.blogspot.jp/2011/06/extract-hex-rgb-color-from-uicolor.html):

- (NSString *) hexFromUIColor:(UIColor *)color { 
    if (CGColorGetNumberOfComponents(color.CGColor) < 4) { 
     const CGFloat *components = CGColorGetComponents(color.CGColor); 
     color = [UIColor colorWithRed:components[0] green:components[0] blue:components[0] alpha:components[1]]; 
    } 
    if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) { 
     return [NSString stringWithFormat:@"#FFFFFF"]; 
    } 
    return [NSString stringWithFormat:@"#%02X%02X%02X", (int)((CGColorGetComponents(color.CGColor))[0]*255.0), (int)((CGColorGetComponents(color.CGColor))[1]*255.0), (int)((CGColorGetComponents(color.CGColor))[2]*255.0)]; 
} 
1

Oto wersja, ale sformatowane jako funkcja C:

static inline NSString *hexFromUIColor(UIColor * _color) { 
    if (CGColorGetNumberOfComponents(_color.CGColor) < 4) { 
     const CGFloat *components = CGColorGetComponents(_color.CGColor); 
     _color = [UIColor colorWithRed:components[0] green:components[0] blue:components[0] alpha:components[1]]; 
    } 
    if (CGColorSpaceGetModel(CGColorGetColorSpace(_color.CGColor)) != kCGColorSpaceModelRGB) { 
     return [NSString stringWithFormat:@"#FFFFFF"]; 
    } 
    return [NSString stringWithFormat:@"#%02X%02X%02X", (int)((CGColorGetComponents(_color.CGColor))[0]*255.0), (int)((CGColorGetComponents(_color.CGColor))[1]*255.0), (int)((CGColorGetComponents(_color.CGColor))[2]*255.0)]; 
}