Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kamy

macrumors member
Original poster
Jul 27, 2011
44
0
Hi Guys,

I'm having trouble setting Colors to an 'NSTextField' instance.

Here are some screenshots

I set the RGB values using 'colorWithCalibratedRed' API of NSColor to the 'NSTextField' instance. But the values dont seem to change

Green label here
Screen Shot 2014-12-15 at 10.44.10 am.png

Changing the Red value makes a change to yellow
Screen Shot 2014-12-15 at 10.44.19 am.png

But changing the Red value does not change the output - it is yellow always
Screen Shot 2014-12-15 at 10.44.36 am.png

Adding a value to the Blue textbox, removes the color!
Screen Shot 2014-12-15 at 10.44.44 am.png

This is the code - where 'self.color' is an instance of NSTextField
NSTextField *color;

Code:
- (IBAction)onAddCharacter:(id)sender
{
    float RedValue = [self.red.stringValue floatValue];
    float GreenValue = [self.green.stringValue floatValue];
    float BlueVal = [self.blue.stringValue floatValue];
    float alphaVal = [self.alpha.stringValue floatValue];
    
    NSColor *myTodayColor = [NSColor colorWithCalibratedRed:RedValue green:GreenValue blue:BlueVal alpha:alphaVal];
    
    [self.color setFont:[NSFont fontWithName:@"Arial" size:48]];
    self.color.refusesFirstResponder = YES;
    [self.color setTextColor:myTodayColor];
    [self.color setStringValue:@"Some Text"];
    //[textField setBackgroundColor:[NSColor blackColor]];
    [self.color setDrawsBackground:YES];
    [self.color setBordered:NO];
    
}

What can be the problem and what am i missing?

Thanks in advance!
 

grandM

macrumors 68000
Oct 14, 2013
1,509
298
Hi Guys,

I'm having trouble setting Colors to an 'NSTextField' instance.

Here are some screenshots

I set the RGB values using 'colorWithCalibratedRed' API of NSColor to the 'NSTextField' instance. But the values dont seem to change

Green label here
View attachment 519680

Changing the Red value makes a change to yellow
View attachment 519681

But changing the Red value does not change the output - it is yellow always
View attachment 519682

Adding a value to the Blue textbox, removes the color!
View attachment 519683

This is the code - where 'self.color' is an instance of NSTextField
NSTextField *color;

Code:
- (IBAction)onAddCharacter:(id)sender
{
    float RedValue = [self.red.stringValue floatValue];
    float GreenValue = [self.green.stringValue floatValue];
    float BlueVal = [self.blue.stringValue floatValue];
    float alphaVal = [self.alpha.stringValue floatValue];
    
    NSColor *myTodayColor = [NSColor colorWithCalibratedRed:RedValue green:GreenValue blue:BlueVal alpha:alphaVal];
    
    [self.color setFont:[NSFont fontWithName:@"Arial" size:48]];
    self.color.refusesFirstResponder = YES;
    [self.color setTextColor:myTodayColor];
    [self.color setStringValue:@"Some Text"];
    //[textField setBackgroundColor:[NSColor blackColor]];
    [self.color setDrawsBackground:YES];
    [self.color setBordered:NO];
    
}

What can be the problem and what am i missing?

Thanks in advance!

the class method colorWithCalibratedRed asks for a CGfloat and you're feeding it a float
maybe that's the problem
 
Last edited:

kamy

macrumors member
Original poster
Jul 27, 2011
44
0
are you sure your textfields are hooked up to the right properties

Hey,

Thanks for responding.

Do you mean hooking the NXTextField from Interface Builder to the property?

Yes, that is done.

Every textbox in IB hooked to a property.

Code:
@property (weak) IBOutlet NSTextField *color;
@property (weak) IBOutlet NSTextField *red;
@property (weak) IBOutlet NSTextField *blue;
@property (weak) IBOutlet NSTextField *green;
@property (weak) IBOutlet NSTextField *alpha;

That is not the problem, even if i hardcode the color - i face the same issues
Code:
myTodayColor = [NSColor colorWithCalibratedRed:30 green:255 blue:5 alpha:1.0f];

self.color.textColor = myTodayColor

Have attached the executable..
View attachment FirstCocoa.app.zip
 

grandM

macrumors 68000
Oct 14, 2013
1,509
298
Hey,

Thanks for responding.

Do you mean hooking the NXTextField from Interface Builder to the property?

Yes, that is done.

Every textbox in IB hooked to a property.

Code:
@property (weak) IBOutlet NSTextField *color;
@property (weak) IBOutlet NSTextField *red;
@property (weak) IBOutlet NSTextField *blue;
@property (weak) IBOutlet NSTextField *green;
@property (weak) IBOutlet NSTextField *alpha;

That is not the problem, even if i hardcode the color - i face the same issues
Code:
myTodayColor = [NSColor colorWithCalibratedRed:30 green:255 blue:5 alpha:1.0f];

self.color.textColor = myTodayColor

Have attached the executable..
View attachment 519692
I 've edited my post
the class method colorWithCalibratedRed asks for a CGfloat and you're feeding it a float
maybe that's the problem
 

