Get color hex from colorWithHue - ios

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]];

Related

how do i fix when UIColor colorwithRed green blue only show blue color regardless of the RGB float value?

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];
}

How to scale colors using HSV in Objective C

If I take a photo of a white block that reads RGB of (255,255,255) and an HSV of (0,0,100).
Now I take a second photo of a purple block inside of different lighting conditions the RGB turns out to be (79, 40, 145) and an HSV of (262,72,56). Now since the fact that their values are different (different lighting conditions) in their HSV it is apparent that their RGB values cannot be accurately compared to test.
So therefore I need to use the HSV. So in order to scale "up" my HSV I need to multiply the purple value by 100/56 to bring it up to 100. Therefore the resulting color is HSV (262, 72, 100). Now. This is great and dandy - but is there a way in Objective-C to read the HSV back into RGB or convert the HSV into RGB? I cannot seem to find any good resources on the matter.
I am not sure what to read my color with but here is example code of what I am trying to accomplish:
CIColor *myColor = [CIColor colorWithRed:rn green:gn blue:bn alpha:1];
where rn,gn, and bn are the RGB of my purple.
Now what I want to do is get the HSV from that CIColor object.
hn = myColor.hue;
sn = myColor.saturation;
vn = myColor.value;
Next I want to take that HSV and scale it up to the white like such
CIColor *myNewColor = [CIColor colorWithHue:hn, saturation:sn, value:vn*(vw/vn)];
and change it back to RGB.
Sorry if this is confusing - perhaps there is a better way to go about this.
EDIT 2:
rw = [self.whiteRGBValues.red floatValue];
gw = [self.whiteRGBValues.green floatValue];
bw = [self.whiteRGBValues.blue floatValue];
hw = [self.whiteRGBValues.hue floatValue];
sw = [self.whiteRGBValues.sat floatValue];
vw = [self.whiteRGBValues.val floatValue];
NSLog([NSString stringWithFormat:#"%f,%f,%f,%f,%f,%f",rw,gw,bw,rn,gn,bn]);
float scalar = vw/vn;
UIColor *myColor = [UIColor colorWithRed:rn green:gn blue:bn alpha:1];
CGFloat myHue, mySat, myBright, myAlpha;
[myColor getHue:&myHue saturation:&mySat brightness:&myBright alpha:&myAlpha];
UIColor *scaledColor = [UIColor colorWithHue:myHue
saturation:mySat
brightness:myBright*scalar
alpha:myAlpha];
CGFloat myScaledR, myScaledG, myScaledB, myScaledAlpha;
CGFloat myScaledHue, myScaledSat, myScaledBright;
[scaledColor getRed:&myScaledR green:&myScaledG blue:&myScaledB alpha:&myScaledAlpha];
self.rgbScaledValues.red = [[NSNumber numberWithFloat:myScaledR] stringValue];
self.rgbScaledValues.green = [[NSNumber numberWithFloat:myScaledG] stringValue];
self.rgbScaledValues.blue = [[NSNumber numberWithFloat:myScaledB] stringValue];
[scaledColor getHue:&myScaledHue saturation:&myScaledSat brightness:&myScaledBright alpha:&myScaledAlpha];
self.rgbScaledValues.hue = [[NSNumber numberWithFloat:myScaledHue] stringValue];
self.rgbScaledValues.sat = [[NSNumber numberWithFloat:myScaledSat] stringValue];
self.rgbScaledValues.val = [[NSNumber numberWithFloat:myScaledBright] stringValue];
I get an error thrown with breakpoint on the line with
`UIColor *myColor = [UIColor colorWithRed:rn green:gn blue:bn alpha:1];`
You can get the HSB and RGB components by using the built-in methods. First, convert your CIColor to a UIColor.
UIColor *color = [UIColor colorWithCIColor: myColor];
CGFloat hue;
CGFloat saturation;
CGFloat brightness;
CGFloat alpha;
[color getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
// those 4 floats now hold the HSB values of this color
CGFloat red;
CGFloat green;
CGFloat blue;
CGFloat alpha2;
[color getRed:&red green:&green blue:&blue alpha:&alpha2];
// those 4 floats now hold the RGB values of this color
From there, you can now do whatever you want with these values including manipulating them to create new colors.

How to generate a random UIColor?

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

How can I get the very color with string UED give to me?

My UED tell me the color must be #595959 and #333333.
But which delegate method I should use in UIColor to get the color with strings above?
Anyone?
Thank you .
#implementation UIColor (Hex)
+ (UIColor *)colorWithHex:(NSUInteger)hex // accepts values like 0x556677
{
CGFloat colors[3];
for(int i=0; i<3; ++i) {
NSUInteger val = hex & 0xFF;
colors[i] = (CGFloat)val/255.0f;
hex >>= 8;
}
return [UIColor colorWithRed:colors[2] green:colors[1] blue:colors[0] alpha:1.0f];
}
#end

iOS SDK Interface Builder's RGB slider producing different color than UIColor withRGB

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

Resources