aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/lua/gradients.lua
blob: 71e8c30d176f1c28e0a4d01c903baa968afe27fe (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
filename = ""

function sk_scrape_startcanvas(c, fileName)
    filename = fileName
end

function sk_scrape_endcanvas(c, fileName)

end

LuaDoubleNearlyZero = 1.0 / bit32.lshift(1.0, 12)

function LuaDoubleNearlyEqual(a, b)
    return math.abs(a-b) <= LuaDoubleNearlyZero
end

function bounds(rect)
    local width  = rect.right  - rect.left
    local height = rect.bottom - rect.top

    return width, height
end

gradients = {}

i = 1

function sk_scrape_accumulate(t)
    local p = t.paint
    if p then
        local s = p:getShader()
        if s then
            local g = s:asAGradient()
            if g then
                gradients[i] = {}

                gradients[i].filename = filename

                local width, height = -1, -1
                if t.rect then
                    width, height = bounds(t.rect)
                elseif t.rrect then
                    width, height = bounds(t.rrect:rect())
                elseif t.path then
                    width, height = bounds(t.path:getBounds())
                end
                gradients[i].boundsWidth  = width
                gradients[i].boundsHeight = height

                gradients[i].colorCount = g.colorCount
                gradients[i].type       = g.type
                gradients[i].tile       = g.tile

                isEvenlySpaced = true
                for j = 1, g.colorCount, 1 do
                    if not LuaDoubleNearlyEqual(g.positions[j], (j-1)/(g.colorCount-1)) then
                        isEvenlySpaced = false
                    end
                end
                gradients[i].isEvenlySpaced = isEvenlySpaced

                numHardStops = 0
                for j = 2, g.colorCount, 1 do
                    if LuaDoubleNearlyEqual(g.positions[j], g.positions[j-1]) then
                        numHardStops = numHardStops + 1
                    end
                end
                gradients[i].numHardStops = numHardStops

                gradients[i].verb = t.verb
                
                gradients[i].positions = {}
                for j = 1, g.colorCount, 1 do
                    gradients[i].positions[j] = g.positions[j]
                end

                i = i + 1
            end
        end
    end
end

function sk_scrape_summarize()
    for k, v in pairs(gradients) do
        local pos = ""
        for j = 1, v.colorCount , 1 do
            pos = pos .. v.positions[j]
            if j ~= v.colorCount then
                pos = pos .. ","
            end
        end

        io.write(string.format("%s %d %s %s %d %d %s %d %d %s\n",
                                v.filename,
                                v.colorCount,
                                v.type,
                                v.tile,
                                tonumber(v.isEvenlySpaced and 1 or 0),
                                v.numHardStops,
                                v.verb,
                                v.boundsWidth,
                                v.boundsHeight,
                                pos))
    end
end