ColorPicker.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// A color picker window that allows the user to select from a gamut of colors.
  9. /// </summary>
  10. public class ColorPicker : ModalWindow
  11. {
  12. private const int SliderIndividualWidth = 150;
  13. private const int SliderIndividualHeight = 15;
  14. private const int ColorBoxWidth = 200;
  15. private const int ColorBoxHeight = 200;
  16. private const int SliderSideWidth = 40;
  17. private const int SliderSideHeight = 200;
  18. private float colRed, colGreen, colBlue;
  19. private float colHue, colSaturation, colValue;
  20. private float colAlpha = 1.0f;
  21. private ColorSlider1DHorz sliderR, sliderG, sliderB, sliderA;
  22. private ColorSlider2D colorBox;
  23. private ColorSlider1DVert sideSlider;
  24. private ColorBoxMode colorBoxMode = ColorBoxMode.BG_R;
  25. private SliderMode sliderMode = SliderMode.HSV;
  26. private GUIColorField guiColor;
  27. private GUITexture guiSlider2DTex;
  28. private GUITexture guiSliderVertTex;
  29. private GUITexture guiSliderRHorzTex;
  30. private GUITexture guiSliderGHorzTex;
  31. private GUITexture guiSliderBHorzTex;
  32. private GUITexture guiSliderAHorzTex;
  33. private GUIButton guiColorBoxBtn;
  34. private GUIButton guiColorModeBtn;
  35. private GUISliderV guiSliderVert;
  36. private GUISliderH guiSliderRHorz;
  37. private GUISliderH guiSliderGHorz;
  38. private GUISliderH guiSliderBHorz;
  39. private GUISliderH guiSliderAHorz;
  40. private GUITexture guiSlider2DHandle;
  41. private GUILabel guiLabelR;
  42. private GUILabel guiLabelG;
  43. private GUILabel guiLabelB;
  44. private GUILabel guiLabelA;
  45. private GUIIntField guiInputR;
  46. private GUIIntField guiInputG;
  47. private GUIIntField guiInputB;
  48. private GUIIntField guiInputA;
  49. private GUIButton guiOK;
  50. private GUIButton guiCancel;
  51. private Action<bool, Color> closedCallback;
  52. /// <summary>
  53. /// Determines color gamut shown in the color box.
  54. /// </summary>
  55. public enum ColorBoxMode
  56. {
  57. BG_R,
  58. BR_G,
  59. RG_B,
  60. SV_H,
  61. HV_S,
  62. HS_V
  63. }
  64. /// <summary>
  65. /// Determines color gamut shown in the horizontal sliders.
  66. /// </summary>
  67. public enum SliderMode
  68. {
  69. RGB,
  70. HSV
  71. }
  72. /// <summary>
  73. /// Returns currently selected color as RGBA values.
  74. /// </summary>
  75. public Color SelectedColor
  76. {
  77. get
  78. {
  79. return new Color(colRed, colGreen, colBlue, colAlpha);
  80. }
  81. }
  82. /// <summary>
  83. /// Shows the color picker window.
  84. /// </summary>
  85. /// <param name="closedCallback">Optional callback to trigger when the user selects a color or cancels out
  86. /// of the dialog.</param>
  87. /// <returns>A. instance of the color picker window.</returns>
  88. public static ColorPicker Show(Action<bool, Color> closedCallback = null)
  89. {
  90. ColorPicker picker = new ColorPicker(Color.Black, closedCallback);
  91. return picker;
  92. }
  93. /// <summary>
  94. /// Shows the color picker window and sets the initial color to show.
  95. /// </summary>
  96. /// <param name="color">Initial color to display.</param>
  97. /// <param name="closedCallback">Optional callback to trigger when the user selects a color or cancels out
  98. /// of the dialog.</param>
  99. /// <returns>A. instance of the color picker window.</returns>
  100. public static ColorPicker Show(Color color, Action<bool, Color> closedCallback = null)
  101. {
  102. ColorPicker picker = new ColorPicker(color, closedCallback);
  103. return picker;
  104. }
  105. /// <summary>
  106. /// Constructs a new color picker window.
  107. /// </summary>
  108. /// <param name="color">Initial color to display.</param>
  109. /// <param name="closedCallback">Optional callback to trigger when the user selects a color or cancels out
  110. /// of the dialog.</param>
  111. protected ColorPicker(Color color, Action<bool, Color> closedCallback = null)
  112. : base(false)
  113. {
  114. Title = new LocEdString("Color Picker");
  115. Width = 270;
  116. Height = 400;
  117. colRed = color.r;
  118. colGreen = color.g;
  119. colBlue = color.b;
  120. colAlpha = color.a;
  121. RGBToHSV();
  122. this.closedCallback = closedCallback;
  123. }
  124. private void OnInitialize()
  125. {
  126. guiColor = new GUIColorField("", GUIOption.FixedWidth(100));
  127. guiSlider2DTex = new GUITexture(null, GUIOption.FixedHeight(ColorBoxHeight), GUIOption.FixedWidth(ColorBoxWidth));
  128. guiSliderVertTex = new GUITexture(null, GUIOption.FixedHeight(SliderSideHeight), GUIOption.FixedWidth(SliderSideWidth));
  129. guiSliderRHorzTex = new GUITexture(null, GUIOption.FixedHeight(SliderIndividualHeight));
  130. guiSliderGHorzTex = new GUITexture(null, GUIOption.FixedHeight(SliderIndividualHeight));
  131. guiSliderBHorzTex = new GUITexture(null, GUIOption.FixedHeight(SliderIndividualHeight));
  132. guiSliderAHorzTex = new GUITexture(null, GUIOption.FixedHeight(SliderIndividualHeight));
  133. guiColorBoxBtn = new GUIButton(colorBoxMode.ToString());
  134. guiColorModeBtn = new GUIButton(sliderMode.ToString());
  135. guiSliderVert = new GUISliderV(EditorStyles.ColorSliderVert);
  136. guiSliderRHorz = new GUISliderH(EditorStyles.ColorSliderHorz);
  137. guiSliderGHorz = new GUISliderH(EditorStyles.ColorSliderHorz);
  138. guiSliderBHorz = new GUISliderH(EditorStyles.ColorSliderHorz);
  139. guiSliderAHorz = new GUISliderH(EditorStyles.ColorSliderHorz);
  140. guiSlider2DHandle = new GUITexture(null, EditorStyles.ColorSlider2DHandle);
  141. guiLabelR = new GUILabel(new LocEdString("R"));
  142. guiLabelG = new GUILabel(new LocEdString("G"));
  143. guiLabelB = new GUILabel(new LocEdString("B"));
  144. guiLabelA = new GUILabel(new LocEdString("A"));
  145. guiInputR = new GUIIntField();
  146. guiInputG = new GUIIntField();
  147. guiInputB = new GUIIntField();
  148. guiInputA = new GUIIntField();
  149. guiOK = new GUIButton(new LocEdString("OK"));
  150. guiCancel = new GUIButton(new LocEdString("Cancel"));
  151. guiColorBoxBtn.OnClick += OnColorBoxModeChanged;
  152. guiColorModeBtn.OnClick += OnSliderModeChanged;
  153. guiSliderVert.OnChanged += OnSliderVertChanged;
  154. guiSliderRHorz.OnChanged += OnSliderRHorzChanged;
  155. guiSliderGHorz.OnChanged += OnSliderGHorzChanged;
  156. guiSliderBHorz.OnChanged += OnSliderBHorzChanged;
  157. guiSliderAHorz.OnChanged += OnSliderAHorzChanged;
  158. guiInputR.OnChanged += OnInputRChanged;
  159. guiInputG.OnChanged += OnInputGChanged;
  160. guiInputB.OnChanged += OnInputBChanged;
  161. guiInputA.OnChanged += OnInputAChanged;
  162. guiOK.OnClick += OnOK;
  163. guiCancel.OnClick += OnCancel;
  164. GUIPanel mainPanel = GUI.AddPanel(0);
  165. GUILayout v0 = mainPanel.AddLayoutY();
  166. v0.AddSpace(5);
  167. GUILayout h0 = v0.AddLayoutX();
  168. h0.AddSpace(10);
  169. h0.AddElement(guiColor);
  170. h0.AddFlexibleSpace();
  171. h0.AddElement(guiColorBoxBtn);
  172. h0.AddElement(guiColorModeBtn);
  173. h0.AddSpace(10);
  174. v0.AddSpace(10);
  175. GUILayout h1 = v0.AddLayoutX();
  176. h1.AddSpace(10);
  177. h1.AddElement(guiSlider2DTex);
  178. h1.AddFlexibleSpace();
  179. h1.AddElement(guiSliderVertTex);
  180. h1.AddSpace(10);
  181. v0.AddSpace(10);
  182. GUILayout h2 = v0.AddLayoutX();
  183. h2.AddSpace(10);
  184. h2.AddElement(guiLabelR);
  185. h2.AddFlexibleSpace();
  186. h2.AddElement(guiSliderRHorzTex);
  187. h2.AddFlexibleSpace();
  188. h2.AddElement(guiInputR);
  189. h2.AddSpace(10);
  190. v0.AddSpace(5);
  191. GUILayout h3 = v0.AddLayoutX();
  192. h3.AddSpace(10);
  193. h3.AddElement(guiLabelG);
  194. h3.AddFlexibleSpace();
  195. h3.AddElement(guiSliderGHorzTex);
  196. h3.AddFlexibleSpace();
  197. h3.AddElement(guiInputG);
  198. h3.AddSpace(10);
  199. v0.AddSpace(5);
  200. GUILayout h4 = v0.AddLayoutX();
  201. h4.AddSpace(10);
  202. h4.AddElement(guiLabelB);
  203. h4.AddFlexibleSpace();
  204. h4.AddElement(guiSliderBHorzTex);
  205. h4.AddFlexibleSpace();
  206. h4.AddElement(guiInputB);
  207. h4.AddSpace(10);
  208. v0.AddSpace(5);
  209. GUILayout h5 = v0.AddLayoutX();
  210. h5.AddSpace(10);
  211. h5.AddElement(guiLabelA);
  212. h5.AddFlexibleSpace();
  213. h5.AddElement(guiSliderAHorzTex);
  214. h5.AddFlexibleSpace();
  215. h5.AddElement(guiInputA);
  216. h5.AddSpace(10);
  217. v0.AddSpace(10);
  218. GUILayout h6 = v0.AddLayoutX();
  219. h6.AddFlexibleSpace();
  220. h6.AddElement(guiOK);
  221. h6.AddSpace(10);
  222. h6.AddElement(guiCancel);
  223. h6.AddFlexibleSpace();
  224. v0.AddSpace(5);
  225. GUIPanel overlay = GUI.AddPanel(-1);
  226. overlay.SetWidth(Width);
  227. overlay.SetHeight(Height);
  228. overlay.AddElement(guiSliderVert);
  229. overlay.AddElement(guiSliderRHorz);
  230. overlay.AddElement(guiSliderGHorz);
  231. overlay.AddElement(guiSliderBHorz);
  232. overlay.AddElement(guiSliderAHorz);
  233. overlay.AddElement(guiSlider2DHandle);
  234. colorBox = new ColorSlider2D(guiSlider2DTex, guiSlider2DHandle, ColorBoxWidth, ColorBoxHeight);
  235. sideSlider = new ColorSlider1DVert(guiSliderVertTex, guiSliderVert, SliderSideWidth, SliderSideHeight);
  236. sliderR = new ColorSlider1DHorz(guiSliderRHorzTex, guiSliderRHorz, SliderIndividualWidth, SliderIndividualHeight);
  237. sliderG = new ColorSlider1DHorz(guiSliderGHorzTex, guiSliderGHorz, SliderIndividualWidth, SliderIndividualHeight);
  238. sliderB = new ColorSlider1DHorz(guiSliderBHorzTex, guiSliderBHorz, SliderIndividualWidth, SliderIndividualHeight);
  239. sliderA = new ColorSlider1DHorz(guiSliderAHorzTex, guiSliderAHorz, SliderIndividualWidth, SliderIndividualHeight);
  240. colorBox.OnValueChanged += OnColorBoxValueChanged;
  241. Color startA = new Color(0, 0, 0, 1);
  242. Color stepA = new Color(1, 1, 1, 0);
  243. sliderA.UpdateTexture(startA, stepA, false);
  244. guiInputA.SetRange(0, 255);
  245. guiInputA.Value = 255;
  246. guiSliderAHorz.Percent = 1.0f;
  247. guiColor.Value = SelectedColor;
  248. UpdateInputBoxValues();
  249. Update2DSliderValues();
  250. Update1DSliderValues();
  251. UpdateSliderMode();
  252. Update2DSliderTextures();
  253. Update1DSliderTextures();
  254. }
  255. private void OnEditorUpdate()
  256. {
  257. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  258. colorBox.UpdateInput(windowPos);
  259. }
  260. /// <summary>
  261. /// Fills a 2D area with colors using the provided starting point and gradients.
  262. /// </summary>
  263. /// <param name="width">Width of the area to fill.</param>
  264. /// <param name="height">Height of the area to fill.</param>
  265. /// <param name="colors">Array to contain the output. Must be of
  266. /// <paramref name="width"/>*<paramref name="height"/> size.</param>
  267. /// <param name="start">Initial color in the top-left corner of the area.</param>
  268. /// <param name="rightGradient">Gradient towards which the colors increase to the right of the area.</param>
  269. /// <param name="downGradient">Gradient towards which the colors increase to the bottom of the area.</param>
  270. private static void FillArea(int width, int height, Color[] colors, Color start, Color rightGradient, Color downGradient)
  271. {
  272. Color rightDelta = new Color(0, 0, 0, 0);
  273. if (width > 1)
  274. rightDelta = rightGradient / (width - 1);
  275. Color downDelta = new Color(0, 0, 0, 0);
  276. if (height > 1)
  277. downDelta = downGradient / (height - 1);
  278. Color verticalColor = start;
  279. for (int y = 0; y < height; y++)
  280. {
  281. int rowIdx = (height - y - 1) * width;
  282. Color currentColor = verticalColor;
  283. for (int x = 0; x < width; x++)
  284. {
  285. colors[rowIdx + x] = currentColor;
  286. currentColor += rightDelta;
  287. }
  288. verticalColor += downDelta;
  289. }
  290. }
  291. /// <summary>
  292. /// Converts the currently selected color from HSV to RGB color space.
  293. /// </summary>
  294. void HSVToRGB()
  295. {
  296. Color hsv = new Color(colHue, colSaturation, colValue);
  297. Color rgb = Color.HSV2RGB(hsv);
  298. colRed = rgb.r;
  299. colGreen = rgb.g;
  300. colBlue = rgb.b;
  301. }
  302. /// <summary>
  303. /// Converts the currently selected color from RGB to HSV color space.
  304. /// </summary>
  305. void RGBToHSV()
  306. {
  307. Color rgb = new Color(colRed, colGreen, colBlue);
  308. Color hsv = Color.RGB2HSV(rgb);
  309. colHue = hsv.r;
  310. colSaturation = hsv.g;
  311. colValue = hsv.b;
  312. }
  313. /// <summary>
  314. /// Triggered when the user selects a new mode for the color box, changing the gamut of colors displayed in the box.
  315. /// </summary>
  316. void OnColorBoxModeChanged()
  317. {
  318. int maxModes = Enum.GetNames(colorBoxMode.GetType()).Length;
  319. colorBoxMode = (ColorBoxMode)(((int)colorBoxMode + 1) % maxModes);
  320. guiColorBoxBtn.SetContent(colorBoxMode.ToString());
  321. Update2DSliderTextures();
  322. Update2DSliderValues();
  323. }
  324. /// <summary>
  325. /// Triggered when the user selects a new mode for the side slider, changing the gamut of colors displayed in the
  326. /// slider.
  327. /// </summary>
  328. void OnSliderModeChanged()
  329. {
  330. int maxModes = Enum.GetNames(sliderMode.GetType()).Length;
  331. sliderMode = (SliderMode)(((int)sliderMode + 1) % maxModes);
  332. UpdateSliderMode();
  333. guiColorModeBtn.SetContent(sliderMode.ToString());
  334. UpdateInputBoxValues();
  335. Update1DSliderTextures();
  336. Update1DSliderValues();
  337. }
  338. /// <summary>
  339. /// Triggered when the user selects a color in the color box.
  340. /// </summary>
  341. /// <param name="value">Location on the color box that was selected.</param>
  342. void OnColorBoxValueChanged(Vector2 value)
  343. {
  344. switch (colorBoxMode)
  345. {
  346. case ColorBoxMode.BG_R:
  347. colBlue = value.x;
  348. colGreen = value.y;
  349. RGBToHSV();
  350. break;
  351. case ColorBoxMode.BR_G:
  352. colBlue = value.x;
  353. colRed = value.y;
  354. RGBToHSV();
  355. break;
  356. case ColorBoxMode.RG_B:
  357. colRed = value.x;
  358. colGreen = value.y;
  359. RGBToHSV();
  360. break;
  361. case ColorBoxMode.SV_H:
  362. colSaturation = value.x;
  363. colValue = value.y;
  364. HSVToRGB();
  365. break;
  366. case ColorBoxMode.HV_S:
  367. colHue = value.x;
  368. colValue = value.y;
  369. HSVToRGB();
  370. break;
  371. case ColorBoxMode.HS_V:
  372. colHue = value.x;
  373. colSaturation = value.y;
  374. HSVToRGB();
  375. break;
  376. }
  377. guiColor.Value = SelectedColor;
  378. UpdateInputBoxValues();
  379. Update1DSliderTextures();
  380. Update1DSliderValues();
  381. UpdateSideSliderTexture();
  382. Vector2 xy;
  383. float z;
  384. GetColorBoxValues(out xy, out z);
  385. guiSliderVert.Percent = 1.0f - z;
  386. }
  387. /// <summary>
  388. /// Triggered when the user moves the side slider.
  389. /// </summary>
  390. /// <param name="percent">New value of the slider.</param>
  391. void OnSliderVertChanged(float percent)
  392. {
  393. percent = 1.0f - percent;
  394. switch (colorBoxMode)
  395. {
  396. case ColorBoxMode.BG_R:
  397. colRed = percent;
  398. RGBToHSV();
  399. break;
  400. case ColorBoxMode.BR_G:
  401. colGreen = percent;
  402. RGBToHSV();
  403. break;
  404. case ColorBoxMode.RG_B:
  405. colBlue = percent;
  406. RGBToHSV();
  407. break;
  408. case ColorBoxMode.SV_H:
  409. colHue = percent;
  410. HSVToRGB();
  411. break;
  412. case ColorBoxMode.HV_S:
  413. colSaturation = percent;
  414. HSVToRGB();
  415. break;
  416. case ColorBoxMode.HS_V:
  417. colValue = percent;
  418. HSVToRGB();
  419. break;
  420. }
  421. guiColor.Value = SelectedColor;
  422. UpdateInputBoxValues();
  423. Update1DSliderTextures();
  424. Update1DSliderValues();
  425. }
  426. /// <summary>
  427. /// Triggered when the user moves the horizontal Red/Hue slider.
  428. /// </summary>
  429. /// <param name="percent">New value of the slider.</param>
  430. void OnSliderRHorzChanged(float percent)
  431. {
  432. bool isHSV = sliderMode == SliderMode.HSV;
  433. if (isHSV)
  434. {
  435. colHue = percent;
  436. HSVToRGB();
  437. }
  438. else
  439. {
  440. colRed = percent;
  441. RGBToHSV();
  442. }
  443. guiColor.Value = SelectedColor;
  444. UpdateInputBoxValues();
  445. Update2DSliderTextures();
  446. Update2DSliderValues();
  447. }
  448. /// <summary>
  449. /// Triggered when the user moves the horizontal Green/Saturation slider.
  450. /// </summary>
  451. /// <param name="percent">New value of the slider.</param>
  452. void OnSliderGHorzChanged(float percent)
  453. {
  454. bool isHSV = sliderMode == SliderMode.HSV;
  455. if (isHSV)
  456. {
  457. colSaturation = percent;
  458. HSVToRGB();
  459. }
  460. else
  461. {
  462. colGreen = percent;
  463. RGBToHSV();
  464. }
  465. guiColor.Value = SelectedColor;
  466. UpdateInputBoxValues();
  467. Update2DSliderTextures();
  468. Update2DSliderValues();
  469. }
  470. /// <summary>
  471. /// Triggered when the user moves the horizontal Blue/Value slider.
  472. /// </summary>
  473. /// <param name="percent">New value of the slider.</param>
  474. void OnSliderBHorzChanged(float percent)
  475. {
  476. bool isHSV = sliderMode == SliderMode.HSV;
  477. if (isHSV)
  478. {
  479. colValue = percent;
  480. HSVToRGB();
  481. }
  482. else
  483. {
  484. colBlue = percent;
  485. RGBToHSV();
  486. }
  487. guiColor.Value = SelectedColor;
  488. UpdateInputBoxValues();
  489. Update2DSliderTextures();
  490. Update2DSliderValues();
  491. }
  492. /// <summary>
  493. /// Triggered when the user moves the horizontal alpha slider.
  494. /// </summary>
  495. /// <param name="percent">New value of the slider.</param>
  496. void OnSliderAHorzChanged(float percent)
  497. {
  498. colAlpha = percent;
  499. guiColor.Value = SelectedColor;
  500. guiInputA.Value = MathEx.RoundToInt(colAlpha * 255.0f);
  501. }
  502. /// <summary>
  503. /// Triggered when the user inputs new value in the Red/Hue input box.
  504. /// </summary>
  505. /// <param name="value">New value in the input box.</param>
  506. void OnInputRChanged(int value)
  507. {
  508. bool isHSV = sliderMode == SliderMode.HSV;
  509. if (isHSV)
  510. {
  511. colHue = value/359.0f;
  512. HSVToRGB();
  513. }
  514. else
  515. {
  516. colRed = value/255.0f;
  517. RGBToHSV();
  518. }
  519. guiColor.Value = SelectedColor;
  520. Update1DSliderValues();
  521. Update2DSliderTextures();
  522. Update2DSliderValues();
  523. }
  524. /// <summary>
  525. /// Triggered when the user inputs new value in the Green/Saturation input box.
  526. /// </summary>
  527. /// <param name="value">New value in the input box.</param>
  528. void OnInputGChanged(int value)
  529. {
  530. bool isHSV = sliderMode == SliderMode.HSV;
  531. if (isHSV)
  532. {
  533. colSaturation = value / 255.0f;
  534. HSVToRGB();
  535. }
  536. else
  537. {
  538. colGreen = value / 255.0f;
  539. RGBToHSV();
  540. }
  541. guiColor.Value = SelectedColor;
  542. Update1DSliderValues();
  543. Update2DSliderTextures();
  544. Update2DSliderValues();
  545. }
  546. /// <summary>
  547. /// Triggered when the user inputs new value in the Blue/Value input box.
  548. /// </summary>
  549. /// <param name="value">New value in the input box.</param>
  550. void OnInputBChanged(int value)
  551. {
  552. bool isHSV = sliderMode == SliderMode.HSV;
  553. if (isHSV)
  554. {
  555. colValue = value / 255.0f;
  556. HSVToRGB();
  557. }
  558. else
  559. {
  560. colBlue = value / 255.0f;
  561. RGBToHSV();
  562. }
  563. guiColor.Value = SelectedColor;
  564. Update1DSliderValues();
  565. Update2DSliderTextures();
  566. Update2DSliderValues();
  567. }
  568. /// <summary>
  569. /// Triggered when the user inputs new value in the alpha input box.
  570. /// </summary>
  571. /// <param name="value">New value in the input box.</param>
  572. void OnInputAChanged(int value)
  573. {
  574. colAlpha = value/255.0f;
  575. guiColor.Value = SelectedColor;
  576. guiSliderAHorz.Percent = colAlpha;
  577. }
  578. /// <summary>
  579. /// Triggered when the user selects a color and closes the dialog.
  580. /// </summary>
  581. void OnOK()
  582. {
  583. if (closedCallback != null)
  584. closedCallback(true, SelectedColor);
  585. Close();
  586. }
  587. /// <summary>
  588. /// Triggered when the user cancels color selection and closes the dialog.
  589. /// </summary>
  590. void OnCancel()
  591. {
  592. if (closedCallback != null)
  593. closedCallback(false, SelectedColor);
  594. Close();
  595. }
  596. /// <summary>
  597. /// Updates Red/Green/Blue or Hue/Saturation/Value labels and input box ranges depending on currently active mode.
  598. /// </summary>
  599. void UpdateSliderMode()
  600. {
  601. if (sliderMode == SliderMode.RGB)
  602. {
  603. guiLabelR.SetContent(new LocEdString("R"));
  604. guiLabelG.SetContent(new LocEdString("G"));
  605. guiLabelB.SetContent(new LocEdString("B"));
  606. guiInputR.SetRange(0, 255);
  607. guiInputG.SetRange(0, 255);
  608. guiInputB.SetRange(0, 255);
  609. }
  610. else
  611. {
  612. guiLabelR.SetContent(new LocEdString("H"));
  613. guiLabelG.SetContent(new LocEdString("S"));
  614. guiLabelB.SetContent(new LocEdString("V"));
  615. guiInputR.SetRange(0, 359);
  616. guiInputG.SetRange(0, 255);
  617. guiInputB.SetRange(0, 255);
  618. }
  619. }
  620. /// <summary>
  621. /// Updates Red/Green/Blue or Hue/Saturation/Value input boxes with currently selected color.
  622. /// </summary>
  623. void UpdateInputBoxValues()
  624. {
  625. bool isHSV = sliderMode == SliderMode.HSV;
  626. if (isHSV)
  627. {
  628. guiInputR.Value = MathEx.RoundToInt(colHue * 359.0f);
  629. guiInputG.Value = MathEx.RoundToInt(colSaturation * 255.0f);
  630. guiInputB.Value = MathEx.RoundToInt(colValue * 255.0f);
  631. }
  632. else
  633. {
  634. guiInputR.Value = MathEx.RoundToInt(colRed * 255.0f);
  635. guiInputG.Value = MathEx.RoundToInt(colGreen * 255.0f);
  636. guiInputB.Value = MathEx.RoundToInt(colBlue * 255.0f);
  637. }
  638. }
  639. /// <summary>
  640. /// Updates Red/Green/Blue or Hue/Saturation/Value sliders with currently selected color.
  641. /// </summary>
  642. void Update1DSliderValues()
  643. {
  644. bool isHSV = sliderMode == SliderMode.HSV;
  645. if (isHSV)
  646. {
  647. guiSliderRHorz.Percent = colHue;
  648. guiSliderGHorz.Percent = colSaturation;
  649. guiSliderBHorz.Percent = colValue;
  650. }
  651. else
  652. {
  653. guiSliderRHorz.Percent = colRed;
  654. guiSliderGHorz.Percent = colGreen;
  655. guiSliderBHorz.Percent = colBlue;
  656. }
  657. }
  658. /// <summary>
  659. /// Returns the current color in the form of color box and side slider coordinates.
  660. /// </summary>
  661. /// <param name="xy">Coordinates on the color box the current color is located on.</param>
  662. /// <param name="z">Coordinates on the side slider the current color is located on.</param>
  663. void GetColorBoxValues(out Vector2 xy, out float z)
  664. {
  665. xy = Vector2.Zero;
  666. z = 0.0f;
  667. switch (colorBoxMode)
  668. {
  669. case ColorBoxMode.BG_R:
  670. xy.x = colBlue;
  671. xy.y = colGreen;
  672. z = colRed;
  673. break;
  674. case ColorBoxMode.BR_G:
  675. xy.x = colBlue;
  676. xy.y = colRed;
  677. z = colGreen;
  678. break;
  679. case ColorBoxMode.RG_B:
  680. xy.x = colRed;
  681. xy.y = colGreen;
  682. z = colBlue;
  683. break;
  684. case ColorBoxMode.SV_H:
  685. xy.x = colSaturation;
  686. xy.y = colValue;
  687. z = colHue;
  688. break;
  689. case ColorBoxMode.HV_S:
  690. xy.x = colHue;
  691. xy.y = colValue;
  692. z = colSaturation;
  693. break;
  694. case ColorBoxMode.HS_V:
  695. xy.x = colHue;
  696. xy.y = colSaturation;
  697. z = colValue;
  698. break;
  699. }
  700. }
  701. /// <summary>
  702. /// Updates values of the color box and side slider according to the current color.
  703. /// </summary>
  704. void Update2DSliderValues()
  705. {
  706. Vector2 xy = Vector2.Zero;
  707. float z = 0.0f;
  708. GetColorBoxValues(out xy, out z);
  709. colorBox.SetValue(xy);
  710. guiSliderVert.Percent = z;
  711. }
  712. /// <summary>
  713. /// Generates textures to display for all horizontal (RGB/HSV) sliders depending on active slider mode.
  714. /// </summary>
  715. void Update1DSliderTextures()
  716. {
  717. bool isHSV = sliderMode == SliderMode.HSV;
  718. if (isHSV)
  719. {
  720. Color startH = new Color(0, 1, 1);
  721. Color stepH = new Color(1, 0, 0, 0);
  722. sliderR.UpdateTexture(startH, stepH, true);
  723. Color startS = new Color(colHue, 0, MathEx.Max(colValue, 0.2f));
  724. Color stepS = new Color(0, 1, 0, 0);
  725. sliderG.UpdateTexture(startS, stepS, true);
  726. Color startV = new Color(colHue, colSaturation, 0);
  727. Color stepV = new Color(0, 0, 1, 0);
  728. sliderB.UpdateTexture(startV, stepV, true);
  729. }
  730. else
  731. {
  732. Color startR = new Color(0, colGreen, colBlue);
  733. Color stepR = new Color(1, 0, 0, 0);
  734. sliderR.UpdateTexture(startR, stepR, false);
  735. Color startG = new Color(colRed, 0, colBlue);
  736. Color stepG = new Color(0, 1, 0, 0);
  737. sliderG.UpdateTexture(startG, stepG, false);
  738. Color startB = new Color(colRed, colGreen, 0);
  739. Color stepB = new Color(0, 0, 1, 0);
  740. sliderB.UpdateTexture(startB, stepB, false);
  741. }
  742. }
  743. /// <summary>
  744. /// Generates a texture for the side slider depending on active color box mode.
  745. /// </summary>
  746. void UpdateSideSliderTexture()
  747. {
  748. switch (colorBoxMode)
  749. {
  750. case ColorBoxMode.BG_R:
  751. sideSlider.UpdateTexture(new Color(0, colGreen, colBlue, 1), new Color(1, 0, 0, 0), false);
  752. break;
  753. case ColorBoxMode.BR_G:
  754. sideSlider.UpdateTexture(new Color(colRed, 0, colBlue, 1), new Color(0, 1, 0, 0), false);
  755. break;
  756. case ColorBoxMode.RG_B:
  757. sideSlider.UpdateTexture(new Color(colRed, colGreen, 0, 1), new Color(0, 0, 1, 0), false);
  758. break;
  759. case ColorBoxMode.SV_H:
  760. sideSlider.UpdateTexture(new Color(0, 1, 1, 1), new Color(1, 0, 0, 0), true);
  761. break;
  762. case ColorBoxMode.HV_S:
  763. sideSlider.UpdateTexture(new Color(colHue, 0, MathEx.Max(colValue, 0.2f), 1), new Color(0, 1, 0, 0), true);
  764. break;
  765. case ColorBoxMode.HS_V:
  766. sideSlider.UpdateTexture(new Color(colHue, colSaturation, 0, 1), new Color(0, 0, 1, 0), true);
  767. break;
  768. }
  769. }
  770. /// <summary>
  771. /// Generates textures for the color box and the side slider depending on active color box mode.
  772. /// </summary>
  773. void Update2DSliderTextures()
  774. {
  775. UpdateSideSliderTexture();
  776. float[] valueLookup = new float[] { colRed, colGreen, colBlue, colHue, colSaturation, colValue };
  777. colorBox.UpdateTexture(colorBoxMode, valueLookup[(int)colorBoxMode]);
  778. }
  779. /// <summary>
  780. /// Manages GUI for a 1D horizontal slider (used RGB/HSV display).
  781. /// </summary>
  782. public class ColorSlider1DHorz
  783. {
  784. private const int SLIDER_X_OFFSET = 3;
  785. private const int SLIDER_Y_OFFSET = 5;
  786. private int width, height;
  787. private Texture2D texture;
  788. private SpriteTexture spriteTexture;
  789. private GUITexture guiTexture;
  790. private GUISliderH guiSlider;
  791. /// <summary>
  792. /// Creates a new horizontal slider.
  793. /// </summary>
  794. /// <param name="guiTexture">GUI element to display the slider color range on.</param>
  795. /// <param name="guiSlider">Slider rendered on top of the texture that may be moved by the user to select a
  796. /// color.</param>
  797. /// <param name="width">Width of the slider in pixels.</param>
  798. /// <param name="height">Height of the slider in pixels.</param>
  799. public ColorSlider1DHorz(GUITexture guiTexture, GUISliderH guiSlider, int width, int height)
  800. {
  801. this.width = width;
  802. this.height = height;
  803. this.guiTexture = guiTexture;
  804. this.guiSlider = guiSlider;
  805. texture = new Texture2D(width, height);
  806. spriteTexture = new SpriteTexture(texture);
  807. }
  808. /// <summary>
  809. /// Updates the displayed texture with specified color information.
  810. /// </summary>
  811. /// <param name="start">Initial color on the left of the slider.</param>
  812. /// <param name="step">Final color to the right of the slider.</param>
  813. /// <param name="isHSV">Determines are the provided colors in RGB or HSV space.</param>
  814. public void UpdateTexture(Color start, Color step, bool isHSV)
  815. {
  816. Color[] colors = new Color[width * height];
  817. FillArea(width, height, colors, start, step, new Color(0, 0, 0, 0));
  818. if (isHSV)
  819. {
  820. for (int i = 0; i < colors.Length; i++)
  821. colors[i] = Color.HSV2RGB(colors[i]);
  822. }
  823. texture.SetPixels(colors);
  824. guiTexture.SetTexture(spriteTexture);
  825. Rect2I sliderBounds = guiTexture.Bounds;
  826. sliderBounds.x -= SLIDER_X_OFFSET;
  827. sliderBounds.width += SLIDER_X_OFFSET * 2;
  828. sliderBounds.y -= SLIDER_Y_OFFSET;
  829. sliderBounds.height += SLIDER_Y_OFFSET;
  830. guiSlider.Bounds = sliderBounds;
  831. }
  832. }
  833. /// <summary>
  834. /// Manages GUI for a 1D vertical slider (side slider along with the color box).
  835. /// </summary>
  836. public class ColorSlider1DVert
  837. {
  838. private const int SLIDER_X_OFFSET = 5;
  839. private const int SLIDER_Y_OFFSET = 3;
  840. private int width, height;
  841. private Texture2D texture;
  842. private SpriteTexture spriteTexture;
  843. private GUITexture guiTexture;
  844. private GUISliderV guiSlider;
  845. /// <summary>
  846. /// Creates a new vertical slider.
  847. /// </summary>
  848. /// <param name="guiTexture">GUI element to display the slider color range on.</param>
  849. /// <param name="guiSlider">Slider rendered on top of the texture that may be moved by the user to select a
  850. /// color.</param>
  851. /// <param name="width">Width of the slider in pixels.</param>
  852. /// <param name="height">Height of the slider in pixels.</param>
  853. public ColorSlider1DVert(GUITexture guiTexture, GUISliderV guiSlider, int width, int height)
  854. {
  855. this.width = width;
  856. this.height = height;
  857. this.guiTexture = guiTexture;
  858. this.guiSlider = guiSlider;
  859. texture = new Texture2D(width, height);
  860. spriteTexture = new SpriteTexture(texture);
  861. }
  862. /// <summary>
  863. /// Updates the displayed texture with specified color information.
  864. /// </summary>
  865. /// <param name="start">Initial color on the top of the slider.</param>
  866. /// <param name="step">Final color to the bottom of the slider.</param>
  867. /// <param name="isHSV">Determines are the provided colors in RGB or HSV space.</param>
  868. public void UpdateTexture(Color start, Color step, bool isHSV)
  869. {
  870. Color[] colors = new Color[width * height];
  871. FillArea(width, height, colors, start, new Color(0, 0, 0, 0), step);
  872. if (isHSV)
  873. {
  874. for (int i = 0; i < colors.Length; i++)
  875. colors[i] = Color.HSV2RGB(colors[i]);
  876. }
  877. texture.SetPixels(colors);
  878. guiTexture.SetTexture(spriteTexture);
  879. Rect2I sliderBounds = guiTexture.Bounds;
  880. sliderBounds.x -= SLIDER_X_OFFSET;
  881. sliderBounds.width += SLIDER_X_OFFSET;
  882. sliderBounds.y -= SLIDER_Y_OFFSET;
  883. sliderBounds.height += SLIDER_Y_OFFSET * 2;
  884. guiSlider.Bounds = sliderBounds;
  885. }
  886. }
  887. /// <summary>
  888. /// Manages GUI for a 2D color box, as well as manually handling color box input. Color box serves as a 2D sliders
  889. /// as you can portion of it to select a color.
  890. /// </summary>
  891. public class ColorSlider2D
  892. {
  893. private int width, height;
  894. private Texture2D texture;
  895. private SpriteTexture spriteTexture;
  896. private GUITexture guiTexture;
  897. private GUITexture guiSliderHandle;
  898. private Vector2 oldValue = new Vector2(-1, -1);
  899. public delegate void OnValueChangedDelegate(Vector2 value);
  900. public event OnValueChangedDelegate OnValueChanged;
  901. /// <summary>
  902. /// Creates a new color box.
  903. /// </summary>
  904. /// <param name="guiTexture">GUI element to display the 2D color range on.</param>
  905. /// <param name="guiSliderHandle">Texture to be used for displaying the position of the currently selected
  906. /// color.</param>
  907. /// <param name="width">Width of the slider in pixels.</param>
  908. /// <param name="height">Height of the slider in pixels.</param>
  909. public ColorSlider2D(GUITexture guiTexture, GUITexture guiSliderHandle, int width, int height)
  910. {
  911. this.width = width;
  912. this.height = height;
  913. this.guiTexture = guiTexture;
  914. this.guiSliderHandle = guiSliderHandle;
  915. texture = new Texture2D(width, height);
  916. spriteTexture = new SpriteTexture(texture);
  917. }
  918. /// <summary>
  919. /// Updates the texture displayed on the color box.
  920. /// </summary>
  921. /// <param name="mode">Mode determining the color gamut shown in the color box.</param>
  922. /// <param name="value">Value of the third component (normally retrieved from the separate side slider).</param>
  923. public void UpdateTexture(ColorBoxMode mode, float value)
  924. {
  925. Color[] colors = new Color[width * height];
  926. switch (mode)
  927. {
  928. case ColorBoxMode.BG_R:
  929. FillArea(width, height, colors, new Color(value, 0, 0, 1), new Color(0, 0, 1, 0), new Color(0, 1, 0, 0));
  930. break;
  931. case ColorBoxMode.BR_G:
  932. FillArea(width, height, colors, new Color(0, value, 0, 1), new Color(0, 0, 1, 0), new Color(1, 0, 0, 0));
  933. break;
  934. case ColorBoxMode.RG_B:
  935. FillArea(width, height, colors, new Color(0, 0, value, 1), new Color(1, 0, 0, 0), new Color(0, 1, 0, 0));
  936. break;
  937. case ColorBoxMode.SV_H:
  938. FillArea(width, height, colors, new Color(value, 0, 0, 1), new Color(0, 1, 0, 0), new Color(0, 0, 1, 0));
  939. for (int i = 0; i < colors.Length; i++)
  940. colors[i] = Color.HSV2RGB(colors[i]);
  941. break;
  942. case ColorBoxMode.HV_S:
  943. FillArea(width, height, colors, new Color(0, value, 0, 1), new Color(1, 0, 0, 0), new Color(0, 0, 1, 0));
  944. for (int i = 0; i < colors.Length; i++)
  945. colors[i] = Color.HSV2RGB(colors[i]);
  946. break;
  947. case ColorBoxMode.HS_V:
  948. FillArea(width, height, colors, new Color(0, 0, value, 1), new Color(1, 0, 0, 0), new Color(0, 1, 0, 0));
  949. for (int i = 0; i < colors.Length; i++)
  950. colors[i] = Color.HSV2RGB(colors[i]);
  951. break;
  952. }
  953. texture.SetPixels(colors);
  954. guiTexture.SetTexture(spriteTexture);
  955. }
  956. /// <summary>
  957. /// Handles input over the color box, moving the handle as needed.
  958. /// </summary>
  959. /// <param name="windowPos">Position of the pointer relative to the color picker window.</param>
  960. public void UpdateInput(Vector2I windowPos)
  961. {
  962. if (Input.IsPointerButtonHeld(PointerButton.Left))
  963. {
  964. Rect2I bounds = guiTexture.Bounds;
  965. if (bounds.Contains(windowPos))
  966. {
  967. Vector2 newValue = Vector2.Zero;
  968. newValue.x = (windowPos.x - bounds.x) / (float)bounds.width;
  969. newValue.y = 1.0f - (windowPos.y - bounds.y) / (float)bounds.height;
  970. SetValue(newValue);
  971. }
  972. }
  973. }
  974. /// <summary>
  975. /// Moves the handle to a specific location on the color box and selects that color.
  976. /// </summary>
  977. /// <param name="value">Coordinates relative to the color box.</param>
  978. public void SetValue(Vector2 value)
  979. {
  980. Vector2 pos = value;
  981. pos.y = 1.0f - pos.y;
  982. if (oldValue == value)
  983. return;
  984. Rect2I handleBounds = guiSliderHandle.Bounds;
  985. Rect2I boxBounds = guiTexture.Bounds;
  986. handleBounds.x = boxBounds.x + MathEx.RoundToInt(pos.x * boxBounds.width) - handleBounds.width / 2;
  987. handleBounds.y = boxBounds.y + MathEx.RoundToInt(pos.y * boxBounds.height) - handleBounds.height / 2;
  988. guiSliderHandle.Bounds = handleBounds;
  989. oldValue = value;
  990. if (OnValueChanged != null)
  991. OnValueChanged(value);
  992. }
  993. }
  994. }
  995. }