ColorMath.RgbToHcv: read green-dominant colours with the right channel order

The else branch swapped y and z, so the hue read back from many colours was
wrong (values above 1). Nothing else consumed it; the sun-hue test reads hues
back from RGB. Builds; first run of SunsNeverGreenTests failed on this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-16 17:38:37 +02:00
commit 5b1bfe6cec
2 changed files with 13 additions and 1 deletions

View file

@ -41,7 +41,7 @@ public static class ColorMath
public static float3 RgbToHcv(in float3 rgb)
{
// Based on work by Sam Hocevar and Emil Persson
float4 p = (rgb.y < rgb.z) ? float4(rgb.zy, -1.0f, 2.0f/3.0f) : float4(rgb.zy, 0.0f, -1.0f/3.0f);
float4 p = (rgb.y < rgb.z) ? float4(rgb.zy, -1.0f, 2.0f/3.0f) : float4(rgb.yz, 0.0f, -1.0f/3.0f);
float4 q = (rgb.x < p.x) ? float4(p.xyw, rgb.x) : float4(rgb.x, p.yzx);
float c = q.x - min(q.w, q.y);
float h = abs((q.w - q.y) / (6 * c + Epsilon) + q.z);

View file

@ -53,6 +53,18 @@ public sealed class SunsNeverGreenTests : IDisposable
Assert.False(ColorMath.HueInBand(degrees * Degree, settings.SunHueExcludedStart, settings.SunHueExcludedEnd));
}
// The hues below are read back from RGB the generator wrote, so the reader must invert the writer.
[Fact]
public void ReadingHueBackFromRgbInvertsWritingIt()
{
for (var degrees = 5; degrees < 360; degrees += 5)
foreach (var value in new[] { .5f, 1f })
{
var rgb = ColorMath.HsvToRgb(new float3(degrees * Degree, .75f, value));
Assert.Equal(degrees * Degree, ColorMath.RgbToHsv(rgb).x, 4);
}
}
[Fact]
public void TheDrawMapsOntoTheWholeAllowedArcAndNothingElse()
{