File: NSColorExtensions.m

package info (click to toggle)
timemon.app 4.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 580 kB
  • sloc: objc: 698; ansic: 161; makefile: 23; sh: 9
file content (39 lines) | stat: -rw-r--r-- 1,148 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#import <Foundation/NSString.h>
#import <Foundation/NSArray.h>
#import <AppKit/NSGraphics.h>
#import "NSColorExtensions.h"

@implementation NSColor (GetColorsFromString)
+ (NSColor *)colorFromStringRepresentation:(NSString *)colorString
{
    CGFloat r, g, b, a;
    NSArray *array = [colorString componentsSeparatedByString:@" "];

    if (!array) 
      {
	return nil;
      }

    if ([array count] < 3) 
      {
        NSLog(@"%@: + colorFromStringRepresentation", [[self class] description]);
        NSLog(@"%@: String must contain red, green, and blue components", [[self class] description]);
        return nil;
      }

    r = [[array objectAtIndex:0] floatValue];
    g = [[array objectAtIndex:1] floatValue];
    b = [[array objectAtIndex:2] floatValue];
    a = [array count] > 3 ? [[array objectAtIndex:3] floatValue] : 1.0;

    return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
}

- (NSString *)stringRepresentation
{
    CGFloat r, g, b, a;

    [[self colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&r green:&g blue:&b alpha:&a];
    return [NSString stringWithFormat:@"%f %f %f %f",r,g,b,a];
}
@end