ColorPicker.cs 26 KB

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