280north / cappuccino

Web Application Framework in JavaScript and Objective-J

This URL has Read+Write access

boucher (author)
Mon Nov 02 16:47:07 -0800 2009
commit  93c90274b5376b5c7ef0c9dea0c1ffcec4499ab7
tree    c1e8d95726cd11b873a89fc143ea104a0f436c03
parent  e8b1ff88461f8b4eb303e5435079f00b40159af2
cappuccino / Tools / nib2cib / NSColor.j
100644 147 lines (120 sloc) 5.129 kb
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* NSColor.j
* nib2cib
*
* Portions based on NSColor.m (12/18/2008) in Cocotron (http://www.cocotron.org/)
* Copyright (c) 2006-2007 Christopher J. W. Lloyd
*
* Created by Thomas Robinson.
* Copyright 2008, 280 North, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
 
@import <AppKit/CPColor.j>
 
var NSUnknownColorSpaceModel = -1,
    NSGrayColorSpaceModel = 0,
    NSRGBColorSpaceModel = 1,
    NSCMYKColorSpaceModel = 2,
    NSLABColorSpaceModel = 3,
    NSDeviceNColorSpaceModel = 4,
    NSIndexedColorSpaceModel = 5,
    NSPatternColorSpaceModel = 6;
 
@implementation CPColor (NSCoding)
 
- (id)NS_initWithCoder:(CPCoder)aCoder
{
    var colorSpace = [aCoder decodeIntForKey:@"NSColorSpace"],
        result;
 
    switch (colorSpace)
    {
        case 1: // [NSColor colorWithCalibratedRed:values[0] green:values[1] blue:values[2] alpha:values[3]];
        case 2: // [NSColor colorWithDeviceRed:values[0] green:values[1] blue:values[2] alpha:values[3]];
        
            // NSComponents data
            // NSCustomColorSpace NSColorSpace
            var rgb = [aCoder decodeBytesForKey:@"NSRGB"],
                string = bytes_to_string(rgb),
                components = [string componentsSeparatedByString:@" "],
                values = [0,0,0,1];
 
            for (var i = 0; i < components.length && i < 4; i++)
                values[i] = [components[i] floatValue];
            
            CPLog.warn("rgb="+rgb+" string=" + string + " values=" + values);
 
            result = [CPColor colorWithCalibratedRed:values[0] green:values[1] blue:values[2] alpha:values[3]];
            break;
            
        case 3: // [NSColor colorWithCalibratedWhite:values[0] alpha:values[1]];
        case 4: // [NSColor colorWithDeviceWhite:values[0] alpha:values[1]];
        
            var bytes = [aCoder decodeBytesForKey:@"NSWhite"],
                string = bytes_to_string(bytes),
                components = [string componentsSeparatedByString:@" "],
                values = [0,1];
                
            for (var i = 0; i < components.length && i < 2; i++)
                values[i] = [components[i] floatValue];
 
            result = [CPColor colorWithCalibratedWhite:values[0] alpha:values[1]];
            break;
/*
case 5:
var cmyk = [aCoder decodeBytesForKey:@"NSCMYK"],
string = bytes_to_string(rgb),
components = [string componentsSeparatedByString:@" "],
values = [0,0,0,0,1];
for (var i = 0; i < components.length && i < 5; i++)
values[i] = [components[i] floatValue];
 
result = [CPColor colorWithDeviceCyan:values[0] magenta:values[1] yellow:values[2] black:values[3] alpha:values[4]];
break;
*/
        case 6:
            var catalogName = [aCoder decodeObjectForKey:@"NSCatalogName"],
                colorName = [aCoder decodeObjectForKey:@"NSColorName"],
                color = [aCoder decodeObjectForKey:@"NSColor"];
                
            // We don't having color mappings implemented, so just use the cached NSColor
            
            if (catalogName === @"System")
            {
                var //display = [NSDisplay currentDisplay],
                    result = null;//[display colorWithName: colorName];
 
                if (colorName === @"controlColor")
                    result = nil;
 
                else if (colorName === @"controlBackgroundColor")
                    result = [CPColor whiteColor];
 
                else if (!result)
                {
                    result = color;
                    //[display _addSystemColor: result forName: colorName];
                }
            }
            else
            {
                result = null;//[CPColor colorWithCatalogName: catalogName colorName: colorName];
                if (!result)
                    result = color;
            }
            break;
        default:
            CPLog(@"-[%@ %s] unknown color space %d", isa, _cmd, colorSpace);
            result = [CPColor blackColor];
            break;
    }
 
    return result;
}
 
@end
 
@implementation NSColor : CPColor
{
}
 
- (id)initWithCoder:(CPCoder)aCoder
{
    return [self NS_initWithCoder:aCoder];
}
 
- (Class)classForKeyedArchiver
{
    return [CPColor class];
}
 
@end