Elin Decompiled Documentation EA 23.102 Nightly
Loading...
Searching...
No Matches
ColorPalette.cs
Go to the documentation of this file.
1using System;
3using UnityEngine;
4using UnityEngine.EventSystems;
5using UnityEngine.UI;
6
8
9public class ColorPalette : MonoBehaviour, IDragHandler, IEventSystemHandler, IPointerDownHandler, IInitializePotentialDragHandler
10{
12
13 private Color[] colorArray;
14
15 private Texture2D texture;
16
17 [SerializeField]
18 private RawImage paletteImage;
19
20 [SerializeField]
21 private RectTransform marker;
22
23 [SerializeField]
24 private RectTransform dropperSelectionMarker;
25
26 private bool initiated;
27
28 public Vector2 MarkerPosition => marker.anchorMin;
29
30 public event Action<Vector2> ColorPicked = delegate
31 {
32 };
33
34 private void Awake()
35 {
36 Init();
37 }
38
39 private void Init()
40 {
41 if (!initiated)
42 {
43 initiated = true;
44 int width = paletteImage.mainTexture.width;
45 int height = paletteImage.mainTexture.height;
46 texture = new Texture2D(width, height, TextureFormat.RGB24, mipChain: false);
47 colorArray = new Color[width * height];
48 }
49 }
50
51 public void SelectColor(HSVColor hsv)
52 {
53 Init();
54 if (Mathf.Approximately(current.h, current.h))
55 {
57 }
59 }
60
61 public void OnPointerDown(PointerEventData eventData)
62 {
63 OnDrag(eventData);
64 }
65
66 public void OnDrag(PointerEventData eventData)
67 {
68 this.ColorPicked(GetNormalisedPointerPosition(eventData.position));
69 }
70
71 private Vector2 GetNormalisedPointerPosition(Vector2 position)
72 {
73 Vector2 clampedLocalPosition = GetClampedLocalPosition(position);
74 return new Vector2(clampedLocalPosition.x / paletteImage.rectTransform.rect.width, clampedLocalPosition.y / paletteImage.rectTransform.rect.height);
75 }
76
77 public void OnInitializePotentialDrag(PointerEventData ped)
78 {
79 ped.useDragThreshold = false;
80 }
81
82 private Vector2 GetClampedLocalPosition(Vector2 position)
83 {
84 Vector3 vector = paletteImage.gameObject.transform.InverseTransformPoint(position);
85 vector.x = Mathf.Clamp(vector.x, 0f, paletteImage.rectTransform.rect.width);
86 vector.y = Mathf.Clamp(vector.y, 0f, paletteImage.rectTransform.rect.height);
87 return vector;
88 }
89
91 {
92 current = hsv;
93 int width = texture.width;
94 for (int i = 0; i < width; i++)
95 {
96 for (int num = width - 1; num > 0; num--)
97 {
98 colorArray[i * width - 1 + num] = Colorist.HSVtoRGB(current.h, (float)num / (float)width, (float)i / (float)width);
99 }
100 }
101 texture.SetPixels(colorArray);
102 texture.Apply();
103 paletteImage.texture = texture;
104 }
105
107 {
108 SetMarkerPosition(new Vector2(hsv.s, hsv.v));
109 }
110
111 private void SetMarkerPosition(Vector2 position)
112 {
113 marker.anchorMin = position;
114 marker.anchorMax = position;
115 }
116
117 public void ShowEnlargedPixels(Color[] colors)
118 {
119 Color[] array = new Color[texture.width * texture.height];
120 for (int i = 0; i < texture.width; i++)
121 {
122 for (int j = 0; j < texture.width; j++)
123 {
124 int num = i * texture.width + j;
125 int num2 = i / 32 * 16 + j / 32;
126 array[num] = colors[num2];
127 }
128 }
129 texture.SetPixels(array);
130 texture.Apply();
131 paletteImage.texture = texture;
132 }
133
134 public void ShowDropperMarker(bool value)
135 {
136 marker.gameObject.SetActive(!value);
137 dropperSelectionMarker.gameObject.SetActive(value);
138 }
139}
void OnDrag(PointerEventData eventData)
Definition: ColorPalette.cs:66
Vector2 GetClampedLocalPosition(Vector2 position)
Definition: ColorPalette.cs:82
void ShowEnlargedPixels(Color[] colors)
void SetMarkerPosition(Vector2 position)
void OnInitializePotentialDrag(PointerEventData ped)
Definition: ColorPalette.cs:77
void OnPointerDown(PointerEventData eventData)
Definition: ColorPalette.cs:61
void UpdateMarkerPosition(HSVColor hsv)
void SelectColor(HSVColor hsv)
Definition: ColorPalette.cs:51
void GeneratePaletteTexture(HSVColor hsv)
Definition: ColorPalette.cs:90
Vector2 GetNormalisedPointerPosition(Vector2 position)
Definition: ColorPalette.cs:71
static Color HSVtoRGB(HSVColor hsv)
Definition: Colorist.cs:107
float h
Definition: HSVColor.cs:3
float s
Definition: HSVColor.cs:5
float v
Definition: HSVColor.cs:7