githubEdit

Color Types

How to define color values

Color values can passed as properties of different components, like Materialsarrow-up-right, Textarrow-up-right or different properties of UI entitiesarrow-up-right. Color values can either be of type Color3 or Color4.

Color3 contains three properties, red, green, and blue. Color4 has those same three plus alpha, for transparency values.

Set a color

You can make a color by using the Color3.create() or the Color4.create() functions.

// Red color
const red3 = Color3.create(1, 0, 0)
const red4 = Color4.create(1, 0, 0, 1)

You can also create certain predetermined colors that are part of the Color3 and Color4 namespaces.

const red = Color3.Red()
const blue = Color3.Blue()
const black = Color3.Black()
circle-info

💡 Tip: Write Color3. or Color4. and Visual Studio should suggest all the possible values in an intelligent dropdown.

You can otherwise pick a random color using the following function:

const randomColor = Color3.Random()

If you prefer to describe a color in hexadecimal, use Color3.fromHexString().

const red = Color3.fromHexString('FD350A')
const blue = Color3.fromHexString('0A0CFD')

Any object value that includes numeric values for r, g, and b can be interpreted as a Color3 value. Likewise, any object that includes those properties plus an a value can be interpreted as a Color4 value. This allows you to also use the following syntax:

The following example uses a color property as part of a TextShape component, to set the text color.

circle-exclamation

Transparency

Use the alpha property of Color4 to make a color translucid.

If alpha is 1, the color will be completely opaque, if it's 0 it will be completely invisible. Anything in between results in a partially transparent tone.

Lerp

Use the Color3.lerp() or the Color4.lerp() function to define a color that's somewhere between two other colors. These functions work similar to the Vector3.lerp() function.

Both Color3.lerp() or the Color4.lerp() take the following arguments:

  • left: The first color to use as reference

  • right: The second color to use as reference

  • amount: A number from 0 to 1 to define how much of the left color to use in the mix. The closer to 0, the closer to the left color.

You can use a system to gradually change the amount parameter, to create a smooth transition.

Last updated