From d4ed9702c8211f2bc783cd7f56d086218ee68ff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sun, 1 Sep 2013 09:54:50 +0200 Subject: [PATCH 1/3] spaces to tabs and StyleCop cleanup --- OpenRA.FileFormats/Palette.cs | 43 ++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/OpenRA.FileFormats/Palette.cs b/OpenRA.FileFormats/Palette.cs index be5bf5e6bf..ceccccd81b 100644 --- a/OpenRA.FileFormats/Palette.cs +++ b/OpenRA.FileFormats/Palette.cs @@ -8,16 +8,22 @@ */ #endregion +using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; -using System; using System.Reflection; namespace OpenRA.FileFormats { public class Palette { + public static Palette Load(string filename, int[] remap) + { + using (var s = File.OpenRead(filename)) + return new Palette(s, remap); + } + uint[] colors; public Color GetColor(int index) { @@ -41,8 +47,8 @@ namespace OpenRA.FileFormats public void ApplyRemap(IPaletteRemap r) { - for(int i = 0; i < 256; i++) - colors[i] = (uint)r.GetRemappedColor(Color.FromArgb((int)colors[i]),i).ToArgb(); + for (int i = 0; i < 256; i++) + colors[i] = (uint)r.GetRemappedColor(Color.FromArgb((int)colors[i]), i).ToArgb(); } public Palette(Stream s, int[] remapShadow) @@ -60,7 +66,7 @@ namespace OpenRA.FileFormats } } - colors[0] = 0; //convert black background to transparency + colors[0] = 0; // convert black background to transparency foreach (int i in remapShadow) colors[i] = 140u << 24; } @@ -101,24 +107,19 @@ namespace OpenRA.FileFormats } public Bitmap AsBitmap() - { - var b = new Bitmap(256, 1, PixelFormat.Format32bppArgb); - var data = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), - ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); - unsafe - { - uint* c = (uint*)data.Scan0; - for (var x = 0; x < 256; x++) - *(c + x) = colors[x]; - } - b.UnlockBits(data); - return b; - } - - public static Palette Load(string filename, int[] remap) { - using(var s = File.OpenRead(filename)) - return new Palette(s, remap); + var b = new Bitmap(256, 1, PixelFormat.Format32bppArgb); + var data = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), + ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); + unsafe + { + uint* c = (uint*)data.Scan0; + for (var x = 0; x < 256; x++) + *(c + x) = colors[x]; + } + + b.UnlockBits(data); + return b; } } From 8908b1ec6491e220953f37ddf63e361f87a8be27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sun, 1 Sep 2013 10:05:09 +0200 Subject: [PATCH 2/3] StyleCop clean HSLColor --- OpenRA.FileFormats/HSLColor.cs | 62 +++++++++++++++++----------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/OpenRA.FileFormats/HSLColor.cs b/OpenRA.FileFormats/HSLColor.cs index ef95c203be..b19fa432c7 100644 --- a/OpenRA.FileFormats/HSLColor.cs +++ b/OpenRA.FileFormats/HSLColor.cs @@ -19,29 +19,11 @@ namespace OpenRA.FileFormats public readonly byte L; public readonly Color RGB; - public HSLColor(byte h, byte s, byte l) - { - H = h; - S = s; - L = l; - RGB = RGBFromHSL(H / 255f, S / 255f, L / 255f); - } - - public void ToHSV(out float h, out float s, out float v) - { - var ll = 2*L / 255f; - var ss = S / 255f * ((ll <= 1) ? ll : 2 - ll); - - h = H / 255f; - s = (2 * ss) / (ll + ss); - v = (ll + ss) / 2; - } - public static HSLColor FromHSV(float h, float s, float v) { - var ll = 0.5f*(2 - s)*v; - var ss = (ll >= 1 || v <= 0) ? 0 : 0.5f*s*v / (ll <= 0.5f ? ll : 1 - ll); - return new HSLColor((byte)(255*h), (byte)(255*ss), (byte)(255*ll)); + var ll = 0.5f * (2 - s) * v; + var ss = (ll >= 1 || v <= 0) ? 0 : 0.5f * s * v / (ll <= 0.5f ? ll : 1 - ll); + return new HSLColor((byte)(255 * h), (byte)(255 * ss), (byte)(255 * ll)); } public static HSLColor FromRGB(int r, int g, int b) @@ -59,7 +41,7 @@ namespace OpenRA.FileFormats var q = (l < 0.5f) ? l * (1 + s) : l + s - (l * s); var p = 2 * l - q; - float[] trgb = { h + 1 / 3.0f, h, h - 1/3.0f }; + float[] trgb = { h + 1 / 3.0f, h, h - 1 / 3.0f }; float[] rgb = { 0, 0, 0 }; for (int k = 0; k < 3; k++) @@ -70,27 +52,45 @@ namespace OpenRA.FileFormats for (int k = 0; k < 3; k++) { - if (trgb[k] < 1 / 6.0f) { rgb[k] = (p + ((q - p) * 6 * trgb[k])); } + if (trgb[k] < 1 / 6.0f) { rgb[k] = p + ((q - p) * 6 * trgb[k]); } else if (trgb[k] >= 1 / 6.0f && trgb[k] < 0.5) { rgb[k] = q; } - else if (trgb[k] >= 0.5f && trgb[k] < 2.0f / 3) { rgb[k] = (p + ((q - p) * 6 * (2.0f / 3 - trgb[k]))); } + else if (trgb[k] >= 0.5f && trgb[k] < 2.0f / 3) { rgb[k] = p + ((q - p) * 6 * (2.0f / 3 - trgb[k])); } else { rgb[k] = p; } } return Color.FromArgb((int)(rgb[0] * 255), (int)(rgb[1] * 255), (int)(rgb[2] * 255)); } + public static bool operator ==(HSLColor me, HSLColor other) + { + return me.H == other.H && me.S == other.S && me.L == other.L; + } + + public static bool operator !=(HSLColor me, HSLColor other) { return !(me == other); } + + public HSLColor(byte h, byte s, byte l) + { + H = h; + S = s; + L = l; + RGB = RGBFromHSL(H / 255f, S / 255f, L / 255f); + } + + public void ToHSV(out float h, out float s, out float v) + { + var ll = 2 * L / 255f; + var ss = S / 255f * ((ll <= 1) ? ll : 2 - ll); + + h = H / 255f; + s = (2 * ss) / (ll + ss); + v = (ll + ss) / 2; + } + public override string ToString() { return "{0},{1},{2}".F(H, S, L); } - public static bool operator ==(HSLColor me, HSLColor other) - { - return (me.H == other.H && me.S == other.S && me.L == other.L); - } - - public static bool operator !=(HSLColor me, HSLColor other) { return !(me == other); } - public override int GetHashCode() { return H.GetHashCode() ^ S.GetHashCode() ^ L.GetHashCode(); } public override bool Equals(object obj) From e4c5ba26a9f2e16472f02e68fe3f1858ba117e7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Sun, 1 Sep 2013 10:05:25 +0200 Subject: [PATCH 3/3] StyleCop clean PlayerColorRemap spaces to tabs --- OpenRA.FileFormats/PlayerColorRemap.cs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/OpenRA.FileFormats/PlayerColorRemap.cs b/OpenRA.FileFormats/PlayerColorRemap.cs index 24befa83bb..c33b43f155 100755 --- a/OpenRA.FileFormats/PlayerColorRemap.cs +++ b/OpenRA.FileFormats/PlayerColorRemap.cs @@ -15,33 +15,33 @@ using System.Linq; namespace OpenRA.FileFormats { - public class PlayerColorRemap : IPaletteRemap { Dictionary remapColors; - public static int GetRemapIndex(int[] Ramp, int i) + public static int GetRemapIndex(int[] ramp, int i) { - return Ramp[i]; + return ramp[i]; } - public PlayerColorRemap(int[] Ramp, HSLColor c, float rampFraction) + public PlayerColorRemap(int[] ramp, HSLColor c, float rampFraction) { // Increase luminosity if required to represent the full ramp - var rampRange = (byte)((1 - rampFraction)*c.L); + var rampRange = (byte)((1 - rampFraction) * c.L); var c1 = new HSLColor(c.H, c.S, (byte)Math.Max(rampRange, c.L)).RGB; var c2 = new HSLColor(c.H, c.S, (byte)Math.Max(0, c.L - rampRange)).RGB; - var baseIndex = Ramp[0]; - var RemapRamp = Ramp.Select(r => r - Ramp[0]).ToArray(); + var baseIndex = ramp[0]; + var remapRamp = ramp.Select(r => r - ramp[0]).ToArray(); - if (Ramp[0] > Ramp[15]) // reversed remapping - { - baseIndex = Ramp[15]; + // reversed remapping + if (ramp[0] > ramp[15]) + { + baseIndex = ramp[15]; for (var i = 15; i > 0; i--) - RemapRamp = Ramp.Select(r => r - Ramp[15]).ToArray(); + remapRamp = ramp.Select(r => r - ramp[15]).ToArray(); } - remapColors = RemapRamp.Select((x, i) => Pair.New(baseIndex + i, Exts.ColorLerp(x / 16f, c1, c2))) + remapColors = remapRamp.Select((x, i) => Pair.New(baseIndex + i, Exts.ColorLerp(x / 16f, c1, c2))) .ToDictionary(u => u.First, u => u.Second); }