kamy

macrumors member
Original poster
Jul 27, 2011
44
0
I 've edited my post
the class method colorWithCalibratedRed asks for a CGfloat and you're feeding it a float
maybe that's the problem

I just tried using CGFloat but same issues.

Even if i hardcode it like this there is an issue....

Code:
myTodayColor = [NSColor colorWithCalibratedRed:30 green:255 blue:5 alpha:1.0f];
self.color.textColor = myTodayColor
 

grandM

macrumors 68000
Oct 14, 2013
1,509
298
Hey,

Thanks for responding.

Do you mean hooking the NXTextField from Interface Builder to the property?

Yes, that is done.

Every textbox in IB hooked to a property.

Code:
@property (weak) IBOutlet NSTextField *color;
@property (weak) IBOutlet NSTextField *red;
@property (weak) IBOutlet NSTextField *blue;
@property (weak) IBOutlet NSTextField *green;
@property (weak) IBOutlet NSTextField *alpha;

That is not the problem, even if i hardcode the color - i face the same issues
Code:
myTodayColor = [NSColor colorWithCalibratedRed:30 green:255 blue:5 alpha:1.0f];

self.color.textColor = myTodayColor

Have attached the executable..
View attachment 519692
I assume onAddCharacter is hooked up to the button
 

grandM

macrumors 68000
Oct 14, 2013
1,509
298
I just tried using CGFloat but same issues.

Even if i hardcode it like this there is an issue....

Code:
myTodayColor = [NSColor colorWithCalibratedRed:30 green:255 blue:5 alpha:1.0f];
self.color.textColor = myTodayColor
try 30.0f 255.0f 5.0f 1.0f
then 255.0f 255.0f 5.0f 1.0f

----------

Hi Guys,

I'm having trouble setting Colors to an 'NSTextField' instance.

Here are some screenshots

I set the RGB values using 'colorWithCalibratedRed' API of NSColor to the 'NSTextField' instance. But the values dont seem to change

Green label here
View attachment 519680

Changing the Red value makes a change to yellow
View attachment 519681

But changing the Red value does not change the output - it is yellow always
View attachment 519682

Adding a value to the Blue textbox, removes the color!
View attachment 519683

This is the code - where 'self.color' is an instance of NSTextField
NSTextField *color;

Code:
- (IBAction)onAddCharacter:(id)sender
{
    float RedValue = [self.red.stringValue floatValue];
    float GreenValue = [self.green.stringValue floatValue];
    float BlueVal = [self.blue.stringValue floatValue];
    float alphaVal = [self.alpha.stringValue floatValue];
    
    NSColor *myTodayColor = [NSColor colorWithCalibratedRed:RedValue green:GreenValue blue:BlueVal alpha:alphaVal];
    
    [self.color setFont:[NSFont fontWithName:@"Arial" size:48]];
    self.color.refusesFirstResponder = YES;
    [self.color setTextColor:myTodayColor];
    [self.color setStringValue:@"Some Text"];
    //[textField setBackgroundColor:[NSColor blackColor]];
    [self.color setDrawsBackground:YES];
    [self.color setBordered:NO];
    
}

What can be the problem and what am i missing?

Thanks in advance!
I think I have it
you can only feed it decimals smaller than or equal to 1 :) so 0.05 etc
meaning you must divide the value of RedValue etc
 

kamy

macrumors member
Original poster
Jul 27, 2011
44
0
try 30.0f 255.0f 5.0f 1.0f
then 255.0f 255.0f 5.0f 1.0f

i just tried what u suggested by hardcoding values, just to try

This gives me Yellow
Code:
    NSColor *myTodayColor = [NSColor colorWithCalibratedRed:30.0f green:255.0f blue:0.0f alpha:1.0f];

    [self.color setTextColor:myTodayColor];

Yellow
1.png

The moment i set the BLUE value to 48.0f or even 1.0f - it removes the color!

Code:
 NSColor *myTodayColor = [NSColor colorWithCalibratedRed:30.0f green:255.0f blue:48.0f alpha:1.0f];


[self.color setTextColor:myTodayColor];

2.png

This definitely is a NSTextField bug, u think?
 

grandM

macrumors 68000
Oct 14, 2013
1,509
298
i just tried what u suggested by hardcoding values, just to try

This gives me Yellow
Code:
    NSColor *myTodayColor = [NSColor colorWithCalibratedRed:30.0f green:255.0f blue:0.0f alpha:1.0f];

    [self.color setTextColor:myTodayColor];

Yellow
View attachment 519696

The moment i set the BLUE value to 48.0f or even 1.0f - it removes the color!

Code:
 NSColor *myTodayColor = [NSColor colorWithCalibratedRed:30.0f green:255.0f blue:48.0f alpha:1.0f];


[self.color setTextColor:myTodayColor];

View attachment 519697

This definitely is a NSTextField bug, u think?
I think I have it
you can only feed it decimals smaller than or equal to 1 so 0.05 etc
meaning you must divide the value of RedValue etc

----------

