RGB color to UIColor

Is there a simple way for constructing a UIColor object from an RGB value without parsing the rgb values?

Thanks

MAc Mini, Mac OS X (10.5.2)

Posted on May 5, 2008 3:55 PM

Reply
10 replies
Sort By: 

May 6, 2008 2:42 PM in response to mochasoft

Most of the time my rgb values are read from an xml document and they are specified as: 0x335bC0
for example. I will need to parse this value and convert it to r g and b values first which is really tedious.

Scott,
is there a class called "ref" ?

Thanks
Reply

May 6, 2008 6:18 PM in response to rab101

Seems like every one of Scott Squire's replies I come across is an RTFM comment. Ugh. I hope this is more helpful:

I had the same color creation irritation and made some simple utilities to help: If you want to make colors out of an int specified in hex such as 0x00ff00ff (rgba) you can use the following macro:


#define HEXCOLOR(c) [UIColor colorWithRed:((c>>24)&0xFF)/255.0 
 green:((c>>16)&0xFF)/255.0 
 blue:((c>>8)&0xFF)/255.0 
 alpha:((c)&0xFF)/255.0];
// usage:
UIColor* c = HEXCOLOR(0xff00ffff);


Then I found that CGContext<?> functions take colors as float arrays instead of using UIColors (ugh again!). So you can use this cheese here:


static float* HexToFloats(int c)
{
static float components[4];
components[0] = ((c>>24)&0xFF)/255.0;
components[1] = ((c>>16)&0xFF)/255.0;
components[2] = ((c>> 8)&0xFF)/255.0;
components[3] = ((c )&0xFF)/255.0;
return components;
}
// usage:
CGContextSetStrokeColor(c, HexToFloats(0x808080ff));
Reply

May 6, 2008 6:58 PM in response to ooo27

1. I don't have code to parse hex data nor do I have time to write it for someone.
2. If I have a clear idea of what the person wants/needs and have the code I'm fine with posting it.
In this case they actually want to take hex code data and convert into UIColor. Not the original question.
3. If you show a man to fish... in many cases there are usually answers to the questions including additional information available in the documents. Without reviewing the API or documents it's difficult for people to know which direction to go in next.
Reply

May 7, 2008 8:11 AM in response to ooo27

Thank you very much for the examples. This is exactly what I was looking for.

Scott,

Thank you for your help and suggestions.
But just for the records, I was not asking people to write code for me.
I know how to write code to parse anything. I was hopping that someone out there was already irritated by the problem and figured it out without doing any parsing.
Also, I always check the documentation, examples etc... before asking questions. I really do believe in this, because I know that everyone out there is also busy and does not have time to go out of his way to spend it solving my problem.
The whole idea is that some one out there has already faced the same problem and is willing to share his solution, that it would not cost him more than a couple of minutes to type it.

Anyway, thank you all for your help and suggestions.
Reply

Aug 9, 2008 7:59 AM in response to ooo27

Thanks for this post-- saved me some time. Here's a version that simply creates an opaque color from a six-digit hex (HTML / CSS) style color code:

#define OPAQUE_HEXCOLOR(c) [UIColor colorWithRed:((c>>16)&0xFF)/255.0 \
green:((c>>8)&0xFF)/255.0 \
blue:(c&0xFF)/255.0 \
alpha:1.0];
Reply

Apr 23, 2009 2:12 PM in response to Scott Squires1

It's really annoying when people throw "RTFM" responses immediately. I perceive these comments as "How dare you waste my time with your amateurish question?" Of course, there are cases where people constantly ask questions with obvious answers, but if you're going to go through the trouble of posting a response ANYWAY, don't bother inserting the elitist "RTFM" response. Either don't respond, or give a direct answer.
Reply

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

RGB color to UIColor

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.