Luminance

The Color Pro package includes a few functions for calculating and testing color luminance and perceived brightness. These are defined as extension methods in the static class Luminance.


☀️ Relative Luminance

Relative luminance is calculated according to the HSL color model and is most often used when calculating the contrast between colors. See the contrast manual for more information.

float luminance = color.RelativeLuminance();

As an example, here's how contrast is calculated between colors using relative luminance:

public static float ContrastRatio(this Color color, Color other)
{
    float luminanceA = color.RelativeLuminance();
    float luminanceB = other.RelativeLuminance();

    float darker = Mathf.Min(luminanceA, luminanceB);
    float lighter = Mathf.Max(luminanceA, luminanceB);

    return (lighter + 0.05f) / (darker + 0.05f);
}

🔆 Perceived Brightness

Perceived brightness is measured according to the HSP color model and can be used to get a more accurate perception of when a color is considered "light" or "dark".

float brightness = color.PerceivedBrightness();