Skip to content
Newer
Older
100644 84 lines (69 sloc) 2.78 KB
4deff4d Initial check-in.
uli authored Jul 4, 2009
1 //
2 // NSArray+Color.m
3 // CocoaTADS
4 //
5 // Created by Uli Kusterer on Mon Jun 02 2003.
67e2a78 @uliwitness Updated license, corrected category name.
authored Mar 7, 2010
6 // Copyright (c) 2003 Uli Kusterer.
7 //
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
4deff4d Initial check-in.
uli authored Jul 4, 2009
26 //
27
28 // -----------------------------------------------------------------------------
29 // Headers:
30 // -----------------------------------------------------------------------------
31
32 #import "NSArray+Color.h"
33
34
67e2a78 @uliwitness Updated license, corrected category name.
authored Mar 7, 2010
35 @implementation NSArray (UKColor)
4deff4d Initial check-in.
uli authored Jul 4, 2009
36
37 // -----------------------------------------------------------------------------
38 // arrayWithColor:
39 // Converts the color to an RGB color if needed, and then creates an array
40 // with its red, green, blue and alpha components (in that order).
41 //
42 // REVISIONS:
43 // 2004-05-18 witness documented.
44 // -----------------------------------------------------------------------------
45
46 +(NSArray*) arrayWithColor: (NSColor*) col
47 {
adeeb14 @uliwitness Make 64-bit safe.
authored Mar 13, 2010
48 CGFloat fRed = 0, fGreen = 0, fBlue = 0, fAlpha = 1.0;
4deff4d Initial check-in.
uli authored Jul 4, 2009
49
50 col = [col colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
51 [col getRed: &fRed green: &fGreen blue: &fBlue alpha: &fAlpha];
52
53 return [self arrayWithObjects: [NSNumber numberWithFloat:fRed], [NSNumber numberWithFloat:fGreen],
54 [NSNumber numberWithFloat:fBlue], [NSNumber numberWithFloat:fAlpha], nil];
55 }
56
57
58 // -----------------------------------------------------------------------------
59 // colorValue:
60 // Converts an NSArray with three (or four) NSValues into an RGB Color
61 // (plus alpha, if specified).
62 //
63 // REVISIONS:
64 // 2004-05-18 witness documented.
65 // -----------------------------------------------------------------------------
66
67 -(NSColor*) colorValue
68 {
adeeb14 @uliwitness Make 64-bit safe.
authored Mar 13, 2010
69 float fRed = 0, fGreen = 0, fBlue = 0, fAlpha = 1.0;
4deff4d Initial check-in.
uli authored Jul 4, 2009
70
adeeb14 @uliwitness Make 64-bit safe.
authored Mar 13, 2010
71 if( [self count] >= 3 )
72 {
73 fRed = [[self objectAtIndex:0] floatValue];
74 fGreen = [[self objectAtIndex:1] floatValue];
75 fBlue = [[self objectAtIndex:2] floatValue];
76 }
4deff4d Initial check-in.
uli authored Jul 4, 2009
77 if( [self count] > 3 ) // Have alpha info?
78 fAlpha = [[self objectAtIndex:3] floatValue];
79
80 return [NSColor colorWithCalibratedRed: fRed green: fGreen blue: fBlue alpha: fAlpha];
81 }
82
83 @end
Something went wrong with that request. Please try again.