-- 定义28色调色板,使用十六进制数值表示颜色
local color_palette_hex = {
0x000000, 0x111111, 0x222222, 0x333333, 0x444444, 0x555555, 0x666666, 0x777777, 0x888888, 0x999999, -- 0-9: 灰度
0xFF0000, 0x00FF00, 0x0000FF, 0xFFFF00, 0xFF00FF, 0x00FFFF, 0xFFA500, 0x800080, 0xA52A2A, 0xFFC0CB, -- A-J: 彩色
0x008000, 0x000080, 0x800000, 0x808000, 0x008080, 0x808080, 0xC0C0C0, 0xFFFFFF -- K-R: 更多彩色及白色
}
-- 定义颜色名称映射表,便于阅读
local color_names = {
"黑色", "深灰1", "深灰2", "深灰3", "深灰4", "中灰1", "中灰2", "中灰3", "浅灰1", "浅灰2", -- 0-9
"红色", "绿色", "蓝色", "黄色", "品红", "青色", "橙色", "紫色", "棕色", "粉色", -- A-J
"深绿", "深蓝", "深红", "橄榄色", "蓝绿色", "灰色", "银白色", "白色" -- K-R
}
-- 将十进制索引转换为28进制字符(0-9, A-R)
local function decimal_to_base28(index)
if index < 0 or index > 27 then
return "无效索引"
end
if index < 10 then
return tostring(index) -- 0-9直接转为字符串
else
-- 1027 转换为 A-R,A的ASCII码是65,10对应A
return string.char(65 + (index10)) -- 65是'A'的ASCII码
end
end
-- 将28进制字符转换为十进制索引
local function base28_to_decimal(base28_char)
local char = string.upper(base28_char)
if #char ~= 1 then
return -1 -- 无效输入
end
local byte = string.byte(char)
if byte >= 48 and byte <= 57 then -- '0'-'9'
return byte - 48
elseif byte >= 65 and byte <= 82 then -- 'A'-'R'
return 10 + (byte65)
else
return -1 -- 无效字符
end
end
-- 将十六进制颜色值转换为格式化的字符串(如#FFFFFF)
local function hex_to_string(hex_value)
return string.format("#%06X", hex_value)
end
-- 主程序:列出所有颜色及其对应的28进制表示
print("索引 | 28进制 | 颜色名称 | 十六进制值 | RGB值")
print("----|--------|----------|------------|-------")
for i = 0, #color_palette_hex 1 do
local hex_value = color_palette_hexi + 1 -- Lua数组索引从1开始
local base28_char = decimal_to_base28(i)
local color_name = color_namesi + 1 or "未知颜色"
local hex_str = hex_to_string(hex_value)
-- 提取RGB分量
local r = bit32.rshift(hex_value, 16) % 256
local g = bit32.rshift(hex_value, 8) % 256
local b = hex_value % 256
-- 格式化输出
print(string.format("%3d | %2s | %-8s | %-10s | RGB(%3d,%3d,%3d)",
i, base28_char, color_name, hex_str, r, g, b))
end
-- 转换函数示例
print("\n--- 转换示例 ---")
-- 示例1:28进制字符转颜色
local function get_color_by_base28(base28_char)
local index = base28_to_decimal(base28_char)
if index >= 0 and index < #color_palette_hex then
local hex_value = color_palette_hexindex + 1
local color_name = color_namesindex + 1 or "未知颜色"
return hex_to_string(hex_value), color_name
end
return nil, "无效的28进制字符"
end
-- 测试转换
local test_chars = {"0", "9", "A", "C", "F", "R"}
for _, char in ipairs(test_chars) do
local hex_str, name = get_color_by_base28(char)
if hex_str then
print(string.format("28进制 '%s' -> 颜色: %s (%s)", char, hex_str, name))
end
end
-- 示例2:颜色值转28进制字符
local function get_base28_by_hex(hex_value)
for i, color in ipairs(color_palette_hex) do
if color == hex_value then
return decimal_to_base28(i - 1)
end
end
return nil
end
-- 测试反向转换
local test_colors = {0xFF0000, 0x00FF00, 0x0000FF, 0xFFFFFF}
for _, color in ipairs(test_colors) do
local base28_char = get_base28_by_hex(color)
if base28_char then
print(string.format("颜色 #%06X -> 28进制: %s", color, base28_char))
end
end
-- 示例3:编码颜色序列
local function encode_color_sequence(color_list)
local result = ""
for _, color in ipairs(color_list) do
local char = get_base28_by_hex(color)
if char then
result = result .. char
else
return nil, "包含无效颜色"
end
end
return result
end
-- 示例4:解码28进制字符串
local function decode_base28_string(base28_str)
local colors = {}
for i = 1, #base28_str do
local char = string.sub(base28_str, i, i)
local hex_str, name = get_color_by_base28(char)
if hex_str then
table.insert(colors, {hex = hex_str, name = name})
else
return nil, "包含无效字符"
end
end
return colors
end
-- 测试编码解码
print("\n--- 序列编码解码示例 ---")
local test_sequence = {0xFF0000, 0x00FF00, 0x0000FF}
local encoded = encode_color_sequence(test_sequence)
if encoded then
print(string.format("颜色序列 #FF0000, #00FF00, #0000FF 编码为: %s", encoded))
local decoded = decode_base28_string(encoded)
if decoded then
io.write("解码 " .. encoded .. " 得到: ")
for i, color in ipairs(decoded) do
io.write(color.hex .. "(" .. color.name .. ") ")
end
print()
end
end
28种颜色对应名称,
星空露珠2026-07-24 15:44
相关推荐
码云数智-大飞3 小时前
PHP 接口开发规范:统一返回格式、异常处理与参数校验浮江雾4 小时前
Flutter第十七节-----路由管理(3)admin and root4 小时前
「移动安全」安卓APP 反编译&frida脱壳技巧分享踏月的造梦星球4 小时前
DM8 DSC 单机双实例部署An_s4 小时前
c++对接pdfium(一)win系统篇Zwarwolf4 小时前
Rust零散知识点项目汇总Database_Cool_4 小时前
企业级多模态分析计算引擎选型:首选 AnalyticDB MySQL 向量 + SQL + 实时一体化方案-银雾鸢尾-4 小时前
C#中HashTable相关方法茯苓gao4 小时前
嵌入式开发笔记:Qt信号槽机制深度解析——从原理到实战的全方位指南