15 May 2008

NSColorWell and OpenGL

NSColor objects are specified in a color space. For example, a gray color can be fully specified by one float value in a NSCalibratedBlackColorSpace. However, the same gray color requires three float values in an NSCalibratedRGBColorSpace, three values to represent the red, green, and blue values. To convert between different color spaces, NSColor defines two instance methods which create a new NSColor object from the with a specified color space name. These methods are:

- (NSColor*)colorUsingColorSpaceName:(NSString*)colorSpace device:(NSDictionary*)deviceDescription
- (NSColor*)colorUsingColorSpaceName:(NSString*)colorSpace

OpenGL uses RGBA colors. If you use an NSColorWell to bind to a NSColor object, there is no guarantee that the setter method will be called with a color object specified in the RGB color space. To ensure the color is always in the RGB color space, it can be converted in the setter function, as in the following example for setting the OpenGL clear color.

- (void)setClearColor:(NSColor*)color {
  if (color != clearColor) {
    [clearColor release];
    
    // Convert the arbitrary NSColor from the color well to an RGB color.
    if ([color colorSpace] == [NSColorSpace genericRGBColorSpace]) {
      clearColor = color;
    } else {
      clearColor = [color colorUsingColorSpaceName:NSCalibratedRGBColorSpace];
    }
    [clearColor retain];
  
    // Notify the view that it needs a refresh
    [self setNeedsDisplay:YES];
  }
}

Then, whenever you need to access the RGBA elements of the color, you are assured that they exist and an exception will be raised. For example, in the example above, we could set the clear color with the following snippet.

  // Get the red, green, and blue components of the color
  float red, green, blue, alpha;
  [clearColor getRed:&red green:&green blue:&blue alpha:&alpha];

  // Set the OpenGL clear color
  glClearColor(red, green, blue, alpha);

OpenGL colors and NSColor colors don't exactly match up. Fortunately, the rich NSColor API easily converts from a variety of color spaces to the required RGBA colors useful with OpenGL.

05 May 2008

Sidelight Project Announced

I've started a new open-source project called Sidelight. It is a collection of graphics related tutorials and projects for the Mac. To begin with, I'll be posting tutorials that dive in to a single OpenGL feature. I'll be following the same structure as the OpenGL Programming Guide (the Red Book). Then, building on these simple tutorials, create more advanced examples.

Today I posted a new animation tutorial which uses a double-buffered OpenGL view and a timer. Documentation to follow.

Lorem Ipsum

Heading 1 - Looks like the Blog Title

This post is just to help with modifying the blogger template. It doesn't contain any useful information beyond the formatting.


Above was a horizontal rule. This is paragraph text. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Heading 2 - Looks like the date

Here is a block quote. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Heading 3 - Looks like the post title

Here is some strong text. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Heading 4 - Looks like regular text

Heading 5 - So does 5
Heading 6 - And six

Here is some emphasized text. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Heading 1Heading 2
Data 1Data 2
Data 3Data 4
  1. Ordered
  2. Lists
  3. Are
  4. Cool
  • Unordered
  • Lists
  • Are
  • Rad