API  0.9.10
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 
24 
25 // FIXME: Move this to Objective-J.js!!!
27 
28 function CFHashCode(aCFObject)
29 {
30  if (!aCFObject.hash)
31  aCFObject.hash = ++CFTypeGlobalCount;
32 
33  return aCFObject;
34 }
35 
36 kCGColorWhite = "kCGColorWhite";
37 kCGColorBlack = "kCGColorBlack";
38 kCGColorClear = "kCGColorClear";
39 
40 var _CGColorMap = { };
41 
42 function CGColorGetConstantColor(aColorName)
43 {
44  alert("FIX ME");
45 }
46 
50 function CGColorRetain(aColor)
51 {
52  return aColor;
53 }
54 
58 function CGColorRelease()
59 {
60 }
61 
69 function CGColorCreate(aColorSpace, components)
70 {
71  if (!aColorSpace || !components)
72  return NULL;
73 
74  var components = components.slice();
75 
76  CGColorSpaceStandardizeComponents(aColorSpace, components);
77 
78  var UID = CFHashCode(aColorSpace) + components.join("");
79 
80  if (_CGColorMap[UID])
81  return _CGColorMap[UID];
82 
83  return _CGColorMap[UID] = { colorspace:aColorSpace, pattern:NULL, components:components };
84 }
85 
94 function CGColorCreateCopy(aColor)
95 {
96  // Colors should be treated as immutable, so don't mutate it!
97  return aColor;
98 }
99 
107 function CGColorCreateGenericGray(gray, alpha)
108 {
109  return CGColorCreate(CGColorSpaceCreateDeviceRGB(), [gray, gray, gray, alpha]);
110 }
111 
121 function CGColorCreateGenericRGB(red, green, blue, alpha)
122 {
123  return CGColorCreate(CGColorSpaceCreateDeviceRGB(), [red, green, blue, alpha]);
124 }
125 
136 function CGColorCreateGenericCMYK(cyan, magenta, yellow, black, alpha)
137 {
139  [cyan, magenta, yellow, black, alpha]);
140 }
141 
149 function CGColorCreateCopyWithAlpha(aColor, anAlpha)
150 {
151  if (!aColor)
152  return aColor; // Avoid error null pointer in next line
153 
154  var components = aColor.components.slice();
155 
156  if (anAlpha == components[components.length - 1])
157  return aColor;
158 
159  // set new alpha value now so that a potentially a new cache entry is made and
160  // not that an existing cache entry is mutated.
161  components[components.length - 1] = anAlpha;
162 
163  if (aColor.pattern)
164  return CGColorCreateWithPattern(aColor.colorspace, aColor.pattern, components);
165  else
166  return CGColorCreate(aColor.colorspace, components);
167 }
168 
177 function CGColorCreateWithPattern(aColorSpace, aPattern, components)
178 {
179  if (!aColorSpace || !aPattern || !components)
180  return NULL;
181 
182  return { colorspace:aColorSpace, pattern:aPattern, components:components.slice() };
183 }
184 
192 function CGColorEqualToColor(lhs, rhs)
193 {
194  if (lhs == rhs)
195  return true;
196 
197  if (!lhs || !rhs)
198  return false;
199 
200  var lhsComponents = lhs.components,
201  rhsComponents = rhs.components,
202  lhsComponentCount = lhsComponents.length;
203 
204  if (lhsComponentCount != rhsComponents.length)
205  return false;
206 
207  while (lhsComponentCount--)
208  if (lhsComponents[lhsComponentCount] != rhsComponents[lhsComponentCount])
209  return false;
210 
211  if (lhs.pattern != rhs.pattern)
212  return false;
213 
214  if (CGColorSpaceEqualToColorSpace(lhs.colorspace, rhs.colorspace))
215  return false;
216 
217  return true;
218 }
219 
226 function CGColorGetAlpha(aColor)
227 {
228  var components = aColor.components;
229 
230  return components[components.length - 1];
231 }
232 
238 function CGColorGetColorSpace(aColor)
239 {
240  return aColor.colorspace;
241 }
242 
249 function CGColorGetComponents(aColor)
250 {
251  return aColor.components;
252 }
253 
262 {
263  return aColor.components.length;
264 }
265 
272 function CGColorGetPattern(aColor)
273 {
274  return aColor.pattern;
275 }
276 
277 /* var components = aColor.components;
278 
279  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);
280  _cssString = (hasAlpha ? "rgba(" : "rgb(") +
281  parseInt(_components[0] * 255.0) + ", " +
282  parseInt(_components[1] * 255.0) + ", " +
283  parseInt(_components[2] * 255.0) +
284  (hasAlpha ? (", " + _components[3]) : "") + ")";
285 
286 function CFStringFromColor()
287 {
288 
289 }
290 */
function CGColorCreateGenericCMYK(cyan, magenta, yellow, black, alpha)
Definition: CGColor.j:136
function CGColorSpaceCreateDeviceRGB()
Definition: CGColorSpace.j:107
function CGColorEqualToColor(lhs, rhs)
Definition: CGColor.j:192
function CGColorCreateCopy(aColor)
Definition: CGColor.j:94
function CGColorSpaceStandardizeComponents(aColorSpace, components)
Definition: CGColorSpace.j:229
function CGColorRelease()
Definition: CGColor.j:58
function CGColorCreateCopyWithAlpha(aColor, anAlpha)
Definition: CGColor.j:149
kCGColorClear
Definition: CGColor.j:38
function CGColorGetConstantColor(aColorName)
Definition: CGColor.j:42
function CGColorGetAlpha(aColor)
Definition: CGColor.j:226
function CGColorCreate(aColorSpace, components)
Definition: CGColor.j:69
function CGColorGetColorSpace(aColor)
Definition: CGColor.j:238
function CGColorCreateGenericGray(gray, alpha)
Definition: CGColor.j:107
function CGColorSpaceCreateDeviceCMYK()
Definition: CGColorSpace.j:97
function CGColorGetComponents(aColor)
Definition: CGColor.j:249
function CFHashCode(aCFObject)
Definition: CGColor.j:28
kCGColorWhite
Definition: CGColor.j:36
var CFTypeGlobalCount
Definition: CGColor.j:26
kCGColorBlack
Definition: CGColor.j:37
function CGColorCreateWithPattern(aColorSpace, aPattern, components)
Definition: CGColor.j:177
function CGColorGetNumberOfComponents(aColor)
Definition: CGColor.j:261
function CGColorGetPattern(aColor)
Definition: CGColor.j:272
function CGColorCreateGenericRGB(red, green, blue, alpha)
Definition: CGColor.j:121
function CGColorRetain(aColor)
Definition: CGColor.j:50