From 8ad18ad1611928f36e0f9babf807f4d38d29a4a7 Mon Sep 17 00:00:00 2001 From: Oliver Brakmann Date: Fri, 26 Aug 2016 22:06:10 +0200 Subject: [PATCH] Rewrite color inversal in a reusable way --- OpenRA.Game/Graphics/SpriteFont.cs | 6 ++++++ OpenRA.Game/Widgets/WidgetUtils.cs | 6 ++++++ .../Widgets/SupportPowerTimerWidget.cs | 16 ++++++++-------- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/OpenRA.Game/Graphics/SpriteFont.cs b/OpenRA.Game/Graphics/SpriteFont.cs index b25c29cc34..ccc3a5f9e6 100644 --- a/OpenRA.Game/Graphics/SpriteFont.cs +++ b/OpenRA.Game/Graphics/SpriteFont.cs @@ -14,6 +14,7 @@ using System.Drawing; using System.Linq; using OpenRA.Primitives; using OpenRA.Support; +using OpenRA.Widgets; using SharpFont; namespace OpenRA.Graphics @@ -93,6 +94,11 @@ namespace OpenRA.Graphics DrawText(text, location, fg); } + public void DrawTextWithContrast(string text, float2 location, Color fg, Color bgDark, Color bgLight, int offset) + { + DrawTextWithContrast(text, location, fg, WidgetUtils.GetContrastColor(fg, bgDark, bgLight), offset); + } + public int2 Measure(string text) { if (string.IsNullOrEmpty(text)) diff --git a/OpenRA.Game/Widgets/WidgetUtils.cs b/OpenRA.Game/Widgets/WidgetUtils.cs index 993d3472a7..c02d013792 100644 --- a/OpenRA.Game/Widgets/WidgetUtils.cs +++ b/OpenRA.Game/Widgets/WidgetUtils.cs @@ -250,6 +250,12 @@ namespace OpenRA.Widgets return trimmed; } + + public static Color GetContrastColor(Color fgColor, Color bgDark, Color bgLight) + { + var fg = new HSLColor(fgColor); + return fg.RGB == Color.White || fg.L > 80 ? bgDark : bgLight; + } } public class CachedTransform diff --git a/OpenRA.Mods.Common/Widgets/SupportPowerTimerWidget.cs b/OpenRA.Mods.Common/Widgets/SupportPowerTimerWidget.cs index bd9038fd87..c67e48e3d0 100644 --- a/OpenRA.Mods.Common/Widgets/SupportPowerTimerWidget.cs +++ b/OpenRA.Mods.Common/Widgets/SupportPowerTimerWidget.cs @@ -29,7 +29,8 @@ namespace OpenRA.Mods.Common.Widgets readonly int timestep; readonly IEnumerable powers; - Tuple[] texts; + readonly Color bgDark, bgLight; + Pair[] texts; [ObjectCreator.UseCtor] public SupportPowerTimerWidget(World world) @@ -43,6 +44,9 @@ namespace OpenRA.Mods.Common.Widgets timestep = world.Timestep; if (world.IsReplay) timestep = world.WorldActor.Trait().GameSpeed.Timestep; + + bgDark = ChromeMetrics.Get("TextContrastColorDark"); + bgLight = ChromeMetrics.Get("TextContrastColorLight"); } public override void Tick() @@ -66,11 +70,7 @@ namespace OpenRA.Mods.Common.Widgets var color = !p.Ready || Game.LocalTick % 50 < 25 ? playerColor : Color.White; - var inversedColor = self.Owner.Color; - var inversedL = color == Color.White || inversedColor.L > 128 ? (byte)0 : (byte)255; - inversedColor = new HSLColor(inversedColor.H, 0, inversedL); - - return Tuple.Create(text, color, inversedColor.RGB); + return Pair.New(text, color); }).ToArray(); } @@ -83,8 +83,8 @@ namespace OpenRA.Mods.Common.Widgets foreach (var t in texts) { var font = Game.Renderer.Fonts[Font]; - font.DrawTextWithContrast(t.Item1, new float2(Bounds.Location) + new float2(0, y), t.Item2, t.Item3, 1); - y += (font.Measure(t.Item1).Y + 5) * (int)Order; + font.DrawTextWithContrast(t.First, new float2(Bounds.Location) + new float2(0, y), t.Second, bgDark, bgLight, 1); + y += (font.Measure(t.First).Y + 5) * (int)Order; } }