ColorPicker.cs 30 KB

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