diff --git a/OpenRA.Mods.Common/Traits/Palettes/PaletteFromGrayscale.cs b/OpenRA.Mods.Common/Traits/Palettes/PaletteFromGrayscale.cs new file mode 100644 index 0000000000..ce30b98fd9 --- /dev/null +++ b/OpenRA.Mods.Common/Traits/Palettes/PaletteFromGrayscale.cs @@ -0,0 +1,58 @@ +#region Copyright & License Information +/* + * Copyright 2007-2021 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, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System.Linq; +using OpenRA.Graphics; +using OpenRA.Primitives; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Traits +{ + [TraitLocation(SystemActors.World | SystemActors.EditorWorld)] + [Desc("Creates a greyscale palette without any base palette file.")] + class PaletteFromGrayscaleInfo : TraitInfo + { + [PaletteDefinition] + [FieldLoader.Require] + [Desc("internal palette name")] + public readonly string Name = null; + + [Desc("If defined, load the palette only for this tileset.")] + public readonly string Tileset = null; + + public readonly bool AllowModifiers = true; + + [Desc("Index set to be fully transparent/invisible.")] + public readonly int TransparentIndex = 0; + + public override object Create(ActorInitializer init) { return new PaletteFromGrayscale(init.World, this); } + } + + class PaletteFromGrayscale : ILoadsPalettes + { + readonly World world; + readonly PaletteFromGrayscaleInfo info; + public PaletteFromGrayscale(World world, PaletteFromGrayscaleInfo info) + { + this.world = world; + this.info = info; + } + + public void LoadPalettes(WorldRenderer wr) + { + // Enable palette only for a specific tileset + if (info.Tileset != null && !string.Equals(info.Tileset, world.Map.Tileset, System.StringComparison.InvariantCultureIgnoreCase)) + return; + + wr.AddPalette(info.Name, new ImmutablePalette(Enumerable.Range(0, Palette.Size).Select(i => (i == info.TransparentIndex) ? 0 : (uint)Color.FromArgb(255, i, i, i).ToArgb())), info.AllowModifiers); + } + } +}