Module Util

Some general utility functions.

Functions

collectFullRectTiles (x, y, w, h) Collects all tile positions in a rectangle.
collectRectTiles (x, y, w, h[, noCorners=false]) Collects all tile positions along a rectangle.
color2hex (red[, green=red[, blue=green]]) Converts three integer color values to a color hex string.
hex2color (hex) Converts a color hex string into r, g, b color values.
hsv2rgb (h, s, v) Converts a hsv color to the same color in rgb space.
optStorage (src, name[, constructor]) Returns the named content of a storage table.
rgb2hsv (r, g, b) Converts a rgb color to the same color in hsv space.
unite (table1[, table2[, ...]]) Unites acces on multiple tables.


Functions

collectFullRectTiles (x, y, w, h)
Collects all tile positions in a rectangle.

New feature:

    This is a new feature that was added in version 1.10.46

Parameters:

  • x int X component of a tile position.
  • y int Y component of a tile position.
  • w int Width of the rect.
  • h int Height of the rect.

Returns:

    array An array of {x=..., y=...} positions.

Usage:

    local xys = Util.collectFullRectTiles(1, 1, 3, 3)
    xys:forEach(function(xy) print(xy.x, xy.y) end)
    
collectRectTiles (x, y, w, h[, noCorners=false])
Collects all tile positions along a rectangle. The listing is in clock-wise order and starts at the top left corner of the rectangle.

Parameters:

  • x int X component of a tile position.
  • y int Y component of a tile position.
  • w int Width of the rect.
  • h int Height of the rect.
  • noCorners bool If true the corners won't be included. (default false)

Returns:

    array An array of {x=..., y=...} positions.

Usage:

    local xys = Util.collectRectTiles(1, 1, 3, 3)
    xys:forEach(function(xy) print(xy.x, xy.y) end)
    
color2hex (red[, green=red[, blue=green]])
Converts three integer color values to a color hex string.

New feature:

    This is a new feature that was added in version 1.12.18

Parameters:

  • red int Red color channel in range 0..255.
  • green int Green color channel in range 0..255. (default red)
  • blue int Blue color channel in range 0..255. (default green)

Returns:

    string The color hex string that represents the provided color.
hex2color (hex)
Converts a color hex string into r, g, b color values. Example: 'ff88aa' will output 255, 136, 170

New feature:

    This is a new feature that was added in version 1.12.18

Parameters:

  • hex string A color hex string to convert.

Returns:

    int,int,int The color components, each in range 0..255.
hsv2rgb (h, s, v)
Converts a hsv color to the same color in rgb space.

New feature:

    This is a new feature that was added in version 1.12.28

Parameters:

  • h number Hue in range 0..360.
  • s number Saturation in range 0..1.
  • v number Value in range 0..1.

Returns:

    int,int,int red in 0..255, green in 0..255, blue in 0..255
optStorage (src, name[, constructor])
Returns the named content of a storage table. If no such entry exists it will be created by a given constructor, saved into the table and returned.

Parameters:

  • src table Source table.
  • name string Name of the entry to load.
  • constructor function or table A function that returns a new object or the object itself. (optional)

Returns:

    The object that's effectively in src[name] after the call.
rgb2hsv (r, g, b)
Converts a rgb color to the same color in hsv space.

New feature:

    This is a new feature that was added in version 1.12.28

Parameters:

  • r int Red color channel in range 0..255.
  • g int Green color channel in range 0..255.
  • b int Blue color channel in range 0..255.

Returns:

    number,number,number hue in 0..360, saturation in 0..1, value in 0..1
unite (table1[, table2[, ...]])
Unites acces on multiple tables. For read access the value of the first table that contains the queried key will be returned. For write access the value is written into all tables using the provided key.

Parameters:

  • table1 table
  • table2 table (optional)
  • ... (optional)

Usage:

    local a = { x = 42 }
    local b = { y = 43 }
    local m = unite(a, b)
    print(m.x)  --Prints 42
    print(m.y)  --Prints 43
    m.z = 7
    print(m.z)  --Prints 7
    print(a.z)  --Prints 7
    print(b.z)  --Prints 7
    
    
generated by LDoc 1.4.3 Last updated 2025-09-23 15:07:35