> For the complete documentation index, see [llms.txt](https://docs.parsue.io/unity-develop-tips/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.parsue.io/unity-develop-tips/shader-graph/color.md).

# 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!

{% tabs %}
{% tab title="Explainaion" %}
Formula:&#x20;

`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.
{% endtab %}

{% tab title="Calculation" %}

```csharp
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;
```

{% endtab %}

{% tab title="c#" %}

```csharp
// 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);
    }
}
```

{% endtab %}

{% tab title="Shader Graph" %}

<figure><img src="https://450923066-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F0NZ9dzs7UjJOfYPcGo7G%2Fuploads%2FpMDoIfJRy4mGVmofOx2w%2Fimage.png?alt=media&amp;token=d74c9b91-15c4-4c8e-bc77-cf5bfe809f16" alt=""><figcaption></figcaption></figure>

Making as a [Sub Graph](/unity-develop-tips/shader-graph/sub-graph.md) for advantages on Shader Graph.
{% endtab %}
{% endtabs %}

If a grey tone color is needed, apply the brightness value to all channel of color.

***
