//
//  NSColor_extras.m
//  MySQLWorkbench
//
//  Created by Alfredo Kojima on 10/Apr/09.
//  Copyright 2009 Sun Microsystems Inc. All rights reserved.
//

#import "NSColor_extras.h"


@implementation NSColor(NSColor_extras)

+ (NSColor*)colorFromHexString:(NSString*)hexcolor
{
  int r, g, b;
  
  if (sscanf([hexcolor UTF8String], "#%02x%02x%02x", &r, &g, &b) != 3)
    return nil;
  
  return [NSColor colorWithCalibratedRed:r/256.0 green:g/256.0 blue:b/256.0 alpha:1.0];
}


- (NSString*)hexString
{
  CGFloat red, green, blue, alpha;
  
  [[self colorUsingColorSpaceName: NSCalibratedRGBColorSpace] getRed:&red green:&green blue:&blue alpha:&alpha];
  
  return [NSString stringWithFormat:@"#%02x%02x%02x", (int)(red*256), (int)(green*256), (int)(blue*256)];
}

@end
