unity中调色板制作

 
  1. using UnityEngine;

  2. using System.Collections;

  3. using UnityEngine.UI;

  4.  
  5. public class ColorPick : MonoBehaviour

  6. {

  7.  
  8. public Image Saturation;

  9. public Image Hue;

  10. public Image Paint;

  11.  
  12. public RectTransform Point_Stauration;

  13. public RectTransform Point_Hue;

  14.  
  15. private Sprite Saturation_Sprite;

  16. private Sprite Hue_Sprite;

  17.  
  18. private Color32 currentHue = Color.red;

  19.  
  20.  
  21. private void Awake()

  22. {

  23.  
  24. }

  25.  
  26. private void Start()

  27. {

  28. UpdateStauration();

  29. UpdateHue();

  30. }

  31.  
  32. float sWidth = 200, sHeight = 200;

  33. //更新饱和度

  34. private void UpdateStauration()

  35. {

  36.  
  37. Saturation_Sprite = Sprite.Create(new Texture2D((int)sWidth, (int)sHeight), new Rect(0, 0, sWidth, sHeight), new Vector2(0, 0));

  38.  
  39. for (int y = 0; y <= sHeight; y++)

  40. {

  41. for (int x = 0; x < sWidth; x++)

  42. {

  43. var pixColor = GetSaturation(currentHue, x / sWidth, y / sHeight);

  44. Saturation_Sprite.texture.SetPixel(x, ((int)sHeight - y), pixColor);

  45. }

  46. }

  47. Saturation_Sprite.texture.Apply();

  48.  
  49. Saturation.sprite = Saturation_Sprite;

  50. }

  51.  
  52. //更新色泽度

  53. private void UpdateHue()

  54. {

  55.  
  56. float w = 50, h = 50;

  57.  
  58. Hue_Sprite = Sprite.Create(new Texture2D((int)w, (int)h), new Rect(0, 0, w, h), new Vector2(0, 0));

  59.  
  60. for (int y = 0; y <= h; y++)

  61. {

  62. for (int x = 0; x < w; x++)

  63. {

  64. var pixColor = GetHue(y / h);

  65. Hue_Sprite.texture.SetPixel(x, ((int)h - y), pixColor);

  66. }

  67. }

  68. Hue_Sprite.texture.Apply();

  69.  
  70. Hue.sprite = Hue_Sprite;

  71. }

  72.  
  73. private Vector2 clickPoint = Vector2.zero;

  74. public void OnStaurationClick(ColorPickClick sender)

  75. {

  76. var size2 = Saturation.rectTransform.sizeDelta / 2;

  77. var pos = Vector2.zero;

  78. pos.x = Mathf.Clamp(sender.ClickPoint.x, -size2.x, size2.x);

  79. pos.y = Mathf.Clamp(sender.ClickPoint.y, -size2.y, size2.y);

  80. Point_Stauration.anchoredPosition = clickPoint = pos;

  81.  
  82. UpdateColor();

  83. }

  84.  
  85. public void UpdateColor()

  86. {

  87. var size2 = Saturation.rectTransform.sizeDelta / 2;

  88. var pos = clickPoint;

  89. pos += size2;

  90.  
  91. var color = GetSaturation(currentHue, pos.x / Saturation.rectTransform.sizeDelta.x, 1 - pos.y / Saturation.rectTransform.sizeDelta.y);

  92. Paint.color = color;

  93. }

  94.  
  95. public void OnHueClick(ColorPickClick sender)

  96. {

  97. var h = Hue.rectTransform.sizeDelta.y / 2.0f;

  98. var y = Mathf.Clamp(sender.ClickPoint.y, -h, h);

  99. Point_Hue.anchoredPosition = new Vector2(0, y);

  100.  
  101. y += h;

  102. currentHue = GetHue(1 - y / Hue.rectTransform.sizeDelta.y);

  103. UpdateStauration();

  104. UpdateColor();

  105. }

  106.  
  107.  
  108. private static Color GetSaturation(Color color, float x, float y)

  109. {

  110. Color newColor = Color.white;

  111. for (int i = 0; i < 3; i++)

  112. {

  113. if (color[i] != 1)

  114. {

  115. newColor[i] = (1 - color[i]) * (1 - x) + color[i];

  116. }

  117. }

  118.  
  119. newColor *= (1 - y);

  120. newColor.a = 1;

  121. return newColor;

  122. }

  123.  
  124.  
  125. //B,r,G,b,R,g //大写是升,小写是降

  126. private readonly static int[] hues = new int[] { 2, 0, 1, 2, 0, 1 };

  127.  
  128. private readonly static Color[] colors = new Color[] { Color.red, Color.blue, Color.blue, Color.green, Color.green, Color.red };

  129.  
  130. private readonly static float c = 1.0f / hues.Length;

  131.  
  132. private static Color GetHue(float y)

  133. {

  134. y = Mathf.Clamp01(y);

  135.  
  136. var index = (int)(y / c);

  137.  
  138. var h = hues[index];

  139.  
  140. var newColor = colors[index];

  141.  
  142. float less = (y - index * c) / c;

  143.  
  144. newColor[h] = index % 2 == 0 ? less : 1 - less;

  145.  
  146. return newColor;

  147. }

  148. }

 
  1. using UnityEngine;

  2. using System.Collections;

  3. using UnityEngine.EventSystems;

  4. using System;

  5. using UnityEngine.UI;

  6. public class ColorPickClick : MonoBehaviour, IPointerDownHandler, IDragHandler

  7. {

  8. public Button.ButtonClickedEvent Click;

  9.  
  10. public Vector3 ClickPoint { get; set; }

  11.  
  12. public void OnDrag(PointerEventData eventData)

  13. {

  14. var rect = transform as RectTransform;

  15. ClickPoint = rect.InverseTransformPoint(eventData.position);

  16. Click.Invoke();

  17. }

  18.  
  19. public void OnPointerDown(PointerEventData eventData)

  20. {

  21. var rect = transform as RectTransform;

  22. ClickPoint = rect.InverseTransformPoint(eventData.position);

  23. Click.Invoke();

  24. }

  25. }

调色板的UI制作

代码添加

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值