How do I get the hexadecimal value of an NSColor object?
Q: How do I get the hexadecimal value of an NSColor object?
A: How do I get the hexadecimal value of an NSColor object?
Below are the steps required to get the hexadecimal value of an NSColor object.
Convert the color object to the RGB color space.
This step is important for it is illegal to access the components of an NSColor that are not defined for its color space. Use the
colorUsingColorSpaceName
method to convert a color to the RGB color space.Get the red, green, and blue components of the color.
These components are floating point values on the domain [0, 1] with the minimum intensity being 0 and the maximum intensity being 1. Use the
getRed:(CGFloat *)red green:(CGFloat *)green blue:(CGFloat *)blue alpha:(CGFloat *)alpha
method to obtain a color's red, green, and blue components.Convert the components to the web color space, which consists of numbers (unsigned decimal integer) between 0 and 255.
The goal here is to produce three two-digit hexadecimal color components with values that range from 0x00 ( 0 decimal, minimum intensity) through 0xFF ( 255 decimal, maximum intensity).
Convert each number to a two-digit hex string.
Concatenate the red, green, and blue components' hex strings together with a "#".
Listing 1 shows how to programmatically implement these steps; it builds an Objective-C category
that adds the hexadecimalValueOfAnNSColor
method to the NSColor class.
Listing 1 Adding a category to NSColor.
#import <Cocoa/Cocoa.h> @interface NSColor(NSColorHexadecimalValue) -(NSString *)hexadecimalValueOfAnNSColor; @end @implementation NSColor(NSColorHexadecimalValue) -(NSString *)hexadecimalValueOfAnNSColor { float redFloatValue, greenFloatValue, blueFloatValue; int redIntValue, greenIntValue, blueIntValue; NSString *redHexValue, *greenHexValue, *blueHexValue; //Convert the NSColor to the RGB color space before we can access its components NSColor *convertedColor=[self colorUsingColorSpaceName:NSCalibratedRGBColorSpace]; if(convertedColor) { // Get the red, green, and blue components of the color [convertedColor getRed:&redFloatValue green:&greenFloatValue blue:&blueFloatValue alpha:NULL]; // Convert the components to numbers (unsigned decimal integer) between 0 and 255 redIntValue=redFloatValue*255.99999f; greenIntValue=greenFloatValue*255.99999f; blueIntValue=blueFloatValue*255.99999f; // Convert the numbers to hex strings redHexValue=[NSString stringWithFormat:@"%02x", redIntValue]; greenHexValue=[NSString stringWithFormat:@"%02x", greenIntValue]; blueHexValue=[NSString stringWithFormat:@"%02x", blueIntValue]; // Concatenate the red, green, and blue components' hex strings together with a "#" return [NSString stringWithFormat:@"#%@%@%@", redHexValue, greenHexValue, blueHexValue]; } return nil; } @end |
Listing 2 Getting the hexadecimal value of an NSColor object.
//aColor is an NSColor object NSString *hexValue=[aColor hexadecimalValueOfAnNSColor]; |
Further Reading
Document Revision History
Date | Notes |
---|---|
2007-12-19 | New document that describes how to convert an NSColor object to its Hexadecimal value. |
Copyright © 2007 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2007-12-19