cocotutch
07-30-2009, 05:32 PM
Open up Xcode and choose a View Based Application.
http://www.ipodtouchfans.com/forums/imgcache3/e87d43f9bca91a2ce63d2da23af6d767.png?orig_url=http ://img187.imageshack.us/img187/899/picture4m.png
Go to File > New File and choose an Objective-C Class. Name this file set "ColorC" and make sure "Make ColorC.h" is ticked too.
http://www.ipodtouchfans.com/forums/imgcache3/2191190c3a7e451b3a00f6b69fbb4b9a.png?orig_url=http ://img187.imageshack.us/img187/3526/picture7egf.png
Open up ColorC.h and edit it to look like this:
http://www.ipodtouchfans.com/forums/imgcache3/e0ccd0c1ab1b6948f45b336a532ec482.png?orig_url=http ://img60.imageshack.us/img60/8998/picture5a.png
And then open up ColorC.m and edit it to look like this:
http://www.ipodtouchfans.com/forums/imgcache3/5a13e845790cccf12577d7b2f7bf71d0.png?orig_url=http ://img56.imageshack.us/img56/4846/picture6o.png
Now go to your <ProjectName>ViewController.m file.
Usage:
[self.view setBackgroundColor:[UIColor colorFromRGBIntegers:255 green:165 blue:214 alpha:1.0]]; This colour is Pale Pink.
[self.view setBackgroundColor:[UIColor colorFromRGBIntegers:204 green:119 blue:34 alpha:1.0]]; This colour is Ochre.
[self.view setBackgroundColor:[UIColor colorFromRGBIntegers:175 green:238 blue:238 alpha:1.0]]; This colour is Pale Blue.
A very easy way to make colours for this, is open Photoshop's Colour Picker, find a colour you simply adore and copy in the RGB values into Xcode for that same colour to appear on your device/simulator.
Don't forget to #import "ColorC.h" at the top of your file! But if you #import "ColorC.h" in your <ProjectName>_prefix.pch file it will auto import it for every class file in the same project :D
Enjoy your custom UIColors!
cocotutch
P.S. If you are compiling for Device, add:
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
To your <ProjectName>_Prefix.pch file in Xcode.
Chicken
07-30-2009, 05:38 PM
OR you can use this one line of code...
[UIColor colorWithRed:231 green:142 blue:234 alpha:0.5];
cocotutch
07-30-2009, 05:41 PM
True. But I have a CGColor based Ball object in my project and I had to discover another way for it to work without crashing :)
spoonforknife
07-30-2009, 05:43 PM
OR you can use this one line of code...
[UIColor colorWithRed:231 green:142 blue:234 alpha:0.5];
Unless I'm painfully wrong, any value above 1 will yield white. You must divide those values by 255, like he did in the ColorC header.
Chicken
07-30-2009, 05:45 PM
Unless I'm painfully wrong, any value above 1 will yield white. You must divide those values by 255, like he did in the ColorC header.
I just put random numbers, in there because I couldn’t be bothered searching/creating a actual colour. I was referring to the method I used more.
spoonforknife
07-30-2009, 05:46 PM
I just put random numbers, in there because I couldn’t be bothered searching/creating a actual colour. I was referring to the method I used more.
Of course. Yeah, the custom method is nice to have though, for something where you're using a lot of different colo(u)rs being copied from Photoshop or something.
Chicken
07-30-2009, 05:49 PM
Sorry I don't understand what you mean "copied from photoshop" in the end, we both put in the same types of values. I mean you could get a colour from photoshop and use it in the method I provided as well.....
spoonforknife
07-30-2009, 05:52 PM
Sorry I don't understand what you mean "copied from photoshop" in the end, we both put in the same types of values. I mean you could get a colour from photoshop and use it in the method I provided as well.....
I mean the raw RGB values out of anything else. Yes, you could divide the RGB value by 255 each time you enter a value, but having a method do it for you is easier.
SkylarEC
07-30-2009, 06:24 PM
Do you have any idea what an integer is? Because your method accepts floats, not integers. They are NOT the same thing, not even close.
Furthermore, your helper function "name" is VERY wrong. Please redo this the correct way.
If you need a CGColor, use UIColor's CGColor property. Ie, [UIColor redColor].CGColor. Also, look into CoreGraphics if you ate going to use CoreGraphics. const float *components = CGColorGetComponents(CGColorRef aColor).
cocotutch
07-30-2009, 06:38 PM
I am not understanding you Skylar. It works perfectly for me:
ball.color = [UIColor colorFromRGBIntegers:204 green:119 blue:34 alpha:1.0].CGColor;
That is how I use it. And ColorC is what I named the Header and Method files because I couldn't think of a name. This works with .CGColor, and I adapted this for general use in UIViews.
And if it didn't accept Integers, why does it work? Floats may be different, but it would have given me a big fat warning if I had made a shocking mistake.
Next time I try to contribute here, I would appreciate it if you didn't come across so condescending. A nice "Oh mate, Integers and Floats are very different, and the name of your method isn't self-explanatory. You need to make it easier to recognize and understand" would suffice.
cocotutch
spoonforknife
07-30-2009, 09:10 PM
I am not understanding you Skylar. It works perfectly for me:
ball.color = [UIColor colorFromRGBIntegers:204 green:119 blue:34 alpha:1.0].CGColor;
That is how I use it. And ColorC is what I named the Header and Method files because I couldn't think of a name. This works with .CGColor, and I adapted this for general use in UIViews.
And if it didn't accept Integers, why does it work? Floats may be different, but it would have given me a big fat warning if I had made a shocking mistake.
Next time I try to contribute here, I would appreciate it if you didn't come across so condescending. A nice "Oh mate, Integers and Floats are very different, and the name of your method isn't self-explanatory. You need to make it easier to recognize and understand" would suffice.
cocotutch
They're still floats.
http://www.ipodtouchfans.com/forums/imgcache3/922ce80c6fb935b29415e6d96ae8f439.png?orig_url=http ://img.skitch.com/20090731-pxuxubgunyeh6qx2p1g7ycm61r.png
SkylarEC
07-30-2009, 09:21 PM
I am condescending because this is in the Tutorials section. And as such, there is a standard that must be upheld. Please see the other tutorials in this section, as I come down this way on all subpar material.
Issue number 1)
Your + (UIColor)colorWithRGBIntegers:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha says that it accepts integers, but actually is designed to accept floats. In this example, it's just bad coding. Continuing with this habit, and having others who may not know better start this habit, will result in very bad consequences down the road. If you have a method that says stringFromChars: and you accept ints, for example, the chars will be casted into integers, and will give you a very different result that if you were to feed chars into the method. This would create a very problematic bug, as the end user would be feeding letters and getting garbage in return.
Related, there is major difference between ints and floats. Floats take up much more memory than a regular integer (which peaks out at non decimal digits ~+-32000). Flots go considerable higher and are far more taxing on the CPU (due to the decimals, and floating point maths). Remember that this is a mobile device, and when a method calls for integers, use integers without casting into floats.
Issue number 2)
CGFloat alphaF = alpha / 1.0; This line of code does nothing and is a waste of precious CPU cycles. It should be omitted.
Issue number 3)
Your + (CGColorRef)createRGBValue:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha; shoule actually be
CGColorRef createColorFromRGBValues(CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha)
Issue number 4)
You are seriously over complicating things. The custom color method should be
+ (UIColor)colorFromIntegersRed:(int)red green:(int)green blue:(int)blue alpha:(int)alpha) {
return [UIColor colorWithRed:red/255 green:green/255 blue:blue/255 alpha:alpha];
}
Issue number 5)
I see you creating a CGColorRef, but never releasing it with CGColorRelease(CGColorRef color);
Again, I wasn't trying to be a jerk, so much as I am trying to uphold the standards of the tutorials section.
ChriB
07-31-2009, 01:59 AM
Of course. Yeah, the custom method is nice to have though, for something where you're using a lot of different colo(u)rs being copied from Photoshop or something.
If you need that, this (http://www.panic.com/~wade/picker/) could help you.
cocotutch
07-31-2009, 08:00 PM
Okay.
Since I have no experience in this sort of stuff -- I stopped when it just worked -- how can I rewrite this:
ColorC.m:
#import "ColorC.h"
@implementation UIColor(ColorC)
// helper function
+(CGColorRef)createRGBValue:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha
{
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[4] = {red, green, blue, alpha};
CGColorRef color = CGColorCreate(colorspace, components);
CGColorSpaceRelease(colorspace);
return color;
}
// create and return the new UIColor
+(UIColor *)colorFromRGBIntegers:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha
{
CGFloat redF = red/255;
CGFloat greenF = green/255;
CGFloat blueF = blue/255;
CGFloat alphaF = alpha/1.0;
CGColorRef color = [UIColor createRGBValue:redF green:greenF blue:blueF alpha:alphaF];
return [UIColor colorWithCGColor:color];
}
@end
And ColorC.h:
#import <Foundation/Foundation.h>
@interface UIColor(ColorC)
+(UIColor *)colorFromRGBIntegers:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
+(CGColorRef)createRGBValue:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;
@end
Much neater, less memory and CPU usage and still be able to use it like so:
ball.color = [UIColor colorFromRGBIntegers:172 green:225 blue:175 alpha:1.0].CGColor;
Where it works perfectly, mind.
This goes into the Prefix Header too, do I need to change this?:
#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
I have no idea how to rewrite this for maximum performance. Can you help me do this?
Thankyou,
cocotutch
spoonforknife
08-01-2009, 08:49 PM
'Optimized' Code:
[UIColor colorWithRed:1.0 green:1.0 blue: 1.0 alpha:1.0];
SkylarEC
08-02-2009, 08:43 PM
What you put into your prefix isn't even used by anything that you have posted. I'd delete it entirely. Furthermore, the prefix is the wrong place to put that macro anyway. The prefix is exactly what it sounds like, a prefix. It will be attached to the beginnign of EVERY source code file that you have. And there is no need to have that code in every piece of source that you have. http://en.wikipedia.org/wiki/Prefix_header
Next, I would completely delete the ColorC class and remove all references from your code. It should be as if you never made it.
To get the desired effect, just simply use this:
ball.backgroundColor = [UIColor colorWithRed:redValue/255 green:greenValue/255 blue:blueValue/255 alpha:alphaValue];
That's it. Your problem will be solved, and all will be well.
NolesFans
08-03-2009, 11:41 PM
What you put into your prefix isn't even used by anything that you have posted. I'd delete it entirely. Furthermore, the prefix is the wrong place to put that macro anyway. The prefix is exactly what it sounds like, a prefix. It will be attached to the beginnign of EVERY source code file that you have. And there is no need to have that code in every piece of source that you have. http://en.wikipedia.org/wiki/Prefix_header
Next, I would completely delete the ColorC class and remove all references from your code. It should be as if you never made it.
To get the desired effect, just simply use this:
ball.backgroundColor = [UIColor colorWithRed:redValue/255 green:greenValue/255 blue:blueValue/255 alpha];
That's it. Your problem will be solved, and all will be well.
no alpha:1.0?
SkylarEC
08-04-2009, 05:08 AM
no alpha:1.0?
Good catch. I have the place for it, I just didn't add it in. It should be fixed now.
Thanks!