From 523183431b7b2c3dbc553dea7844a3e6329688cc Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Tue, 27 Dec 2011 14:47:17 +1300 Subject: [PATCH] remove some duplication and dead code --- OpenRA.FileFormats/Exts.cs | 14 ++++++++ OpenRA.FileFormats/PlayerColorRemap.cs | 19 +++++----- OpenRA.Game/Graphics/Util.cs | 40 ++-------------------- OpenRA.Mods.Cnc/CncMenuPaletteEffect.cs | 2 +- OpenRA.Mods.RA/ChronoshiftPaletteEffect.cs | 6 ++-- OpenRA.Mods.RA/Effects/Contrail.cs | 2 +- OpenRA.Mods.RA/NukePaletteEffect.cs | 5 +-- OpenRA.Mods.RA/Widgets/PowerBinWidget.cs | 2 +- OpenRA.Utility/Command.cs | 9 +++-- 9 files changed, 38 insertions(+), 61 deletions(-) diff --git a/OpenRA.FileFormats/Exts.cs b/OpenRA.FileFormats/Exts.cs index bb0085ceb3..affb235edc 100755 --- a/OpenRA.FileFormats/Exts.cs +++ b/OpenRA.FileFormats/Exts.cs @@ -181,5 +181,19 @@ namespace OpenRA { return ts.Concat(moreTs); } + + public static Color ColorLerp(float t, Color c1, Color c2) + { + return Color.FromArgb( + (int)(t * c2.A + (1 - t) * c1.A), + (int)(t * c2.R + (1 - t) * c1.R), + (int)(t * c2.G + (1 - t) * c1.G), + (int)(t * c2.B + (1 - t) * c1.B)); + } + } + + public static class Enum + { + public static T Parse(string s) { return (T)Enum.Parse(typeof(T), s); } } } diff --git a/OpenRA.FileFormats/PlayerColorRemap.cs b/OpenRA.FileFormats/PlayerColorRemap.cs index 854c31661a..dd071ca122 100755 --- a/OpenRA.FileFormats/PlayerColorRemap.cs +++ b/OpenRA.FileFormats/PlayerColorRemap.cs @@ -24,16 +24,21 @@ namespace OpenRA.FileFormats static readonly int[] CncRemapRamp = new[] { 0, 2, 4, 6, 8, 10, 13, 15, 1, 3, 5, 7, 9, 11, 12, 14 }; static readonly int[] NormalRemapRamp = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; - public static int GetRemapBase(PaletteFormat fmt) + static int GetRemapBase(PaletteFormat fmt) { return (fmt == PaletteFormat.cnc) ? 0xb0 : (fmt == PaletteFormat.d2k) ? 240 : 80; } - public static int[] GetRemapRamp(PaletteFormat fmt) + static int[] GetRemapRamp(PaletteFormat fmt) { return (fmt == PaletteFormat.cnc) ? CncRemapRamp : NormalRemapRamp; } + public static int GetRemapIndex(PaletteFormat fmt, int i) + { + return GetRemapBase(fmt) + GetRemapRamp(fmt)[i]; + } + public PlayerColorRemap(PaletteFormat fmt, ColorRamp c) { var c1 = c.GetColor(0); @@ -42,18 +47,10 @@ namespace OpenRA.FileFormats var baseIndex = GetRemapBase(fmt); var ramp = GetRemapRamp(fmt); - remapColors = ramp.Select((x, i) => Pair.New(baseIndex + i, ColorLerp(x / 16f, c1, c2))) + remapColors = ramp.Select((x, i) => Pair.New(baseIndex + i, Exts.ColorLerp(x / 16f, c1, c2))) .ToDictionary(u => u.First, u => u.Second); } - public static Color ColorLerp(float t, Color c1, Color c2) - { - return Color.FromArgb(255, - (int)(t * c2.R + (1 - t) * c1.R), - (int)(t * c2.G + (1 - t) * c1.G), - (int)(t * c2.B + (1 - t) * c1.B)); - } - public Color GetRemappedColor(Color original, int index) { Color c; diff --git a/OpenRA.Game/Graphics/Util.cs b/OpenRA.Game/Graphics/Util.cs index 424bf30069..9d2cab089c 100644 --- a/OpenRA.Game/Graphics/Util.cs +++ b/OpenRA.Game/Graphics/Util.cs @@ -19,21 +19,7 @@ namespace OpenRA.Graphics { public static class Util { - public static string[] ReadAllLines(Stream s) - { - List result = new List(); - using (StreamReader reader = new StreamReader(s)) - while(!reader.EndOfStream) - { - var line = reader.ReadLine(); - if( !string.IsNullOrEmpty( line ) && line[0] != '#' ) - result.Add( line ); - } - - return result.ToArray(); - } - - public static T[] MakeArray(int count, Converter f) + public static T[] MakeArray(int count, Func f) { T[] result = new T[count]; for (int i = 0; i < count; i++) @@ -59,6 +45,7 @@ namespace OpenRA.Graphics } static readonly int[] channelMasks = { 2, 1, 0, 3 }; // yes, our channel order is nuts. + public static void FastCopyIntoChannel(Sprite dest, byte[] src) { var data = dest.sheet.Data; @@ -79,28 +66,5 @@ namespace OpenRA.Graphics destOffset += destSkip; } } - - public static Color Lerp(float t, Color a, Color b) - { - return Color.FromArgb( - LerpChannel(t, a.A, b.A), - LerpChannel(t, a.R, b.R), - LerpChannel(t, a.G, b.G), - LerpChannel(t, a.B, b.B)); - } - - public static int LerpARGBColor(float t, int c1, int c2) - { - int a = LerpChannel(t, (c1 >> 24) & 255, (c2 >> 24) & 255); - int r = LerpChannel(t, (c1 >> 16) & 255, (c2 >> 16) & 255); - int g = LerpChannel(t, (c1 >> 8) & 255, (c2 >> 8) & 255); - int b = LerpChannel(t, c1 & 255, c2 & 255); - return (a << 24) | (r << 16) | (g << 8) | b; - } - - public static int LerpChannel(float t, int a, int b) - { - return (int)((1 - t) * a + t * b); - } } } diff --git a/OpenRA.Mods.Cnc/CncMenuPaletteEffect.cs b/OpenRA.Mods.Cnc/CncMenuPaletteEffect.cs index 9d8220ec8f..bed2867125 100644 --- a/OpenRA.Mods.Cnc/CncMenuPaletteEffect.cs +++ b/OpenRA.Mods.Cnc/CncMenuPaletteEffect.cs @@ -82,7 +82,7 @@ namespace OpenRA.Mods.Cnc else { var f = ColorForEffect(from, orig); - pal.Value.SetColor(x, OpenRA.Graphics.Util.Lerp((float)remainingFrames / Info.FadeLength, t, f)); + pal.Value.SetColor(x, Exts.ColorLerp((float)remainingFrames / Info.FadeLength, t, f)); } } } diff --git a/OpenRA.Mods.RA/ChronoshiftPaletteEffect.cs b/OpenRA.Mods.RA/ChronoshiftPaletteEffect.cs index 0a34c39ad6..242f112c8e 100644 --- a/OpenRA.Mods.RA/ChronoshiftPaletteEffect.cs +++ b/OpenRA.Mods.RA/ChronoshiftPaletteEffect.cs @@ -32,6 +32,8 @@ namespace OpenRA.Mods.RA if (remainingFrames > 0) remainingFrames--; } + + static List excludePalettes = new List{"cursor", "chrome", "colorpicker", "shroud", "fog"}; public void AdjustPalette(Dictionary palettes) { @@ -39,7 +41,7 @@ namespace OpenRA.Mods.RA return; var frac = (float)remainingFrames / chronoEffectLength; - var excludePalettes = new List(){"cursor", "chrome", "colorpicker", "shroud", "fog"}; + foreach (var pal in palettes) { if (excludePalettes.Contains(pal.Key)) @@ -50,7 +52,7 @@ namespace OpenRA.Mods.RA var orig = pal.Value.GetColor(x); var lum = (int)(255 * orig.GetBrightness()); var desat = Color.FromArgb(orig.A, lum, lum, lum); - pal.Value.SetColor(x, OpenRA.Graphics.Util.Lerp(frac, orig, desat)); + pal.Value.SetColor(x, Exts.ColorLerp(frac, orig, desat)); } } } diff --git a/OpenRA.Mods.RA/Effects/Contrail.cs b/OpenRA.Mods.RA/Effects/Contrail.cs index 9bb5fb0ea8..3fe86a8112 100755 --- a/OpenRA.Mods.RA/Effects/Contrail.cs +++ b/OpenRA.Mods.RA/Effects/Contrail.cs @@ -62,7 +62,7 @@ namespace OpenRA.Mods.RA public static Color ChooseColor(Actor self) { var ownerColor = Color.FromArgb(255, self.Owner.ColorRamp.GetColor(0)); - return PlayerColorRemap.ColorLerp(0.5f, ownerColor, Color.White); + return Exts.ColorLerp(0.5f, ownerColor, Color.White); } public ContrailHistory(int trailLength, Color color) diff --git a/OpenRA.Mods.RA/NukePaletteEffect.cs b/OpenRA.Mods.RA/NukePaletteEffect.cs index ad4be642ad..3e463fdd11 100644 --- a/OpenRA.Mods.RA/NukePaletteEffect.cs +++ b/OpenRA.Mods.RA/NukePaletteEffect.cs @@ -32,6 +32,8 @@ namespace OpenRA.Mods.RA if (remainingFrames > 0) remainingFrames--; } + + static List excludePalettes = new List{ "cursor", "chrome", "colorpicker", "shroud", "fog" }; public void AdjustPalette(Dictionary palettes) { @@ -40,7 +42,6 @@ namespace OpenRA.Mods.RA var frac = (float)remainingFrames / nukeEffectLength; - var excludePalettes = new List(){"cursor", "chrome", "colorpicker", "shroud", "fog"}; foreach (var pal in palettes) { if (excludePalettes.Contains(pal.Key)) @@ -50,7 +51,7 @@ namespace OpenRA.Mods.RA { var orig = pal.Value.GetColor(x); var white = Color.FromArgb(orig.A, 255, 255, 255); - pal.Value.SetColor(x, OpenRA.Graphics.Util.Lerp(frac,orig,white)); + pal.Value.SetColor(x, Exts.ColorLerp(frac,orig,white)); } } } diff --git a/OpenRA.Mods.RA/Widgets/PowerBinWidget.cs b/OpenRA.Mods.RA/Widgets/PowerBinWidget.cs index dcff22e2f6..4b96290618 100755 --- a/OpenRA.Mods.RA/Widgets/PowerBinWidget.cs +++ b/OpenRA.Mods.RA/Widgets/PowerBinWidget.cs @@ -77,7 +77,7 @@ namespace OpenRA.Mods.RA.Widgets var color = GetPowerColor(power); - var colorDark = Graphics.Util.Lerp(0.25f, color, Color.Black); + var colorDark = Exts.ColorLerp(0.25f, color, Color.Black); for (int i = 0; i < powerSize.Height; i++) { color = (i - 1 < powerSize.Height / 2) ? color : colorDark; diff --git a/OpenRA.Utility/Command.cs b/OpenRA.Utility/Command.cs index ab6520cb90..c3bcc040f8 100644 --- a/OpenRA.Utility/Command.cs +++ b/OpenRA.Utility/Command.cs @@ -15,7 +15,6 @@ using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Runtime.InteropServices; -using System.Windows.Forms; using OpenRA.FileFormats; using OpenRA.FileFormats.Graphics; using OpenRA.GameRules; @@ -190,13 +189,13 @@ namespace OpenRA.Utility for( var i = 0; i < 4; i++ ) remap[i] = i; - var srcPaletteType = (PaletteFormat)Enum.Parse(typeof(PaletteFormat), args[1].Split(':')[0]); - var destPaletteType = (PaletteFormat)Enum.Parse(typeof(PaletteFormat), args[2].Split(':')[0]); + var srcPaletteType = Enum.Parse(args[1].Split(':')[0]); + var destPaletteType = Enum.Parse(args[2].Split(':')[0]); /* the remap range is always 16 entries, but their location and order changes */ for( var i = 0; i < 16; i++ ) - remap[ PlayerColorRemap.GetRemapBase(srcPaletteType) + PlayerColorRemap.GetRemapRamp(srcPaletteType)[i] ] - = PlayerColorRemap.GetRemapBase(destPaletteType) + PlayerColorRemap.GetRemapRamp(destPaletteType)[i]; + remap[ PlayerColorRemap.GetRemapIndex(srcPaletteType, i) ] + = PlayerColorRemap.GetRemapIndex(destPaletteType, i); /* map everything else to the best match based on channel-wise distance */ var srcPalette = Palette.Load(args[1].Split(':')[1], false);