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.
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:
Property | Color RGBA | Color |
---|
Color.black | RGBA (0, 0, 0, 1) | |
Color.blue | RGBA (0, 0, 1, 1) | |
Color.clear | RGBA (0, 0, 0, 0) | |
Color.cyan | RGBA (0, 1, 1, 1) | |
Color.gray | RGBA (0.5, 0.5, 0.5, 1) | |
Color.green | RGBA (0, 1, 0, 1) | |
Color.grey | RGBA (0.5, 0.5, 0.5, 1) | |
Color.magenta | RGBA (1, 0, 1, 1) | |
Color.red | RGBA (1, 0, 0, 1) | |
Color.white | RGBA (1, 1, 1, 1) | |
Color.yellow | RGBA (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)
}
“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.