remove some duplication and dead code

This commit is contained in:
Chris Forbes
2011-12-27 14:47:17 +13:00
parent 132c678594
commit 523183431b
9 changed files with 38 additions and 61 deletions

View File

@@ -181,5 +181,19 @@ namespace OpenRA
{ {
return ts.Concat(moreTs); 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<T>
{
public static T Parse(string s) { return (T)Enum.Parse(typeof(T), s); }
} }
} }

View File

@@ -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[] 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 }; 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; 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; 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) public PlayerColorRemap(PaletteFormat fmt, ColorRamp c)
{ {
var c1 = c.GetColor(0); var c1 = c.GetColor(0);
@@ -42,18 +47,10 @@ namespace OpenRA.FileFormats
var baseIndex = GetRemapBase(fmt); var baseIndex = GetRemapBase(fmt);
var ramp = GetRemapRamp(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); .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) public Color GetRemappedColor(Color original, int index)
{ {
Color c; Color c;

View File

@@ -19,21 +19,7 @@ namespace OpenRA.Graphics
{ {
public static class Util public static class Util
{ {
public static string[] ReadAllLines(Stream s) public static T[] MakeArray<T>(int count, Func<int, T> f)
{
List<string> result = new List<string>();
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<T>(int count, Converter<int, T> f)
{ {
T[] result = new T[count]; T[] result = new T[count];
for (int i = 0; i < count; i++) 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. static readonly int[] channelMasks = { 2, 1, 0, 3 }; // yes, our channel order is nuts.
public static void FastCopyIntoChannel(Sprite dest, byte[] src) public static void FastCopyIntoChannel(Sprite dest, byte[] src)
{ {
var data = dest.sheet.Data; var data = dest.sheet.Data;
@@ -79,28 +66,5 @@ namespace OpenRA.Graphics
destOffset += destSkip; 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);
}
} }
} }

View File

@@ -82,7 +82,7 @@ namespace OpenRA.Mods.Cnc
else else
{ {
var f = ColorForEffect(from, orig); 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));
} }
} }
} }

View File

@@ -32,6 +32,8 @@ namespace OpenRA.Mods.RA
if (remainingFrames > 0) if (remainingFrames > 0)
remainingFrames--; remainingFrames--;
} }
static List<string> excludePalettes = new List<string>{"cursor", "chrome", "colorpicker", "shroud", "fog"};
public void AdjustPalette(Dictionary<string,Palette> palettes) public void AdjustPalette(Dictionary<string,Palette> palettes)
{ {
@@ -39,7 +41,7 @@ namespace OpenRA.Mods.RA
return; return;
var frac = (float)remainingFrames / chronoEffectLength; var frac = (float)remainingFrames / chronoEffectLength;
var excludePalettes = new List<string>(){"cursor", "chrome", "colorpicker", "shroud", "fog"};
foreach (var pal in palettes) foreach (var pal in palettes)
{ {
if (excludePalettes.Contains(pal.Key)) if (excludePalettes.Contains(pal.Key))
@@ -50,7 +52,7 @@ namespace OpenRA.Mods.RA
var orig = pal.Value.GetColor(x); var orig = pal.Value.GetColor(x);
var lum = (int)(255 * orig.GetBrightness()); var lum = (int)(255 * orig.GetBrightness());
var desat = Color.FromArgb(orig.A, lum, lum, lum); 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));
} }
} }
} }

View File

@@ -62,7 +62,7 @@ namespace OpenRA.Mods.RA
public static Color ChooseColor(Actor self) public static Color ChooseColor(Actor self)
{ {
var ownerColor = Color.FromArgb(255, self.Owner.ColorRamp.GetColor(0)); 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) public ContrailHistory(int trailLength, Color color)

View File

@@ -32,6 +32,8 @@ namespace OpenRA.Mods.RA
if (remainingFrames > 0) if (remainingFrames > 0)
remainingFrames--; remainingFrames--;
} }
static List<string> excludePalettes = new List<string>{ "cursor", "chrome", "colorpicker", "shroud", "fog" };
public void AdjustPalette(Dictionary<string,Palette> palettes) public void AdjustPalette(Dictionary<string,Palette> palettes)
{ {
@@ -40,7 +42,6 @@ namespace OpenRA.Mods.RA
var frac = (float)remainingFrames / nukeEffectLength; var frac = (float)remainingFrames / nukeEffectLength;
var excludePalettes = new List<string>(){"cursor", "chrome", "colorpicker", "shroud", "fog"};
foreach (var pal in palettes) foreach (var pal in palettes)
{ {
if (excludePalettes.Contains(pal.Key)) if (excludePalettes.Contains(pal.Key))
@@ -50,7 +51,7 @@ namespace OpenRA.Mods.RA
{ {
var orig = pal.Value.GetColor(x); var orig = pal.Value.GetColor(x);
var white = Color.FromArgb(orig.A, 255, 255, 255); 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));
} }
} }
} }

View File

@@ -77,7 +77,7 @@ namespace OpenRA.Mods.RA.Widgets
var color = GetPowerColor(power); 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++) for (int i = 0; i < powerSize.Height; i++)
{ {
color = (i - 1 < powerSize.Height / 2) ? color : colorDark; color = (i - 1 < powerSize.Height / 2) ? color : colorDark;

View File

@@ -15,7 +15,6 @@ using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Windows.Forms;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.FileFormats.Graphics; using OpenRA.FileFormats.Graphics;
using OpenRA.GameRules; using OpenRA.GameRules;
@@ -190,13 +189,13 @@ namespace OpenRA.Utility
for( var i = 0; i < 4; i++ ) for( var i = 0; i < 4; i++ )
remap[i] = i; remap[i] = i;
var srcPaletteType = (PaletteFormat)Enum.Parse(typeof(PaletteFormat), args[1].Split(':')[0]); var srcPaletteType = Enum<PaletteFormat>.Parse(args[1].Split(':')[0]);
var destPaletteType = (PaletteFormat)Enum.Parse(typeof(PaletteFormat), args[2].Split(':')[0]); var destPaletteType = Enum<PaletteFormat>.Parse(args[2].Split(':')[0]);
/* the remap range is always 16 entries, but their location and order changes */ /* the remap range is always 16 entries, but their location and order changes */
for( var i = 0; i < 16; i++ ) for( var i = 0; i < 16; i++ )
remap[ PlayerColorRemap.GetRemapBase(srcPaletteType) + PlayerColorRemap.GetRemapRamp(srcPaletteType)[i] ] remap[ PlayerColorRemap.GetRemapIndex(srcPaletteType, i) ]
= PlayerColorRemap.GetRemapBase(destPaletteType) + PlayerColorRemap.GetRemapRamp(destPaletteType)[i]; = PlayerColorRemap.GetRemapIndex(destPaletteType, i);
/* map everything else to the best match based on channel-wise distance */ /* map everything else to the best match based on channel-wise distance */
var srcPalette = Palette.Load(args[1].Split(':')[1], false); var srcPalette = Palette.Load(args[1].Split(':')[1], false);