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

http://github.com/jfahrenkrug/MapKit-HelloWorld · Unknown · 286 lines · 285 code · 1 blank · 0 comment · 0 complexity · ec51568e7fe077af9c51f658bc1c0279 MD5 · raw file

  1. i;15;CPColorPicker.jc;17294;
  2. {var the_class = objj_allocateClassPair(CPColorPicker, "CPSliderColorPicker"),
  3. meta_class = the_class.isa;class_addIvars(the_class, [new objj_ivar("_contentView"), new objj_ivar("_redSlider"), new objj_ivar("_greenSlider"), new objj_ivar("_blueSlider"), new objj_ivar("_hueSlider"), new objj_ivar("_saturationSlider"), new objj_ivar("_brightnessSlider"), new objj_ivar("_rgbLabel"), new objj_ivar("_hsbLabel"), new objj_ivar("_redLabel"), new objj_ivar("_greenLabel"), new objj_ivar("_blueLabel"), new objj_ivar("_hueLabel"), new objj_ivar("_saturationLabel"), new objj_ivar("_brightnessLabel"), new objj_ivar("_hexLabel"), new objj_ivar("_redValue"), new objj_ivar("_greenValue"), new objj_ivar("_blueValue"), new objj_ivar("_hueValue"), new objj_ivar("_saturationValue"), new objj_ivar("_brightnessValue"), new objj_ivar("_hexValue")]);
  4. objj_registerClassPair(the_class);
  5. objj_addClassForBundle(the_class, objj_getBundleWithPath(OBJJ_CURRENT_BUNDLE.path));
  6. class_addMethods(the_class, [new objj_method(sel_getUid("initWithPickerMask:colorPanel:"), function $CPSliderColorPicker__initWithPickerMask_colorPanel_(self, _cmd, mask, owningColorPanel)
  7. { with(self)
  8. {
  9. return objj_msgSendSuper({ receiver:self, super_class:objj_getClass("CPColorPicker") }, "initWithPickerMask:colorPanel:", mask, owningColorPanel);
  10. }
  11. }), new objj_method(sel_getUid("initView"), function $CPSliderColorPicker__initView(self, _cmd)
  12. { with(self)
  13. {
  14. aFrame = CPRectMake(0, 0, CPColorPickerViewWidth, CPColorPickerViewHeight);
  15. _contentView = objj_msgSend(objj_msgSend(CPView, "alloc"), "initWithFrame:", aFrame);
  16. objj_msgSend(_contentView, "setAutoresizingMask:", CPViewWidthSizable|CPViewHeightSizable);
  17. _rgbLabel = objj_msgSend(objj_msgSend(CPTextField, "alloc"), "initWithFrame:", CPRectMake(0, 10, 100, 20));
  18. objj_msgSend(_rgbLabel, "setStringValue:", "Red, Green, Blue");
  19. objj_msgSend(_rgbLabel, "setTextColor:", objj_msgSend(CPColor, "blackColor"));
  20. _redLabel = objj_msgSend(objj_msgSend(CPTextField, "alloc"), "initWithFrame:", CPRectMake(0, 35, 15, 20));
  21. objj_msgSend(_redLabel, "setStringValue:", "R");
  22. objj_msgSend(_redLabel, "setTextColor:", objj_msgSend(CPColor, "blackColor"));
  23. _redSlider = objj_msgSend(objj_msgSend(CPSlider, "alloc"), "initWithFrame:", CPRectMake(15, 35, aFrame.size.width - 70, 20));
  24. objj_msgSend(_redSlider, "setMaxValue:", 1.0);
  25. objj_msgSend(_redSlider, "setMinValue:", 0.0);
  26. objj_msgSend(_redSlider, "setTarget:", self);
  27. objj_msgSend(_redSlider, "setAction:", sel_getUid("sliderChanged:"));
  28. objj_msgSend(_redSlider, "setAutoresizingMask:", CPViewWidthSizable);
  29. var updateFunction = function(aDOMEvent)
  30. {
  31. if(isNaN(this.value))
  32. return;
  33. switch(this)
  34. {
  35. case _redValue: objj_msgSend(_redSlider, "setFloatValue:", MAX(MIN(ROUND(this.value), 255) / 255.0, 0));
  36. break;
  37. case _greenValue: objj_msgSend(_greenSlider, "setFloatValue:", MAX(MIN(ROUND(this.value), 255) / 255.0, 0));
  38. break;
  39. case _blueValue: objj_msgSend(_blueSlider, "setFloatValue:", MAX(MIN(ROUND(this.value), 255) / 255.0, 0));
  40. break;
  41. case _hueValue: objj_msgSend(_hueSlider, "setFloatValue:", MAX(MIN(ROUND(this.value), 360), 0));
  42. break;
  43. case _saturationValue: objj_msgSend(_saturationSlider, "setFloatValue:", MAX(MIN(ROUND(this.value), 100), 0));
  44. break;
  45. case _brightnessValue: objj_msgSend(_brightnessSlider, "setFloatValue:", MAX(MIN(ROUND(this.value), 100), 0));
  46. break;
  47. }
  48. this.blur();
  49. };
  50. var keypressFunction = function(aDOMEvent)
  51. {
  52. aDOMEvent = aDOMEvent || window.event;
  53. if (aDOMEvent.keyCode == 13)
  54. {
  55. updateFunction(aDOMEvent);
  56. if(aDOMEvent.preventDefault)
  57. aDOMEvent.preventDefault();
  58. else if(aDOMEvent.stopPropagation)
  59. aDOMEvent.stopPropagation();
  60. }
  61. }
  62. var redValue = objj_msgSend(objj_msgSend(CPView, "alloc"), "initWithFrame:", CPRectMake(aFrame.size.width - 45, 35, 45, 20));
  63. objj_msgSend(redValue, "setAutoresizingMask:", CPViewMinXMargin);
  64. _redValue = document.createElement("input");
  65. _redValue.style.width = "40px";
  66. _redValue.style.backgroundColor = "transparent";
  67. _redValue.style.border = "1px solid black";
  68. _redValue.style.color = "black";
  69. _redValue.style.position = "absolute";
  70. _redValue.style.top = "0px";
  71. _redValue.style.left = "0px";
  72. _redValue.onchange = updateFunction;
  73. redValue._DOMElement.appendChild(_redValue);
  74. objj_msgSend(_contentView, "addSubview:", redValue);
  75. _greenLabel = objj_msgSend(objj_msgSend(CPTextField, "alloc"), "initWithFrame:", CPRectMake(0, 58, 15, 20));
  76. objj_msgSend(_greenLabel, "setStringValue:", "G");
  77. objj_msgSend(_greenLabel, "setTextColor:", objj_msgSend(CPColor, "blackColor"));
  78. _greenSlider = objj_msgSend(objj_msgSend(CPSlider, "alloc"), "initWithFrame:", CPRectMake(15, 58, aFrame.size.width - 70, 20));
  79. objj_msgSend(_greenSlider, "setMaxValue:", 1.0);
  80. objj_msgSend(_greenSlider, "setMinValue:", 0.0);
  81. objj_msgSend(_greenSlider, "setTarget:", self);
  82. objj_msgSend(_greenSlider, "setAction:", sel_getUid("sliderChanged:"));
  83. objj_msgSend(_greenSlider, "setAutoresizingMask:", CPViewWidthSizable);
  84. var greenValue = objj_msgSend(objj_msgSend(CPView, "alloc"), "initWithFrame:", CPRectMake(aFrame.size.width - 45, 58, 45, 20));
  85. objj_msgSend(greenValue, "setAutoresizingMask:", CPViewMinXMargin);
  86. _greenValue = _redValue.cloneNode(false);
  87. _greenValue.onchange = updateFunction;
  88. greenValue._DOMElement.appendChild(_greenValue);
  89. objj_msgSend(_contentView, "addSubview:", greenValue);
  90. _blueLabel = objj_msgSend(objj_msgSend(CPTextField, "alloc"), "initWithFrame:", CPRectMake(0, 81, 15, 20));
  91. objj_msgSend(_blueLabel, "setStringValue:", "B");
  92. objj_msgSend(_blueLabel, "setTextColor:", objj_msgSend(CPColor, "blackColor"));
  93. _blueSlider = objj_msgSend(objj_msgSend(CPSlider, "alloc"), "initWithFrame:", CPRectMake(15, 81, aFrame.size.width - 70, 20));
  94. objj_msgSend(_blueSlider, "setMaxValue:", 1.0);
  95. objj_msgSend(_blueSlider, "setMinValue:", 0.0);
  96. objj_msgSend(_blueSlider, "setTarget:", self);
  97. objj_msgSend(_blueSlider, "setAction:", sel_getUid("sliderChanged:"));
  98. objj_msgSend(_blueSlider, "setAutoresizingMask:", CPViewWidthSizable);
  99. var blueValue = objj_msgSend(objj_msgSend(CPView, "alloc"), "initWithFrame:", CPRectMake(aFrame.size.width - 45, 81, 45, 20));
  100. objj_msgSend(blueValue, "setAutoresizingMask:", CPViewMinXMargin);
  101. _blueValue = _redValue.cloneNode(false);
  102. _blueValue.onchange = updateFunction;
  103. blueValue._DOMElement.appendChild(_blueValue);
  104. objj_msgSend(_contentView, "addSubview:", blueValue);
  105. _hsbLabel = objj_msgSend(objj_msgSend(CPTextField, "alloc"), "initWithFrame:", CPRectMake(0, 120, 190, 20));
  106. objj_msgSend(_hsbLabel, "setStringValue:", "Hue, Saturation, Brightness");
  107. objj_msgSend(_hsbLabel, "setTextColor:", objj_msgSend(CPColor, "blackColor"));
  108. _hueLabel = objj_msgSend(objj_msgSend(CPTextField, "alloc"), "initWithFrame:", CPRectMake(0, 145, 15, 20));
  109. objj_msgSend(_hueLabel, "setStringValue:", "H");
  110. objj_msgSend(_hueLabel, "setTextColor:", objj_msgSend(CPColor, "blackColor"));
  111. _hueSlider = objj_msgSend(objj_msgSend(CPSlider, "alloc"), "initWithFrame:", CPRectMake(15, 145, aFrame.size.width - 70, 20));
  112. objj_msgSend(_hueSlider, "setMaxValue:", 359.0);
  113. objj_msgSend(_hueSlider, "setMinValue:", 0.0);
  114. objj_msgSend(_hueSlider, "setTarget:", self);
  115. objj_msgSend(_hueSlider, "setAction:", sel_getUid("sliderChanged:"));
  116. objj_msgSend(_hueSlider, "setAutoresizingMask:", CPViewWidthSizable);
  117. var hueValue = objj_msgSend(objj_msgSend(CPView, "alloc"), "initWithFrame:", CPRectMake(aFrame.size.width - 45, 145, 45, 20));
  118. objj_msgSend(hueValue, "setAutoresizingMask:", CPViewMinXMargin);
  119. _hueValue = _redValue.cloneNode(false);
  120. _hueValue.onchange = updateFunction;
  121. hueValue._DOMElement.appendChild(_hueValue);
  122. objj_msgSend(_contentView, "addSubview:", hueValue);
  123. _saturationLabel = objj_msgSend(objj_msgSend(CPTextField, "alloc"), "initWithFrame:", CPRectMake(0, 168, 15, 20));
  124. objj_msgSend(_saturationLabel, "setStringValue:", "S");
  125. objj_msgSend(_saturationLabel, "setTextColor:", objj_msgSend(CPColor, "blackColor"));
  126. _saturationSlider = objj_msgSend(objj_msgSend(CPSlider, "alloc"), "initWithFrame:", CPRectMake(15, 168, aFrame.size.width - 70, 20));
  127. objj_msgSend(_saturationSlider, "setMaxValue:", 100.0);
  128. objj_msgSend(_saturationSlider, "setMinValue:", 0.0);
  129. objj_msgSend(_saturationSlider, "setTarget:", self);
  130. objj_msgSend(_saturationSlider, "setAction:", sel_getUid("sliderChanged:"));
  131. objj_msgSend(_saturationSlider, "setAutoresizingMask:", CPViewWidthSizable);
  132. var saturationValue = objj_msgSend(objj_msgSend(CPView, "alloc"), "initWithFrame:", CPRectMake(aFrame.size.width - 45, 168, 45, 20));
  133. objj_msgSend(saturationValue, "setAutoresizingMask:", CPViewMinXMargin);
  134. _saturationValue = _redValue.cloneNode(false);
  135. _saturationValue.onchange = updateFunction;
  136. saturationValue._DOMElement.appendChild(_saturationValue);
  137. objj_msgSend(_contentView, "addSubview:", saturationValue);
  138. _brightnessLabel = objj_msgSend(objj_msgSend(CPTextField, "alloc"), "initWithFrame:", CPRectMake(0, 191, 15, 20));
  139. objj_msgSend(_brightnessLabel, "setStringValue:", "B");
  140. objj_msgSend(_brightnessLabel, "setTextColor:", objj_msgSend(CPColor, "blackColor"));
  141. _brightnessSlider = objj_msgSend(objj_msgSend(CPSlider, "alloc"), "initWithFrame:", CPRectMake(15, 191, aFrame.size.width - 70, 20));
  142. objj_msgSend(_brightnessSlider, "setMaxValue:", 100.0);
  143. objj_msgSend(_brightnessSlider, "setMinValue:", 0.0);
  144. objj_msgSend(_brightnessSlider, "setTarget:", self);
  145. objj_msgSend(_brightnessSlider, "setAction:", sel_getUid("sliderChanged:"));
  146. objj_msgSend(_brightnessSlider, "setAutoresizingMask:", CPViewWidthSizable);
  147. var brightnessValue = objj_msgSend(objj_msgSend(CPView, "alloc"), "initWithFrame:", CPRectMake(aFrame.size.width - 45, 191, 45, 20));
  148. objj_msgSend(brightnessValue, "setAutoresizingMask:", CPViewMinXMargin);
  149. _brightnessValue = _redValue.cloneNode(false);
  150. _brightnessValue.onchange = updateFunction;
  151. brightnessValue._DOMElement.appendChild(_brightnessValue);
  152. objj_msgSend(_contentView, "addSubview:", brightnessValue);
  153. _hexLabel = objj_msgSend(objj_msgSend(CPTextField, "alloc"), "initWithFrame:", CPRectMake(0, 230, 30, 20));
  154. objj_msgSend(_hexLabel, "setStringValue:", "Hex");
  155. objj_msgSend(_hexLabel, "setTextColor:", objj_msgSend(CPColor, "blackColor"));
  156. _hexValue = _redValue.cloneNode(false);
  157. _hexValue.style.top = "228px";
  158. _hexValue.style.width = "80px";
  159. _hexValue.style.left = "35px";
  160. _hexValue.onkeypress = function(aDOMEvent)
  161. {
  162. aDOMEvent = aDOMEvent || window.event;
  163. if (aDOMEvent.keyCode == 13)
  164. {
  165. var newColor = objj_msgSend(CPColor, "colorWithHexString:", this.value);
  166. if(newColor)
  167. {
  168. objj_msgSend(self, "setColor:", newColor);
  169. objj_msgSend(objj_msgSend(self, "colorPanel"), "setColor:", newColor);
  170. }
  171. if(aDOMEvent.preventDefault)
  172. aDOMEvent.preventDefault();
  173. else if(aDOMEvent.stopPropagation)
  174. aDOMEvent.stopPropagation();
  175. this.blur();
  176. }
  177. };
  178. _contentView._DOMElement.appendChild(_hexValue);
  179. objj_msgSend(_contentView, "addSubview:", _rgbLabel);
  180. objj_msgSend(_contentView, "addSubview:", _redLabel);
  181. objj_msgSend(_contentView, "addSubview:", _greenLabel);
  182. objj_msgSend(_contentView, "addSubview:", _blueLabel);
  183. objj_msgSend(_contentView, "addSubview:", _redSlider);
  184. objj_msgSend(_contentView, "addSubview:", _greenSlider);
  185. objj_msgSend(_contentView, "addSubview:", _blueSlider);
  186. objj_msgSend(_contentView, "addSubview:", _hsbLabel);
  187. objj_msgSend(_contentView, "addSubview:", _hueLabel);
  188. objj_msgSend(_contentView, "addSubview:", _saturationLabel);
  189. objj_msgSend(_contentView, "addSubview:", _brightnessLabel);
  190. objj_msgSend(_contentView, "addSubview:", _hueSlider);
  191. objj_msgSend(_contentView, "addSubview:", _saturationSlider);
  192. objj_msgSend(_contentView, "addSubview:", _brightnessSlider);
  193. objj_msgSend(_contentView, "addSubview:", _hexLabel);
  194. }
  195. }), new objj_method(sel_getUid("provideNewView:"), function $CPSliderColorPicker__provideNewView_(self, _cmd, initialRequest)
  196. { with(self)
  197. {
  198. if (initialRequest)
  199. objj_msgSend(self, "initView");
  200. return _contentView;
  201. }
  202. }), new objj_method(sel_getUid("currentMode"), function $CPSliderColorPicker__currentMode(self, _cmd)
  203. { with(self)
  204. {
  205. return CPSliderColorPickerMode;
  206. }
  207. }), new objj_method(sel_getUid("supportsMode:"), function $CPSliderColorPicker__supportsMode_(self, _cmd, mode)
  208. { with(self)
  209. {
  210. return (mode == CPSliderColorPickerMode) ? YES : NO;
  211. }
  212. }), new objj_method(sel_getUid("sliderChanged:"), function $CPSliderColorPicker__sliderChanged_(self, _cmd, sender)
  213. { with(self)
  214. {
  215. var newColor,
  216. colorPanel = objj_msgSend(self, "colorPanel"),
  217. alpha = objj_msgSend(colorPanel, "opacity");
  218. switch(sender)
  219. {
  220. case _hueSlider:
  221. case _saturationSlider:
  222. case _brightnessSlider: newColor = objj_msgSend(CPColor, "colorWithHue:saturation:brightness:alpha:", objj_msgSend(_hueSlider, "floatValue"), objj_msgSend(_saturationSlider, "floatValue"), objj_msgSend(_brightnessSlider, "floatValue"), alpha);
  223. objj_msgSend(self, "updateRGBSliders:", newColor);
  224. break;
  225. case _redSlider:
  226. case _greenSlider:
  227. case _blueSlider: newColor = objj_msgSend(CPColor, "colorWithCalibratedRed:green:blue:alpha:", objj_msgSend(_redSlider, "floatValue"), objj_msgSend(_greenSlider, "floatValue"), objj_msgSend(_blueSlider, "floatValue"), alpha);
  228. objj_msgSend(self, "updateHSBSliders:", newColor);
  229. break;
  230. }
  231. objj_msgSend(self, "updateLabels");
  232. objj_msgSend(self, "updateHex:", newColor);
  233. objj_msgSend(colorPanel, "setColor:", newColor);
  234. }
  235. }), new objj_method(sel_getUid("setColor:"), function $CPSliderColorPicker__setColor_(self, _cmd, aColor)
  236. { with(self)
  237. {
  238. objj_msgSend(self, "updateRGBSliders:", aColor);
  239. objj_msgSend(self, "updateHSBSliders:", aColor);
  240. objj_msgSend(self, "updateHex:", aColor);
  241. objj_msgSend(self, "updateLabels");
  242. }
  243. }), new objj_method(sel_getUid("updateHSBSliders:"), function $CPSliderColorPicker__updateHSBSliders_(self, _cmd, aColor)
  244. { with(self)
  245. {
  246. var hsb = objj_msgSend(aColor, "hsbComponents");
  247. objj_msgSend(_hueSlider, "setFloatValue:", hsb[0]);
  248. objj_msgSend(_saturationSlider, "setFloatValue:", hsb[1]);
  249. objj_msgSend(_brightnessSlider, "setFloatValue:", hsb[2]);
  250. }
  251. }), new objj_method(sel_getUid("updateHex:"), function $CPSliderColorPicker__updateHex_(self, _cmd, aColor)
  252. { with(self)
  253. {
  254. _hexValue.value = objj_msgSend(aColor, "hexString");
  255. }
  256. }), new objj_method(sel_getUid("updateRGBSliders:"), function $CPSliderColorPicker__updateRGBSliders_(self, _cmd, aColor)
  257. { with(self)
  258. {
  259. var rgb = objj_msgSend(aColor, "components");
  260. objj_msgSend(_redSlider, "setFloatValue:", rgb[0]);
  261. objj_msgSend(_greenSlider, "setFloatValue:", rgb[1]);
  262. objj_msgSend(_blueSlider, "setFloatValue:", rgb[2]);
  263. }
  264. }), new objj_method(sel_getUid("updateLabels"), function $CPSliderColorPicker__updateLabels(self, _cmd)
  265. { with(self)
  266. {
  267. _hueValue.value = ROUND(objj_msgSend(_hueSlider, "floatValue"));
  268. _saturationValue.value = ROUND(objj_msgSend(_saturationSlider, "floatValue"));
  269. _brightnessValue.value = ROUND(objj_msgSend(_brightnessSlider, "floatValue"));
  270. _redValue.value = ROUND(objj_msgSend(_redSlider, "floatValue") * 255);
  271. _greenValue.value = ROUND(objj_msgSend(_greenSlider, "floatValue") * 255);
  272. _blueValue.value = ROUND(objj_msgSend(_blueSlider, "floatValue") * 255);
  273. }
  274. }), new objj_method(sel_getUid("provideNewButtonImage"), function $CPSliderColorPicker__provideNewButtonImage(self, _cmd)
  275. { with(self)
  276. {
  277. return objj_msgSend(objj_msgSend(CPImage, "alloc"), "initWithContentsOfFile:size:", objj_msgSend(objj_msgSend(CPBundle, "bundleForClass:", CPColorPicker), "pathForResource:", "slider_button.png"), CGSizeMake(32, 32));
  278. }
  279. }), new objj_method(sel_getUid("provideNewAlternateButtonImage"), function $CPSliderColorPicker__provideNewAlternateButtonImage(self, _cmd)
  280. { with(self)
  281. {
  282. return objj_msgSend(objj_msgSend(CPImage, "alloc"), "initWithContentsOfFile:size:", objj_msgSend(objj_msgSend(CPBundle, "bundleForClass:", CPColorPicker), "pathForResource:", "slider_button_h.png"), CGSizeMake(32, 32));
  283. }
  284. })]);
  285. }