Color
Math of Color
There are a lots of calculation related on color in Shader, here is some tips.
Calculation
If the color is in grey tone, value of RGB is same, splite it and use r channel instead of color.
Color is a float3, 3 floats. Multipling a color will produce 4 calculations. If the color is in grey tone, use single channel will produce only 1 calculation.
Color to Brightness (luminance)
Brightness of color is NOT average of RGB value!
Formula:
brightness = r * 0.2126 + g * 0.7152 + b * 0.0722
This is based on the luminance coefficients defined by the ITU-R BT.709 standard, which reflects how the human eye perceives brightness for red, green, and blue components. The weights (0.2126, 0.7152, 0.0722) correspond to the human eye's sensitivity to these color channels:
Red (r): 0.2126
Green (g): 0.7152
Blue (b): 0.0722
These weights emphasize green (the human eye is most sensitive to green light) while de-emphasizing blue and red.
Color color = new Color(0.8f, 0.26f, 0.78f);
var r = color.r * 0.2126f;
var g = color.g * 0.7152f;
var b = color.b * 0.0722f;
var brightness = r + g + b;// extension method
public float Brightness(this Color color, bool gammaCorrection = false)
{
// Apply gamma correction if necessary
var r = gammaCorrection ? Linearize(color.r) : color.r;
var g = gammaCorrection ? Linearize(color.g) : color.g;
var b = gammaCorrection ? Linearize(color.b) : color.b;
// Calculate relative luminance using ITU-R BT.709 weights
var brightness = r * 0.2126f + g * 0.7152f + b * 0.0722f;
return brightness;
// Helper function to linearize sRGB values
float Linearize(float value)
{
return value <= 0.04045f
? value / 12.92f
: Mathf.Pow((value + 0.055f) / 1.055f, 2.4f);
}
}
Making as a Sub Graph for advantages on Shader Graph.
If a grey tone color is needed, apply the brightness value to all channel of color.
Last updated