Just a quick question...
In interface builder, you have several colours that you can assign to objects.
I would like to set the headers of my tableview to the Cayenne colour but how can I call this colour programmatically ?
Any idea ?
Thanks,
Mike
Actually, I have found a way to do it by using Digital Color Meter which is a too you can find in the Utilities Folder in your Application Folder on MacOsx.
You pick the color with the tool, that gives you an RGB value and then you divide the numbers by 100. For the Cayenne, color, the tool values were 49.8 0 0, and so the equivalent is :
[UIColor colorWithRed:.498 green:0 blue:.0 alpha:1]
Hope it will help others.
Miky Mike
What MikyMike had is close, but you need to divide by 255, not by 100. The accepted answer in Using [UIColor colorWithRed:green:blue:alpha:] doesn't work with UITableView seperatorColor? shows a good way to do this.
Related
I created a UIView with a set background color. Lets say RGB value 185, 45, 42. For some reason, when I take a screenshot of this view, it is not that color. It is a little bit darker. Is there a reason why UIView would do this?
The UIView background color is set in interface builder like this:
When I run it on the simulator and take a screenshot and use the eye drop tool to determine the color, the numbers that show up are a little bit darker than what I entered. Same with a button.
I have other screens with the same red color and the screenshot I take of those, the red actually comes out correctly. I've been trying to determine what the difference is between those screens, but so far have not seen any. So I was just wondering if anyone would have any knowledge of anything that "could" case such a color change.
A common mistake when setting a color numerically in Interface Builder is to neglect the color space:
Different color spaces will give different colors (visually) for the same RGB values.
When you set RGB color, you should notice that all the three color values range from 0 to 1, so give them a value greater than 1 would never work. Try this:
RGB(185 / 255.0, 45 / 255.0, 42 / 255.0)
I have a single view app. I define two UIView instances inside that single view. I want both to have same color.
For first view I define it in Interface Builder and pick a color. I want #999999 (grey). New color picker has possibility to enter hex color values so it is easy to enter: 999999.
For the second view, I define it by setting background color of second view. I set same #999999 color using [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:1].
Simple calculation: #99 -> 153 -> 153/255 == 0.6. So far good.
However resulting colors are different. Why? This feels like a bug in xcode. After debugging resulting colors:
Color defined in IB: UIDeviceRGBColorSpace 0.529648 0.529632 0.529641 1
Color defined in code:UIDeviceRGBColorSpace 0.6 0.6 0.6 1
UPDATE:
I know about "duplicates" to this question. They have one thing in common. They properly identified the problem but failed miserably to solve it. It tested developer color pickers from Panic and Skala. They have same problem. And as insult to injury they offer copy color as hex values. Guest what, numbers there are not affected by color profile at all. So for example color #999 they provide exactly 0.6 for each color component. Unfortunately, if color is used directly it has color profile attached and resulting color is different. Only way to get exact RBG color is to use "safe web colors" where resulting color is with generic RGB color profile, in other words not affected. Problem is that safe web colors doesn't contain all colors our designers use.
Here is a category on UIColor that can create a UIColor from a hex string if it will help.
+ (UIColor *)rz_colorFromHex:(uint32_t)hexLiteral
{
uint8_t r = (uint8_t)(hexLiteral >> 16);
uint8_t g = (uint8_t)(hexLiteral >> 8);
uint8_t b = (uint8_t)hexLiteral;
return [self rz_colorFrom8BitRed:r green:g blue:b];
}
+ (UIColor *)rz_colorFromHexString:(NSString *)string
{
NSParameterAssert(string);
if ( string == nil ) {
return nil;
}
unsigned int hexInteger = 0;
NSScanner *scanner = [NSScanner scannerWithString:string];
[scanner setCharactersToBeSkipped:[NSCharacterSet characterSetWithCharactersInString:#"x#"]];
[scanner scanHexInt:&hexInteger];
return [self rz_colorFromHex:hexInteger];
}
As to why your colors are different. I would set a breakpoint and see that the colors are in fact the same while it is running. You can even print out the rgb channels for each of the colors to make sure they aren't different.
I would be surprised if it is an Xcode bug, but let me know what happens when you inspect the colors from the debugger.
EDIT:
So looking into this more there are different color spaces that you can use for RGB. By default interface builder is using a different one that iOS uses. To switch them click the little gear when you are using the color picker and set it to Generic RGB.
You can do it in such way
And code for another view:
self.testView.backgroundColor = [UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0];
UPDATE:
In Xcode, click the colorspace popup in the color picker and choose "Generic RGB", then enter the red, green and blue values from Photoshop, NOT THE HEX VALUE
And we can find so many duplicates:
Wrong color in Interface Builder's color picker
Weird colors in XCode Interface Builder?
Wrong color in Interface Builder
Xcode 6 beta color picker issue
...
This whole issue is fixed by Apple in El Capitan. So if you are using xcode 7 on El Capitan this is no longer an issue. Entering #hex color value doesn't change color space anymore.
I have a tiny simple problem with the function
[UIColor colorWithHue: saturation: brightness: alpha: ]
I want to create a simple green color but with a bit less brightness like I already tested on "This" site. This site told me I have to use the RGB-color(163, 255, 133) with a brightness of 76% or to say it in "HSL" it would has the value 105 (Saturation: 100%). So I thought okay easy doing lets code:
UIColor *green = [[UIColor alloc] initWithHue:105/360 saturation:1 brightness:0.76 alpha:1];
But when I test this line of code and debug it my green is a wine-red. The debug-session says that my UIColor RGBA-code is (0.76, 0, 0, 1)?!
HSV, HSL, HSI and HSB are not interchangeable. To make matters worse, I believe that in some cases, there are different implementations of each representation that of course yield different results with converted to/from RGB, and, worse still, sometimes people think they are using HSL when in fact they are using HSV or some other such combo. So your best bet is to just keep trying until it works.
In your specific case, you need to find a way to convert from RGB to HSB (or HS*), and hope that your converter and the method of UIColor that you're calling do things the same way.
Also, 105/360 equals 0 in integer division, which is what you are using. Try using double values, e.g. 105.0/360.0 to get a double result.
On provided homepage site, it appears that, when You change brightness, RGB also changes.. So You can probably skip the hue/saturation/brightness part and simply use RGB on application side like this:
[UIColor colorWithRed:77/255.0f green:200/255.0f blue:57/255.0f alpha:1.0f];
Simply, I have 2 views in interface builder, one is set to the color 99,99,99 using the RGB sliders in interface builder.
The other view is colored programmatically to achieve a certain shape. I fill it using:
//Obviously, this is in drawRect.
[[UIColor leadColor] set];
CGContextEOFillPath(myContext);
//And this is a category on UIColor
+ (UIColor *)leadColor {
return [UIColor colorWithWhite:99/255.0 alpha:1.0];
}
The result:
Why does this difference exist??
EDIT: (unecessary drawRect Code removed)
EDIT2:
So, here I am lying to myself .. "Interface builder showed RGB 99,99,99 as 80,80,80. I bet it offsets the number by 19." >.> ... A desperate man using Xcode thinks crazy stuff like this .. The result:
PERFECT!!, but why???? Another Xcode bug? I found like 10 of those in the past month ...
I finally reached the fine-tuning stage of this app, and had to solve this issue, so, I searched and easily found the solution:
How do I enter RGB values into Interface Builder?
Illustration:
colorWithWhite uses grayscale space, and a color of 99 in grayscale space doesn't map to a color of (99,99,99) in RGB space.
So in order to get the same result as in Interface Builder, you need to use RGB space. Replace your call to colorWithWhite with this:
[UIColor colorWithRed:99/255.0 green:99/255.0 blue:99/255.0 alpha:1.0]
So I have a color I've created and I'm trying to return a value of how white it is...
color1 = [UIColor colorWithRed:188/255.f green:50/255.f blue:219/255.f alpha:1];
I can return the values of Hue, Saturation, Brightness using this method:
[color1 getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
It works great and I get all kinds of float values
But when I try to use Apple's "GetWhite" method the value returned is always 0.00000 no matter which color I input.
[color1 getWhite:&white alpha:&alpha];
What am I doing wrong? Do I need to convert the color to greyscale first? If so how do I do this? Is there and easier way to detect how white something is... just averaging the red/green/blue values then dividing by 255? Thanks!
(*Also my reputation isn't high enough to add the tag "getWhite" to this, can somebody create it... When I googled for an answer for this nothing came up... maybe creating that tag will help others in need.)
Your question indicates that you may not understand the purpose of getWhite. From the docs:
If the color is in a compatible color space, the color is converted into grayscale format...
So the purpose of this function is to get the color value in grayscale. If you describe your purpose for getting this value, we may be able to help you in more detail.
That said, if this is really the function you want to use, you should test the return value of getWhite as well. The docs mention this about the return value:
YES if the color could be converted, NO otherwise.