i just tried what u suggested by hardcoding values, just to try

This gives me Yellow
Code:
    NSColor *myTodayColor = [NSColor colorWithCalibratedRed:30.0f green:255.0f blue:0.0f alpha:1.0f];

    [self.color setTextColor:myTodayColor];

Yellow
View attachment 519696

The moment i set the BLUE value to 48.0f or even 1.0f - it removes the color!

Code:
 NSColor *myTodayColor = [NSColor colorWithCalibratedRed:30.0f green:255.0f blue:48.0f alpha:1.0f];


[self.color setTextColor:myTodayColor];

View attachment 519697

This definitely is a NSTextField bug, u think?
try 0.3f 0.5f 0.7f 1.0f
then 0.9f 0.5f 0.7f 1.0f

----------

Hi Guys,

I'm having trouble setting Colors to an 'NSTextField' instance.

Here are some screenshots

I set the RGB values using 'colorWithCalibratedRed' API of NSColor to the 'NSTextField' instance. But the values dont seem to change

Green label here
View attachment 519680

Changing the Red value makes a change to yellow
View attachment 519681

But changing the Red value does not change the output - it is yellow always
View attachment 519682

Adding a value to the Blue textbox, removes the color!
View attachment 519683

This is the code - where 'self.color' is an instance of NSTextField
NSTextField *color;

Code:
- (IBAction)onAddCharacter:(id)sender
{
    float RedValue = [self.red.stringValue floatValue];
    float GreenValue = [self.green.stringValue floatValue];
    float BlueVal = [self.blue.stringValue floatValue];
    float alphaVal = [self.alpha.stringValue floatValue];
    
    NSColor *myTodayColor = [NSColor colorWithCalibratedRed:RedValue green:GreenValue blue:BlueVal alpha:alphaVal];
    
    [self.color setFont:[NSFont fontWithName:@"Arial" size:48]];
    self.color.refusesFirstResponder = YES;
    [self.color setTextColor:myTodayColor];
    [self.color setStringValue:@"Some Text"];
    //[textField setBackgroundColor:[NSColor blackColor]];
    [self.color setDrawsBackground:YES];
    [self.color setBordered:NO];
    
}

What can be the problem and what am i missing?

Thanks in advance!
by the way when naming variables it is easier to maintain a certain logic
for instance redValue, blueValue, greenValue, alphaValue
 

kamy

macrumors member
Original poster
Jul 27, 2011
44
0
I think I have it
you can only feed it decimals smaller than or equal to 1 so 0.05 etc
meaning you must divide the value of RedValue etc

----------



try 0.3f 0.5f 0.7f 1.0f
then 0.9f 0.5f 0.7f 1.0f

Hey!
i see some improvement!

3.png

4.png

So what would be the equivalent decimal values for the
Red:80 green:255 blue:48 ?
 

kamy

macrumors member
Original poster
Jul 27, 2011
44
0
Oh god!

The documentation says this

Code:
+ (NSColor *)colorWithCalibratedRed:(CGFloat)red
                              green:(CGFloat)green
                               blue:(CGFloat)blue
                              alpha:(CGFloat)alpha

Parameters
red
The red component of the color object.
green
The green component of the color object.
blue
The blue component of the color object.
alpha
The opacity value of the color object.

Discussion
Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.

So it has to be between 0 and 1! How dumb of me to miss that point!

So how do we translate R, G, B values into decimal values between 0 to 1?
 

kamy

macrumors member
Original poster
Jul 27, 2011
44
0
Oh god!

The documentation says this

Code:
+ (NSColor *)colorWithCalibratedRed:(CGFloat)red
                              green:(CGFloat)green
                               blue:(CGFloat)blue
                              alpha:(CGFloat)alpha

Parameters
red
The red component of the color object.
green
The green component of the color object.
blue
The blue component of the color object.
alpha
The opacity value of the color object.

Discussion
Values below 0.0 are interpreted as 0.0, and values above 1.0 are interpreted as 1.0.

So it has to be between 0 and 1! How dumb of me to miss that point!

So how do we translate R, G, B values into decimal values between 0 to 1?

Okay!!!
What i needed to do was to convert the 8-bit int RGB values into a 0.0-1.0 float values. To do this simply divided my int value by 255.0. That gave me the correct float value!

It works fine now!

Thanks 'grandM' for your help.

Moral of the story - read each and every line of the Documentation properly!!!
 

grandM

macrumors 68000
Oct 14, 2013
1,509
298
Okay!!!
What i needed to do was to convert the 8-bit int RGB values into a 0.0-1.0 float values. To do this simply divided my int value by 255.0. That gave me the correct float value!

It works fine now!

Thanks 'grandM' for your help.

Moral of the story - read each and every line of the Documentation properly!!!
Glad to help out
 

ahall52

macrumors member
Feb 3, 2013
36
25
So how do we translate R, G, B values into decimal values between 0 to 1?

I quick hack if you are coming from a RGB environment is to simply put
Code:
x/255
where x is the R, G or B value. In the OP's second figure, putting the red value as 30/255 would have achieved the desired effect.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.