28种颜色对应名称,

-- 定义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

相关推荐
似水এ᭄往昔7 小时前
【QT】--常用控件(QWidget的核心属性)
服务器·开发语言·qt
玖玥拾7 小时前
LeetCode 290 单词规律
算法·leetcode·哈希算法·散列表
吴声子夜歌7 小时前
Guava——反射
java·开发语言·guava
lingran__7 小时前
Git 完全指南(三):远程仓库与标签管理
开发语言·git·gitee·ssh·团队协作·远程仓库·分布式版本控制
liliangcsdn7 小时前
多空价差收益序列汇总统计的分析和代码示例
人工智能·算法·机器学习
for_ever_love__7 小时前
python爬虫: 数据解析
开发语言·爬虫·python·xpath
啥都想学点的研究生7 小时前
一篇文章讲清楚:Adaboost算法与GBDT
人工智能·算法
学习星球7 小时前
AI 一句话生成 3D 游戏世界:腾讯 HY-World 2.0 开源深度解析与本地实战
开发语言·人工智能·游戏·3d·ai·课程设计·ai编程
名字还没想好☜7 小时前
Go 用 -race 抓数据竞争:一个偶发崩溃的排查、原理与修复
开发语言·后端·golang·go
旖旎夜光7 小时前
LeetCode 525:连续数组(前缀和) —— 题解
数据结构·c++·算法·leetcode·前缀和