Skip to content

Commit

Permalink
Improve Color settings: Dec/Hex numbers, Visualize alpha
Browse files Browse the repository at this point in the history
Closes #76
  • Loading branch information
ManlyMarco committed Dec 30, 2023
1 parent 0b502ad commit 67b4eef
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 18 deletions.
72 changes: 55 additions & 17 deletions ConfigurationManager.Shared/SettingFieldDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -485,36 +485,74 @@ private static float DrawSingleVectorSlider(float setting, string label)
return x;
}

private static bool _drawColorHex;
private static void DrawColor(SettingEntryBase obj)
{
var setting = (Color)obj.Get();
var colorValue = (Color)obj.Get();

GUI.changed = false;

if (!_colorCache.TryGetValue(obj, out var cacheEntry))
{
cacheEntry = new ColorCacheEntry { Tex = new Texture2D(40, 10, TextureFormat.ARGB32, false), Last = setting };
cacheEntry.Tex.FillTexture(setting);
var tex = new Texture2D(100, 20, TextureFormat.ARGB32, false);
cacheEntry = new ColorCacheEntry { Tex = tex, Last = colorValue };
FillTex(colorValue, tex);
_colorCache[obj] = cacheEntry;
}

GUILayout.Label("R", GUILayout.ExpandWidth(false));
setting.r = GUILayout.HorizontalSlider(setting.r, 0f, 1f, GUILayout.ExpandWidth(true));
GUILayout.Label("G", GUILayout.ExpandWidth(false));
setting.g = GUILayout.HorizontalSlider(setting.g, 0f, 1f, GUILayout.ExpandWidth(true));
GUILayout.Label("B", GUILayout.ExpandWidth(false));
setting.b = GUILayout.HorizontalSlider(setting.b, 0f, 1f, GUILayout.ExpandWidth(true));
GUILayout.Label("A", GUILayout.ExpandWidth(false));
setting.a = GUILayout.HorizontalSlider(setting.a, 0f, 1f, GUILayout.ExpandWidth(true));
GUILayout.BeginVertical();
{
GUILayout.BeginHorizontal();
{
GUILayout.Label(cacheEntry.Tex, GUILayout.ExpandWidth(false));

GUILayout.Space(4);
var colorStr = _drawColorHex ? "#" + ColorUtility.ToHtmlStringRGBA(colorValue) : $"{colorValue.r:F2} {colorValue.g:F2} {colorValue.b:F2} {colorValue.a:F2}";
var newColorStr = GUILayout.TextField(colorStr, GUILayout.ExpandWidth(true));
if (GUI.changed && colorStr != newColorStr)
{
if (_drawColorHex)
{
if (ColorUtility.TryParseHtmlString(newColorStr, out var parsedColor))
colorValue = parsedColor;
}
else
{
var split = newColorStr.Split(' ');
if (split.Length == 4 && float.TryParse(split[0], out var r) && float.TryParse(split[1], out var g) && float.TryParse(split[2], out var b) && float.TryParse(split[3], out var a))
colorValue = new Color(r, g, b, a);
}
}

if (setting != cacheEntry.Last)
_drawColorHex = GUILayout.Toggle(_drawColorHex, "Hex", GUILayout.ExpandWidth(false));
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
{
GUILayout.Label("R", GUILayout.ExpandWidth(false));
colorValue.r = GUILayout.HorizontalSlider(colorValue.r, 0f, 1f, GUILayout.ExpandWidth(true));
GUILayout.Label("G", GUILayout.ExpandWidth(false));
colorValue.g = GUILayout.HorizontalSlider(colorValue.g, 0f, 1f, GUILayout.ExpandWidth(true));
GUILayout.Label("B", GUILayout.ExpandWidth(false));
colorValue.b = GUILayout.HorizontalSlider(colorValue.b, 0f, 1f, GUILayout.ExpandWidth(true));
GUILayout.Label("A", GUILayout.ExpandWidth(false));
colorValue.a = GUILayout.HorizontalSlider(colorValue.a, 0f, 1f, GUILayout.ExpandWidth(true));
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();

if (colorValue != cacheEntry.Last)
{
obj.Set(setting);
cacheEntry.Tex.FillTexture(setting);
cacheEntry.Last = setting;
obj.Set(colorValue);
FillTex(colorValue, cacheEntry.Tex);
cacheEntry.Last = colorValue;
}

GUILayout.Label(cacheEntry.Tex, GUILayout.ExpandWidth(false));
void FillTex(Color color, Texture2D tex)
{
if (color.a < 1f) tex.FillTextureCheckerboard();
tex.FillTexture(color);
}
}

private sealed class ColorCacheEntry
Expand Down
29 changes: 28 additions & 1 deletion ConfigurationManager.Shared/Utilities/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,37 @@ public static string AppendZeroIfFloat(this string s, Type type)
}

public static void FillTexture(this Texture2D tex, Color color)
{
if (color.a < 1f)
{
// SetPixel ignores alpha, so we need to lerp manually
for (var x = 0; x < tex.width; x++)
{
for (var y = 0; y < tex.height; y++)
{
var origColor = tex.GetPixel(x, y);
var lerpedColor = Color.Lerp(origColor, color, color.a);
// Not accurate, but good enough for our purposes
lerpedColor.a = Mathf.Max(origColor.a, color.a);
tex.SetPixel(x, y, lerpedColor);
}
}
}
else
{
for (var x = 0; x < tex.width; x++)
for (var y = 0; y < tex.height; y++)
tex.SetPixel(x, y, color);
}

tex.Apply(false);
}

public static void FillTextureCheckerboard(this Texture2D tex)
{
for (var x = 0; x < tex.width; x++)
for (var y = 0; y < tex.height; y++)
tex.SetPixel(x, y, color);
tex.SetPixel(x, y, (x / 10 + y / 10) % 2 == 1 ? Color.black : Color.white);

tex.Apply(false);
}
Expand Down

0 comments on commit 67b4eef

Please sign in to comment.
  翻译: