From 07ed6a889e45d17139f85ffd3b26c3d2b4fac7d4 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Mon, 20 Nov 2023 10:16:31 +0000 Subject: [PATCH] Move ColorShift traits into the main repo. --- .../Traits/Palettes/ColorPickerColorShift.cs | 84 +++++++++++++++++++ .../Traits/Palettes/FixedColorShift.cs | 66 +++++++++++++++ .../Traits/Palettes/FixedPlayerColorShift.cs | 48 +++++++++++ .../Traits/Palettes/PlayerColorShift.cs | 63 ++++++++++++++ 4 files changed, 261 insertions(+) create mode 100644 OpenRA.Mods.Common/Traits/Palettes/ColorPickerColorShift.cs create mode 100644 OpenRA.Mods.Common/Traits/Palettes/FixedColorShift.cs create mode 100644 OpenRA.Mods.Common/Traits/Palettes/FixedPlayerColorShift.cs create mode 100644 OpenRA.Mods.Common/Traits/Palettes/PlayerColorShift.cs diff --git a/OpenRA.Mods.Common/Traits/Palettes/ColorPickerColorShift.cs b/OpenRA.Mods.Common/Traits/Palettes/ColorPickerColorShift.cs new file mode 100644 index 0000000000..bad3f66957 --- /dev/null +++ b/OpenRA.Mods.Common/Traits/Palettes/ColorPickerColorShift.cs @@ -0,0 +1,84 @@ +#region Copyright & License Information +/* + * Copyright (c) The OpenRA Developers and Contributors + * 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 OpenRA.Graphics; +using OpenRA.Primitives; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Traits +{ + [TraitLocation(SystemActors.World | SystemActors.EditorWorld)] + [Desc("Create a color picker palette from another palette.")] + sealed class ColorPickerColorShiftInfo : TraitInfo + { + [PaletteReference] + [FieldLoader.Require] + [Desc("The name of the palette to base off.")] + public readonly string BasePalette = null; + + [Desc("Hues between this and MaxHue will be shifted.")] + public readonly float MinHue = 0.29f; + + [Desc("Hues between MinHue and this will be shifted.")] + public readonly float MaxHue = 0.37f; + + [Desc("Hue reference for the color shift.")] + public readonly float ReferenceHue = 0.33f; + + [Desc("Saturation reference for the color shift.")] + public readonly float ReferenceSaturation = 0.925f; + + [Desc("Value reference for the color shift.")] + public readonly float ReferenceValue = 0.95f; + + public override object Create(ActorInitializer init) { return new ColorPickerColorShift(this); } + } + + sealed class ColorPickerColorShift : ILoadsPalettes, ITickRender + { + readonly ColorPickerColorShiftInfo info; + Color color; + Color preferredColor; + + public ColorPickerColorShift(ColorPickerColorShiftInfo info) + { + this.info = info; + + // All users need to use the same TraitInfo instance, chosen as the default mod rules + var colorManager = Game.ModData.DefaultRules.Actors[SystemActors.World].TraitInfo(); + colorManager.OnColorPickerColorUpdate += c => preferredColor = c; + preferredColor = Game.Settings.Player.Color; + } + + void ILoadsPalettes.LoadPalettes(WorldRenderer wr) + { + color = preferredColor; + var (r, g, b) = color.ToLinear(); + var (h, s, v) = Color.RgbToHsv(r, g, b); + wr.SetPaletteColorShift(info.BasePalette, + h - info.ReferenceHue, s - info.ReferenceSaturation, v / info.ReferenceValue, + info.MinHue, info.MaxHue); + } + + void ITickRender.TickRender(WorldRenderer wr, Actor self) + { + if (color == preferredColor) + return; + + color = preferredColor; + var (r, g, b) = color.ToLinear(); + var (h, s, v) = Color.RgbToHsv(r, g, b); + wr.SetPaletteColorShift(info.BasePalette, + h - info.ReferenceHue, s - info.ReferenceSaturation, v / info.ReferenceValue, + info.MinHue, info.MaxHue); + } + } +} diff --git a/OpenRA.Mods.Common/Traits/Palettes/FixedColorShift.cs b/OpenRA.Mods.Common/Traits/Palettes/FixedColorShift.cs new file mode 100644 index 0000000000..2b35668cfc --- /dev/null +++ b/OpenRA.Mods.Common/Traits/Palettes/FixedColorShift.cs @@ -0,0 +1,66 @@ +#region Copyright & License Information +/* + * Copyright (c) The OpenRA Developers and Contributors + * 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 OpenRA.Graphics; +using OpenRA.Primitives; +using OpenRA.Traits; + +namespace OpenRA.Mods.Mobius.Traits +{ + [TraitLocation(SystemActors.World | SystemActors.EditorWorld)] + [Desc("Apply a fixed color shift to a palette. Use this to add RGBA compatibility to FixedColorPalette.")] + public class FixedColorShiftInfo : TraitInfo + { + [PaletteReference] + [FieldLoader.Require] + [Desc("The name of the palette to base off.")] + public readonly string BasePalette = null; + + [Desc("The fixed color to remap.")] + public readonly Color Color; + + [Desc("Hues between this and MaxHue will be shifted.")] + public readonly float MinHue = 0.29f; + + [Desc("Hues between MinHue and this will be shifted.")] + public readonly float MaxHue = 0.37f; + + [Desc("Hue reference for the color shift.")] + public readonly float ReferenceHue = 0.33f; + + [Desc("Saturation reference for the color shift.")] + public readonly float ReferenceSaturation = 0.925f; + + [Desc("Value reference for the color shift.")] + public readonly float ReferenceValue = 0.95f; + + public override object Create(ActorInitializer init) { return new FixedColorShift(this); } + } + + public class FixedColorShift : ILoadsPalettes + { + readonly FixedColorShiftInfo info; + + public FixedColorShift(FixedColorShiftInfo info) + { + this.info = info; + } + + public void LoadPalettes(WorldRenderer wr) + { + var (r, g, b) = info.Color.ToLinear(); + var (h, s, v) = Color.RgbToHsv(r, g, b); + wr.SetPaletteColorShift(info.BasePalette, + h - info.ReferenceHue, s - info.ReferenceSaturation, v / info.ReferenceValue, + info.MinHue, info.MaxHue); + } + } +} diff --git a/OpenRA.Mods.Common/Traits/Palettes/FixedPlayerColorShift.cs b/OpenRA.Mods.Common/Traits/Palettes/FixedPlayerColorShift.cs new file mode 100644 index 0000000000..24ea579662 --- /dev/null +++ b/OpenRA.Mods.Common/Traits/Palettes/FixedPlayerColorShift.cs @@ -0,0 +1,48 @@ +#region Copyright & License Information +/* + * Copyright (c) The OpenRA Developers and Contributors + * 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.Collections.Generic; +using OpenRA.Graphics; +using OpenRA.Primitives; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Traits +{ + [TraitLocation(SystemActors.World | SystemActors.EditorWorld)] + [Desc("Add fixed color shifts to player palettes. Use to add RGBA compatibility to IndexedPlayerPalette.")] + public class FixedPlayerColorShiftInfo : TraitInfo + { + [PaletteReference(true)] + [FieldLoader.Require] + [Desc("The name of the palette to base off.")] + public readonly string BasePalette = null; + + public readonly Dictionary PlayerIndex; + + public override object Create(ActorInitializer init) { return new FixedPlayerColorShift(this); } + } + + public class FixedPlayerColorShift : ILoadsPlayerPalettes + { + readonly FixedPlayerColorShiftInfo info; + + public FixedPlayerColorShift(FixedPlayerColorShiftInfo info) + { + this.info = info; + } + + public void LoadPlayerPalettes(WorldRenderer wr, string playerName, Color color, bool replaceExisting) + { + if (info.PlayerIndex.TryGetValue(playerName, out var shift)) + wr.SetPaletteColorShift(info.BasePalette + playerName, shift[0], shift[1], shift[2], shift[3], shift[4]); + } + } +} diff --git a/OpenRA.Mods.Common/Traits/Palettes/PlayerColorShift.cs b/OpenRA.Mods.Common/Traits/Palettes/PlayerColorShift.cs new file mode 100644 index 0000000000..bf154c16c9 --- /dev/null +++ b/OpenRA.Mods.Common/Traits/Palettes/PlayerColorShift.cs @@ -0,0 +1,63 @@ +#region Copyright & License Information +/* + * Copyright (c) The OpenRA Developers and Contributors + * 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 OpenRA.Graphics; +using OpenRA.Primitives; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Traits +{ + [TraitLocation(SystemActors.World | SystemActors.EditorWorld)] + [Desc("Add color shifts to player palettes. Use to add RGBA compatibility to PlayerColorPalette.")] + public class PlayerColorShiftInfo : TraitInfo + { + [PaletteReference(true)] + [FieldLoader.Require] + [Desc("The name of the palette to base off.")] + public readonly string BasePalette = null; + + [Desc("Hues between this and MaxHue will be shifted.")] + public readonly float MinHue = 0.29f; + + [Desc("Hues between MinHue and this will be shifted.")] + public readonly float MaxHue = 0.37f; + + [Desc("Hue reference for the color shift.")] + public readonly float ReferenceHue = 0.33f; + + [Desc("Saturation reference for the color shift.")] + public readonly float ReferenceSaturation = 0.925f; + + [Desc("Value reference for the color shift.")] + public readonly float ReferenceValue = 0.95f; + + public override object Create(ActorInitializer init) { return new PlayerColorShift(this); } + } + + public class PlayerColorShift : ILoadsPlayerPalettes + { + readonly PlayerColorShiftInfo info; + + public PlayerColorShift(PlayerColorShiftInfo info) + { + this.info = info; + } + + public void LoadPlayerPalettes(WorldRenderer wr, string playerName, Color color, bool replaceExisting) + { + var (r, g, b) = color.ToLinear(); + var (h, s, v) = Color.RgbToHsv(r, g, b); + wr.SetPaletteColorShift(info.BasePalette + playerName, + h - info.ReferenceHue, s - info.ReferenceSaturation, v / info.ReferenceValue, + info.MinHue, info.MaxHue); + } + } +}