ColorPick.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. namespace MetaClient
  5. {
  6. public class ColorPick : MonoBehaviour
  7. {
  8. public Image Saturation;
  9. public Image Hue;
  10. public Image Paint;
  11. public RectTransform Point_Stauration;
  12. public RectTransform Point_Hue;
  13. private Sprite Saturation_Sprite;
  14. private Sprite Hue_Sprite;
  15. private Color32 currentHue = Color.red;
  16. public Text colorText;
  17. public UICustom uICustom;
  18. private void Awake()
  19. {
  20. }
  21. private void Start()
  22. {
  23. UpdateStauration();
  24. UpdateHue();
  25. }
  26. float sWidth = 200, sHeight = 200;
  27. //更新饱和度
  28. private void UpdateStauration()
  29. {
  30. Saturation_Sprite = Sprite.Create(new Texture2D((int)sWidth, (int)sHeight), new Rect(0, 0, sWidth, sHeight), new Vector2(0, 0));
  31. for (int y = 0; y <= sHeight; y++)
  32. {
  33. for (int x = 0; x < sWidth; x++)
  34. {
  35. var pixColor = GetSaturation(currentHue, x / sWidth, y / sHeight);
  36. Saturation_Sprite.texture.SetPixel(x, ((int)sHeight - y), pixColor);
  37. }
  38. }
  39. Saturation_Sprite.texture.Apply();
  40. Saturation.sprite = Saturation_Sprite;
  41. }
  42. //更新色泽度
  43. private void UpdateHue()
  44. {
  45. float w = 50, h = 50;
  46. Hue_Sprite = Sprite.Create(new Texture2D((int)w, (int)h), new Rect(0, 0, w, h), new Vector2(0, 0));
  47. for (int y = 0; y <= h; y++)
  48. {
  49. for (int x = 0; x < w; x++)
  50. {
  51. var pixColor = GetHue(y / h);
  52. Hue_Sprite.texture.SetPixel(x, ((int)h - y), pixColor);
  53. }
  54. }
  55. Hue_Sprite.texture.Apply();
  56. Hue.sprite = Hue_Sprite;
  57. }
  58. private Vector2 clickPoint = Vector2.zero;
  59. public void OnStaurationClick(ColorPickClick sender)
  60. {
  61. var size2 = Saturation.rectTransform.sizeDelta / 2;
  62. var pos = Vector2.zero;
  63. pos.x = Mathf.Clamp(sender.ClickPoint.x, -size2.x, size2.x);
  64. pos.y = Mathf.Clamp(sender.ClickPoint.y, -size2.y, size2.y);
  65. Point_Stauration.anchoredPosition = clickPoint = pos;
  66. UpdateColor();
  67. }
  68. public void UpdateColor()
  69. {
  70. var size2 = Saturation.rectTransform.sizeDelta / 2;
  71. var pos = clickPoint;
  72. pos += size2;
  73. var color = GetSaturation(currentHue, pos.x / Saturation.rectTransform.sizeDelta.x, 1 - pos.y / Saturation.rectTransform.sizeDelta.y);
  74. Paint.color = color;
  75. int r = Mathf.RoundToInt(color.r * 255.0f);
  76. int g = Mathf.RoundToInt(color.g * 255.0f);
  77. int b = Mathf.RoundToInt(color.b * 255.0f);
  78. int a = Mathf.RoundToInt(color.a * 255.0f);
  79. string theHexColor = string.Format("{0:X2}{1:X2}{2:X2}{3:X2}", r, g, b, a);
  80. colorText.text = theHexColor + "";
  81. uICustom.ColorChange(new Vector3(color.r, color.g, color.b));
  82. }
  83. public void OnHueClick(ColorPickClick sender)
  84. {
  85. var h = Hue.rectTransform.sizeDelta.y / 2.0f;
  86. var y = Mathf.Clamp(sender.ClickPoint.y, -h, h);
  87. Point_Hue.anchoredPosition = new Vector2(0, y);
  88. y += h;
  89. currentHue = GetHue(1 - y / Hue.rectTransform.sizeDelta.y);
  90. UpdateStauration();
  91. UpdateColor();
  92. }
  93. private static Color GetSaturation(Color color, float x, float y)
  94. {
  95. Color newColor = Color.white;
  96. for (int i = 0; i < 3; i++)
  97. {
  98. if (color[i] != 1)
  99. {
  100. newColor[i] = (1 - color[i]) * (1 - x) + color[i];
  101. }
  102. }
  103. newColor *= (1 - y);
  104. newColor.a = 1;
  105. return newColor;
  106. }
  107. //B,r,G,b,R,g //大写是升,小写是降
  108. private readonly static int[] hues = new int[] { 2, 0, 1, 2, 0, 1 };
  109. private readonly static Color[] colors = new Color[] { Color.red, Color.blue, Color.blue, Color.green, Color.green, Color.red };
  110. private readonly static float c = 1.0f / hues.Length;
  111. private static Color GetHue(float y)
  112. {
  113. y = Mathf.Clamp01(y);
  114. var index = (int)(y / c);
  115. var h = hues[index];
  116. var newColor = colors[index];
  117. float less = (y - index * c) / c;
  118. newColor[h] = index % 2 == 0 ? less : 1 - less;
  119. return newColor;
  120. }
  121. }
  122. }