From cb599f24dfe0731ca5981db970317a0545e4d56e Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Mon, 9 Dec 2024 18:25:33 +0000 Subject: [PATCH] Close #1: Finally cracked the colored lighting problem --- KeepersCompound.Lightmapper/Utils.cs | 29 +++++++++++++--------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/KeepersCompound.Lightmapper/Utils.cs b/KeepersCompound.Lightmapper/Utils.cs index 20c7ea2..cf648fd 100644 --- a/KeepersCompound.Lightmapper/Utils.cs +++ b/KeepersCompound.Lightmapper/Utils.cs @@ -6,26 +6,23 @@ 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 + // This is actually not accurate to *real* hsb to rgb. But I'm replicating the inaccuracies of Dark :( + // See issue#1 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 + hue *= 3; + var color = hue 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), + < 1 => new Vector3(1f - hue, hue, 0), + < 2 => new Vector3(2f - hue, hue - 1f, 0), + _ => new Vector3(3f - hue, hue - 2, 0), }; + + color *= saturation; + color += Vector3.One * (1.0f - saturation); + color *= brightness; + + return color; } }