advertisement

Article:
  Creating a Color Meter Using Cocoa
Subject:   Color wheel...
Date:   2001-06-20 12:50:22
From:   davidmasters
Response to: Color wheel...

It is so cool seeing the sliders move as you pick the color from the color picker! The method I wrote was this:


- (IBAction)colorWellChanged:(id)sender
{
NSColor *aColor = [[sender color] colorUsingColorSpaceName:@"NSDeviceRGBColorSpace"];
[aColor getRed:&redValue green:&greenValue blue:&blueValue alpha:&alphaValue];

[alphaField setFloatValue:alphaValue];
[alphaSlider setFloatValue:alphaValue];
[blueField setFloatValue:blueValue];
[blueSlider setFloatValue:blueValue];
[greenField setFloatValue:greenValue];
[greenSlider setFloatValue:greenValue];
[redField setFloatValue:redValue];
[redSlider setFloatValue:redValue];


[self updateColor];
}


Make sure that in IB you control-drag from the color well to the Controller, and link the target outlet to the method - in this case colorWellChanged.


Also remember to declare the method in Controller.h:


- (IBAction)colorWellChanged:(id)sender;


(I've been doing Java/WebObjects programming - so I keep forgetting to declare things in the .h file!)



In the method it is not strictly necessary to call [self updateColor] at the end - after all you're changing the color well directly at this point.


Using colorUsingColorSpaceName helps to ensure that you get rgb and alpha values irrespective of the color space used in the individual panel of the color picker.


Hope this helps!


David


1 to 4 of 4
  1. Color wheel...
    2001-06-22 09:17:54  donarb [View]

  2. Color wheel...
    2001-06-20 19:54:29  johnts [View]

  3. Color wheel...
    2001-06-20 19:39:24  johnts [View]

  4. Color wheel...
    2001-06-20 19:28:20  johnts [View]

1 to 4 of 4