![]() |
API 0.9.5
|
00001 /* 00002 * CGColor.j 00003 * AppKit 00004 * 00005 * Created by Francisco Tolmasky. 00006 * Copyright 2008, 280 North, Inc. 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public 00010 * License as published by the Free Software Foundation; either 00011 * version 2.1 of the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 //import "CGPattern.j" 00024 00025 00026 // FIXME: Move this to Objective-J.js!!! 00027 var CFTypeGlobalCount = 0; 00028 00029 function CFHashCode(aCFObject) 00030 { 00031 if (!aCFObject.hash) 00032 aCFObject.hash = ++CFTypeGlobalCount; 00033 00034 return aCFObject; 00035 } 00036 00037 kCGColorWhite = "kCGColorWhite"; 00038 kCGColorBlack = "kCGColorBlack"; 00039 kCGColorClear = "kCGColorClear"; 00040 00041 var _CGColorMap = { }; 00042 00043 function CGColorGetConstantColor(aColorName) 00044 { 00045 alert("FIX ME"); 00046 } 00047 00051 function CGColorRetain(aColor) 00052 { 00053 return aColor; 00054 } 00055 00059 function CGColorRelease() 00060 { 00061 } 00062 00070 function CGColorCreate(aColorSpace, components) 00071 { 00072 if (!aColorSpace || !components) 00073 return NULL; 00074 00075 var components = components.slice(); 00076 00077 CGColorSpaceStandardizeComponents(aColorSpace, components); 00078 00079 var UID = CFHashCode(aColorSpace) + components.join(""); 00080 00081 if (_CGColorMap[UID]) 00082 return _CGColorMap[UID]; 00083 00084 return _CGColorMap[UID] = { colorspace:aColorSpace, pattern:NULL, components:components }; 00085 } 00086 00095 function CGColorCreateCopy(aColor) 00096 { 00097 // Colors should be treated as immutable, so don't mutate it! 00098 return aColor; 00099 } 00100 00108 function CGColorCreateGenericGray(gray, alpha) 00109 { 00110 return CGColorCreate(CGColorSpaceCreateDeviceRGB(), [gray, gray, gray, alpha]); 00111 } 00112 00122 function CGColorCreateGenericRGB(red, green, blue, alpha) 00123 { 00124 return CGColorCreate(CGColorSpaceCreateDeviceRGB(), [red, green, blue, alpha]); 00125 } 00126 00137 function CGColorCreateGenericCMYK(cyan, magenta, yellow, black, alpha) 00138 { 00139 return CGColorCreate(CGColorSpaceCreateDeviceCMYK(), 00140 [cyan, magenta, yellow, black, alpha]); 00141 } 00142 00150 function CGColorCreateCopyWithAlpha(aColor, anAlpha) 00151 { 00152 if (!aColor) 00153 return aColor; // Avoid error null pointer in next line 00154 00155 var components = aColor.components.slice(); 00156 00157 if (anAlpha == components[components.length - 1]) 00158 return aColor; 00159 00160 // set new alpha value now so that a potentially a new cache entry is made and 00161 // not that an existing cache entry is mutated. 00162 components[components.length - 1] = anAlpha; 00163 00164 if (aColor.pattern) 00165 return CGColorCreateWithPattern(aColor.colorspace, aColor.pattern, components); 00166 else 00167 return CGColorCreate(aColor.colorspace, components); 00168 } 00169 00178 function CGColorCreateWithPattern(aColorSpace, aPattern, components) 00179 { 00180 if (!aColorSpace || !aPattern || !components) 00181 return NULL; 00182 00183 return { colorspace:aColorSpace, pattern:aPattern, components:components.slice() }; 00184 } 00185 00193 function CGColorEqualToColor(lhs, rhs) 00194 { 00195 if (lhs == rhs) 00196 return true; 00197 00198 if (!lhs || !rhs) 00199 return false; 00200 00201 var lhsComponents = lhs.components, 00202 rhsComponents = rhs.components, 00203 lhsComponentCount = lhsComponents.length; 00204 00205 if (lhsComponentCount != rhsComponents.length) 00206 return false; 00207 00208 while (lhsComponentCount--) 00209 if (lhsComponents[lhsComponentCount] != rhsComponents[lhsComponentCount]) 00210 return false; 00211 00212 if (lhs.pattern != rhs.pattern) 00213 return false; 00214 00215 if (CGColorSpaceEqualToColorSpace(lhs.colorspace, rhs.colorspace)) 00216 return false; 00217 00218 return true; 00219 } 00220 00227 function CGColorGetAlpha(aColor) 00228 { 00229 var components = aColor.components; 00230 00231 return components[components.length - 1]; 00232 } 00233 00239 function CGColorGetColorSpace(aColor) 00240 { 00241 return aColor.colorspace; 00242 } 00243 00250 function CGColorGetComponents(aColor) 00251 { 00252 return aColor.components; 00253 } 00254 00262 function CGColorGetNumberOfComponents(aColor) 00263 { 00264 return aColor.components.length; 00265 } 00266 00273 function CGColorGetPattern(aColor) 00274 { 00275 return aColor.pattern; 00276 } 00277 00278 /* var components = aColor.components; 00279 00280 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); 00281 _cssString = (hasAlpha ? "rgba(" : "rgb(") + 00282 parseInt(_components[0] * 255.0) + ", " + 00283 parseInt(_components[1] * 255.0) + ", " + 00284 parseInt(_components[2] * 255.0) + 00285 (hasAlpha ? (", " + _components[3]) : "") + ")"; 00286 00287 function CFStringFromColor() 00288 { 00289 00290 } 00291 */