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

varsis

macrumors regular
Original poster
Nov 30, 2005
209
1
Hi,

I am currently making a plugin, and it uses HEX values for color (It goes on the internet.)

The code I am using looks like this:
Code:
NSColor *myColor = [userColor color];
	float redPart = [myColor redComponent];
	float greenPart = [myColor greenComponent];
	float bluePart = [myColor blueComponent];
	int redHexString = redPart * 255;
	int greenHexString = greenPart * 255;
	int blueHexString = bluePart * 255;
	NSString *rgbHexString = [NSString stringWithFormat:@"0x%.2x%.2x%.2x", redHexString, greenHexString, blueHexString];
	NSString *rgbString = [NSString stringWithFormat:@"%.2x%.2x%.2x", redHexString, greenHexString, blueHexString];
Problem is the user can use CKMY,grayscale... etc... how do I convert these to Hex? Or only allow hex?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Problem is the user can use CKMY,grayscale... etc... how do I convert these to Hex? Or only allow hex?

I'm not really sure what you mean here. Are you referring to the NSColor object you're getting from the color well?
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
Let NSColor convert to RGB space for you. From the Color Programming Topics docs (link off the NSColor API reference):

If you need to ask an NSColor for components that aren’t in its color space (for instance, when you’ve gotten the color from the color panel), first convert the color to the appropriate color space using the colorUsingColorSpaceName: method. If the color is already in the specified color space, you get the same color back; otherwise you get a conversion that’s usually lossy or that’s correct only for the current device. You get back nil if the specified conversion can’t be done.

I think you'd probably want to use "device" RGB space to avoid values changing with calibration, but I'm not sure what the usage is.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.