Replace ColorRamp with HSLColor everywhere.

Fixes:
* Nuclear-purple color exploit.
* #3247.
* Removes a bunch of unnecessary color conversions every frame.

Caveats:
* The ramp range is now defined on the palette, so ramps can no longer be set per-player (may impact maps which define custom colors).
* It's no longer possible to perfectly recreate the original WW color ramps (I doubt we care).
* The old ColorRamp setting isn't migrated, so players will lose their color settings.
This commit is contained in:
Paul Chote
2013-05-10 17:14:22 +12:00
parent abcc30f0b7
commit 656476991f
41 changed files with 112 additions and 168 deletions

View File

@@ -1,62 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Drawing;
namespace OpenRA.FileFormats
{
public struct ColorRamp
{
public readonly HSLColor Color;
public byte Ramp;
public ColorRamp(HSLColor color, byte ramp)
{
Color = color;
Ramp = ramp;
}
public ColorRamp(byte h, byte s, byte l, byte r)
{
Color = new HSLColor(h, s, l);
Ramp = r;
}
/* returns a color along the Lum ramp */
public Color GetColor(float t)
{
var l = float2.Lerp(Color.L, Color.L*Ramp/255f, t);
return HSLColor.RGBFromHSL(Color.H/255f, Color.S/255f, l/255f);
}
public override string ToString()
{
return "{0},{1}".F(Color.ToString(), Ramp);
}
public static bool operator ==(ColorRamp me, ColorRamp other)
{
return (me.Color == other.Color && me.Ramp == other.Ramp);
}
public static bool operator !=(ColorRamp me, ColorRamp other) { return !(me == other); }
public override int GetHashCode() { return Color.GetHashCode() ^ Ramp.GetHashCode(); }
public override bool Equals(object obj)
{
if (obj == null)
return false;
ColorRamp o = (ColorRamp)obj;
return o == this;
}
}
}

View File

@@ -148,15 +148,15 @@ namespace OpenRA.FileFormats
return InvalidValueAction(x,fieldType, field);
}
else if (fieldType == typeof(ColorRamp))
else if (fieldType == typeof(HSLColor))
{
var parts = x.Split(',');
if (parts.Length == 4)
return new ColorRamp(
// Allow old ColorRamp format to be parsed as HSLColor
if (parts.Length == 3 || parts.Length == 4)
return new HSLColor(
(byte)int.Parse(parts[0]).Clamp(0, 255),
(byte)int.Parse(parts[1]).Clamp(0, 255),
(byte)int.Parse(parts[2]).Clamp(0, 255),
(byte)int.Parse(parts[3]).Clamp(0, 255));
(byte)int.Parse(parts[2]).Clamp(0, 255));
return InvalidValueAction(x, fieldType, field);
}

View File

@@ -17,17 +17,14 @@ namespace OpenRA.FileFormats
public readonly byte H;
public readonly byte S;
public readonly byte L;
public readonly Color RGB;
public HSLColor(byte h, byte s, byte l)
{
H = h;
S = s;
L = l;
}
public Color ToColor()
{
return RGBFromHSL(H / 255f, S / 255f, L / 255f);
RGB = RGBFromHSL(H / 255f, S / 255f, L / 255f);
}
public void ToHSV(out float h, out float s, out float v)
@@ -47,6 +44,15 @@ namespace OpenRA.FileFormats
return new HSLColor((byte)(255*h), (byte)(255*ss), (byte)(255*ll));
}
public static HSLColor FromRGB(int r, int g, int b)
{
var c = Color.FromArgb(r, g, b);
var h = (byte)((c.GetHue() / 360.0f) * 255);
var s = (byte)(c.GetSaturation() * 255);
var l = (byte)(c.GetBrightness() * 255);
return new HSLColor(h, s, l);
}
public static Color RGBFromHSL(float h, float s, float l)
{
// Convert from HSL to RGB

View File

@@ -28,8 +28,10 @@ namespace OpenRA.FileFormats
public bool LockRace = false;
public string Race;
// ColorRamp naming retained for backward compatibility
public bool LockColor = false;
public ColorRamp ColorRamp = new ColorRamp(0,0,238,34);
public HSLColor ColorRamp = new HSLColor(0,0,238);
public HSLColor Color { get { return ColorRamp; } set { ColorRamp = value; }}
public bool LockSpawn = false;
public int Spawn = 0;

View File

@@ -77,7 +77,6 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ColorRamp.cs" />
<Compile Include="Evaluator.cs" />
<Compile Include="Exts.cs" />
<Compile Include="FieldLoader.cs" />

View File

@@ -8,6 +8,7 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
@@ -24,18 +25,19 @@ namespace OpenRA.FileFormats
return Ramp[i];
}
public PlayerColorRemap(int[] Ramp, ColorRamp c)
public PlayerColorRemap(int[] Ramp, HSLColor c, float rampFraction)
{
var c1 = c.GetColor(0);
var c2 = c.GetColor(1); // temptemp: this can be expressed better
// 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();
if (Ramp[0] > Ramp[15]) // reversed remapping
{
{
baseIndex = Ramp[15];
for (int i=15; i>0; i--)
for (var i = 15; i > 0; i--)
RemapRamp = Ramp.Select(r => r - Ramp[15]).ToArray();
}