I'm trying to generate a random colour from 8 options. All of the stack overflow posts / tutorials I've found have been ANY random colour. In my prefix.pch I defined 8 different sets of colour definitions this is a single example:
#define cola1 209/255.
#define colb1 0/255.
#define colc1 0/255.
#define cold1 1.0/255.
Defining different colour values for cola1-8, colb1-8, colc1-8, and cold1-8.
Then I set up a random number generator:
int randomNumber;
randomNumber = arc4random() %8;
randomNumber = randomNumber + 1;
whatRandomNumberIs = randomNumber;
I then tried setting up an [NSString stringWithFormat:#"cola%i", randomNumber]; inside the [UIColor colorWithRed etc]
like this:
[UIColor colorWithRed:[NSString stringWithFormat:#"cola%i", whatRandomNumberIs] green:[NSString stringWithFormat:#"colb%i", whatRandomNumberIs] blue:[NSString stringWithFormat:#"colc%i", whatRandomNumberIs] alpha:[NSString stringWithFormat:#"cold%i", whatRandomNumberIs]];
But then realised you cannot put an NSString in a CGFloat.
So now I'm stuck. How would I go about installing a random number from 1-8 inside the red, green, blue and alpha values without doing an NSString stringWithFormat? Is there another way to return a random UIColor value that is defined because I only want it to be specific colours??
Below is what you can do...
In prefix.pch you have as below.
#define colorCombination1 [UIColor colorWithRed:.... alpha:1.0];
#define colorCombination2 [UIColor colorWithRed:.... alpha:1.0];
#define colorCombination3 [UIColor colorWithRed:.... alpha:1.0];
#define colorCombination4 [UIColor colorWithRed:.... alpha:1.0];
#define colorCombination5 [UIColor colorWithRed:.... alpha:1.0];
#define colorCombination6 [UIColor colorWithRed:.... alpha:1.0];
#define colorCombination7 [UIColor colorWithRed:.... alpha:1.0];
#define colorCombination8 [UIColor colorWithRed:.... alpha:1.0];
Now you create array of this colors..
NSArray *myColorArray = [[NSArray alloc] initWithObjects:colorCombination1, colorCombination2, colorCombination3, colorCombination4, colorCombination5, colorCombination6, colorCombination7, colorCombination8, nil];
Now you get random number say variable as generatedRandomNumber.
UIColor *myRandomColor = [myColorArray objectAtIndex:generatedRandomNumber%8];
generatedRandomNumber%8 will give you remainder from the generatedRandomNumber.
Hope this is what you want.
Way you can get random color is by using hue , saturation and brightness
//random color
CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0
CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white
CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black
UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
You are trying to construct a string at runtime and then use it as the name of a macro that was defined at compile-time. That doesn't work. No information about the name of a compile-time macro is available at runtime.
Here is one correct way to choose a random color from a set defined at compile time. Define a method to return a random color, in a category on UIColor:
#interface UIColor (Liam_RandomColor)
+ (UIColor *)Liam_randomColor;
#end
Implement the method to first (one time only) initialize an array of the predefined colors, and second (every time) to return an element of the array at random:
#implementation UIColor (Liam_RandomColor)
+ (UIColor *)Liam_randomColor {
static dispatch_once_t onceToken;
static NSArray *colors;
dispatch_once(&onceToken, ^{
colors = #[
[UIColor colorWithRed:209/255.0 green:0 blue:0 alpha:1/255.0],
[UIColor colorWithRed:50/255.0 green:100/255.0 blue:100/255.0 alpha:1],
// etc.
];
});
return colors[arc4random_uniform(colors.count)];
}
#end
You could create a category on UIColor and wrap your predefined colors in a method, something similar to this:
#interface UIColor (myCategory)
+ (UIColor *)randomColorForInt(int n);
#end
#implementation
+ (UIColor *)randomColorForInt(int n) {
if (n == 0) {
return [UIColor colorWithRed:cola1 green:colb1 blue:colc1 alpha:cold1]];
}
...
}
#end
Related
I can get color hex from RGB.
Now, is there any way to get color hex from colorWithHue?
return [UIColor colorWithHue:180.0f / 360.0f saturation:0.02f brightness:0.85f alpha:0.8f];
The following link does a good job showing you how to! It also has several code snippets! :-) You can also use the calculator on the same website to check if your calculations are correct!
The Link
You can use this method too i guess: How can I get a hex string from UIColor or from rgb
- (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;
}
UIColor *color = [UIColor colorWithHue:180.0f / 360.0f saturation:0.02f brightness:0.85f alpha:0.8f];
return [self hexStringForColor:color];
colorWithHue method of UIColor class returns an UIColor object. You can then use the following category to get the hex code.
https://github.com/burhanuddin353/TFTColor
[UIColor hexStringForColor:[UIColor colorWithHue:180/360.0f saturation:0.02f brightness:0.85f alpha:0.8f]];
Im trying to save color values in array where the color ranges from light red color to total red color .
float INCREMENT = 0.05;
for (float greenblue = 0.80; greenblue > 0; greenblue -= INCREMENT) {
UIColor *Redcolor = [UIColor colorWithRed:1.00
green:greenblue
blue:greenblue
alpha:1.0];
[self.RedColors addObject:Redcolor];
}
This is how I save the UIColors in the array.
The problem comes in when I try to use these color values for setTintColor.
Try this.
UIColor *colorVal = (UIColor*) [self.RedColors objectAtIndex:arrayIndex];
[view setTintColor:colorVal];
Hope it helps..
For UIColor in objective C, don't forget to divide it by 255.0 as below:-
float INCREMENT = 0.05/255.0f;
for (float greenblue = 0.80/255.0f; greenblue > 0/255.0f; greenblue -= INCREMENT) {
UIColor *Redcolor = [UIColor colorWithRed:1.00/255.0f
green:greenblue
blue:greenblue
alpha:1.0];
[self.RedColors addObject:Redcolor];
}
I want to have 30+ constant UIColors so I can easily access them in my app. I'd like to be able to do something like this:
[self setBackgroundColor:[UIColor skyColor]];
[self setBackgroundColor:[UIColor dirtColor]];
[self setBackgroundColor:[UIColor yankeesColor]];
How can I do this?
Thanks!!
Define a category for UIColor:
In UIColor+MyColors.h:
#interface UIColor (MyColors)
+ (UIColor *)skyColor;
+ (UIColor *)dirtColor;
// and the rest of them
#end
In UIColor+MyColors.m:
#implementation UIColor (MyColors)
+ (UIColor *)skyColor {
static UIColor color = nil;
if (!color) {
// replace r, g, and b with the proper values
color = [UIColor colorWithRed:r green:g blue:b alpha:1];
}
return color;
}
+ (UIColor *)dirtColor {
static UIColor color = nil;
if (!color) {
// replace r, g, and b with the proper values
color = [UIColor colorWithRed:r green:g blue:b alpha:1];
}
return color;
}
// and the rest
#end
Edit:
As Martin R points out, a more modern approach to initializing the static color variable would be:
+ (UIColor *)skyColor {
static UIColor color = nil;
static dispatch_once_t predicate = 0;
dispatch_once(&predicate, ^{
// replace r, g, and b with the proper values
color = [UIColor colorWithRed:r green:g blue:b alpha:1];
});
return color;
}
This may actually be overkill in this case since there is no bad side-effect if two threads happen to initialize the nil static variable at the same time using the original code. But it is a better habit to use dispatch_once.
You can add lines like this:
#define kFirstColor [UIColor whiteColor]
#define kSecondColor [UIColor colorWithRed:100.0/255 green:100.0/255 blue:100.0/255 alpha:1.0]
At the beginning of a class or add a Color.h header to your project and import it when needed.
#import "Color.h"
Then you can use your custom colors this way:
self.view.backgroundColor = kSecondColor;
In interface builder I changed the color of a UILabel to this screenshot, having Red 255, Green 159, Blue 0 and Opacity at 100%. which gives an orange color.
I programmatically change the UILabel color than change it back to the original color using this...
timeLabel.textColor = [UIColor colorWithRed:255.0 green:159.0 blue:0.0 alpha:1.0];
and it gives this color....
I thought it should be the same, does anyone know what I'm doing wrong?
please help, thanks.
EDIT: Two years later, this post still get some karma and comments. Fixed my old answer to a better one.
The most sensible, and reusable way to add a function which can take input between 0 and 255 for UIColor, is to create a custom category. Easier to read, easier to debug, easier for other people to contribute to, and keeps the project clean and structured as it grows beyond just a view viewcontrollers. So, add the following files, and import them in your m-files whereever you need them
UIColor+Extra.h
#interface UIColor (Extra)
+ (UIColor *)colorWithR:(uint)red G:(uint)green B:(uint)blue A:(uint) alpha
+ (UIColor *) randomColor;
+ (UIColor *) colorWithHex:(uint) hex;
#end
UIColor+Extra.m
#import "UIColor+Extra.h"
#implementation UIColor (Extra)
+ (UIColor *)colorWithR:(uint)red G:(uint)green B:(uint)blue A:(uint) alpha
{
return [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/100.f];
}
+ (UIColor *) randomColor
{
CGFloat red = (CGFloat)random()/(CGFloat)RAND_MAX;
CGFloat blue = (CGFloat)random()/(CGFloat)RAND_MAX;
CGFloat green = (CGFloat)random()/(CGFloat)RAND_MAX;
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
}
+ (UIColor *) colorWithHex:(uint) hex
{
int red, green, blue, alpha;
blue = hex & 0x000000FF;
green = ((hex & 0x0000FF00) >> 8);
red = ((hex & 0x00FF0000) >> 16);
alpha = ((hex & 0xFF000000) >> 24);
return [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.f];
}
#end
timeLabel.textColor = [UIColor colorWithRed:255/255.0f green:159.0/255.0f blue:0.0/255.0f alpha:1.0];
A very easy way to reproduce the colors in the crayon palette of Xcode is to use this link https://github.com/rob-brown/RBCategories/blob/master/UIColor+RBExtras.m
it allows for crayons colors like this.... UIColor *ThisColor = [UIColor blueberryCrayonColor];
Try setting to "Device RGB" -- This worked for me
Debugger show some value in color . Please what am I doing wrong
#synthesize textLabel;
#synthesize textField;
#synthesize sliderRed;
#synthesize sliderGreen;
#synthesize sliderBlue;
- (void)dealloc
{
[textLabel release];
[super dealloc];
}
- (IBAction)updateLabel
{
NSString * textValue = [textField text];
float red = [sliderRed value]/255.f;
float green = [sliderGreen value]/255.f;
float blue = [sliderBlue value]/255.f;
UIColor *textColour = [[UIColor alloc]initWithRed:red green:green blue:blue alpha:1.0];
[textLabel setText:textValue];
[textLabel setTextColor:textColour];
}
I guess sliderRed, sliderGreen and sliderBlue are UISlider instances? What are their min/max values? If you left it at default 0.0 to 1.0 then this code would give you some really low values:
float red = [sliderRed value]/255.f;
float green = [sliderGreen value]/255.f;
float blue = [sliderBlue value]/255.f;
UIColor's method you use takes float parameters from 0.0 to 1.0 so simply passing it the slider values without dividing them would work.
And don't forget to put [textColour release]; at the end of that method or you will be leaking a new UIColor instance every time the method gets called.
Nothing wrong on your codes. maybe something wrong in other places.
Try to replace this line:
UIColor *textColour = [[UIColor alloc]initWithRed:red green:green blue:blue alpha:1.0];
to
UIColor *textColour = [UIColor redColor];
And check if the redColor works. if not, so it might something wrong in your other codes.
Try adding [textLabel setNeeedsDisplay] after you've changed the drawing attributes.