# 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="/files/pf3JaTtKvyOhOjEkx8gG" 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.

***


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.parsue.io/unity-develop-tips/shader-graph/color.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
