Merge pull request #3773 from Mailaender/palette-clean

Cleaned up the palette code
This commit is contained in:
Chris Forbes
2013-09-02 03:34:39 -07:00
3 changed files with 65 additions and 64 deletions

View File

@@ -19,24 +19,6 @@ 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;
@@ -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)

View File

@@ -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)
{
@@ -111,15 +117,10 @@ namespace OpenRA.FileFormats
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);
}
}
public interface IPaletteRemap { Color GetRemappedColor(Color original, int index); }

View File

@@ -15,33 +15,33 @@ using System.Linq;
namespace OpenRA.FileFormats
{
public class PlayerColorRemap : IPaletteRemap
{
Dictionary<int, Color> 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 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
// reversed remapping
if (ramp[0] > ramp[15])
{
baseIndex = 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);
}