Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert hexadecimal to RGB

I want to make a conversion from hexadecimal to RGB, but the hexadecimal deal with a string like #FFFFFF. How can I do that?

like image 801
R. Dewi Avatar asked Nov 24 '10 09:11

R. Dewi


People also ask

How do you convert hexadecimal to color?

Hex color codes start with a pound sign or hashtag (#) and are followed by six letters and/or numbers. The first two letters/numbers refer to red, the next two refer to green, and the last two refer to blue. The color values are defined in values between 00 and FF (instead of from 0 to 255 in RGB).

Are RGB and hexadecimal the same?

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. HEX, along with its sister RGB, is one of the color languages used in coding, such as CSS. HEX is a numeric character based reference of RGB numbers.

How is RGB calculated?

The function R*0.2126+ G*0.7152+ B*0.0722 is said to calculate the perceived brightness (or equivalent grayscale color) for a given an RGB color. Assuming we use the interval [0,1] for all RGB values, we can calculate the following: yellow = RGB(1,1,0) => brightness=0.9278.


1 Answers

I've just expanded my UIColor Category for you.
use it like UIColor *green = [UIColor colorWithHexString:@"#00FF00"];

// //  UIColor_Categories.h // //  Created by Matthias Bauch on 24.11.10. //  Copyright 2010 Matthias Bauch. All rights reserved. //  #import <Foundation/Foundation.h>   @interface UIColor(MBCategory)   + (UIColor *)colorWithHex:(UInt32)col; + (UIColor *)colorWithHexString:(NSString *)str;  @end  // //  UIColor_Categories.m // //  Created by Matthias Bauch on 24.11.10. //  Copyright 2010 Matthias Bauch. All rights reserved. //  #import "UIColor_Categories.h"  @implementation UIColor(MBCategory)  // takes @"#123456" + (UIColor *)colorWithHexString:(NSString *)str {     const char *cStr = [str cStringUsingEncoding:NSASCIIStringEncoding];     long x = strtol(cStr+1, NULL, 16);     return [UIColor colorWithHex:x]; }  // takes 0x123456 + (UIColor *)colorWithHex:(UInt32)col {     unsigned char r, g, b;     b = col & 0xFF;     g = (col >> 8) & 0xFF;     r = (col >> 16) & 0xFF;     return [UIColor colorWithRed:(float)r/255.0f green:(float)g/255.0f blue:(float)b/255.0f alpha:1]; }  @end 
like image 76
Matthias Bauch Avatar answered Sep 21 '22 18:09

Matthias Bauch