Files
OpenRA/OpenRA.Mods.Common/Traits/PaletteEffects/GlobalLightingPaletteEffect.cs
2015-04-11 12:05:25 +02:00

68 lines
2.0 KiB
C#

#region Copyright & License Information
/*
* Copyright 2007-2015 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;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Used for day/night effects.")]
class GlobalLightingPaletteEffectInfo : ITraitInfo
{
[Desc("Do not modify graphics that use any palette in this list.")]
public readonly string[] ExcludePalettes = { "cursor", "chrome", "colorpicker", "fog", "shroud", "alpha" };
[Desc("Do not modify graphics that start with these letters.")]
public readonly string[] ExcludePalettePrefixes = { };
public readonly float Red = 1f;
public readonly float Green = 1f;
public readonly float Blue = 1f;
public readonly float Ambient = 1f;
public object Create(ActorInitializer init) { return new GlobalLightingPaletteEffect(this); }
}
class GlobalLightingPaletteEffect : IPaletteModifier
{
readonly GlobalLightingPaletteEffectInfo info;
public GlobalLightingPaletteEffect(GlobalLightingPaletteEffectInfo info)
{
this.info = info;
}
public void AdjustPalette(IReadOnlyDictionary<string, MutablePalette> palettes)
{
foreach (var kvp in palettes)
{
if (info.ExcludePalettes.Contains(kvp.Key))
continue;
if (info.ExcludePalettePrefixes.Any(kvp.Key.StartsWith))
continue;
var palette = kvp.Value;
for (var x = 0; x < Palette.Size; x++)
{
var from = palette.GetColor(x);
var red = (int)(from.R * info.Ambient * info.Red).Clamp(0, 255);
var green = (int)(from.G * info.Ambient * info.Green).Clamp(0, 255);
var blue = (int)(from.B * info.Ambient * info.Blue).Clamp(0, 255);
palette.SetColor(x, Color.FromArgb(from.A, red, green, blue));
}
}
}
}
}