ColorPicker.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. using System;
  2. using BansheeEngine;
  3. namespace BansheeEditor
  4. {
  5. public class ColorPicker : ModalWindow
  6. {
  7. private const int SliderIndividualWidth = 150;
  8. private const int SliderIndividualHeight = 20;
  9. private const int ColorBoxWidth = 150;
  10. private const int ColorBoxHeight = 150;
  11. private const int SliderSideWidth = 40;
  12. private const int SliderSideHeight = 150;
  13. private float colRed, colGreen, colBlue;
  14. private float colHue, colSaturation, colValue;
  15. private float colAlpha = 1.0f;
  16. private ColorSlider1DHorz sliderR, sliderG, sliderB, sliderA;
  17. private ColorSlider2D colorBox;
  18. private ColorSlider1DVert sideSlider;
  19. private ColorBoxMode colorBoxMode;
  20. private SliderMode sliderMode;
  21. private GUIColorField guiColor;
  22. private GUITexture guiSlider2DTex;
  23. private GUITexture guiSliderVertTex;
  24. private GUITexture guiSliderRHorzTex;
  25. private GUITexture guiSliderGHorzTex;
  26. private GUITexture guiSliderBHorzTex;
  27. private GUITexture guiSliderAHorzTex;
  28. private GUIButton guiColorBoxBtn;
  29. private GUIButton guiColorModeBtn;
  30. private GUISliderV guiSliderVert;
  31. private GUISliderH guiSliderRHorz;
  32. private GUISliderH guiSliderGHorz;
  33. private GUISliderH guiSliderBHorz;
  34. private GUISliderH guiSliderAHorz;
  35. private GUITexture guiSlider2DHandle;
  36. private GUILabel guiLabelR;
  37. private GUILabel guiLabelG;
  38. private GUILabel guiLabelB;
  39. private GUILabel guiLabelA;
  40. private GUIIntField guiInputR;
  41. private GUIIntField guiInputG;
  42. private GUIIntField guiInputB;
  43. private GUIIntField guiInputA;
  44. public enum ColorBoxMode
  45. {
  46. BG_R,
  47. BR_G,
  48. RG_B,
  49. SV_H,
  50. HV_S,
  51. HS_V
  52. }
  53. public enum SliderMode
  54. {
  55. RGB,
  56. HSV
  57. }
  58. public Color SelectedColor
  59. {
  60. get
  61. {
  62. return new Color(colRed, colGreen, colBlue, colAlpha);
  63. }
  64. }
  65. public static ColorPicker Show()
  66. {
  67. return new ColorPicker();
  68. }
  69. protected ColorPicker()
  70. : base(false)
  71. {
  72. Title = "Color Picker";
  73. Width = 400;
  74. Height = 800;
  75. }
  76. private void OnInitialize()
  77. {
  78. guiColor = new GUIColorField();
  79. guiSlider2DTex = new GUITexture(null);
  80. guiSliderVertTex = new GUITexture(null);
  81. guiSliderRHorzTex = new GUITexture(null);
  82. guiSliderGHorzTex = new GUITexture(null);
  83. guiSliderBHorzTex = new GUITexture(null);
  84. guiSliderAHorzTex = new GUITexture(null);
  85. guiColorBoxBtn = new GUIButton(colorBoxMode.ToString());
  86. guiColorModeBtn = new GUIButton(sliderMode.ToString());
  87. guiSliderVert = new GUISliderV(EditorStyles.ColorSliderVert);
  88. guiSliderRHorz = new GUISliderH(EditorStyles.ColorSliderHorz);
  89. guiSliderGHorz = new GUISliderH(EditorStyles.ColorSliderHorz);
  90. guiSliderBHorz = new GUISliderH(EditorStyles.ColorSliderHorz);
  91. guiSliderAHorz = new GUISliderH(EditorStyles.ColorSliderHorz);
  92. guiSlider2DHandle = new GUITexture(null);
  93. guiLabelR = new GUILabel("R");
  94. guiLabelG = new GUILabel("G");
  95. guiLabelB = new GUILabel("B");
  96. guiLabelA = new GUILabel("A");
  97. guiInputR = new GUIIntField();
  98. guiInputG = new GUIIntField();
  99. guiInputB = new GUIIntField();
  100. guiInputA = new GUIIntField();
  101. guiColorBoxBtn.OnClick += OnColorBoxModeChanged;
  102. guiColorModeBtn.OnClick += OnSliderModeChanged;
  103. guiSliderVert.OnChanged += OnSliderVertChanged;
  104. guiSliderRHorz.OnChanged += OnSliderRHorzChanged;
  105. guiSliderGHorz.OnChanged += OnSliderGHorzChanged;
  106. guiSliderBHorz.OnChanged += OnSliderBHorzChanged;
  107. guiSliderAHorz.OnChanged += OnSliderAHorzChanged;
  108. guiInputR.OnChanged += OnInputRChanged;
  109. guiInputG.OnChanged += OnInputGChanged;
  110. guiInputB.OnChanged += OnInputBChanged;
  111. guiInputA.OnChanged += OnInputAChanged;
  112. GUILayout v0 = GUI.layout.AddLayoutY();
  113. GUILayout h0 = v0.AddLayoutX();
  114. h0.AddElement(guiColor);
  115. h0.AddFlexibleSpace();
  116. h0.AddElement(guiColorBoxBtn);
  117. h0.AddElement(guiColorModeBtn);
  118. v0.AddSpace(20);
  119. GUILayout h1 = v0.AddLayoutX();
  120. h1.AddElement(guiSlider2DTex);
  121. h1.AddFlexibleSpace();
  122. h1.AddElement(guiSliderVertTex);
  123. h1.AddSpace(10);
  124. v0.AddSpace(30);
  125. GUILayout h2 = v0.AddLayoutX();
  126. h2.AddElement(guiLabelR);
  127. h2.AddFlexibleSpace();
  128. h2.AddElement(guiSliderRHorzTex);
  129. h2.AddFlexibleSpace();
  130. h2.AddElement(guiInputR);
  131. v0.AddSpace(15);
  132. GUILayout h3 = v0.AddLayoutX();
  133. h3.AddElement(guiLabelG);
  134. h3.AddFlexibleSpace();
  135. h3.AddElement(guiSliderGHorzTex);
  136. h3.AddFlexibleSpace();
  137. h3.AddElement(guiInputG);
  138. v0.AddSpace(15);
  139. GUILayout h4 = v0.AddLayoutX();
  140. h4.AddElement(guiLabelB);
  141. h4.AddFlexibleSpace();
  142. h4.AddElement(guiSliderBHorzTex);
  143. h4.AddFlexibleSpace();
  144. h4.AddElement(guiInputB);
  145. v0.AddSpace(15);
  146. GUILayout h5 = v0.AddLayoutX();
  147. h5.AddElement(guiLabelA);
  148. h5.AddFlexibleSpace();
  149. h5.AddElement(guiSliderAHorzTex);
  150. h5.AddFlexibleSpace();
  151. h5.AddElement(guiInputA);
  152. GUIArea overlay = GUI.AddArea(0, 0, Width, Height, -1, GUILayoutType.Explicit);
  153. overlay.layout.AddElement(guiSliderVert);
  154. overlay.layout.AddElement(guiSliderRHorz);
  155. overlay.layout.AddElement(guiSliderGHorz);
  156. overlay.layout.AddElement(guiSliderBHorz);
  157. overlay.layout.AddElement(guiSliderAHorz);
  158. overlay.layout.AddElement(guiSlider2DHandle);
  159. colorBox = new ColorSlider2D(guiSlider2DTex, guiSlider2DHandle, ColorBoxWidth, ColorBoxHeight);
  160. sideSlider = new ColorSlider1DVert(guiSliderVertTex, guiSliderVert, SliderSideWidth, SliderSideHeight);
  161. sliderR = new ColorSlider1DHorz(guiSliderRHorzTex, guiSliderRHorz, SliderIndividualWidth, SliderIndividualHeight);
  162. sliderG = new ColorSlider1DHorz(guiSliderGHorzTex, guiSliderGHorz, SliderIndividualWidth, SliderIndividualHeight);
  163. sliderB = new ColorSlider1DHorz(guiSliderBHorzTex, guiSliderBHorz, SliderIndividualWidth, SliderIndividualHeight);
  164. sliderA = new ColorSlider1DHorz(guiSliderAHorzTex, guiSliderAHorz, SliderIndividualWidth, SliderIndividualHeight);
  165. colorBox.OnValueChanged += OnColorBoxValueChanged;
  166. guiColor.Value = SelectedColor;
  167. UpdateInputBoxValues();
  168. Update2DSliderTextures();
  169. Update2DSliderValues();
  170. Update1DSliderTextures();
  171. Update1DSliderValues();
  172. Color startA = new Color(0, 0, 0, 1);
  173. Color stepA = new Color(1, 1, 1, 0);
  174. sliderA.UpdateTexture(startA, stepA, false);
  175. guiInputA.SetRange(0, 255);
  176. }
  177. private void OnEditorUpdate()
  178. {
  179. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  180. colorBox.UpdateInput(windowPos);
  181. Debug.Log(Width + " - " + Height + " - " + GUI.childAreas[0].mCachedPtr);
  182. }
  183. private static void FillArea(int width, int height, Color[] colors, Color start, Color rightGradient, Color downGradient)
  184. {
  185. Color rightDelta = new Color(0, 0, 0, 0);
  186. if (width > 1)
  187. rightDelta = rightGradient / (width - 1);
  188. Color downDelta = new Color(0, 0, 0, 0);
  189. if (height > 1)
  190. downDelta = downGradient / (height - 1);
  191. Color verticalColor = start;
  192. int idx = 0;
  193. for (int y = 0; y < height; y++)
  194. {
  195. Color currentColor = verticalColor;
  196. for (int x = 0; x < width; x++)
  197. {
  198. colors[idx++] = currentColor;
  199. currentColor += rightDelta;
  200. }
  201. verticalColor += downDelta;
  202. }
  203. }
  204. void HSVToRGB()
  205. {
  206. Color hsv = new Color(colHue, colSaturation, colValue);
  207. Color rgb = Color.HSV2RGB(hsv);
  208. colRed = rgb.r;
  209. colGreen = rgb.g;
  210. colBlue = rgb.b;
  211. }
  212. void RGBToHSV()
  213. {
  214. Color rgb = new Color(colRed, colGreen, colBlue);
  215. Color hsv = Color.RGB2HSV(rgb);
  216. colHue = hsv.r;
  217. colSaturation = hsv.g;
  218. colValue = hsv.b;
  219. }
  220. void OnColorBoxModeChanged()
  221. {
  222. int maxModes = Enum.GetNames(colorBoxMode.GetType()).Length;
  223. colorBoxMode = (ColorBoxMode)(((int)colorBoxMode + 1) % maxModes);
  224. guiColorBoxBtn.SetContent(colorBoxMode.ToString());
  225. Update2DSliderTextures();
  226. Update2DSliderValues();
  227. }
  228. void OnSliderModeChanged()
  229. {
  230. int maxModes = Enum.GetNames(sliderMode.GetType()).Length;
  231. sliderMode = (SliderMode)(((int)sliderMode + 1) % maxModes);
  232. if (sliderMode == SliderMode.RGB)
  233. {
  234. guiLabelR.SetContent("R");
  235. guiLabelG.SetContent("G");
  236. guiLabelB.SetContent("B");
  237. guiInputR.SetRange(0, 255);
  238. guiInputG.SetRange(0, 255);
  239. guiInputB.SetRange(0, 255);
  240. }
  241. else
  242. {
  243. guiLabelR.SetContent("H");
  244. guiLabelG.SetContent("S");
  245. guiLabelB.SetContent("V");
  246. guiInputR.SetRange(0, 359);
  247. guiInputG.SetRange(0, 255);
  248. guiInputB.SetRange(0, 255);
  249. }
  250. guiColorModeBtn.SetContent(sliderMode.ToString());
  251. UpdateInputBoxValues();
  252. Update1DSliderTextures();
  253. Update1DSliderValues();
  254. }
  255. void OnColorBoxValueChanged(Vector2 value)
  256. {
  257. switch (colorBoxMode)
  258. {
  259. case ColorBoxMode.BG_R:
  260. colGreen = value.x;
  261. colBlue = value.y;
  262. RGBToHSV();
  263. break;
  264. case ColorBoxMode.BR_G:
  265. colRed = value.x;
  266. colBlue = value.y;
  267. RGBToHSV();
  268. break;
  269. case ColorBoxMode.RG_B:
  270. colRed = value.x;
  271. colGreen = value.y;
  272. RGBToHSV();
  273. break;
  274. case ColorBoxMode.SV_H:
  275. colSaturation = value.x;
  276. colValue = value.y;
  277. HSVToRGB();
  278. break;
  279. case ColorBoxMode.HV_S:
  280. colHue = value.x;
  281. colValue = value.y;
  282. HSVToRGB();
  283. break;
  284. case ColorBoxMode.HS_V:
  285. colHue = value.x;
  286. colSaturation = value.y;
  287. HSVToRGB();
  288. break;
  289. }
  290. guiColor.Value = SelectedColor;
  291. UpdateInputBoxValues();
  292. Update1DSliderTextures();
  293. Update1DSliderValues();
  294. }
  295. void OnSliderVertChanged(float percent)
  296. {
  297. switch (colorBoxMode)
  298. {
  299. case ColorBoxMode.BG_R:
  300. colRed = percent;
  301. RGBToHSV();
  302. break;
  303. case ColorBoxMode.BR_G:
  304. colGreen = percent;
  305. RGBToHSV();
  306. break;
  307. case ColorBoxMode.RG_B:
  308. colBlue = percent;
  309. RGBToHSV();
  310. break;
  311. case ColorBoxMode.SV_H:
  312. colHue = percent;
  313. HSVToRGB();
  314. break;
  315. case ColorBoxMode.HV_S:
  316. colSaturation = percent;
  317. HSVToRGB();
  318. break;
  319. case ColorBoxMode.HS_V:
  320. colValue = percent;
  321. HSVToRGB();
  322. break;
  323. }
  324. guiColor.Value = SelectedColor;
  325. UpdateInputBoxValues();
  326. Update1DSliderTextures();
  327. Update1DSliderValues();
  328. }
  329. void OnSliderRHorzChanged(float percent)
  330. {
  331. bool isHSV = sliderMode == SliderMode.HSV;
  332. if (isHSV)
  333. {
  334. colHue = percent;
  335. HSVToRGB();
  336. }
  337. else
  338. {
  339. colRed = percent;
  340. RGBToHSV();
  341. }
  342. guiColor.Value = SelectedColor;
  343. UpdateInputBoxValues();
  344. Update2DSliderTextures();
  345. Update2DSliderValues();
  346. }
  347. void OnSliderGHorzChanged(float percent)
  348. {
  349. bool isHSV = sliderMode == SliderMode.HSV;
  350. if (isHSV)
  351. {
  352. colSaturation = percent;
  353. HSVToRGB();
  354. }
  355. else
  356. {
  357. colGreen = percent;
  358. RGBToHSV();
  359. }
  360. guiColor.Value = SelectedColor;
  361. UpdateInputBoxValues();
  362. Update2DSliderTextures();
  363. Update2DSliderValues();
  364. }
  365. void OnSliderBHorzChanged(float percent)
  366. {
  367. bool isHSV = sliderMode == SliderMode.HSV;
  368. if (isHSV)
  369. {
  370. colValue = percent;
  371. HSVToRGB();
  372. }
  373. else
  374. {
  375. colBlue = percent;
  376. RGBToHSV();
  377. }
  378. guiColor.Value = SelectedColor;
  379. UpdateInputBoxValues();
  380. Update2DSliderTextures();
  381. Update2DSliderValues();
  382. }
  383. void OnSliderAHorzChanged(float percent)
  384. {
  385. colAlpha = percent;
  386. guiColor.Value = SelectedColor;
  387. guiInputA.Value = MathEx.RoundToInt(colValue * 255.0f);
  388. }
  389. void OnInputRChanged(int value)
  390. {
  391. bool isHSV = sliderMode == SliderMode.HSV;
  392. if (isHSV)
  393. {
  394. colHue = value/359.0f;
  395. HSVToRGB();
  396. }
  397. else
  398. {
  399. colRed = value/255.0f;
  400. RGBToHSV();
  401. }
  402. guiColor.Value = SelectedColor;
  403. Update1DSliderValues();
  404. Update2DSliderTextures();
  405. Update2DSliderValues();
  406. }
  407. void OnInputGChanged(int value)
  408. {
  409. bool isHSV = sliderMode == SliderMode.HSV;
  410. if (isHSV)
  411. {
  412. colSaturation = value / 255.0f;
  413. HSVToRGB();
  414. }
  415. else
  416. {
  417. colGreen = value / 255.0f;
  418. RGBToHSV();
  419. }
  420. guiColor.Value = SelectedColor;
  421. Update1DSliderValues();
  422. Update2DSliderTextures();
  423. Update2DSliderValues();
  424. }
  425. void OnInputBChanged(int value)
  426. {
  427. bool isHSV = sliderMode == SliderMode.HSV;
  428. if (isHSV)
  429. {
  430. colValue = value / 255.0f;
  431. HSVToRGB();
  432. }
  433. else
  434. {
  435. colBlue = value / 255.0f;
  436. RGBToHSV();
  437. }
  438. guiColor.Value = SelectedColor;
  439. Update1DSliderValues();
  440. Update2DSliderTextures();
  441. Update2DSliderValues();
  442. }
  443. void OnInputAChanged(int value)
  444. {
  445. colAlpha = value/255.0f;
  446. guiColor.Value = SelectedColor;
  447. guiSliderAHorz.Percent = colAlpha;
  448. }
  449. void UpdateInputBoxValues()
  450. {
  451. bool isHSV = sliderMode == SliderMode.HSV;
  452. if (isHSV)
  453. {
  454. guiInputR.Value = MathEx.RoundToInt(colHue * 359.0f);
  455. guiInputG.Value = MathEx.RoundToInt(colSaturation * 255.0f);
  456. guiInputB.Value = MathEx.RoundToInt(colValue * 255.0f);
  457. }
  458. else
  459. {
  460. guiInputR.Value = MathEx.RoundToInt(colRed * 255.0f);
  461. guiInputG.Value = MathEx.RoundToInt(colGreen * 255.0f);
  462. guiInputB.Value = MathEx.RoundToInt(colBlue * 255.0f);
  463. }
  464. }
  465. void Update1DSliderValues()
  466. {
  467. bool isHSV = sliderMode == SliderMode.HSV;
  468. if (isHSV)
  469. {
  470. guiSliderRHorz.Percent = colHue;
  471. guiSliderGHorz.Percent = colSaturation;
  472. guiSliderBHorz.Percent = colValue;
  473. }
  474. else
  475. {
  476. guiSliderRHorz.Percent = colRed;
  477. guiSliderGHorz.Percent = colGreen;
  478. guiSliderBHorz.Percent = colBlue;
  479. }
  480. }
  481. void Update2DSliderValues()
  482. {
  483. Vector2 xy = Vector2.zero;
  484. float z = 0.0f;
  485. switch (colorBoxMode)
  486. {
  487. case ColorBoxMode.BG_R:
  488. xy.x = colBlue;
  489. xy.y = colGreen;
  490. z = colRed;
  491. break;
  492. case ColorBoxMode.BR_G:
  493. xy.x = colRed;
  494. xy.y = colBlue;
  495. z = colGreen;
  496. break;
  497. case ColorBoxMode.RG_B:
  498. xy.x = colRed;
  499. xy.y = colGreen;
  500. z = colBlue;
  501. break;
  502. case ColorBoxMode.SV_H:
  503. xy.x = colSaturation;
  504. xy.y = colValue;
  505. z = colHue;
  506. break;
  507. case ColorBoxMode.HV_S:
  508. xy.x = colHue;
  509. xy.y = colValue;
  510. z = colSaturation;
  511. break;
  512. case ColorBoxMode.HS_V:
  513. xy.x = colHue;
  514. xy.y = colSaturation;
  515. z = colValue;
  516. break;
  517. }
  518. colorBox.SetValue(xy);
  519. guiSliderVert.Percent = z;
  520. }
  521. void Update1DSliderTextures()
  522. {
  523. bool isHSV = sliderMode == SliderMode.HSV;
  524. if (isHSV)
  525. {
  526. Color startH = new Color(0, 1, 1);
  527. Color stepH = new Color(1, 0, 0, 0);
  528. sliderR.UpdateTexture(startH, stepH, false);
  529. Color startS = new Color(colHue, 0, MathEx.Max(colValue, 0.2f));
  530. Color stepS = new Color(0, 1, 0, 0);
  531. sliderG.UpdateTexture(startS, stepS, false);
  532. Color startV = new Color(colHue, colSaturation, 0);
  533. Color stepV = new Color(0, 0, 1, 0);
  534. sliderB.UpdateTexture(startV, stepV, false);
  535. }
  536. else
  537. {
  538. Color startR = new Color(0, colGreen, colBlue);
  539. Color stepR = new Color(1, 0, 0, 0);
  540. sliderR.UpdateTexture(startR, stepR, false);
  541. Color startG = new Color(colRed, 0, colBlue);
  542. Color stepG = new Color(0, 1, 0, 0);
  543. sliderG.UpdateTexture(startG, stepG, false);
  544. Color startB = new Color(colRed, colGreen, 0);
  545. Color stepB = new Color(0, 0, 1, 0);
  546. sliderB.UpdateTexture(startB, stepB, false);
  547. }
  548. }
  549. void Update2DSliderTextures()
  550. {
  551. switch (colorBoxMode)
  552. {
  553. case ColorBoxMode.BG_R:
  554. sideSlider.UpdateTexture(new Color(0, colGreen, colBlue, 1), new Color(1, 0, 0, 0), false);
  555. break;
  556. case ColorBoxMode.BR_G:
  557. sideSlider.UpdateTexture(new Color(colRed, 0, colBlue, 1), new Color(0, 1, 0, 0), false);
  558. break;
  559. case ColorBoxMode.RG_B:
  560. sideSlider.UpdateTexture(new Color(colRed, colGreen, 0, 1), new Color(0, 0, 1, 0), false);
  561. break;
  562. case ColorBoxMode.SV_H:
  563. sideSlider.UpdateTexture(new Color(0, 1, 1, 1), new Color(1, 0, 0, 0), true);
  564. break;
  565. case ColorBoxMode.HV_S:
  566. sideSlider.UpdateTexture(new Color(colHue, 0, MathEx.Max(colValue, 0.2f), 1), new Color(0, 1, 0, 0), true);
  567. break;
  568. case ColorBoxMode.HS_V:
  569. sideSlider.UpdateTexture(new Color(colHue, colSaturation, 0, 1), new Color(0, 0, 1, 0), true);
  570. break;
  571. }
  572. float[] valueLookup = new float[] { colRed, colGreen, colBlue, colHue, colSaturation, colValue };
  573. colorBox.UpdateTexture(colorBoxMode, valueLookup[(int)colorBoxMode]);
  574. }
  575. public class ColorSlider1DHorz
  576. {
  577. private const int SLIDER_HEIGHT = 8;
  578. private int width, height;
  579. private Texture2D texture;
  580. private SpriteTexture spriteTexture;
  581. private GUITexture guiTexture;
  582. private GUISliderH guiSlider;
  583. public ColorSlider1DHorz(GUITexture guiTexture, GUISliderH guiSlider, int width, int height)
  584. {
  585. this.width = width;
  586. this.height = height;
  587. this.guiTexture = guiTexture;
  588. this.guiSlider = guiSlider;
  589. texture = new Texture2D(width, height);
  590. spriteTexture = new SpriteTexture(texture);
  591. Rect2I sliderBounds = guiTexture.Bounds;
  592. sliderBounds.y -= SLIDER_HEIGHT;
  593. sliderBounds.height += SLIDER_HEIGHT;
  594. guiSlider.Bounds = sliderBounds;
  595. }
  596. public void UpdateTexture(Color start, Color step, bool isHSV)
  597. {
  598. Color[] colors = new Color[width * height];
  599. FillArea(width, height, colors, start, step, new Color(0, 0, 0, 0));
  600. if (isHSV)
  601. {
  602. for (int i = 0; i < colors.Length; i++)
  603. colors[i] = Color.HSV2RGB(colors[i]);
  604. }
  605. texture.SetPixels(colors);
  606. guiTexture.SetTexture(spriteTexture);
  607. }
  608. }
  609. public class ColorSlider1DVert
  610. {
  611. private const int SLIDER_WIDTH = 7;
  612. private int width, height;
  613. private Texture2D texture;
  614. private SpriteTexture spriteTexture;
  615. private GUITexture guiTexture;
  616. private GUISliderV guiSlider;
  617. public ColorSlider1DVert(GUITexture guiTexture, GUISliderV guiSlider, int width, int height)
  618. {
  619. this.width = width;
  620. this.height = height;
  621. this.guiTexture = guiTexture;
  622. this.guiSlider = guiSlider;
  623. texture = new Texture2D(width, height);
  624. spriteTexture = new SpriteTexture(texture);
  625. Rect2I sliderBounds = guiTexture.Bounds;
  626. sliderBounds.x -= SLIDER_WIDTH;
  627. sliderBounds.width += SLIDER_WIDTH;
  628. guiSlider.Bounds = sliderBounds;
  629. }
  630. public void UpdateTexture(Color start, Color step, bool isHSV)
  631. {
  632. Color[] colors = new Color[width * height];
  633. FillArea(width, height, colors, start, new Color(0, 0, 0, 0), step);
  634. if (isHSV)
  635. {
  636. for (int i = 0; i < colors.Length; i++)
  637. colors[i] = Color.HSV2RGB(colors[i]);
  638. }
  639. texture.SetPixels(colors);
  640. guiTexture.SetTexture(spriteTexture);
  641. }
  642. }
  643. public class ColorSlider2D
  644. {
  645. private int width, height;
  646. private Texture2D texture;
  647. private SpriteTexture spriteTexture;
  648. private GUITexture guiTexture;
  649. private GUITexture guiSliderHandle;
  650. private Vector2 oldValue;
  651. public delegate void OnValueChangedDelegate(Vector2 value);
  652. public event OnValueChangedDelegate OnValueChanged;
  653. public ColorSlider2D(GUITexture guiTexture, GUITexture guiSliderHandle, int width, int height)
  654. {
  655. this.width = width;
  656. this.height = height;
  657. this.guiTexture = guiTexture;
  658. this.guiSliderHandle = guiSliderHandle;
  659. texture = new Texture2D(width, height);
  660. spriteTexture = new SpriteTexture(texture);
  661. }
  662. public void UpdateTexture(ColorBoxMode mode, float value)
  663. {
  664. Color[] colors = new Color[width * height];
  665. switch (mode)
  666. {
  667. case ColorBoxMode.BG_R:
  668. FillArea(width, height, colors, new Color(value, 0, 0, 1), new Color(0, 0, 1, 0), new Color(0, 1, 0, 0));
  669. break;
  670. case ColorBoxMode.BR_G:
  671. FillArea(width, height, colors, new Color(0, value, 0, 1), new Color(0, 0, 1, 0), new Color(1, 0, 0, 0));
  672. break;
  673. case ColorBoxMode.RG_B:
  674. FillArea(width, height, colors, new Color(0, 0, value, 1), new Color(1, 0, 0, 0), new Color(0, 1, 0, 0));
  675. break;
  676. case ColorBoxMode.SV_H:
  677. FillArea(width, height, colors, new Color(value, 0, 0, 1), new Color(0, 1, 0, 0), new Color(0, 0, 1, 0));
  678. for (int i = 0; i < colors.Length; i++)
  679. colors[i] = Color.HSV2RGB(colors[i]);
  680. break;
  681. case ColorBoxMode.HV_S:
  682. FillArea(width, height, colors, new Color(0, value, 0, 1), new Color(1, 0, 0, 0), new Color(0, 0, 1, 0));
  683. for (int i = 0; i < colors.Length; i++)
  684. colors[i] = Color.HSV2RGB(colors[i]);
  685. break;
  686. case ColorBoxMode.HS_V:
  687. FillArea(width, height, colors, new Color(0, 0, value, 1), new Color(1, 0, 0, 0), new Color(0, 1, 0, 0));
  688. for (int i = 0; i < colors.Length; i++)
  689. colors[i] = Color.HSV2RGB(colors[i]);
  690. break;
  691. }
  692. texture.SetPixels(colors);
  693. guiTexture.SetTexture(spriteTexture);
  694. }
  695. public void UpdateInput(Vector2I windowPos)
  696. {
  697. if (Input.IsPointerButtonHeld(PointerButton.Left))
  698. {
  699. Rect2I bounds = guiTexture.Bounds;
  700. if (bounds.Contains(windowPos))
  701. {
  702. Vector2 newValue = Vector2.zero;
  703. newValue.x = (windowPos.x - bounds.x) / (float)bounds.width;
  704. newValue.y = (windowPos.y - bounds.y) / (float)bounds.height;
  705. SetValue(newValue);
  706. }
  707. }
  708. }
  709. public void SetValue(Vector2 value)
  710. {
  711. if (oldValue == value)
  712. return;
  713. Rect2I handleBounds = guiSliderHandle.Bounds;
  714. Rect2I boxBounds = guiTexture.Bounds;
  715. handleBounds.x = boxBounds.x + MathEx.RoundToInt(value.x * boxBounds.width) - handleBounds.width / 2;
  716. handleBounds.y = boxBounds.y + MathEx.RoundToInt(value.y * boxBounds.height) - handleBounds.height / 2;
  717. guiSliderHandle.Bounds = handleBounds;
  718. oldValue = value;
  719. if (OnValueChanged != null)
  720. OnValueChanged(value);
  721. }
  722. }
  723. }
  724. }