diff options
author | Adam <adamdharrison@gmail.com> | 2023-10-10 16:25:10 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-10 16:25:10 -0400 |
commit | b51140f1df453346ef7823b6799286677d761ae0 (patch) | |
tree | 74eeeafad8d431653e3f7af2d79f24076fbf9117 /scripts/make_preview_image.lua | |
parent | 311b5389f88a61c260b4fcbada878ee556c3271d (diff) | |
parent | 7db1dfb381912b4335779246f6f11ed405c6583e (diff) | |
download | lite-xl-colors-b51140f1df453346ef7823b6799286677d761ae0.tar.gz lite-xl-colors-b51140f1df453346ef7823b6799286677d761ae0.zip |
Merge branch 'master' into PR/subl
Diffstat (limited to 'scripts/make_preview_image.lua')
-rwxr-xr-x | scripts/make_preview_image.lua | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/scripts/make_preview_image.lua b/scripts/make_preview_image.lua new file mode 100755 index 0000000..d1d48be --- /dev/null +++ b/scripts/make_preview_image.lua @@ -0,0 +1,50 @@ +#!/usr/bin/lua + +for i, filename in ipairs({ ... }) do + local name = filename:match("([^\\/]+)%..*$") + + -- get colors + local text = io.open(filename):read("*a") + local colors = {} + for r, g, b in text:gmatch("#(%x%x)(%x%x)(%x%x)") do + r = tonumber(r, 16) + g = tonumber(g, 16) + b = tonumber(b, 16) + table.insert(colors, { r, g, b }) + end + + table.sort(colors, function(a, b) + return a[1] + a[2] + a[3] < b[1] + b[2] + b[3] + end) + + local function eq(a, b) + return a[1] == b[1] and a[2] == b[2] and a[3] == b[3] + end + + local prev = {} + for i = #colors, 1, -1 do + if eq(colors[i], prev) then + table.remove(colors, i) + else + prev = colors[i] + end + end + + + --generate svg file + local w = 200 + local h = 16 + local fp = io.open("previews/" .. name .. ".svg", "wb") + + fp:write('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="'..w..'" height="'..h..'" shape-rendering="crispEdges"> \n') + + for i = 1, #colors do + local width = w/#colors + local r, g, b = table.unpack(colors[i]) + local rect = '<rect x="'..(i-1)*width..'" width="'..width..'" height="'..h..'" fill="rgb('..r..', '..g..', '..b..')"></rect> \n' + fp:write(rect) + end + + fp:write('</svg>') + fp:close() +end |