-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultitone.lua
More file actions
36 lines (33 loc) · 1.1 KB
/
multitone.lua
File metadata and controls
36 lines (33 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
--- Module to generate a duotone effect.
-- It also supports gradients of more than two colors.
local base = [[
<filter id='$id'>
<feColorMatrix type="matrix" values=".375 .5 .125 0 0 .375 .5 .125 0 0 .375 .5 .125 0 0 0 0 0 1 0" />
<feComponentTransfer color-interpolation-filter="sRGB">
<feFuncR type="table" tableValues="$red" />
<feFuncG type="table" tableValues="$green" />
<feFuncB type="table" tableValues="$blue" />
</feComponentTransfer>
</filter>
]]
local function normal(number)
return math.min(1, math.max(0, math.floor(number*1000)/1000))
end
local function multitone(id, ...)
local components = {{}, {}, {}}
for i, color in ipairs({...}) do
components[1][i] = tostring(normal(color[1]))
components[2][i] = tostring(normal(color[2]))
components[3][i] = tostring(normal(color[3]))
end
local options = {
red = table.concat(components[1], ' ');
green = table.concat(components[2], ' ');
blue = table.concat(components[3], ' ');
id = id
}
return (base:gsub("$%w+", function(key)
return options[key:sub(2)] or error("Key "..key.." not found in options!")
end))
end
return multitone