Fix calls to other util functions from within util functions

This commit is contained in:
Thomas Goyne 2013-05-02 07:42:02 -07:00
parent 9bcc6efcc8
commit 1b96bf748f
2 changed files with 406 additions and 369 deletions

View file

@ -1,5 +1,6 @@
local utils = {
copy = function(tbl)
local sformat = string.format
local copy
copy = function(tbl)
return (function()
local _tbl_0 = { }
for k, v in pairs(tbl) do
@ -7,10 +8,10 @@ local utils = {
end
return _tbl_0
end)()
end,
deep_copy = function(tbl)
end
local deep_copy
deep_copy = function(tbl)
local seen = { }
local copy
copy = function(val)
if type(tbl) ~= 'table' then
return val
@ -28,17 +29,21 @@ local utils = {
end)()
end
return copy(tbl)
end,
ass_color = function(r, g, b)
return string.format("&H%02X%02X%02X&", b, g, r)
end,
ass_alpha = function(a)
return string.format("&H%02X&", a)
end,
ass_style_color = function(r, g, b, a)
return string.format("&H%02X%02X%02X%02X", a, b, g, r)
end,
extract_color = function(s)
end
local ass_color
ass_color = function(r, g, b)
return sformat("&H%02X%02X%02X&", b, g, r)
end
local ass_alpha
ass_alpha = function(a)
return sformat("&H%02X&", a)
end
local ass_style_color
ass_style_color = function(r, g, b, a)
return sformat("&H%02X%02X%02X%02X", a, b, g, r)
end
local extract_color
extract_color = function(s)
local a, b, g, r
a, b, g, r = s:match('&H(%x%x)(%x%x)(%x%x)(%x%x)')
if a then
@ -56,15 +61,18 @@ local utils = {
if r then
return tonumber(r, 16), tonumber(g, 16) or 0, tonumber(b, 16) or 0, tonumber(a, 16) or 0
end
end,
alpha_from_style = function(scolor)
end
local alpha_from_style
alpha_from_style = function(scolor)
return ass_alpha(select(4, extract_color(scolor)))
end,
color_from_style = function(scolor)
end
local color_from_style
color_from_style = function(scolor)
local r, g, b = extract_color(scolor)
return ass_color(r or 0, g or 0, b or 0)
end,
HSV_to_RGB = function(H, S, V)
end
local HSV_to_RGB
HSV_to_RGB = function(H, S, V)
local r, g, b = 0, 0, 0
if S == 0 then
r = self:clamp(V * 255, 0, 255)
@ -106,8 +114,9 @@ local utils = {
end
end
return r, g, b
end,
HSL_to_RGB = function(H, S, L)
end
local HSL_to_RGB
HSL_to_RGB = function(H, S, L)
local r, g, b
H = math.abs(H) % 360
S = clamp(S, 0, 1)
@ -156,29 +165,33 @@ local utils = {
b = get_component(Tb)
end
return math.floor(r * 255 + 0.5), math.floor(g * 255 + 0.5), math.floor(b * 255 + 0.5)
end,
trim = function(s)
end
local trim
trim = function(s)
return s:gsub('^%s*(.-)%s*$', '%1')
end,
headtail = function(s)
end
local headtail
headtail = function(s)
local a, b, head, tail = s:find('(.-)%s+(.*)')
if a then
return head, tail
else
return s, ''
end
end,
words = function(s)
end
local words
words = function(s)
return function()
if s == '' then
return
end
local head, tail = string.headtail(s)
local head, tail = headtail(s)
s = tail
return head
end
end,
clamp = function(val, min, max)
end
local clamp
clamp = function(val, min, max)
if val < min then
return min
elseif val > max then
@ -186,8 +199,9 @@ local utils = {
else
return val
end
end,
interpolate = function(pct, min, max)
end
local interpolate
interpolate = function(pct, min, max)
if pct <= 0 then
return min
elseif pct >= 1 then
@ -195,15 +209,34 @@ local utils = {
else
return pct * (max - min) + min
end
end,
interpolate_color = function(pct, first, last)
end
local interpolate_color
interpolate_color = function(pct, first, last)
local r1, g1, b1 = extract_color(first)
local r2, g2, b2 = extract_color(last)
local r, g, b = interpolate(pct, r1, r2), interpolate(pct, g1, g2), interpolate(pct, b1, b2)
return ass_color(r, g, b)
end,
interpolate_alpha = function(pct, first, last)
end
local interpolate_alpha
interpolate_alpha = function(pct, first, last)
return ass_alpha(interpolate(pct, select(4, extract_color(first)), select(4, extract_color(last))))
end
end
return {
copy = copy,
deep_copy = deep_copy,
ass_color = ass_color,
ass_alpha = ass_alpha,
ass_style_color = ass_style_color,
extract_color = extract_color,
alpha_from_style = alpha_from_style,
color_from_style = color_from_style,
HSV_to_RGB = HSV_to_RGB,
HSL_to_RGB = HSL_to_RGB,
trim = trim,
headtail = headtail,
words = words,
clamp = clamp,
interpolate = interpolate,
interpolate_color = interpolate_color,
interpolate_alpha = interpolate_alpha
}
return utils

View file

@ -13,13 +13,14 @@
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
utils =
-- Make a shallow copy of a table
copy: (tbl) -> {k, v for k, v in pairs tbl}
sformat = string.format
-- Make a deep copy of a table
-- Retains equality of table references inside the copy and handles self-referencing structures
deep_copy: (tbl) ->
-- Make a shallow copy of a table
copy = (tbl) -> {k, v for k, v in pairs tbl}
-- Make a deep copy of a table
-- Retains equality of table references inside the copy and handles self-referencing structures
deep_copy = (tbl) ->
seen = {}
copy = (val) ->
return val if type(tbl) != 'table'
@ -28,15 +29,15 @@ utils =
{k, copy(v) for k, v in pairs val}
copy tbl
-- Generates ASS hexadecimal string from R, G, B integer components, in &HBBGGRR& format
ass_color: (r, g, b) -> string.format "&H%02X%02X%02X&", b, g, r
-- Format an alpha-string for \Xa style overrides
ass_alpha: (a) -> string.format "&H%02X&", a
-- Format an ABGR string for use in style definitions (these don't end with & either)
ass_style_color: (r, g, b, a) -> string.format "&H%02X%02X%02X%02X", a, b, g, r
-- Generates ASS hexadecimal string from R, G, B integer components, in &HBBGGRR& format
ass_color = (r, g, b) -> sformat "&H%02X%02X%02X&", b, g, r
-- Format an alpha-string for \Xa style overrides
ass_alpha = (a) -> sformat "&H%02X&", a
-- Format an ABGR string for use in style definitions (these don't end with & either)
ass_style_color = (r, g, b, a) -> sformat "&H%02X%02X%02X%02X", a, b, g, r
-- Extract colour components of an ASS colour
extract_color: (s) ->
-- Extract colour components of an ASS colour
extract_color = (s) ->
local a, b, g, r
-- Try a style first
@ -59,16 +60,16 @@ utils =
if r then
return tonumber(r, 16), tonumber(g, 16) or 0, tonumber(b, 16) or 0, tonumber(a, 16) or 0
-- Create an alpha override code from a style definition colour code
alpha_from_style: (scolor) -> ass_alpha select 4, extract_color scolor
-- Create an alpha override code from a style definition colour code
alpha_from_style = (scolor) -> ass_alpha select 4, extract_color scolor
-- Create an colour override code from a style definition colour code
color_from_style: (scolor) ->
-- Create an colour override code from a style definition colour code
color_from_style = (scolor) ->
r, g, b = extract_color scolor
ass_color r or 0, g or 0, b or 0
-- Converts HSV (Hue, Saturation, Value) to RGB
HSV_to_RGB: (H, S, V) ->
-- Converts HSV (Hue, Saturation, Value) to RGB
HSV_to_RGB = (H, S, V) ->
r, g, b = 0, 0, 0
-- Saturation is zero, make grey
@ -117,9 +118,9 @@ utils =
return r, g, b
-- Convert HSL (Hue, Saturation, Luminance) to RGB
-- Contributed by Gundamn
HSL_to_RGB: (H, S, L) ->
-- Convert HSL (Hue, Saturation, Luminance) to RGB
-- Contributed by Gundamn
HSL_to_RGB = (H, S, L) ->
local r, g, b
-- Make sure input is in range
@ -172,40 +173,43 @@ utils =
return math.floor(r*255+0.5), math.floor(g*255+0.5), math.floor(b*255+0.5)
-- Removes spaces at the start and end of string
trim: (s) -> s\gsub '^%s*(.-)%s*$', '%1'
-- Removes spaces at the start and end of string
trim = (s) -> s\gsub '^%s*(.-)%s*$', '%1'
-- Get the 'head' and 'tail' of a string, treating it as a sequence of words separated by one or more space-characters
headtail: (s) ->
-- Get the 'head' and 'tail' of a string, treating it as a sequence of words separated by one or more space-characters
headtail = (s) ->
a, b, head, tail = s\find '(.-)%s+(.*)'
if a then head, tail else s, ''
-- Iterator function for headtail
words: (s) -> ->
-- Iterator function for headtail
words = (s) -> ->
return if s == ''
head, tail = string.headtail s
head, tail = headtail s
s = tail
head
-- Clamp a number value to a range
clamp: (val, min, max) ->
-- Clamp a number value to a range
clamp = (val, min, max) ->
if val < min then min elseif val > max then max else val
-- Interpolate between two numbers
interpolate: (pct, min, max) ->
-- Interpolate between two numbers
interpolate = (pct, min, max) ->
if pct <= 0 then min elseif pct >= 1 then max else pct * (max - min) + min
-- Interpolate between two colour values, given in either style definition or style override format
-- Return in style override format
interpolate_color: (pct, first, last) ->
-- Interpolate between two colour values, given in either style definition or style override format
-- Return in style override format
interpolate_color = (pct, first, last) ->
r1, g1, b1 = extract_color first
r2, g2, b2 = extract_color last
r, g, b = interpolate(pct, r1, r2), interpolate(pct, g1, g2), interpolate(pct, b1, b2)
ass_color r, g, b
-- Interpolate between two alpha values, given either in style override or as part as a style definition colour
-- Return in style override format
interpolate_alpha: (pct, first, last) ->
-- Interpolate between two alpha values, given either in style override or as part as a style definition colour
-- Return in style override format
interpolate_alpha = (pct, first, last) ->
ass_alpha interpolate pct, select(4, extract_color first), select(4, extract_color last)
utils
{ :copy, :deep_copy, :ass_color, :ass_alpha, :ass_style_color,
:extract_color, :alpha_from_style, :color_from_style, :HSV_to_RGB,
:HSL_to_RGB, :trim, :headtail, :words, :clamp, :interpolate,
:interpolate_color, :interpolate_alpha }