Skip to content

Instantly share code, notes, and snippets.

@LazyKnightX
Last active January 4, 2025 14:36
Show Gist options
  • Save LazyKnightX/690886fdad0f535fcdf93a3b51116c58 to your computer and use it in GitHub Desktop.
Save LazyKnightX/690886fdad0f535fcdf93a3b51116c58 to your computer and use it in GitHub Desktop.
魔兽Lua 文字染色渐变
------------------------------------------------------------------------
-- 本代码使用MIT协议 @ Lazy Knight (@LazyKnightX) 2025
------------------------------------------------------------------------
--
-- file: run.bat
--
-- @echo off
-- echo run lua - %1
-- tools\w3x2lni\bin\w3x2lni-lua.exe %1
--
--
-- 版本: 0.1.0
-- 联系: lazyknight#foxmail.com (#替换为@)
--
-- 记录:
-- 0.1.0 - 草稿实现。
--
local io = require 'io'
function lerp(a, b, t)
return a + (b - a) * t
end
function math.round(r)
return math.floor(r + 0.5)
end
function int(v)
return math.round(v)
end
function rgba255(r, g, b, a)
-- |cAARRGGBB
r = int(r)
g = int(g)
b = int(b)
a = int(a)
return (a << 24) + (r << 16) + (g << 8) + b
end
function rgba255s(r, g, b, a)
r = int(r)
g = int(g)
b = int(b)
a = int(a)
local R = string.format('%02X', r)
local G = string.format('%02X', g)
local B = string.format('%02X', b)
local A = string.format('%02X', a)
return string.format('|c%s%s%s%s', A, R, G, B)
end
function s2rgba255(s)
s = string.gsub(s, '|c', '')
local A = string.sub(s, 1, 2)
local R = string.sub(s, 3, 4)
local G = string.sub(s, 5, 6)
local B = string.sub(s, 7, 8)
local a = tonumber(A, 16)
local r = tonumber(R, 16)
local g = tonumber(G, 16)
local b = tonumber(B, 16)
return r, g, b, a
end
function scolorfade(s1, s2, t)
local r1, g1, b1, a1 = s2rgba255(s1)
local r2, g2, b2, a2 = s2rgba255(s2)
local r = lerp(r1, r2, t)
local g = lerp(g1, g2, t)
local b = lerp(b1, b2, t)
local a = lerp(a1, a2, t)
return rgba255s(r, g, b, a)
end
local _log = ''
function log(...)
local args = {...}
local s = table.concat(args, '\t')
_log = _log .. s .. '\n'
end
function logout()
local f = io.open('temp\\log.txt', 'w')
f:write(_log)
f:close()
end
function stextfade(s, s1, s2)
local n = utf8.len(s)
log('==============')
log(string.sub(s, 1, 3))
local a = {}
for i = 1, n do
local t = (i - 1) / (n - 1)
local x = scolorfade(s1, s2, t)
local shift = utf8.offset(s, i)
log(string.format('i: %s, shift: %s', i, shift))
local c = string.sub(s, shift, shift + 2)
log(c)
table.insert(a, string.format('%s%s|r', x, c))
end
return table.concat(a, ''), n
end
function test()
print(lerp(1, 5, 0.5))
print(lerp(0xff, 0x00, 0.5))
print(rgba255(0x00, 0x00, 0xff, 0x00), rgba255(0x00, 0x00, 0xff, 0x00) == (0xff - 0))
print(rgba255(0x00, 0xff, 0xff, 0x00), rgba255(0x00, 0xff, 0xff, 0x00) == (0xffff - 0))
print(rgba255(0xff, 0xff, 0xff, 0x00), rgba255(0xff, 0xff, 0xff, 0x00) == (0xffffff - 0))
print(rgba255(0xff, 0xff, 0xff, 0xff), rgba255(0xff, 0xff, 0xff, 0xff) == (0xffffffff - 0))
print(rgba255s(255, 255, 0, 98))
print(s2rgba255('|cF0E0D0C0|r'))
print(stextfade('钛合', '|cffff0000|r', '|cff00ff00|r'))
print(stextfade('钛合金超闪绝杀技', '|cffff0000|r', '|cff00ff00|r'))
log(stextfade('钛合金超闪绝杀技', '|cffff0000|r', '|cff00ff00|r'))
end
test()
logout()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment