Skip to content

Instantly share code, notes, and snippets.

@behreajj
Created November 10, 2021 03:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save behreajj/8f54231401ca0628279c6b19b369b534 to your computer and use it in GitHub Desktop.
Save behreajj/8f54231401ca0628279c6b19b369b534 to your computer and use it in GitHub Desktop.
Perceptual Luminance With Conversion
static float StandardToLinearChannel (in float x)
{
return x > 0.04045f ?
Mathf.Pow ((x + 0.055f) / 1.055f, 2.4f) :
x / 12.92f;
}
static float LinearToStandardChannel (in float x)
{
return x > 0.0031308f ?
Mathf.Pow (x, 1.0f / 2.4f) * 1.055f - 0.055f :
x * 12.92f;
}
static Color StandardToLinear (in Color c)
{
return new Color (
StandardToLinearChannel (c.r),
StandardToLinearChannel (c.g),
StandardToLinearChannel (c.b),
c.a);
}
static float GrayLumCorrect (in Color srgb)
{
Color lrgb = StandardToLinear (srgb);
float lum = 0.21264935f * lrgb.r + 0.71516913f * lrgb.g + 0.07218152f * lrgb.b;
return LinearToStandardChannel (lum);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment