From b525f471326d9d1be822cf407d833e5f6fbcddef Mon Sep 17 00:00:00 2001 From: Arne Kiesewetter Date: Fri, 13 Dec 2024 16:01:23 +0100 Subject: [PATCH] Adds a fix for https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/316 --- .../NonHDRColorClamping.cs | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 CommunityBugFixCollection/NonHDRColorClamping.cs diff --git a/CommunityBugFixCollection/NonHDRColorClamping.cs b/CommunityBugFixCollection/NonHDRColorClamping.cs new file mode 100644 index 0000000..c1e7318 --- /dev/null +++ b/CommunityBugFixCollection/NonHDRColorClamping.cs @@ -0,0 +1,60 @@ +using Elements.Core; +using HarmonyLib; +using MonkeyLoader.Resonite; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; + +namespace CommunityBugFixCollection +{ + internal sealed class NonHDRColorClamping : ResoniteMonkey + { + public override bool CanBeDisabled => true; + + [HarmonyPatch] + [HarmonyPatchCategory(nameof(NonHDRColorClamping))] + private static class ColorXPatches + { + private static bool Prepare() => Enabled; + + private static IEnumerable TargetMethods() + { + var methodNames = new[] + { + nameof(colorX.AddR), nameof(colorX.AddG), nameof(colorX.AddB), nameof(colorX.AddA) + }; + + return methodNames + .Select(name => AccessTools.DeclaredMethod(typeof(colorX), name)) + .Where(method => method is not null); + } + + private static colorX Postfix(colorX __result) + => MathX.Clamp01(in __result); + } + + [HarmonyPatch] + [HarmonyPatchCategory(nameof(NonHDRColorClamping))] + private static class ColorPatches + { + private static bool Prepare() => Enabled; + + private static IEnumerable TargetMethods() + { + var methodNames = new[] + { + nameof(color.AddR), nameof(color.AddG), nameof(color.AddB), nameof(color.AddA) + }; + + return methodNames + .Select(name => AccessTools.DeclaredMethod(typeof(color), name)) + .Where(method => method is not null); + } + + private static color Postfix(color __result) + => new(MathX.Clamp01(__result.rgba)); + } + } +}