Vixen
02b45cc4
csharp
public static class ColorSpace

Conversion between sRGB-encoded and linear values — the one piece of colour handling that is wrong in most renderers and invisible until it is pointed out.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

The engine works in linear space. Lighting, blending and filtering are only correct on values proportional to radiance; sRGB encoding is a storage format that devotes more of its 8 bits to the dark end, where the eye is more sensitive. Adding two sRGB values is not adding two lights.

These functions are for the cases the hardware does not cover. A texture sampled through an sRGB view format is decoded by the sampler, for free and with correct filtering; doing it in a shader instead is both slower and wrong, because the filtering then happens before the decode. What is left for these: colours typed by a human into an inspector, colours parsed from hex, and anything crossing the boundary on the CPU.

The exact piecewise transfer function, not the pow(x, 2.2) approximation. The two differ by up to about 1% in the darks, which is precisely where banding is visible, and the exact form costs one comparison.

Methods (3)

  • public static float SrgbToLinear(float value)

    Decodes one sRGB-encoded channel to linear.

  • public static float LinearToSrgb(float value)

    Encodes one linear channel as sRGB.

  • public static float Luminance(Vector3 linear)

    The relative luminance of a linear colour, using the Rec. 709 primaries that sRGB shares.

Used by (7)

  • Color3Vixen.Core.Mathematics
  • Color4Vixen.Core.Mathematics
  • ColorTestsVixen.Core.Mathematics.Tests
  • GradientVixen.Ui.Controls.Advanced
  • OkLchVixen.Ui.Controls.Advanced
  • StyleValueParserVixen.Ui.Styling
  • StyleValueTestsVixen.Ui.Styling.Tests