Color
The Color datatype represents an RGBA (Red Green Blue Alpha) color. Colors are represented with numbers between 0 and 1, where 1 signifies the maximum intensity of a particular color component. RGB components define the color while the Alpha component defines the transparency.
Provides various functions and predefined colors which ranges from common colors like red, green, blue, white, black, to more specific colors such as yellow, cyan, magenta, gray, etc. Further it also provides functionalities such as linear interpolation between two colors, conversion from/to HSV color model, and generating a grayscale color.
Properties
The grayscale value of the color based on the intensities of the red, green, and blue components.
local color = Color.new(1, 0, 0) -- Red color
print(color.grayscale) -- 0.299
The linear (gamma space) equivalent of this color object. Game graphics are usually rendered in a color space known as gamma space, but many computations are done in linear space, thus this property can be useful when such conversions are necessary.
local color = Color.new(1, 0, 0) -- Red color
print(color.linear) -- 0.2126
The gamma space equivalent of this color object. When the computations are done in linear space, this conversion can be useful to switch back to Gamma space for rendering.
local color = Color.new(1, 0, 0) -- Red color
print(color.gamma) -- 0.2126
The maximum RGB component in the color. It can be useful when you want to identify the dominant color component.
local color = Color.new(1, 0, 0) -- Red color
print(color.maxColorComponent) -- 1
Access or change the red component value of the color.
local color = Color.new(1, 0, 0) -- Red color
print(color.r) -- 1
Access or change the green component value of the color.
local color = Color.new(1, 0, 0) -- Red color
print(color.g) -- 0
Access or change the blue component value of the color.
local color = Color.new(1, 0, 0) -- Red color
print(color.b) -- 0
Access or change the alpha (transparency) component of the color where '1' is fully opaque and '0' is fully transparent.
local color = Color.new(1, 0, 0) -- Red color
color.a = 0.5
print(color.a) -- 0.5
Predefined Color object with red component set to maximum and all other components set to zero. It's a static property, accessible directly from the class.
print(Color.red) -- Color object with red component set to maximum
Predefined color object with green component at maximum while red, blue, and alpha components at zero. As a static property, it can be accessed directly from the class.
print(Color.green) -- Color object with green component set to maximum
Predefined color object with blue component set to maximum and all other components set to zero. One major use of this property can be for quick access to the pure blue color.
print(Color.blue) -- Color object with blue component set to maximum
Predefined color object with all components set to maximum, producing white color. It is a static property accessible directly from the class.
print(Color.white) -- Color object with all components set to maximum
Predefined black color object with all color components set to zero. As a static property, it can be accessed directly from the class.
print(Color.black) -- Color object with all components set to zero
Predefined yellow color object, offering a quick way to use yellow in your game. It is a static property, accessible directly from the class.
print(Color.yellow) -- Color object with yellow component set to maximum
Predefined cyan color object. It provides a quick and convenient way to use cyan in your game.
print(Color.cyan) -- Color object with cyan component set to maximum
Predefined magenta color object. It allows quick access to the magenta color.
print(Color.magenta) -- Color object with magenta component set to maximum
Predefined gray color object, allowing for quick utilization of gray in a game scene or interface. As a static property, it's accessible directly from the class.
print(Color.gray) -- Color object with gray component set to maximum
Predefined grey color object. As a static property, it can be accessed directly from the class. This makes it convenient to use grey color quickly and easily.
print(Color.grey) -- Color object with grey component set to maximum
Predefined color object with all components set to zero, resulting in a fully transparent color. Quick access to a transparent color can provide utility in many situations.
print(Color.clear) -- Color object with all components set to zero
Methods
Calculates and returns a color that is a certain percentage (defined by the third parameter) between two other colors. This can be used to smoothly transition between two colors.
local colorA = Color.new(1, 0, 0) -- Red color
local colorB = Color.new(0, 1, 0) -- Green color
local transitionColor = Color.Lerp(colorA, colorB, 0.5) -- Transition color between red and green
Works similarly to 'Lerp', but without clamping the percentage (third parameter) between 0 and 1. This allows for extrapolation beyond the two original colors.
local colorA = Color.new(1, 0, 0) -- Red color
local colorB = Color.new(0, 1, 0) -- Green color
local transitionColor = Color.LerpUnclamped(colorA, colorB, 1.5) -- Extrapolated color beyond red and green
Convert a color from HSV (Hue, Saturation, Value) color model to RGB (Red, Green, Blue) model. This function can be quite useful whenever you're dealing with HSV colors and need to convert them for use in RGB format.
local color = Color.HSVToRGB(0.5, 1, 1) -- RGB color from HSV color
Parameters
H
number
The Hue component of the color, ranges between 0 to 1.
S
number
The Saturation component of the color, ranges between 0 to 1.
V
number
The Value component of the color, ranges between 0 to 1.
Returns
Returns a Color object in RGB color space.
Updated 4 months ago