API  0.9.6
 All Classes Files Functions Variables Macros Groups Pages
CGColor.j
Go to the documentation of this file.
1 /*
2  * CGColor.j
3  * AppKit
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 //import "CGPattern.j"
24 
25 
26 // FIXME: Move this to Objective-J.js!!!
28 
29 function CFHashCode(aCFObject)
30 {
31  if (!aCFObject.hash)
32  aCFObject.hash = ++CFTypeGlobalCount;
33 
34  return aCFObject;
35 }
36 
37 kCGColorWhite = "kCGColorWhite";
38 kCGColorBlack = "kCGColorBlack";
39 kCGColorClear = "kCGColorClear";
40 
41 var _CGColorMap = { };
42 
43 function CGColorGetConstantColor(aColorName)
44 {
45  alert("FIX ME");
46 }
47 
51 function CGColorRetain(aColor)
52 {
53  return aColor;
54 }
55 
59 function CGColorRelease()
60 {
61 }
62 
70 function CGColorCreate(aColorSpace, components)
71 {
72  if (!aColorSpace || !components)
73  return NULL;
74 
75  var components = components.slice();
76 
77  CGColorSpaceStandardizeComponents(aColorSpace, components);
78 
79  var UID = CFHashCode(aColorSpace) + components.join("");
80 
81  if (_CGColorMap[UID])
82  return _CGColorMap[UID];
83 
84  return _CGColorMap[UID] = { colorspace:aColorSpace, pattern:NULL, components:components };
85 }
86 
95 function CGColorCreateCopy(aColor)
96 {
97  // Colors should be treated as immutable, so don't mutate it!
98  return aColor;
99 }
100 
108 function CGColorCreateGenericGray(gray, alpha)
109 {
110  return CGColorCreate(CGColorSpaceCreateDeviceRGB(), [gray, gray, gray, alpha]);
111 }
112 
122 function CGColorCreateGenericRGB(red, green, blue, alpha)
123 {
124  return CGColorCreate(CGColorSpaceCreateDeviceRGB(), [red, green, blue, alpha]);
125 }
126 
137 function CGColorCreateGenericCMYK(cyan, magenta, yellow, black, alpha)
138 {
140  [cyan, magenta, yellow, black, alpha]);
141 }
142 
150 function CGColorCreateCopyWithAlpha(aColor, anAlpha)
151 {
152  if (!aColor)
153  return aColor; // Avoid error null pointer in next line
154 
155  var components = aColor.components.slice();
156 
157  if (anAlpha == components[components.length - 1])
158  return aColor;
159 
160  // set new alpha value now so that a potentially a new cache entry is made and
161  // not that an existing cache entry is mutated.
162  components[components.length - 1] = anAlpha;
163 
164  if (aColor.pattern)
165  return CGColorCreateWithPattern(aColor.colorspace, aColor.pattern, components);
166  else
167  return CGColorCreate(aColor.colorspace, components);
168 }
169 
178 function CGColorCreateWithPattern(aColorSpace, aPattern, components)
179 {
180  if (!aColorSpace || !aPattern || !components)
181  return NULL;
182 
183  return { colorspace:aColorSpace, pattern:aPattern, components:components.slice() };
184 }
185 
193 function CGColorEqualToColor(lhs, rhs)
194 {
195  if (lhs == rhs)
196  return true;
197 
198  if (!lhs || !rhs)
199  return false;
200 
201  var lhsComponents = lhs.components,
202  rhsComponents = rhs.components,
203  lhsComponentCount = lhsComponents.length;
204 
205  if (lhsComponentCount != rhsComponents.length)
206  return false;
207 
208  while (lhsComponentCount--)
209  if (lhsComponents[lhsComponentCount] != rhsComponents[lhsComponentCount])
210  return false;
211 
212  if (lhs.pattern != rhs.pattern)
213  return false;
214 
215  if (CGColorSpaceEqualToColorSpace(lhs.colorspace, rhs.colorspace))
216  return false;
217 
218  return true;
219 }
220 
227 function CGColorGetAlpha(aColor)
228 {
229  var components = aColor.components;
230 
231  return components[components.length - 1];
232 }
233 
239 function CGColorGetColorSpace(aColor)
240 {
241  return aColor.colorspace;
242 }
243 
250 function CGColorGetComponents(aColor)
251 {
252  return aColor.components;
253 }
254 
263 {
264  return aColor.components.length;
265 }
266 
273 function CGColorGetPattern(aColor)
274 {
275  return aColor.pattern;
276 }
277 
278 /* var components = aColor.components;
279 
280  case : _CGCSSForColor[CFGetHash(aColor)] = "rgba(" + ROUND(components[0] * 255.0) + ',' + ROUND(components[0] * 255.0) + ',' ROUND(components[0] * 255.0) + ',' + ROUND(components[0] * 255.0);
281  _cssString = (hasAlpha ? "rgba(" : "rgb(") +
282  parseInt(_components[0] * 255.0) + ", " +
283  parseInt(_components[1] * 255.0) + ", " +
284  parseInt(_components[2] * 255.0) +
285  (hasAlpha ? (", " + _components[3]) : "") + ")";
286 
287 function CFStringFromColor()
288 {
289 
290 }
291 */