HTML color values are often used in Internet apps to convert uicolor, such as #FF9900, 0xff9900, and other color strings, which can be converted to Uicolor objects by the following methods.
+ (Uicolor *) colorwithhexstring: (NSString *) Stringtoconvert
{
NSString *cstring = [[Stringtoconvert stringbytrimmingcharactersinset:[nscharacterset Whitespaceandnewlinecharacterset]] uppercasestring];
String should be 6 or 8 characters
if ([cString length] < 6) return default_void_color;
Strip 0X if it appears
if ([cString hasprefix:@ "0X"]) cString = [cString substringfromindex:2];
f ([CString hasprefix:@ "#"]) cString = [cString substringfromindex:1];
if ([cString length]! = 6) return default_void_color;
Separate into R, G, B substrings
Nsrange range;
range.location = 0;
Range.length = 2;
NSString *rstring = [cString substringwithrange:range];
Range.location = 2;
NSString *gstring = [cString substringwithrange:range];
Range.location = 4;
NSString *bstring = [cString substringwithrange:range];
Scan values
unsigned int r, G, B;
[[Nsscanner scannerwithstring:rstring] scanhexint:&r];
[[Nsscanner scannerwithstring:gstring] scanhexint:&g];
[[Nsscanner scannerwithstring:bstring] scanhexint:&b];
return [Uicolor colorwithred: ((float) r/255.0f)
Green: ((float) g/255.0f)
Blue: ((float) b/255.0f)
ALPHA:1.0F];
}
16 binary color (HTML color value) string to Uicolor