/Frameworks/Debug/AppKit/browser.platform/CPColor.j

http://github.com/jfahrenkrug/MapKit-HelloWorld · Unknown · 412 lines · 411 code · 1 blank · 0 comment · 0 complexity · 9cbf78f88e996d931f3c0479b565786b MD5 · raw file

  1. I;21;Foundation/CPObject.ji;9;CGColor.ji;17;CPCompatibility.ji;9;CPImage.jc;19669;
  2. var _redComponent = 0,
  3. _greenComponent = 1,
  4. _blueComponent = 2,
  5. _alphaCompnent = 3;
  6. var _hueComponent = 0,
  7. _saturationComponent = 1,
  8. _brightnessComponent = 2;
  9. var cachedBlackColor,
  10. cachedRedColor,
  11. cachedGreenColor,
  12. cachedBlueColor,
  13. cachedYellowColor,
  14. cachedGrayColor,
  15. cachedLightGrayColor,
  16. cachedDarkGrayColor,
  17. cachedWhiteColor,
  18. cachedShadowColor,
  19. cachedClearColor;
  20. {var the_class = objj_allocateClassPair(CPObject, "CPColor"),
  21. meta_class = the_class.isa;class_addIvars(the_class, [new objj_ivar("_components"), new objj_ivar("_patternImage"), new objj_ivar("_cssString")]);
  22. objj_registerClassPair(the_class);
  23. objj_addClassForBundle(the_class, objj_getBundleWithPath(OBJJ_CURRENT_BUNDLE.path));
  24. class_addMethods(the_class, [new objj_method(sel_getUid("_initWithCSSString:"), function $CPColor___initWithCSSString_(self, _cmd, aString)
  25. { with(self)
  26. {
  27. if(aString.indexOf("rgb") == CPNotFound)
  28. return nil;
  29. self = objj_msgSendSuper({ receiver:self, super_class:objj_getClass("CPObject") }, "init");
  30. var startingIndex = aString.indexOf("(");
  31. var parts = aString.substring(startingIndex+1).split(',');
  32. _components = [
  33. parseInt(parts[0], 10) / 255.0,
  34. parseInt(parts[1], 10) / 255.0,
  35. parseInt(parts[2], 10) / 255.0,
  36. parts[3] ? parseInt(parts[3], 10) / 255.0 : 1.0
  37. ]
  38. _cssString = aString;
  39. return self;
  40. }
  41. }), new objj_method(sel_getUid("_initWithRGBA:"), function $CPColor___initWithRGBA_(self, _cmd, components)
  42. { with(self)
  43. {
  44. self = objj_msgSendSuper({ receiver:self, super_class:objj_getClass("CPObject") }, "init");
  45. if (self)
  46. {
  47. _components = components;
  48. if (!CPFeatureIsCompatible(CPCSSRGBAFeature) && _components[3] != 1.0 && window.Base64 && window.CRC32)
  49. {
  50. var bytes = [0x89,0x50,0x4e,0x47,0xd,0xa,0x1a,0xa,0x0,0x0,0x0,0xd,0x49,0x48,0x44,0x52,0x0,0x0,0x0,0x1,0x0,0x0,0x0,0x1,0x8,0x3,0x0,0x0,0x0,0x28,0xcb,0x34,0xbb,0x0,0x0,0x3,0x0,0x50,0x4c,0x54,0x45,0xff,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x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0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x17,0x89,0x99,0x55,0x0,0x0,0x0,0x1,0x74,0x52,0x4e,0x53,0x0,0x40,0xe6,0xd8,0x66,0x0,0x0,0x0,0x10,0x49,0x44,0x41,0x54,0x78,0xda,0x62,0x60,0x0,0x0,0x0,0x0,0xff,0xff,0x3,0x0,0x0,0x2,0x0,0x1,0x24,0x7f,0x24,0xf1,0x0,0x0,0x0,0x0,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82,0xff];
  51. var r_off = 41;
  52. var g_off = 42;
  53. var b_off = 43;
  54. var a_off = 821;
  55. var plte_crc_off = 809;
  56. var trns_crc_off = 822;
  57. var plte_type_off = 37;
  58. var trns_type_off = 817;
  59. bytes[r_off] = Math.round(_components[0]*255);
  60. bytes[g_off] = Math.round(_components[1]*255);
  61. bytes[b_off] = Math.round(_components[2]*255);
  62. bytes[a_off] = Math.round(_components[3]*255);
  63. var new_plte_crc = integerToBytes(CRC32.getCRC(bytes, plte_type_off, 4+768), 4);
  64. var new_trns_crc = integerToBytes(CRC32.getCRC(bytes, trns_type_off, 4+1), 4);
  65. for (var i = 0; i < 4; i++)
  66. {
  67. bytes[plte_crc_off+i] = new_plte_crc[i];
  68. bytes[trns_crc_off+i] = new_trns_crc[i];
  69. }
  70. var base64image = Base64.encode(bytes);
  71. _cssString = "url(\"data:image/png;base64," + base64image + "\")";
  72. }
  73. else
  74. {
  75. var hasAlpha = CPFeatureIsCompatible(CPCSSRGBAFeature) && _components[3] != 1.0;
  76. _cssString = (hasAlpha ? "rgba(" : "rgb(") +
  77. parseInt(_components[0] * 255.0) + ", " +
  78. parseInt(_components[1] * 255.0) + ", " +
  79. parseInt(_components[2] * 255.0) +
  80. (hasAlpha ? (", " + _components[3]) : "") + ")";
  81. }
  82. }
  83. return self;
  84. }
  85. }), new objj_method(sel_getUid("_initWithPatternImage:"), function $CPColor___initWithPatternImage_(self, _cmd, anImage)
  86. { with(self)
  87. {
  88. self = objj_msgSendSuper({ receiver:self, super_class:objj_getClass("CPObject") }, "init");
  89. if (self)
  90. {
  91. _patternImage = anImage;
  92. _cssString = "url(\"" + objj_msgSend(_patternImage, "filename") + "\")";
  93. }
  94. return self;
  95. }
  96. }), new objj_method(sel_getUid("patternImage"), function $CPColor__patternImage(self, _cmd)
  97. { with(self)
  98. {
  99. return _patternImage;
  100. }
  101. }), new objj_method(sel_getUid("alphaComponent"), function $CPColor__alphaComponent(self, _cmd)
  102. { with(self)
  103. {
  104. return _components[3];
  105. }
  106. }), new objj_method(sel_getUid("blueComponent"), function $CPColor__blueComponent(self, _cmd)
  107. { with(self)
  108. {
  109. return _components[2];
  110. }
  111. }), new objj_method(sel_getUid("greenComponent"), function $CPColor__greenComponent(self, _cmd)
  112. { with(self)
  113. {
  114. return _components[1];
  115. }
  116. }), new objj_method(sel_getUid("redComponent"), function $CPColor__redComponent(self, _cmd)
  117. { with(self)
  118. {
  119. return _components[0];
  120. }
  121. }), new objj_method(sel_getUid("components"), function $CPColor__components(self, _cmd)
  122. { with(self)
  123. {
  124. return _components;
  125. }
  126. }), new objj_method(sel_getUid("colorWithAlphaComponent:"), function $CPColor__colorWithAlphaComponent_(self, _cmd, anAlphaComponent)
  127. { with(self)
  128. {
  129. var components = _components.slice();
  130. components[components.length - 1] = anAlphaComponent;
  131. return objj_msgSend(objj_msgSend(objj_msgSend(self, "class"), "alloc"), "_initWithRGBA:", components);
  132. }
  133. }), new objj_method(sel_getUid("hsbComponents"), function $CPColor__hsbComponents(self, _cmd)
  134. { with(self)
  135. {
  136. var red = ROUND(_components[_redComponent] * 255.0),
  137. green = ROUND(_components[_greenComponent] * 255.0),
  138. blue = ROUND(_components[_blueComponent] * 255.0);
  139. var max = MAX(red, green, blue),
  140. min = MIN(red, green, blue),
  141. delta = max - min;
  142. var brightness = max / 255.0,
  143. saturation = (max != 0) ? delta / max : 0;
  144. var hue;
  145. if(saturation == 0)
  146. hue = 0;
  147. else
  148. {
  149. var rr = (max - red) / delta;
  150. var gr = (max - green) / delta;
  151. var br = (max - blue) / delta;
  152. if (red == max)
  153. hue = br - gr;
  154. else if (green == max)
  155. hue = 2 + rr - br;
  156. else
  157. hue = 4 + gr - rr;
  158. hue /= 6;
  159. if (hue < 0)
  160. hue++;
  161. }
  162. return [
  163. ROUND(hue * 360.0),
  164. ROUND(saturation * 100.0),
  165. ROUND(brightness * 100.0)
  166. ];
  167. }
  168. }), new objj_method(sel_getUid("cssString"), function $CPColor__cssString(self, _cmd)
  169. { with(self)
  170. {
  171. return _cssString;
  172. }
  173. }), new objj_method(sel_getUid("hexString"), function $CPColor__hexString(self, _cmd)
  174. { with(self)
  175. {
  176. return rgbToHex(objj_msgSend(self, "redComponent"), objj_msgSend(self, "greenComponent"), objj_msgSend(self, "blueComponent"))
  177. }
  178. }), new objj_method(sel_getUid("isEqual:"), function $CPColor__isEqual_(self, _cmd, aColor)
  179. { with(self)
  180. {
  181. if (!aColor)
  182. return NO;
  183. if (aColor === self)
  184. return YES;
  185. return objj_msgSend(aColor, "isKindOfClass:", CPColor) && objj_msgSend(aColor, "cssString") === objj_msgSend(self, "cssString");
  186. }
  187. }), new objj_method(sel_getUid("description"), function $CPColor__description(self, _cmd)
  188. { with(self)
  189. {
  190. return objj_msgSendSuper({ receiver:self, super_class:objj_getClass("CPObject") }, "description")+" "+objj_msgSend(self, "cssString");
  191. }
  192. })]);
  193. class_addMethods(meta_class, [new objj_method(sel_getUid("colorWithRed:green:blue:alpha:"), function $CPColor__colorWithRed_green_blue_alpha_(self, _cmd, red, green, blue, alpha)
  194. { with(self)
  195. {
  196. return objj_msgSend(objj_msgSend(CPColor, "alloc"), "_initWithRGBA:", [red, green, blue, alpha]);
  197. }
  198. }), new objj_method(sel_getUid("colorWithCalibratedRed:green:blue:alpha:"), function $CPColor__colorWithCalibratedRed_green_blue_alpha_(self, _cmd, red, green, blue, alpha)
  199. { with(self)
  200. {
  201. return objj_msgSend(self, "colorWithRed:green:blue:alpha:", red, green, blue, alpha);
  202. }
  203. }), new objj_method(sel_getUid("colorWithWhite:alpha:"), function $CPColor__colorWithWhite_alpha_(self, _cmd, white, alpha)
  204. { with(self)
  205. {
  206. return objj_msgSend(objj_msgSend(CPColor, "alloc"), "_initWithRGBA:", [white, white, white, alpha]);
  207. }
  208. }), new objj_method(sel_getUid("colorWithCalibratedWhite:alpha:"), function $CPColor__colorWithCalibratedWhite_alpha_(self, _cmd, white, alpha)
  209. { with(self)
  210. {
  211. return objj_msgSend(self, "colorWithWhite:alpha:", white, alpha);
  212. }
  213. }), new objj_method(sel_getUid("colorWithHue:saturation:brightness:"), function $CPColor__colorWithHue_saturation_brightness_(self, _cmd, hue, saturation, brightness)
  214. { with(self)
  215. {
  216. return objj_msgSend(self, "colorWithHue:saturation:brightness:alpha:", hue, saturation, brightness, 1.0);
  217. }
  218. }), new objj_method(sel_getUid("colorWithHue:saturation:brightness:alpha:"), function $CPColor__colorWithHue_saturation_brightness_alpha_(self, _cmd, hue, saturation, brightness, alpha)
  219. { with(self)
  220. {
  221. if(saturation === 0.0)
  222. return objj_msgSend(CPColor, "colorWithCalibratedWhite:alpha:", brightness / 100.0, alpha);
  223. var f = hue % 60,
  224. p = (brightness * (100 - saturation)) / 10000,
  225. q = (brightness * (6000 - saturation * f)) / 600000,
  226. t = (brightness * (6000 - saturation * (60 -f))) / 600000,
  227. b = brightness / 100.0;
  228. switch(FLOOR(hue / 60))
  229. {
  230. case 0: return objj_msgSend(CPColor, "colorWithCalibratedRed:green:blue:alpha:", b, t, p, alpha);
  231. case 1: return objj_msgSend(CPColor, "colorWithCalibratedRed:green:blue:alpha:", q, b, p, alpha);
  232. case 2: return objj_msgSend(CPColor, "colorWithCalibratedRed:green:blue:alpha:", p, b, t, alpha);
  233. case 3: return objj_msgSend(CPColor, "colorWithCalibratedRed:green:blue:alpha:", p, q, b, alpha);
  234. case 4: return objj_msgSend(CPColor, "colorWithCalibratedRed:green:blue:alpha:", t, p, b, alpha);
  235. case 5: return objj_msgSend(CPColor, "colorWithCalibratedRed:green:blue:alpha:", b, p, q, alpha);
  236. }
  237. }
  238. }), new objj_method(sel_getUid("colorWithHexString:"), function $CPColor__colorWithHexString_(self, _cmd, hex)
  239. { with(self)
  240. {
  241. return objj_msgSend(objj_msgSend(CPColor, "alloc"), "_initWithRGBA:", hexToRGB(hex));
  242. }
  243. }), new objj_method(sel_getUid("blackColor"), function $CPColor__blackColor(self, _cmd)
  244. { with(self)
  245. {
  246. if (!cachedBlackColor)
  247. cachedBlackColor = objj_msgSend(objj_msgSend(CPColor, "alloc"), "_initWithRGBA:", [0.0, 0.0, 0.0, 1.0]);
  248. return cachedBlackColor;
  249. }
  250. }), new objj_method(sel_getUid("blueColor"), function $CPColor__blueColor(self, _cmd)
  251. { with(self)
  252. {
  253. if (!cachedBlueColor)
  254. cachedBlueColor = objj_msgSend(objj_msgSend(CPColor, "alloc"), "_initWithRGBA:", [0.0, 0.0, 1.0, 1.0]);
  255. return cachedBlueColor;
  256. }
  257. }), new objj_method(sel_getUid("darkGrayColor"), function $CPColor__darkGrayColor(self, _cmd)
  258. { with(self)
  259. {
  260. if (!cachedDarkGrayColor)
  261. cachedDarkGrayColor = objj_msgSend(CPColor, "colorWithCalibratedWhite:alpha:", 1.0 / 3.0, 1.0);
  262. return cachedDarkGrayColor;
  263. }
  264. }), new objj_method(sel_getUid("grayColor"), function $CPColor__grayColor(self, _cmd)
  265. { with(self)
  266. {
  267. if (!cachedGrayColor)
  268. cachedGrayColor = objj_msgSend(CPColor, "colorWithCalibratedWhite:alpha:", 0.5, 1.0);
  269. return cachedGrayColor;
  270. }
  271. }), new objj_method(sel_getUid("greenColor"), function $CPColor__greenColor(self, _cmd)
  272. { with(self)
  273. {
  274. if (!cachedGreenColor)
  275. cachedGreenColor = objj_msgSend(objj_msgSend(CPColor, "alloc"), "_initWithRGBA:", [0.0, 1.0, 0.0, 1.0]);
  276. return cachedGreenColor;
  277. }
  278. }), new objj_method(sel_getUid("lightGrayColor"), function $CPColor__lightGrayColor(self, _cmd)
  279. { with(self)
  280. {
  281. if (!cachedLightGrayColor)
  282. cachedLightGrayColor = objj_msgSend(CPColor, "colorWithCalibratedWhite:alpha:", 2.0 / 3.0, 1.0);
  283. return cachedLightGrayColor;
  284. }
  285. }), new objj_method(sel_getUid("redColor"), function $CPColor__redColor(self, _cmd)
  286. { with(self)
  287. {
  288. if (!cachedRedColor)
  289. cachedRedColor = objj_msgSend(objj_msgSend(CPColor, "alloc"), "_initWithRGBA:", [1.0, 0.0, 0.0, 1.0]);
  290. return cachedRedColor;
  291. }
  292. }), new objj_method(sel_getUid("whiteColor"), function $CPColor__whiteColor(self, _cmd)
  293. { with(self)
  294. {
  295. if (!cachedWhiteColor)
  296. cachedWhiteColor = objj_msgSend(objj_msgSend(CPColor, "alloc"), "_initWithRGBA:", [1.0, 1.0, 1.0, 1.0]);
  297. return cachedWhiteColor;
  298. }
  299. }), new objj_method(sel_getUid("yellowColor"), function $CPColor__yellowColor(self, _cmd)
  300. { with(self)
  301. {
  302. if (!cachedYellowColor)
  303. cachedYellowColor = objj_msgSend(objj_msgSend(CPColor, "alloc"), "_initWithRGBA:", [1.0, 1.0, 0.0, 1.0]);
  304. return cachedYellowColor;
  305. }
  306. }), new objj_method(sel_getUid("shadowColor"), function $CPColor__shadowColor(self, _cmd)
  307. { with(self)
  308. {
  309. if (!cachedShadowColor)
  310. cachedShadowColor = objj_msgSend(objj_msgSend(CPColor, "alloc"), "_initWithRGBA:", [0.0, 0.0, 0.0, 1.0 / 3.0]);
  311. return cachedShadowColor;
  312. }
  313. }), new objj_method(sel_getUid("clearColor"), function $CPColor__clearColor(self, _cmd)
  314. { with(self)
  315. {
  316. if (!cachedClearColor)
  317. cachedClearColor = objj_msgSend(self, "colorWithCalibratedWhite:alpha:", 0.0, 0.0);
  318. return cachedClearColor;
  319. }
  320. }), new objj_method(sel_getUid("colorWithPatternImage:"), function $CPColor__colorWithPatternImage_(self, _cmd, anImage)
  321. { with(self)
  322. {
  323. return objj_msgSend(objj_msgSend(CPColor, "alloc"), "_initWithPatternImage:", anImage);
  324. }
  325. }), new objj_method(sel_getUid("colorWithCSSString:"), function $CPColor__colorWithCSSString_(self, _cmd, aString)
  326. { with(self)
  327. {
  328. return objj_msgSend(objj_msgSend(CPColor, "alloc"), "_initWithCSSString:", aString);
  329. }
  330. })]);
  331. }
  332. {
  333. var the_class = objj_getClass("CPColor")
  334. if(!the_class) objj_exception_throw(new objj_exception(OBJJClassNotFoundException, "*** Could not find definition for class \"CPColor\""));
  335. var meta_class = the_class.isa;class_addMethods(the_class, [new objj_method(sel_getUid("set"), function $CPColor__set(self, _cmd)
  336. { with(self)
  337. {
  338. objj_msgSend(self, "setFill");
  339. objj_msgSend(self, "setStroke");
  340. }
  341. }), new objj_method(sel_getUid("setFill"), function $CPColor__setFill(self, _cmd)
  342. { with(self)
  343. {
  344. var ctx = objj_msgSend(objj_msgSend(CPGraphicsContext, "currentContext"), "graphicsPort");
  345. CGContextSetFillColor(ctx, self);
  346. }
  347. }), new objj_method(sel_getUid("setStroke"), function $CPColor__setStroke(self, _cmd)
  348. { with(self)
  349. {
  350. var ctx = objj_msgSend(objj_msgSend(CPGraphicsContext, "currentContext"), "graphicsPort");
  351. CGContextSetStrokeColor(ctx, self);
  352. }
  353. })]);
  354. }
  355. var CPColorComponentsKey = "CPColorComponentsKey",
  356. CPColorPatternImageKey = "CPColorPatternImageKey";
  357. {
  358. var the_class = objj_getClass("CPColor")
  359. if(!the_class) objj_exception_throw(new objj_exception(OBJJClassNotFoundException, "*** Could not find definition for class \"CPColor\""));
  360. var meta_class = the_class.isa;class_addMethods(the_class, [new objj_method(sel_getUid("initWithCoder:"), function $CPColor__initWithCoder_(self, _cmd, aCoder)
  361. { with(self)
  362. {
  363. if (objj_msgSend(aCoder, "containsValueForKey:", CPColorPatternImageKey))
  364. return objj_msgSend(self, "_initWithPatternImage:", objj_msgSend(aCoder, "decodeObjectForKey:", CPColorPatternImageKey));
  365. return objj_msgSend(self, "_initWithRGBA:", objj_msgSend(aCoder, "decodeObjectForKey:", CPColorComponentsKey));
  366. }
  367. }), new objj_method(sel_getUid("encodeWithCoder:"), function $CPColor__encodeWithCoder_(self, _cmd, aCoder)
  368. { with(self)
  369. {
  370. if (_patternImage)
  371. objj_msgSend(aCoder, "encodeObject:forKey:", _patternImage, CPColorPatternImageKey);
  372. else
  373. objj_msgSend(aCoder, "encodeObject:forKey:", _components, CPColorComponentsKey);
  374. }
  375. })]);
  376. }
  377. var hexCharacters = "0123456789ABCDEF";
  378. hexToRGB= function(hex)
  379. {
  380. if ( hex.length == 3 )
  381. hex = hex.charAt(0) + hex.charAt(0) + hex.charAt(1) + hex.charAt(1) + hex.charAt(2) + hex.charAt(2);
  382. if(hex.length != 6)
  383. return null;
  384. hex = hex.toUpperCase();
  385. for(var i=0; i<hex.length; i++)
  386. if(hexCharacters.indexOf(hex.charAt(i)) == -1)
  387. return null;
  388. var red = (hexCharacters.indexOf(hex.charAt(0)) * 16 + hexCharacters.indexOf(hex.charAt(1))) / 255.0;
  389. var green = (hexCharacters.indexOf(hex.charAt(2)) * 16 + hexCharacters.indexOf(hex.charAt(3))) / 255.0;
  390. var blue = (hexCharacters.indexOf(hex.charAt(4)) * 16 + hexCharacters.indexOf(hex.charAt(5))) / 255.0;
  391. return [red, green, blue, 1.0];
  392. }
  393. integerToBytes= function(integer, length) {
  394. if (!length)
  395. length = (integer == 0) ? 1 : Math.round((Math.log(integer)/Math.log(2))/8+0.5);
  396. var bytes = new Array(length);
  397. for (var i = length-1; i >= 0; i--) {
  398. bytes[i] = integer & 255;
  399. integer = integer >> 8
  400. }
  401. return bytes;
  402. }
  403. rgbToHex= function(r,g,b) {
  404. return byteToHex(r) + byteToHex(g) + byteToHex(b);
  405. }
  406. byteToHex= function(n) {
  407. if (!n || isNaN(n)) return "00";
  408. n = ROUND(MIN(255,MAX(0,256*n)));
  409. return hexCharacters.charAt((n - n % 16) / 16) +
  410. hexCharacters.charAt(n % 16);
  411. }