Move colour conversion to separate class
This commit is contained in:
parent
940c78e1cf
commit
cd13cee48f
|
@ -85,30 +85,6 @@ class Program
|
||||||
Console.WriteLine($"Lit {lights.Count} light");
|
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(
|
private static void SetAnimLightCellMaps(
|
||||||
DbFile mis,
|
DbFile mis,
|
||||||
WorldRep worldRep,
|
WorldRep worldRep,
|
||||||
|
@ -193,7 +169,7 @@ class Program
|
||||||
var light = new Light
|
var light = new Light
|
||||||
{
|
{
|
||||||
position = brush.position,
|
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,
|
radius = float.MaxValue,
|
||||||
r2 = float.MaxValue,
|
r2 = float.MaxValue,
|
||||||
lightTableIndex = worldRep.LightingTable.LightCount,
|
lightTableIndex = worldRep.LightingTable.LightCount,
|
||||||
|
@ -266,7 +242,7 @@ class Program
|
||||||
var light = new Light
|
var light = new Light
|
||||||
{
|
{
|
||||||
position = baseLight.position + propLight.Offset,
|
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,
|
innerRadius = propLight.InnerRadius,
|
||||||
radius = propLight.Radius,
|
radius = propLight.Radius,
|
||||||
r2 = propLight.Radius * propLight.Radius,
|
r2 = propLight.Radius * propLight.Radius,
|
||||||
|
@ -303,7 +279,7 @@ class Program
|
||||||
var light = new Light
|
var light = new Light
|
||||||
{
|
{
|
||||||
position = baseLight.position + propAnimLight.Offset,
|
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,
|
innerRadius = propAnimLight.InnerRadius,
|
||||||
radius = propAnimLight.Radius,
|
radius = propAnimLight.Radius,
|
||||||
r2 = propAnimLight.Radius * propAnimLight.Radius,
|
r2 = propAnimLight.Radius * propAnimLight.Radius,
|
||||||
|
|
|
@ -2,6 +2,33 @@ using System.Numerics;
|
||||||
|
|
||||||
namespace KeepersCompound.Lightmapper;
|
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 static class MathUtils
|
||||||
{
|
{
|
||||||
public const float Epsilon = 0.001f;
|
public const float Epsilon = 0.001f;
|
Loading…
Reference in New Issue