Discussion in 'Mac Programming' started by varsis, Jan 13, 2008.

  1. varsis macrumors regular

    Nov 30, 2005

    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:
    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?
  2. kainjow Moderator emeritus


    Jun 15, 2000
    I'm not really sure what you mean here. Are you referring to the NSColor object you're getting from the color well?
  3. HiRez macrumors 603


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

    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.

Share This Page