Automation 4 utils.lua
From Aegisub Manual
The Automation 4 Lua include file utils.lua contains various support functions to aid writing Lua scripts. There is no general theme for the file.
Contents |
[edit] Table functions
Duplicating tables in various ways is a common task. utils.lua provides some functions to handle the most common cases.
[edit] table.copy
Synopsis: newtable = table.copy(oldtable)
Makes a shallow copy of the table passed as parameter. Shallow here means that it does not dive into contained tables and copy those as well, so if oldtable.st
refers to a table, newtable.st
will refer to the same table, such that modifying newtable.st
will also be reflected in oldtable.st
and vice versa.
[edit] copy_line
Alias for table.copy
. Included to help transitioning from Automation 3.
[edit] table.copy_deep
Synopsis: newtable = table.copy_deep(oldtable)
Makes a deep copy of the table passed as parameter. While this function attempts resolving circular references and not do infinite recursion on them, it might not work in all cases. You will rarely need to use this function; if you think you need to do a deep copy, consider your task an extra time.
Warning: This function has not been heavily tested.
[edit] Colour functions
It is often needed to do various transformations on colour data. Several functions for this is included.
[edit] ass_color
Synopsis: colorstring = ass_color(r, g, b)
Makes an ASS colour string in the form &HBBGGRR from the given r
, g
and b
arguments.
Warning: The arguments are not checked for range, values outside the 0..255 range will produce garbage output.
[edit] ass_alpha
Synopsis: alphastring = ass_alpha(a)
Makes an ASS alpha string in the form &HAA& from the given a
argument.
Does not check input range.
[edit] ass_style_color
Synopsis: colorstring = ass_style_color(r, g, b, a)
Makes an ASS colour string suitable for use in Style definitions, ie. in format &HAABBGGRR.
Does not check input range.
[edit] extract_color
Synopsis: r, g, b, a = extract_color(colorstring)
Extracts colour components from a colour string. Several formats of colour strings are recognised:
- Style definition: &HAABBGGRR
- Inline override: &HBBGGRR&
- Alpha override: &HAA&
- HTML with alpha: #RRGGBBAA
Note that this function always returns four numbers when passed a valid colour string. Unused values (depends on the format of the colour string) are assigned 0 (zero.) If an unrecognised colour string is passed, nil
is returned.
Example: After executing r, g, b, a = extract_color("&H7F&")
, the r g b
variables will be 0 and the a
variable will be 127.
[edit] alpha_from_style
Synopsis: alphastring = alpha_from_style(coloralphastring)
Returns the alpha part of a colour string, as a an alpha override string, ie. &HAA& format. This function is a composition of extract_color
and ass_alpha
.
[edit] color_from_style
Synopsis: colorstring = color_from_style(coloralphastring)
Returns the colour part of a colour string, as a colour override string, ie. &HBBGGRR& format. This function is a composition of extract_color
and ass_color
.
[edit] HSV_to_RGB
Synopsis: r, g, b = HSV_to_RGB(h, s, v)
Transforms a colour given in Hue, Saturation, Value space into Red, Green, Blue space.
h
is given in degrees, the nominal range is 0..359; values outside this range will be translated into it. Input range of s
and v
are 0..1, these are not range checked. Output range of r
, g
and b
are 0..255.
[edit] String functions
The Lua standard string
library is sometimes not powerful enough. A few additional helper functions are provided. See also Automation 4 unicode.lua.
[edit] string.trim
Synopsis: outstring = string.trim(instring)
Removes all space characters at the start and end of the input string, and returns the transformed string.
Warning: This function is not UTF-8 safe. It uses the Lua regex %s
class to match spaces, which in some legacy encodings will result in it also matching some prefix bytes in UTF-8 encoded text.
[edit] string.headtail
Synopsis: head, tail = string.headtail(instring)
Splits a string by first space-sequence into a "head" and a "tail", similar to the handling of linked lists in several functional languages.
If instring
does not contain any space characters it returns instring, ""
.
[edit] string.words
Synopsis: for word in string.words(instring) do ... end
Returns an iterator function for use in a for
loop, to loop over all the words in the string using string.headtail
semantics.
[edit] Numeric functions
Functions to handle various operations on numbers.
[edit] clamp
Synopsis: outval = clamp(inval, min, max)
Clamps inval
to be in range min
..max
.
[edit] interpolate
Synopsis: outval = interpolate(t, a, b)
Interpolates between a
and b
. t
is the time variable in range 0..1, values outside this range are clamped.
[edit] interpolate_color
Synopsis: outcolor = interpolate_color(t, color1, color2)
Interpolate between color1
and color2
with t
as time variable in range 0..1. color1
, color2
and outcolor
are colour strings, outcolour
will be in colour override format.
[edit] interpolate_alpha
Synopsis: outalpha = interpolate_alpha(t, alpha1, alpha2)
Similar to interpolate_color
, but interpolates alpha values instead. Also works on colour strings, and will return an alpha override string.
Overview: |
Automation Manager • Running macros • Using export filters • Standard macros • Changes from Automation 3 • Moving from Automation 3 |
---|---|
Karaoke Templater reference: |
Declaring templates • Execution order • Modifiers • Inline-variables ($-variables) • Code lines and blocks • Execution envirionment |
Lua reference: |
Registration • Subtitles object • Progress reporting • Config dialogues • Misc. APIs • karaskel.lua • utils.lua • unicode.lua • cleantags.lua |
Karaskel concepts: |
Style tables • Dialogue line tables • Syllable tables • Inline effects • Furigana |
Category: Lua Reference