3 using Microsoft.Xna.Framework.Graphics;
4 using Microsoft.Xna.Framework;
22 private Texture2D? mainTexture,
25 private Color[]? colorData;
29 private bool mouseHeld;
30 private bool isInitialized;
32 private readonly Color transparentWhite = Color.White * 0.8f,
33 transparentBlack = Color.Black * 0.8f;
39 int tWidth =
Rect.Width;
40 int sliceWidth =
Rect.Width / 8;
42 int mainWidth = tWidth - sliceWidth;
43 int hueWidth = sliceWidth;
48 colorData =
new Color[MainArea.Width * MainArea.Height];
50 if (mainTexture ==
null)
52 int width = MainArea.Width,
53 height = MainArea.Height;
55 GenerateGradient(ref colorData!, width, height, DrawHVArea);
56 mainTexture = CreateGradientTexture(colorData!, MainArea.Width, MainArea.Height);
59 if (hueTexture ==
null)
61 int width = HueArea.Width,
62 height = HueArea.Height;
64 Color[] hueData =
new Color[width * height];
66 GenerateGradient(ref hueData, width, height, DrawHueArea);
67 hueTexture = CreateGradientTexture(hueData, width, height);
71 protected override void Draw(SpriteBatch spriteBatch)
73 if (mainTexture ==
null || hueTexture ==
null || !isInitialized) {
return; }
78 hueArea.Location +=
Rect.Location;
79 mainArea.Location +=
Rect.Location;
81 Vector2 mainLocation = mainArea.Location.ToVector2(),
82 hueLocation = hueArea.Location.ToVector2();
84 spriteBatch.Draw(mainTexture, mainLocation, Color.White);
85 spriteBatch.Draw(hueTexture, hueLocation, Color.White);
87 float hueY = hueLocation.Y + ((
SelectedHue / 360f) * hueArea.Height);
88 spriteBatch.DrawLine(hueArea.Left, hueY, hueArea.Right, hueY, transparentWhite, thickness: 3);
89 spriteBatch.DrawLine(hueArea.Left, hueY, hueArea.Right, hueY, transparentBlack, thickness: 1);
92 float valueY = mainLocation.Y + (1.0f -
SelectedValue) * MainArea.Height;
94 spriteBatch.DrawLine(saturationX, mainArea.Top,saturationX, mainArea.Bottom, transparentWhite, thickness: 3);
95 spriteBatch.DrawLine(mainArea.Left,valueY, mainArea.Right, valueY, transparentWhite, thickness: 3);
97 spriteBatch.DrawLine(saturationX, mainArea.Top,saturationX, mainArea.Bottom, transparentBlack, thickness: 1);
98 spriteBatch.DrawLine(mainArea.Left,valueY, mainArea.Right, valueY, transparentBlack, thickness: 1);
101 protected override void Update(
float deltaTime)
103 base.Update(deltaTime);
108 isInitialized =
true;
116 if (GUI.MouseOn !=
this) {
return; }
121 hueArea.Location +=
Rect.Location;
122 mainArea.Location +=
Rect.Location;
129 selectedRect = HueArea;
133 selectedRect = MainArea;
148 if (selectedRect == HueArea)
151 SelectedHue = Math.Clamp(pos.Y / hueArea.Height * 360f, 0, 360);
155 else if (selectedRect == MainArea)
159 SelectedValue = Math.Clamp(1f - (y / mainArea.Height), 0, 1);
170 mainTexture?.Dispose();
172 hueTexture?.Dispose();
178 if (colorData ==
null || mainTexture ==
null) {
return; }
179 GenerateGradient(ref colorData, mainTexture.Width, mainTexture.Height, DrawHVArea);
180 mainTexture.SetData(colorData);
183 private Texture2D CreateGradientTexture(Color[] data,
int width,
int height)
186 texture.SetData(data);
190 private void GenerateGradient(ref Color[] data,
int width,
int height, Func<float, float, Color> algorithm)
192 for (
int y = 0; y < height; y++)
194 for (
int x = 0; x < width; x++)
196 float relativeX = x / (float) width,
197 relativeY = y / (
float) height;
199 data[y * width + x] = algorithm(relativeX, relativeY);
204 private Color DrawHVArea(
float x,
float y) => ToolBoxCore.HSVToRGB(
SelectedHue, x, 1.0f - y);
205 private Color DrawHueArea(
float x,
float y) => ToolBoxCore.HSVToRGB(y * 360f, 1f, 1f);
GUIColorPicker(RectTransform rectT, string? style=null)
delegate bool OnColorSelectedHandler(GUIColorPicker component, Color color)
OnColorSelectedHandler? OnColorSelected
override void Draw(SpriteBatch spriteBatch)
override void Update(float deltaTime)
static GraphicsDeviceManager GraphicsDeviceManager