Problem with NSColors in cocoa - objective-c

i am trying to compose colors using NSColor and when i am trying to create RGB color with the following values it just displays the white colors instead:
(r,g,b):(50,50,50)
(r,g,b):(100,100,100)
(r,g,b):(150,150,150)
(r,g,b):(200,200,200)
etc...
the code used to create the colors is:
// the code to genearet simple images with background colors
NSColor * myColor = [NSColor colorWithDeviceRed:100.0 green:100.0 blue:100.0 alpha:1.0];
NSImage* image1 = [[NSImage alloc] initWithSize:NSMakeSize(10.0, 100.0)];
NSRect imageBounds1 = NSMakeRect (0, 0, 10, 100);
[image1 lockFocus];
[myColor set];
NSRectFill (imageBounds1);
[image1 unlockFocus];
I couldn't find any resource or sample on the web, which provides some sort of help on my above queries.It's highly appreciated if someone could share his wisdom on how I can achieve this.
Thanks in advance..

If I recall correctly you'll want the range 0-1 as your RGB as well

NSColor components have values in [0..1] so you should normalize the values you have, e.g.:
NSColor * myColor = [NSColor colorWithDeviceRed:100.0/255 green:100.0/255 blue:100.0/255 alpha:1.0];
If you try to set values greater then 1 to colour components then they're interpreted as 1, so your code will be actually equivalent to
NSColor * myColor = [NSColor colorWithDeviceRed:1.0 green:1.0 blue:1.0 alpha:1.0];
Which creates white colour.

As stated in the documentation:
Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0
This means that your values (100,100,100) are going to be converted in (1.0,1.0,1.0) which is white. What you have to do is convert each channel value using the following equation:
100 : 255 = x : 1.0 => x = 100/255
where x is the value that you will use for the method
-(NSColor*)colorWithDeviceRed:CGFloat red green:CGFloat green blue:CGFloat blue alpha:CGFloat alpha];
You should have something like this in your code
[NSColor colorWithDeviceRed:100.0/255.0 green:100.0/255.0 blue:100.0/255.0 alpha:1.0];

Related

Objective-C drawRect Custom RGB Fill Color

I'm trying to fill a circle in Objective-C with a custom RGB color. Here is my drawRect method:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor *color = [UIColor colorWithRed:10.0 green:131.0 blue:254.0 alpha:1.0];
CGContextSetStrokeColorWithColor(context, [color CGColor]);
CGContextSetLineWidth(context, 1.0);
CGContextAddEllipseInRect(context, [ball getRect]);
CGContextStrokePath(context);
}
Whenever I specify anything in place of [color CGColor] like greenColor it works fine, but with the above code nothing gets rendered (an invisible object). I know it is moving around the page because my update method is NSLog'ing a string for every update.
So, I want to fill and get the outer line of the circle to be the specified RGB color above. What am I doing wrong?
Thanks
[UIColor colorWithRed:green:blue:alpha] accept values between 0.0 to 1.0 (Apple Docs Reference) try changing the code like this:
UIColor *color = [UIColor colorWithRed:10.0/255.0 green:131.0/255.0 blue:254.0/255.0 alpha:1.0];
The value for each parameter in this method call:
[UIColor colorWithRed:10.0 green:131.0 blue:254.0 alpha:1.0];
should be between 0.0 and 1.0.

TableView backgroundColor not changing

I have a grouped tableView, and I am trying to change the default background to custom color. I have looked all over, and the closest thing to working is this:
- (void)viewDidLoad {
UIColor *backgroundColor = [UIColor colorWithRed:181 green:293 blue:223 alpha:0];
self.tableView.backgroundView = [[UIView alloc]initWithFrame:self.tableView.bounds];
self.tableView.backgroundView.backgroundColor = backgroundColor;
}
This code changes the background to white, but I can't get it to change to the custom color. Can someone help me out?
You are creating the color incorrectly. The RGBA values need to be in the range 0.0 - 1.0. UIColor will treat anything over 1.0 as 1.0. So your color is being set to white since all three RGB values will be treated as 1.0. Also note that an alpha of 0 means totally transparent. You want 1.0 to mean totally visible.
- (void)viewDidLoad {
UIColor *backgroundColor = [UIColor colorWithRed:181/255.0 green:293/255.0 blue:223/255.0 alpha:1.0];
self.tableView.backgroundView = [[UIView alloc]initWithFrame:self.tableView.bounds];
self.tableView.backgroundView.backgroundColor = backgroundColor;
}
Please note that your green value is 293. That needs to be changed to something from 0 to 255.
Your RGBA values should be 0.0 - 1.0.
Make sure alpha value should not be 0.0, to see the colour effect
UIColor *backgroundColor = [UIColor colorWithRed:0.7 green:1.0 blue:0.85 alpha:1.0];
self.tableView.backgroundView = [[UIView alloc]initWithFrame:self.tableView.bounds];
self.tableView.backgroundView.backgroundColor = backgroundColor;

