From cd13cee48f3cf371e737e233d16da4eb0bfca066 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Sun, 27 Oct 2024 07:53:58 +0000 Subject: [PATCH] Move colour conversion to separate class --- KeepersCompound.Lightmapper/Program.cs | 30 ++----------------- .../{MathUtils.cs => Utils.cs} | 27 +++++++++++++++++ 2 files changed, 30 insertions(+), 27 deletions(-) rename KeepersCompound.Lightmapper/{MathUtils.cs => Utils.cs} (81%) diff --git a/KeepersCompound.Lightmapper/Program.cs b/KeepersCompound.Lightmapper/Program.cs index 5a51ac4..884cbd3 100644 --- a/KeepersCompound.Lightmapper/Program.cs +++ b/KeepersCompound.Lightmapper/Program.cs @@ -85,30 +85,6 @@ class Program Console.WriteLine($"Lit {lights.Count} light"); } - // Expects Hue and Saturation are 0-1, Brightness 0-255 - // https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB - private static Vector3 HsbToRgb(float hue, float saturation, float brightness) - { - hue *= 360; - var hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6; - var f = hue / 60 - Math.Floor(hue / 60); - - var v = Convert.ToInt32(brightness); - var p = Convert.ToInt32(brightness * (1 - saturation)); - var q = Convert.ToInt32(brightness * (1 - f * saturation)); - var t = Convert.ToInt32(brightness * (1 - (1 - f) * saturation)); - - return hi switch - { - 0 => new Vector3(v, t, p), - 1 => new Vector3(q, v, p), - 2 => new Vector3(p, v, t), - 3 => new Vector3(p, q, v), - 4 => new Vector3(t, p, v), - _ => new Vector3(v, p, q), - }; - } - private static void SetAnimLightCellMaps( DbFile mis, WorldRep worldRep, @@ -193,7 +169,7 @@ class Program var light = new Light { position = brush.position, - color = HsbToRgb(sz.Y, sz.Z, Math.Min(sz.X, 255.0f)), + color = Utils.HsbToRgb(sz.Y, sz.Z, Math.Min(sz.X, 255.0f)), radius = float.MaxValue, r2 = float.MaxValue, lightTableIndex = worldRep.LightingTable.LightCount, @@ -266,7 +242,7 @@ class Program var light = new Light { position = baseLight.position + propLight.Offset, - color = HsbToRgb(propLightColor.Hue, propLightColor.Saturation, propLight.Brightness), + color = Utils.HsbToRgb(propLightColor.Hue, propLightColor.Saturation, propLight.Brightness), innerRadius = propLight.InnerRadius, radius = propLight.Radius, r2 = propLight.Radius * propLight.Radius, @@ -303,7 +279,7 @@ class Program var light = new Light { position = baseLight.position + propAnimLight.Offset, - color = HsbToRgb(propLightColor.Hue, propLightColor.Saturation, propAnimLight.MaxBrightness), + color = Utils.HsbToRgb(propLightColor.Hue, propLightColor.Saturation, propAnimLight.MaxBrightness), innerRadius = propAnimLight.InnerRadius, radius = propAnimLight.Radius, r2 = propAnimLight.Radius * propAnimLight.Radius, diff --git a/KeepersCompound.Lightmapper/MathUtils.cs b/KeepersCompound.Lightmapper/Utils.cs similarity index 81% rename from KeepersCompound.Lightmapper/MathUtils.cs rename to KeepersCompound.Lightmapper/Utils.cs index 4a82249..0b4de94 100644 --- a/KeepersCompound.Lightmapper/MathUtils.cs +++ b/KeepersCompound.Lightmapper/Utils.cs @@ -2,6 +2,33 @@ using System.Numerics; namespace KeepersCompound.Lightmapper; +public static class Utils +{ + // Expects Hue and Saturation are 0-1, Brightness 0-255 + // https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB + public static Vector3 HsbToRgb(float hue, float saturation, float brightness) + { + hue *= 360; + var hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6; + var f = hue / 60 - Math.Floor(hue / 60); + + var v = Convert.ToInt32(brightness); + var p = Convert.ToInt32(brightness * (1 - saturation)); + var q = Convert.ToInt32(brightness * (1 - f * saturation)); + var t = Convert.ToInt32(brightness * (1 - (1 - f) * saturation)); + + return hi switch + { + 0 => new Vector3(v, t, p), + 1 => new Vector3(q, v, p), + 2 => new Vector3(p, v, t), + 3 => new Vector3(p, q, v), + 4 => new Vector3(t, p, v), + _ => new Vector3(v, p, q), + }; + } +} + public static class MathUtils { public const float Epsilon = 0.001f;