Sign In/My Account | View Cart  

advertisement

AddThis Social Bookmark Button

Article:
  Creating a Color Meter Using Cocoa
Subject:   Color wheel...
Date:   2001-06-20 06:31:33
From:   rainwadj
As mentioned in the article, clicking on the color well brings up a color wheel (et al). Clicking around in the color wheel updates the color well, but not the sliders or the text fields. How would I do this?
Full Threads Oldest First

Showing messages 1 through 9 of 9.

  • Color wheel...
    2001-06-20 07:46:12  rainwadj [View]

    I finally noticed that this was an extra credit assignment (Doh!). Here's what I came up with:

    - (IBAction)updateControls:(id)sender
    {
    NSColor *theColor = [sender color];

    redValue = [theColor redComponent];
    [redField setFloatValue:redValue];
    [redSlider setFloatValue:redValue];

    greenValue = [theColor greenComponent];
    [greenField setFloatValue:greenValue];
    [greenSlider setFloatValue:greenValue];

    blueValue = [theColor blueComponent];
    [blueField setFloatValue:blueValue];
    [blueSlider setFloatValue:blueValue];

    alphaValue = [theColor alphaComponent];
    [alphaField setFloatValue:alphaValue];
    [alphaSlider setFloatValue:alphaValue];
    }

    Looking at the class defs for NSColor helped a lot. It seems like it would have been cleaner to use getRed:green:blue:alpha, since I wanted them all anyway, but compiling with [theColor getRed:redValue...] complained that redValue... was an incompatible type. The method wants float * for the parameters, and the someValue variables are floats. (I feel like I'm forgetting something very basic here...)
    • Color wheel...
      2001-06-20 08:26:02  davidmasters [View]

      I tried a similar approach, and it does work.


      To get the color I converted it to a named color space (to ensure I could then get the red, green, blue values), and then used getRed:green:blue:alpha to extract the individual values.

      Can someone confirm if this is the right approach?


      NSColor *aColor = [[sender color] colorUsingColorSpaceName:@"NSDeviceRGBColorSpace"];

      [aColor getRed:&redValue green:&greenValue blue:&blueValue alpha:&alphaValue];

      Remember to use & when passing these parameters - you have floats, the method is expecting float *.


      After that, just set the floatValue of the sliders and text fields, and everything should work.

      David
      • Color wheel...
        2001-06-20 10:40:23  rainwadj [View]

        & - that's it. I knew I was forgetting something. My C is a little rusty. Or maybe a lot. :-)

        colorUsingColorSpaceName is a good idea. This program works without it, but we're not supposed to code that way.

        Thanks for the info.
        • Color wheel...
          2001-06-20 11:39:04  johnts [View]

          I have the same thing as the first message. And it worked - almost. The sliders did not change when I clicked on the color wheel. Also, when I changed how the color wheel is shown to show "system pallette" (or something similar), when I clicked on a color, I was getting errors in the project window, saying that "blueComponent does not exist". I'll have to add the thing about converting it to the correct color space.

          But like I said, my big problem is that the sliders don't move!
          • Color wheel...
            2001-06-20 12:50:22  davidmasters [View]

            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
            • Color wheel...
              2001-06-22 09:17:54  donarb [View]

              Why all that code? You already have methods that set the sliders and text fields, so just call those:


              - (IBAction)colorWellAction:(id)sender
              {
              NSColor *aColor = [sender color];

              /* send messages to each color setting routine */
              [self setRed:[NSNumber numberWithFloat:[aColor redComponent]]];
              [self setBlue:[NSNumber numberWithFloat:[aColor blueComponent]]];
              [self setGreen:[NSNumber numberWithFloat:[aColor greenComponent]]];
              // [self setAlpha:[NSNumber numberWithFloat:[aColor alphaComponent]]];
              }

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

              Ok. I figured it out. The code was fine. What I didn't do was connect the Sliders to the slider outlets (Control drag from the Controller object to each slider, and click the corresponding outlet and clicking Connect). Once I did that and rebuilt, it works! What helped me was I put

              NSLog(@"redSlider %@",redSlider);

              into the setRed: method. It wasn't showing anything for the value of the redSlider,

              A lot to remember - connecting everything up to everything - both ways.
            • Color wheel...
              2001-06-20 19:39:24  johnts [View]

              I just noticed too that when I change the value in one of the text boxes, the color changes, but the slider doesn't move either.
            • Color wheel...
              2001-06-20 19:28:20  johnts [View]

              Hmm. Here's mine. It's pretty much the same thing:

              - (IBAction)setAll:(id)sender
              {
              NSColor *theColor = [[sender color] colorUsingColorSpaceName:@"NSDeviceRGBColorSpace"];

              blueValue = [theColor blueComponent];
              redValue = [theColor redComponent];
              greenValue = [theColor greenComponent];
              alphaValue = [theColor alphaComponent];

              [alphaField setFloatValue:alphaValue];
              [alphaSlider setFloatValue:alphaValue];

              [blueField setFloatValue:blueValue];
              [blueSlider setFloatValue:blueValue];

              [greenField setFloatValue:greenValue];
              [greenSlider setFloatValue:greenValue];

              [redField setFloatValue:redValue];
              [redSlider setFloatValue:redValue];

              }

              But the sliders don't move.