NSColor and its hue component

I don't quite understand why the hue component of NSColor behaves like it behaves. Here is something strange:
NSColor *c = [NSColor colorWithCalibratedHue:0.1
saturation:1.0
brightness:1.0
alpha:1.0];
CGFloat hue = 0.0;
[c getHue:&hue saturation:NULL brightness:NULL alpha:NULL];
NSLog(#"hue = %f", hue);
If you run this code you see "hue = 0.1" being logged. But if you run the following code:
NSColor *c = [NSColor colorWithCalibratedHue:0.0
saturation:1.0
brightness:1.0
alpha:1.0];
CGFloat hue = 0.0;
[c getHue:&hue saturation:NULL brightness:NULL alpha:NULL];
NSLog(#"hue = %f", hue);
You see "hue = 1.0" being logged. Is this a bug? I read a lot of documentation on Color Spaces and Colors in general and couldn't find an answer.
In color theory, hue is an angular unit, usually expressed in degrees modulo 360 (0° being the same as 360°).
NSColor maps 0° to the floating point value 0.0 and 360° to 1.0. Therefore, it's perfectly valid for getHue to return 1.0 instead of 0.0, because both values represent the same hue.

Why is my CGGradient not working with a preset UIColor?

I have this working code:
NSMutableArray *shadowColors = [NSMutableArray arrayWithCapacity:2];
color = [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; // Declaration using components
[shadowColors addObject:(id)[color CGColor]];
color = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.0]; // Declaration using components
[shadowColors addObject:(id)[color CGColor]];
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGGradientRef gradient = CGGradientCreateWithColors(space, (CFArrayRef)shadowColors, NULL);
CGColorSpaceRelease(space);
CGFloat sw = 10.0; // shadow width
CGPoint top1 = CGPointMake(x, y+width/2.0);
CGPoint top2 = CGPointMake(x + sw, y+width/2.0);
CGPoint side1 = CGPointMake(x+width/2.0, y);
CGPoint side2 = CGPointMake(x+width/2.0, y+sw);
CGContextDrawLinearGradient(c, gradient, top1, top2, 0);
CGContextDrawLinearGradient(c, gradient, side1, side2, 0);
CGGradientRelease(gradient);
The color declarations are the part I'm interested in, lines 2 and 4. When I declare them as shown, they work just fine, but if I replace those two lines with the equivalent (I thought, at least) [UIColor blackColor] and [UIColor clearColor] then my gradients disappear. The colors I use don't make any difference, I can use greenColor and redColor and they still don't work.
Am I missing something or is this a bug in Apple's frameworks?
The code that doesn't work. And this is just the first section, everything else is the same.
NSMutableArray *shadowColors = [NSMutableArray arrayWithCapacity:2];
color = [UIColor blackColor];
[shadowColors addObject:(id)[color CGColor]];
color = [UIColor clearColor];
[shadowColors addObject:(id)[color CGColor]];
The code looks fine to me. blackColor and clearColor are probably both in a white color space, but the documentation says CGGradientCreateWithColors will convert the colors to the color space you pass in, so that shouldn't matter.
The only thing I can think of would be to try passing NULL for the color space, letting the gradient convert the colors to Generic RGB instead of Device RGB. This may work, but shouldn't make a difference—as far as I can see, it should work either way.
I suggest filing a bug.

White Color to RGB not correct

Check out the code
const CGFloat *c = CGColorGetComponents([[UIColor whiteColor] CGColor]);
slider1.value = c[0];
slider2.value = c[1];
slider3.value = c[2];
c[2] is getting 0. For whiteColor all RGB values shld be 1.0. Why its not returning correct value for the blue component?
Any code snippet? for getting RGB values from white color?
Try CGColorGetColorSpace([[UIColor whiteColor] CGColor])
You will see it is not RGB. It has 2 components only: greyscale and alpha
Use this code:
CGContextSetFillColorWithColor(ctx,[UIColor whiteColor].CGColor);
Works with colors and grayscale.
This snippet of code should work with both RGB and grayscale:
CGFloat *components = (CGFloat *) CGColorGetComponents(<UIColor instance>.CGColor);
if(CGColorGetNumberOfComponents(<UIColor instance>.CGColor) == 2)
{
//assuming it is grayscale - copy the first value
components[2] = components[1] = components[0];
}