Rewrite color inversal in a reusable way
This commit is contained in:
@@ -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))
|
||||
|
||||
@@ -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<T, U>
|
||||
|
||||
@@ -29,7 +29,8 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
|
||||
readonly int timestep;
|
||||
readonly IEnumerable<SupportPowerInstance> powers;
|
||||
Tuple<string, Color, Color>[] texts;
|
||||
readonly Color bgDark, bgLight;
|
||||
Pair<string, Color>[] 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<MapOptions>().GameSpeed.Timestep;
|
||||
|
||||
bgDark = ChromeMetrics.Get<Color>("TextContrastColorDark");
|
||||
bgLight = ChromeMetrics.Get<Color>("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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user