Color module

This module provides utility functions for working with colors, including color conversion, color manipulation, and random color generation. It supports both float4 (linear, sRGB, and HSVA color spaces) and uint (0xAARRGGBB) color formats.

To add this module to your project, add the following line to your project file:

require engine.math.color_utils // or require engine.core

Constants

BLACK_COLOR = float4(0f, 0f, 0f, 1f)

Black color

WHITE_COLOR = float4(1f, 1f, 1f, 1f)

White color

CLEAR_COLOR = float4(0f, 0f, 0f, 0f)

Clear color, alpha = 0

RED_COLOR = float4(1f, 0f, 0f, 1f)

Red color

GREEN_COLOR = float4(0f, 1f, 0f, 1f)

Green color

BLUE_COLOR = float4(0f, 0f, 1f, 1f)

Blue color

YELLOW_COLOR = float4(1f, 0.92f, 0.016f, 1f)

Yellow color

CYAN_COLOR = float4(0f, 1f, 1f, 1f)

Cyan color

MAGENTA_COLOR = float4(1f, 0f, 1f, 1f)

Magenta color

GRAY_COLOR = float4(0.5f, 0.5f, 0.5f, 1f)

Gray color

LIGHT_GRAY_COLOR = float4(0.75f, 0.75f, 0.75f, 1f)

Light gray color

DARK_GRAY_COLOR = float4(0.25f, 0.25f, 0.25f, 1f)

Dark gray color

Functions

float4_to_color32(color: float4): uint

Converts a float4 ‘color’ to a uint color32.

Arguments:
  • color : float4

float4_to_color32(r: float; g: float; b: float; a: float = 1f): uint

Converts a float4 ‘color’ to a uint color32.

Arguments:
  • r : float

  • g : float

  • b : float

  • a : float

make_color32(r: int; g: int; b: int; a: int): uint

Makes a color32 from the given arguments.

Arguments:
  • r : int

  • g : int

  • b : int

  • a : int

color32_to_float4(color: uint): float4

Converts a uint color32 to a float4.

Arguments:
  • color : uint

lerp_color32(a: uint; b: uint; t: float): uint

Linearly interpolates between two color32 values.

Arguments:
  • a : uint

  • b : uint

  • t : float

add_color32(a: uint; b: uint): uint

Adds two color32 values together.

Arguments:
  • a : uint

  • b : uint

sub_color32(a: uint; b: uint): uint

Subtracts one color32 value from another.

Arguments:
  • a : uint

  • b : uint

mul_color32(a: uint; b: uint): uint

Multiplies two color32 values together.

Arguments:
  • a : uint

  • b : uint

mul_color32(a: uint; b: float): uint

Multiplies a color32 value by a scalar.

Arguments:
  • a : uint

  • b : float

desaturate(color: float4; amount: float): float4

Desaturates a color by a given amount.

Arguments:
  • color : float4

  • amount : float

desaturate(color: float4): float4

Desaturates a color.

Arguments:
  • color : float4

desaturate(color: uint): uint

Desaturates a color.

Arguments:
  • color : uint

desaturate(color: uint; amount: float): uint

Desaturates a color by a given amount.

Arguments:
  • color : uint

  • amount : float

srgb_to_linear(color: float4): float4

Converts an sRGB color to linear color space.

Arguments:
  • color : float4

linear_to_srgb(color: float4): float4

Converts a linear color to sRGB color space.

Arguments:
  • color : float4

srgb_to_linear(color: uint): uint

Converts an sRGB color to linear color space.

Arguments:
  • color : uint

linear_to_srgb(color: uint): uint

Converts a linear color to sRGB color space.

Arguments:
  • color : uint

premultiply_alpha(color: float4): float4

Premultiplies the alpha channel of a color.

Arguments:
  • color : float4

premultiply_alpha(color: uint): uint

Premultiplies the alpha channel of a color.

Arguments:
  • color : uint

rgb_to_hsv(rgb: float4): float3

Converts an RGB color to HSV color space. HSV stands for Hue, Saturation, Value. In the returned value, field x represents hue, or the position in the spectrum; y - saturation, the color saturation, z - value, the color brightness.

Arguments:
  • rgb : float4

rgb_to_hsva(rgba: float4): float4

Converts an RGBA color to HSVA color space. Note that in the returned value field w represents the alpha channel.

Arguments:
  • rgba : float4

rgb_to_hsva(rgba: uint): float4

Converts an RGBA color to HSVA color space. Note that in the returned value field w represents the alpha channel.

Arguments:
  • rgba : uint

hsv_to_rgb(hsv: float3; alpha: float = 1f): float4

Converts an HSV color to RGB color space.

Arguments:
  • hsv : float3

  • alpha : float

hsv_to_rgb(h: float; s: float; v: float; alpha: float = 1f): float4

Converts an HSV color to RGB color space.

Arguments:
  • h : float

  • s : float

  • v : float

  • alpha : float

hsv_to_rgb(hsva: float4): float4

Converts an HSVA color to RGBA color space.

Arguments:
  • hsva : float4

get_random_color(hsv_min: float3 = float3(0f, 0.2f, 0.35f); hsv_max: float3 = float3(1f, 1f, 1f)): float4

Returns a random color in the given HSV range.

Arguments:
  • hsv_min : float3

  • hsv_max : float3

get_random_color32(hsv_min: float3 = float3(0f, 0.2f, 0.35f); hsv_max: float3 = float3(1f, 1f, 1f)): uint

Returns a random color.

Arguments:
  • hsv_min : float3

  • hsv_max : float3