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.
Related
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]
I am trying to change the white point/white balance programmatically. This is what I want to accomplish:
- Choose a (random) pixel from the image
- Get color of that pixel
- Transform the image so that all pixels of that color will be transformed to white and all other colors shifted to match
I have accomplished the first two steps but the third step is not really working out.
At first I thought that, as per Apples documentation CIWhitePointAdjust should be the thing to accomplish exactly that but, although it does change the image it is not doing what I would like/expect it to do.
Then it seemed that CIColorMatrix should be something that would help me to shift the colors but I was (and still am) at a loss of what to input to it with those pesky vectors.
I have tried almost everything (same RGB values on all vectors, corresponding values (R for R, etc.) on each vector, 1 - corresponding value, 1 + corresponding value, 1/corresponding value. RGB values and different (1 - x, 1 + x, 1 / x).
I have also come across CITemperatureAndTint that, as per Apples documentation should also help, but I have not yet figured out how to convert from RGB to temperature and tint. I have seen algorithms and formulas about converting from RGB to Temperatur, but nothing regarding tint. I will continue experimenting with this a little though.
Any help much appreciated!
After a lot of experimenting and mathematics I finally got my app to work almost the way I want.
If anyone else will find themselves facing a similar problem then here is what I did.
I ended up using CITemperatureAndTint filter supplying a color in Kelvins calculated from the selected pixels RGB value and user suppliable tint value.
To get to Kelvins I:
- firstly converted RGB to XYZ using the D65 illuminant (ie Daylight).
- then converted from XYZ to Yxy. Both of these conversions were made using the algorithms found from EasyRGB.
- I then calculated Kelvins from Yxy using the McCamry's formula I found in a paper here.
These steps got the image in the ballpark but not quite there, so I added a UISlider for the user to supply the tint value ranging from -100 to 100.
With selecting a point that should be white and choosing values from the positive side of the tint scale (all the images I on my phone tend to be more yellow) an image can now be converted to (more) neutral colors. Yey!
I supplyed the calculated temperature and user chosen tint as inputNeutral vector values.
6500 (D65 daylight) and 0 as inputTargetNeutral vector values to CITTemperatureAndTint filter.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Check if UIColor is dark or bright?
Is there simple way to determine if a UIColor is dark or light? I'd like to put a label on a dynamic background and change its text color to white if the background's dark or to black if its a light color.
I haven't tested this, but it may work for you...
-(BOOL) isLightColor:(UIColor*)clr {
CGFloat white = 0;
[clr getWhite:&white alpha:nil];
return (white >= 0.5);
}
Calculate the color contrast between your label and background and decide on your color from there. Generally, this will involve getting the components of the colors in question.
If you google "Calculate Color Contrast" sans the quotes, you'll find some links. You may not find anything iOS specific, but you should be able to adapt the code you find, especially since they'll generally involve a function of the RGB, which is straight-forward.
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.