forked from mia/Aegisub
Fix calls to other util functions from within util functions
This commit is contained in:
parent
9bcc6efcc8
commit
1b96bf748f
|
@ -1,4 +1,5 @@
|
||||||
local utils = {
|
local sformat = string.format
|
||||||
|
local copy
|
||||||
copy = function(tbl)
|
copy = function(tbl)
|
||||||
return (function()
|
return (function()
|
||||||
local _tbl_0 = { }
|
local _tbl_0 = { }
|
||||||
|
@ -7,10 +8,10 @@ local utils = {
|
||||||
end
|
end
|
||||||
return _tbl_0
|
return _tbl_0
|
||||||
end)()
|
end)()
|
||||||
end,
|
end
|
||||||
|
local deep_copy
|
||||||
deep_copy = function(tbl)
|
deep_copy = function(tbl)
|
||||||
local seen = { }
|
local seen = { }
|
||||||
local copy
|
|
||||||
copy = function(val)
|
copy = function(val)
|
||||||
if type(tbl) ~= 'table' then
|
if type(tbl) ~= 'table' then
|
||||||
return val
|
return val
|
||||||
|
@ -28,16 +29,20 @@ local utils = {
|
||||||
end)()
|
end)()
|
||||||
end
|
end
|
||||||
return copy(tbl)
|
return copy(tbl)
|
||||||
end,
|
end
|
||||||
|
local ass_color
|
||||||
ass_color = function(r, g, b)
|
ass_color = function(r, g, b)
|
||||||
return string.format("&H%02X%02X%02X&", b, g, r)
|
return sformat("&H%02X%02X%02X&", b, g, r)
|
||||||
end,
|
end
|
||||||
|
local ass_alpha
|
||||||
ass_alpha = function(a)
|
ass_alpha = function(a)
|
||||||
return string.format("&H%02X&", a)
|
return sformat("&H%02X&", a)
|
||||||
end,
|
end
|
||||||
|
local ass_style_color
|
||||||
ass_style_color = function(r, g, b, a)
|
ass_style_color = function(r, g, b, a)
|
||||||
return string.format("&H%02X%02X%02X%02X", a, b, g, r)
|
return sformat("&H%02X%02X%02X%02X", a, b, g, r)
|
||||||
end,
|
end
|
||||||
|
local extract_color
|
||||||
extract_color = function(s)
|
extract_color = function(s)
|
||||||
local a, b, g, r
|
local a, b, g, r
|
||||||
a, b, g, r = s:match('&H(%x%x)(%x%x)(%x%x)(%x%x)')
|
a, b, g, r = s:match('&H(%x%x)(%x%x)(%x%x)(%x%x)')
|
||||||
|
@ -56,14 +61,17 @@ local utils = {
|
||||||
if r then
|
if r then
|
||||||
return tonumber(r, 16), tonumber(g, 16) or 0, tonumber(b, 16) or 0, tonumber(a, 16) or 0
|
return tonumber(r, 16), tonumber(g, 16) or 0, tonumber(b, 16) or 0, tonumber(a, 16) or 0
|
||||||
end
|
end
|
||||||
end,
|
end
|
||||||
|
local alpha_from_style
|
||||||
alpha_from_style = function(scolor)
|
alpha_from_style = function(scolor)
|
||||||
return ass_alpha(select(4, extract_color(scolor)))
|
return ass_alpha(select(4, extract_color(scolor)))
|
||||||
end,
|
end
|
||||||
|
local color_from_style
|
||||||
color_from_style = function(scolor)
|
color_from_style = function(scolor)
|
||||||
local r, g, b = extract_color(scolor)
|
local r, g, b = extract_color(scolor)
|
||||||
return ass_color(r or 0, g or 0, b or 0)
|
return ass_color(r or 0, g or 0, b or 0)
|
||||||
end,
|
end
|
||||||
|
local HSV_to_RGB
|
||||||
HSV_to_RGB = function(H, S, V)
|
HSV_to_RGB = function(H, S, V)
|
||||||
local r, g, b = 0, 0, 0
|
local r, g, b = 0, 0, 0
|
||||||
if S == 0 then
|
if S == 0 then
|
||||||
|
@ -106,7 +114,8 @@ local utils = {
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return r, g, b
|
return r, g, b
|
||||||
end,
|
end
|
||||||
|
local HSL_to_RGB
|
||||||
HSL_to_RGB = function(H, S, L)
|
HSL_to_RGB = function(H, S, L)
|
||||||
local r, g, b
|
local r, g, b
|
||||||
H = math.abs(H) % 360
|
H = math.abs(H) % 360
|
||||||
|
@ -156,10 +165,12 @@ local utils = {
|
||||||
b = get_component(Tb)
|
b = get_component(Tb)
|
||||||
end
|
end
|
||||||
return math.floor(r * 255 + 0.5), math.floor(g * 255 + 0.5), math.floor(b * 255 + 0.5)
|
return math.floor(r * 255 + 0.5), math.floor(g * 255 + 0.5), math.floor(b * 255 + 0.5)
|
||||||
end,
|
end
|
||||||
|
local trim
|
||||||
trim = function(s)
|
trim = function(s)
|
||||||
return s:gsub('^%s*(.-)%s*$', '%1')
|
return s:gsub('^%s*(.-)%s*$', '%1')
|
||||||
end,
|
end
|
||||||
|
local headtail
|
||||||
headtail = function(s)
|
headtail = function(s)
|
||||||
local a, b, head, tail = s:find('(.-)%s+(.*)')
|
local a, b, head, tail = s:find('(.-)%s+(.*)')
|
||||||
if a then
|
if a then
|
||||||
|
@ -167,17 +178,19 @@ local utils = {
|
||||||
else
|
else
|
||||||
return s, ''
|
return s, ''
|
||||||
end
|
end
|
||||||
end,
|
end
|
||||||
|
local words
|
||||||
words = function(s)
|
words = function(s)
|
||||||
return function()
|
return function()
|
||||||
if s == '' then
|
if s == '' then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local head, tail = string.headtail(s)
|
local head, tail = headtail(s)
|
||||||
s = tail
|
s = tail
|
||||||
return head
|
return head
|
||||||
end
|
end
|
||||||
end,
|
end
|
||||||
|
local clamp
|
||||||
clamp = function(val, min, max)
|
clamp = function(val, min, max)
|
||||||
if val < min then
|
if val < min then
|
||||||
return min
|
return min
|
||||||
|
@ -186,7 +199,8 @@ local utils = {
|
||||||
else
|
else
|
||||||
return val
|
return val
|
||||||
end
|
end
|
||||||
end,
|
end
|
||||||
|
local interpolate
|
||||||
interpolate = function(pct, min, max)
|
interpolate = function(pct, min, max)
|
||||||
if pct <= 0 then
|
if pct <= 0 then
|
||||||
return min
|
return min
|
||||||
|
@ -195,15 +209,34 @@ local utils = {
|
||||||
else
|
else
|
||||||
return pct * (max - min) + min
|
return pct * (max - min) + min
|
||||||
end
|
end
|
||||||
end,
|
end
|
||||||
|
local interpolate_color
|
||||||
interpolate_color = function(pct, first, last)
|
interpolate_color = function(pct, first, last)
|
||||||
local r1, g1, b1 = extract_color(first)
|
local r1, g1, b1 = extract_color(first)
|
||||||
local r2, g2, b2 = extract_color(last)
|
local r2, g2, b2 = extract_color(last)
|
||||||
local r, g, b = interpolate(pct, r1, r2), interpolate(pct, g1, g2), interpolate(pct, b1, b2)
|
local r, g, b = interpolate(pct, r1, r2), interpolate(pct, g1, g2), interpolate(pct, b1, b2)
|
||||||
return ass_color(r, g, b)
|
return ass_color(r, g, b)
|
||||||
end,
|
end
|
||||||
|
local interpolate_alpha
|
||||||
interpolate_alpha = function(pct, first, last)
|
interpolate_alpha = function(pct, first, last)
|
||||||
return ass_alpha(interpolate(pct, select(4, extract_color(first)), select(4, extract_color(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
|
|
||||||
|
|
|
@ -13,13 +13,14 @@
|
||||||
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
utils =
|
sformat = string.format
|
||||||
|
|
||||||
-- Make a shallow copy of a table
|
-- Make a shallow copy of a table
|
||||||
copy: (tbl) -> {k, v for k, v in pairs tbl}
|
copy = (tbl) -> {k, v for k, v in pairs tbl}
|
||||||
|
|
||||||
-- Make a deep copy of a table
|
-- Make a deep copy of a table
|
||||||
-- Retains equality of table references inside the copy and handles self-referencing structures
|
-- Retains equality of table references inside the copy and handles self-referencing structures
|
||||||
deep_copy: (tbl) ->
|
deep_copy = (tbl) ->
|
||||||
seen = {}
|
seen = {}
|
||||||
copy = (val) ->
|
copy = (val) ->
|
||||||
return val if type(tbl) != 'table'
|
return val if type(tbl) != 'table'
|
||||||
|
@ -29,14 +30,14 @@ utils =
|
||||||
copy tbl
|
copy tbl
|
||||||
|
|
||||||
-- Generates ASS hexadecimal string from R, G, B integer components, in &HBBGGRR& format
|
-- 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
|
ass_color = (r, g, b) -> sformat "&H%02X%02X%02X&", b, g, r
|
||||||
-- Format an alpha-string for \Xa style overrides
|
-- Format an alpha-string for \Xa style overrides
|
||||||
ass_alpha: (a) -> string.format "&H%02X&", a
|
ass_alpha = (a) -> sformat "&H%02X&", a
|
||||||
-- Format an ABGR string for use in style definitions (these don't end with & either)
|
-- 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
|
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 colour components of an ASS colour
|
||||||
extract_color: (s) ->
|
extract_color = (s) ->
|
||||||
local a, b, g, r
|
local a, b, g, r
|
||||||
|
|
||||||
-- Try a style first
|
-- Try a style first
|
||||||
|
@ -60,15 +61,15 @@ utils =
|
||||||
return tonumber(r, 16), tonumber(g, 16) or 0, tonumber(b, 16) or 0, tonumber(a, 16) or 0
|
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
|
-- Create an alpha override code from a style definition colour code
|
||||||
alpha_from_style: (scolor) -> ass_alpha select 4, extract_color scolor
|
alpha_from_style = (scolor) -> ass_alpha select 4, extract_color scolor
|
||||||
|
|
||||||
-- Create an colour override code from a style definition colour code
|
-- Create an colour override code from a style definition colour code
|
||||||
color_from_style: (scolor) ->
|
color_from_style = (scolor) ->
|
||||||
r, g, b = extract_color scolor
|
r, g, b = extract_color scolor
|
||||||
ass_color r or 0, g or 0, b or 0
|
ass_color r or 0, g or 0, b or 0
|
||||||
|
|
||||||
-- Converts HSV (Hue, Saturation, Value) to RGB
|
-- Converts HSV (Hue, Saturation, Value) to RGB
|
||||||
HSV_to_RGB: (H, S, V) ->
|
HSV_to_RGB = (H, S, V) ->
|
||||||
r, g, b = 0, 0, 0
|
r, g, b = 0, 0, 0
|
||||||
|
|
||||||
-- Saturation is zero, make grey
|
-- Saturation is zero, make grey
|
||||||
|
@ -119,7 +120,7 @@ utils =
|
||||||
|
|
||||||
-- Convert HSL (Hue, Saturation, Luminance) to RGB
|
-- Convert HSL (Hue, Saturation, Luminance) to RGB
|
||||||
-- Contributed by Gundamn
|
-- Contributed by Gundamn
|
||||||
HSL_to_RGB: (H, S, L) ->
|
HSL_to_RGB = (H, S, L) ->
|
||||||
local r, g, b
|
local r, g, b
|
||||||
|
|
||||||
-- Make sure input is in range
|
-- Make sure input is in range
|
||||||
|
@ -173,31 +174,31 @@ utils =
|
||||||
return math.floor(r*255+0.5), math.floor(g*255+0.5), math.floor(b*255+0.5)
|
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
|
-- Removes spaces at the start and end of string
|
||||||
trim: (s) -> s\gsub '^%s*(.-)%s*$', '%1'
|
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
|
-- Get the 'head' and 'tail' of a string, treating it as a sequence of words separated by one or more space-characters
|
||||||
headtail: (s) ->
|
headtail = (s) ->
|
||||||
a, b, head, tail = s\find '(.-)%s+(.*)'
|
a, b, head, tail = s\find '(.-)%s+(.*)'
|
||||||
if a then head, tail else s, ''
|
if a then head, tail else s, ''
|
||||||
|
|
||||||
-- Iterator function for headtail
|
-- Iterator function for headtail
|
||||||
words: (s) -> ->
|
words = (s) -> ->
|
||||||
return if s == ''
|
return if s == ''
|
||||||
head, tail = string.headtail s
|
head, tail = headtail s
|
||||||
s = tail
|
s = tail
|
||||||
head
|
head
|
||||||
|
|
||||||
-- Clamp a number value to a range
|
-- Clamp a number value to a range
|
||||||
clamp: (val, min, max) ->
|
clamp = (val, min, max) ->
|
||||||
if val < min then min elseif val > max then max else val
|
if val < min then min elseif val > max then max else val
|
||||||
|
|
||||||
-- Interpolate between two numbers
|
-- Interpolate between two numbers
|
||||||
interpolate: (pct, min, max) ->
|
interpolate = (pct, min, max) ->
|
||||||
if pct <= 0 then min elseif pct >= 1 then max else pct * (max - min) + min
|
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
|
-- Interpolate between two colour values, given in either style definition or style override format
|
||||||
-- Return in style override format
|
-- Return in style override format
|
||||||
interpolate_color: (pct, first, last) ->
|
interpolate_color = (pct, first, last) ->
|
||||||
r1, g1, b1 = extract_color first
|
r1, g1, b1 = extract_color first
|
||||||
r2, g2, b2 = extract_color last
|
r2, g2, b2 = extract_color last
|
||||||
r, g, b = interpolate(pct, r1, r2), interpolate(pct, g1, g2), interpolate(pct, b1, b2)
|
r, g, b = interpolate(pct, r1, r2), interpolate(pct, g1, g2), interpolate(pct, b1, b2)
|
||||||
|
@ -205,7 +206,10 @@ utils =
|
||||||
|
|
||||||
-- Interpolate between two alpha values, given either in style override or as part as a style definition colour
|
-- Interpolate between two alpha values, given either in style override or as part as a style definition colour
|
||||||
-- Return in style override format
|
-- Return in style override format
|
||||||
interpolate_alpha: (pct, first, last) ->
|
interpolate_alpha = (pct, first, last) ->
|
||||||
ass_alpha interpolate pct, select(4, extract_color first), select(4, extract_color 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 }
|
||||||
|
|
Loading…
Reference in a new issue