Now I can convert a hex string to rgb color like this:
// 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 how can I convert rgb to hex string?
there is a method called hexFromUIColor: all you need to do is call it like NSString *hexStr = [UIColor hexFromUIColor:[UIColor redColor]]; Just take the code that you need.
Converting RGBA to hex with the #rgba or #rrggbbaa notation follows virtually the same process as the opaque counterpart. Since the alpha ( a ) is normally a value between 0 and 1, we need to multiply it by 255, round the result, then convert it to hexadecimal.
HEX, or hexadecimal numbers, is a human-friendly way to communicate color to computers. There is no informational difference between RGB and HEX colors; they are simply different ways of communicating the same thing – a red, green, and blue color value.
Use this method :
- (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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With