Skip to content

Commit 67b4eef

Browse files
committed
Improve Color settings: Dec/Hex numbers, Visualize alpha
Closes #76
1 parent 0b502ad commit 67b4eef

File tree

2 files changed

+83
-18
lines changed

2 files changed

+83
-18
lines changed

ConfigurationManager.Shared/SettingFieldDrawer.cs

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -485,36 +485,74 @@ private static float DrawSingleVectorSlider(float setting, string label)
485485
return x;
486486
}
487487

488+
private static bool _drawColorHex;
488489
private static void DrawColor(SettingEntryBase obj)
489490
{
490-
var setting = (Color)obj.Get();
491+
var colorValue = (Color)obj.Get();
492+
493+
GUI.changed = false;
491494

492495
if (!_colorCache.TryGetValue(obj, out var cacheEntry))
493496
{
494-
cacheEntry = new ColorCacheEntry { Tex = new Texture2D(40, 10, TextureFormat.ARGB32, false), Last = setting };
495-
cacheEntry.Tex.FillTexture(setting);
497+
var tex = new Texture2D(100, 20, TextureFormat.ARGB32, false);
498+
cacheEntry = new ColorCacheEntry { Tex = tex, Last = colorValue };
499+
FillTex(colorValue, tex);
496500
_colorCache[obj] = cacheEntry;
497501
}
498502

499-
GUILayout.Label("R", GUILayout.ExpandWidth(false));
500-
setting.r = GUILayout.HorizontalSlider(setting.r, 0f, 1f, GUILayout.ExpandWidth(true));
501-
GUILayout.Label("G", GUILayout.ExpandWidth(false));
502-
setting.g = GUILayout.HorizontalSlider(setting.g, 0f, 1f, GUILayout.ExpandWidth(true));
503-
GUILayout.Label("B", GUILayout.ExpandWidth(false));
504-
setting.b = GUILayout.HorizontalSlider(setting.b, 0f, 1f, GUILayout.ExpandWidth(true));
505-
GUILayout.Label("A", GUILayout.ExpandWidth(false));
506-
setting.a = GUILayout.HorizontalSlider(setting.a, 0f, 1f, GUILayout.ExpandWidth(true));
503+
GUILayout.BeginVertical();
504+
{
505+
GUILayout.BeginHorizontal();
506+
{
507+
GUILayout.Label(cacheEntry.Tex, GUILayout.ExpandWidth(false));
507508

508-
GUILayout.Space(4);
509+
var colorStr = _drawColorHex ? "#" + ColorUtility.ToHtmlStringRGBA(colorValue) : $"{colorValue.r:F2} {colorValue.g:F2} {colorValue.b:F2} {colorValue.a:F2}";
510+
var newColorStr = GUILayout.TextField(colorStr, GUILayout.ExpandWidth(true));
511+
if (GUI.changed && colorStr != newColorStr)
512+
{
513+
if (_drawColorHex)
514+
{
515+
if (ColorUtility.TryParseHtmlString(newColorStr, out var parsedColor))
516+
colorValue = parsedColor;
517+
}
518+
else
519+
{
520+
var split = newColorStr.Split(' ');
521+
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))
522+
colorValue = new Color(r, g, b, a);
523+
}
524+
}
509525

510-
if (setting != cacheEntry.Last)
526+
_drawColorHex = GUILayout.Toggle(_drawColorHex, "Hex", GUILayout.ExpandWidth(false));
527+
}
528+
GUILayout.EndHorizontal();
529+
GUILayout.BeginHorizontal();
530+
{
531+
GUILayout.Label("R", GUILayout.ExpandWidth(false));
532+
colorValue.r = GUILayout.HorizontalSlider(colorValue.r, 0f, 1f, GUILayout.ExpandWidth(true));
533+
GUILayout.Label("G", GUILayout.ExpandWidth(false));
534+
colorValue.g = GUILayout.HorizontalSlider(colorValue.g, 0f, 1f, GUILayout.ExpandWidth(true));
535+
GUILayout.Label("B", GUILayout.ExpandWidth(false));
536+
colorValue.b = GUILayout.HorizontalSlider(colorValue.b, 0f, 1f, GUILayout.ExpandWidth(true));
537+
GUILayout.Label("A", GUILayout.ExpandWidth(false));
538+
colorValue.a = GUILayout.HorizontalSlider(colorValue.a, 0f, 1f, GUILayout.ExpandWidth(true));
539+
}
540+
GUILayout.EndHorizontal();
541+
}
542+
GUILayout.EndVertical();
543+
544+
if (colorValue != cacheEntry.Last)
511545
{
512-
obj.Set(setting);
513-
cacheEntry.Tex.FillTexture(setting);
514-
cacheEntry.Last = setting;
546+
obj.Set(colorValue);
547+
FillTex(colorValue, cacheEntry.Tex);
548+
cacheEntry.Last = colorValue;
515549
}
516550

517-
GUILayout.Label(cacheEntry.Tex, GUILayout.ExpandWidth(false));
551+
void FillTex(Color color, Texture2D tex)
552+
{
553+
if (color.a < 1f) tex.FillTextureCheckerboard();
554+
tex.FillTexture(color);
555+
}
518556
}
519557

520558
private sealed class ColorCacheEntry

ConfigurationManager.Shared/Utilities/Utilities.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,37 @@ public static string AppendZeroIfFloat(this string s, Type type)
4848
}
4949

5050
public static void FillTexture(this Texture2D tex, Color color)
51+
{
52+
if (color.a < 1f)
53+
{
54+
// SetPixel ignores alpha, so we need to lerp manually
55+
for (var x = 0; x < tex.width; x++)
56+
{
57+
for (var y = 0; y < tex.height; y++)
58+
{
59+
var origColor = tex.GetPixel(x, y);
60+
var lerpedColor = Color.Lerp(origColor, color, color.a);
61+
// Not accurate, but good enough for our purposes
62+
lerpedColor.a = Mathf.Max(origColor.a, color.a);
63+
tex.SetPixel(x, y, lerpedColor);
64+
}
65+
}
66+
}
67+
else
68+
{
69+
for (var x = 0; x < tex.width; x++)
70+
for (var y = 0; y < tex.height; y++)
71+
tex.SetPixel(x, y, color);
72+
}
73+
74+
tex.Apply(false);
75+
}
76+
77+
public static void FillTextureCheckerboard(this Texture2D tex)
5178
{
5279
for (var x = 0; x < tex.width; x++)
5380
for (var y = 0; y < tex.height; y++)
54-
tex.SetPixel(x, y, color);
81+
tex.SetPixel(x, y, (x / 10 + y / 10) % 2 == 1 ? Color.black : Color.white);
5582

5683
tex.Apply(false);
5784
}

0 commit comments

Comments
 (0)