Address
304 North Cardinal St.
Dorchester Center, MA 02124

Work Hours
Monday to Friday: 7AM - 7PM
Weekend: 10AM - 5PM

Unity Color Inspector

Color Management in Unity

In the world of Unity, one of the most critical data structures for color management is “Color”, along with its 32-bit counterpart, “Color32”.
These structures empower you to define a wide range of colors and associate them with various components in your Unity project, see my article on Unity how to change color of object.

In this article, we’ll explore how these tools enable you to master color within the Unity universe.

Table of Contents

Difference Between Color and Color32

The key distinction between them lies in the range of values they use. “Color” employs normalized values between 0 and 1, whereas “Color32” operates within the range of 0 to 255.

Defining Colors via the Inspector

Whether you opt for either, both of these structures are easily usable through the Unity inspector interface. Despite their internal differences, their usage within the inspector is nearly identical.

Unity Color Inspector

Defining Colors via Code

To create a color via code, we instantiate a “Color” or “Color32” structure.
Alternatively, you can use the static properties of the “Color” class:

PropertyColor RGBAColor
Color.blackRGBA (0, 0, 0, 1)
 
Color.blueRGBA (0, 0, 1, 1)
 
Color.clearRGBA (0, 0, 0, 0)
 
Color.cyanRGBA (0, 1, 1, 1)
 
Color.grayRGBA (0.5, 0.5, 0.5, 1)
 
Color.greenRGBA (0, 1, 0, 1)
 
Color.greyRGBA (0.5, 0.5, 0.5, 1)
 
Color.magentaRGBA (1, 0, 1, 1)
 
Color.redRGBA (1, 0, 0, 1)
 
Color.whiteRGBA (1, 1, 1, 1)
 
Color.yellowRGBA (1, 0.92, 0.016, 1)
 

 

public void CreateColor()
{
    // The same color as #ffffff, white
    var color = new Color(1f, 1f, 1f, 1f);
    var color32 = new Color32(255, 255, 255, 255);
}

Converting Color to Color32

To convert a Color into a Color32, use a simple cast.

public void ConvertToColor32()
{
    var color = new Color(1f, 1f, 1f, 1f);
    var color32 = new Color32(255, 255, 255, 255);

    Color castColor = color32;
    Color32 castColor32 = color;
}

Hexadecimal to Color Conversion

To convert a hexadecimal color to “Color”, use the static “TryParseHtmlString” method of the “ColorUtility” class.

private void HexToColor()
{
    string hex = "#ffffff";

    if (ColorUtility.TryParseHtmlString(hex, out Color color))
    {
        Debug.Log(color); // Color(1f, 1f, 1f, 1f);
    }
}

Color to Hexadecimal Conversion

To convert a “Color” to hexadecimal color, use the static “ToHtmlStringRGBA” method of the “ColorUtility” class.

private void ColorToHex()
{
    // With alpha
    string hex = ColorUtility.ToHtmlStringRGBA(Color.red);
    Debug.Log(hex); // FF0000FF

    // Without alpha
    string hexNoAlpha = ColorUtility.ToHtmlStringRGB(Color.red);
    Debug.Log(hexNoAlpha); // FF0000
}

Color Interpolation with Lerp

Unity provides you with linear interpolation (Lerp) functions for both “Color” and “Color32.” These functions allow you to create smooth color transitions, making your Unity project visually compelling and dynamic.

private void ColorLerp()
{
    // Interpolate between white and black
    Color interpolatedColor = Color.Lerp(Color.white, Color.black, 0.5f); // Result: RGBA(0.500, 0.500, 0.500, 1.000)

    // Interpolate between two Color32 values
    Color32 interpolatedColor32 = Color32.Lerp(new Color32(255, 255, 255, 255), new Color32(0, 0, 0, 0), 0.5f); // Result: RGBA(127, 127, 127, 127)
}

Unity Color Lerp

“Color.Lerp(a, b, t);”, smoothly transitions between two points, “a” and “b,” based on an interpolant value “t.” This technique is often used to achieve dynamic color changes in Unity, enhancing the overall visual experience of your projects.

More on Lerp.

The “Color” and “Color32” structures, along with conversion methods and interpolation functions, provide you with the tools to work with colors effectively.

Have you just started your Unity adventure?
Check this article to start in the best possible way: The Best Advice I Wish I Had When I Started Game Development

Check our other articles about Unity:

Leave a Reply

Your email address will not be published. Required fields are marked *