模块:ColorHexToDec:修订间差异
来自RIA | Wiki
更多操作
创建页面,内容为“local p = {} -- 十六进制颜色转十进制RGB(完整版) function p.hexToDec(frame) local hex = frame.args[1] or "000000" -- 移除可能存在的#号 hex = hex:gsub("#", "") -- 如果是3位简写,扩展为6位 if hex:match("^[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]$") then hex = hex:gsub("(.)(.)(.)", "%1%1%2%2%3%3") end -- 验证输入格式(6位十六进制) if not hex:match("^[0-9A-Fa-f][0-9A-Fa…” |
无编辑摘要 |
||
| 第1行: | 第1行: | ||
local p = {} | local p = {} | ||
function p.hexToDec(frame) | function p.hexToDec(frame) | ||
local hex = frame.args[1] or "000000" | local hex = frame.args[1] or "000000" | ||
hex = hex:gsub("#", "") | hex = hex:gsub("#", "") | ||
if hex:match("^[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]$") then | if hex:match("^[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]$") then | ||
hex = hex:gsub("(.)(.)(.)", "%1%1%2%2%3%3") | hex = hex:gsub("(.)(.)(.)", "%1%1%2%2%3%3") | ||
end | end | ||
if not hex:match("^[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]$") then | if not hex:match("^[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]$") then | ||
return "000,000,000" | return "000,000,000" | ||
end | end | ||
local r_hex = hex:sub(1, 2) | local r_hex = hex:sub(1, 2) | ||
local g_hex = hex:sub(3, 4) | local g_hex = hex:sub(3, 4) | ||
local b_hex = hex:sub(5, 6) | local b_hex = hex:sub(5, 6) | ||
local r_dec = tonumber(r_hex, 16) or 0 | local r_dec = tonumber(r_hex, 16) or 0 | ||
local g_dec = tonumber(g_hex, 16) or 0 | local g_dec = tonumber(g_hex, 16) or 0 | ||
local b_dec = tonumber(b_hex, 16) or 0 | local b_dec = tonumber(b_hex, 16) or 0 | ||
return string.format("%d,%d,%d", r_dec, g_dec, b_dec) | return string.format("%d,%d,%d", r_dec, g_dec, b_dec) | ||
end | end | ||
return p | return p | ||
2026年3月5日 (四) 10:33的最新版本
此模块的文档可以在模块:ColorHexToDec/doc创建
local p = {}
function p.hexToDec(frame)
local hex = frame.args[1] or "000000"
hex = hex:gsub("#", "")
if hex:match("^[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]$") then
hex = hex:gsub("(.)(.)(.)", "%1%1%2%2%3%3")
end
if not hex:match("^[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]$") then
return "000,000,000"
end
local r_hex = hex:sub(1, 2)
local g_hex = hex:sub(3, 4)
local b_hex = hex:sub(5, 6)
local r_dec = tonumber(r_hex, 16) or 0
local g_dec = tonumber(g_hex, 16) or 0
local b_dec = tonumber(b_hex, 16) or 0
return string.format("%d,%d,%d", r_dec, g_dec, b_dec)
end
return p