Color

The Color class provides methods for working with RGBA color values. The colors can easily be converted between integer (0-255), floating point (0-1), and hex representations. The Color class also makes it easy to change the tint of the color by first converting it to HSLuv and then adjusting the lightness value.

Example

The following demonstrates a simple usage of the Color class.

local MyModule = select(2, ...).MyModule
local Color = MyModule:From("LibTSMUtil"):IncludeClassType("Color")

local color = Color.NewFromHex("#a57f00")
print(color:GetRGBA()) -- 165   127   0   255

local darker = color:GetTint(-20)
print(darker:GetHexNoAlpha()) -- #695000

Memory Management

The Color objects are intended to never be GC’d and have a static lifecycle (i.e. one that’s equal to the lifecycle of the application), but there is nothing preventing them from being GC’d.

API

class Color: Class
staticmethod NewFromHex(hex: string): Color

Create a new color value object from a hex string.

Parameters:

hex (string) – The hex string which represents the color (in either “#AARRGGBB” or “#RRGGBB” format)

staticmethod IsColorValue(arg: any): boolean

Returns whether or not the argument is a color value object.

Parameters:

arg (any) – The argument to check

staticmethod IsValidHexRGB(str: string): boolean

Returns whether or not the passed string is a valid hex RGB string.

Parameters:

str (string) – The value to check

staticmethod HexToRGBA(hex: string): (r: number, g: number, b: number, a: number)

Converts a hex string into an RGB value.

Parameters:

hex (string) – The hex string

SetRGBA(self: Color, r: number, g: number, b: number, a: number)

Sets the RGBA values of the color as values from 0 to 255.

GetTint(self: Color, tintPct: number): Color

Gets a tinted version of the color.

Parameters:

tintPct (number) – The tint percent

GetOpacity(self: Color, opacityPct: number): Color

Gets a version of the color with a different opacity.

Parameters:

opacityPct (number) – The opacity percentage

GetRGBA(self: Color): (r: number, g: number, b: number, a: number)

Gets the RGBA values of the color as values from 0 to 255.

GetFractionalRGBA(self: Color): (r: number, g: number, b: number, a: number)

Gets the RGBA values of the color as values from 0 to 1.

IsLight(self: Color): boolean

Returns whether or not the color is considered light.

GetHex(self: Color): string

Gets the hex string representation of the color.

GetHexNoAlpha(self: Color): string

Gets the hex string representation of the color without the alpha channel.

ColorText(self: Color, text: string | number): string

Colors a text string with the color.

Parameters:

text (string | number) – The text string to color

GetTextColorPrefix(self: Color): string

Gets the prefix used to color a text string.