2023年政策修订增补工作正在进行中,欢迎参与!
  • Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

Module:Sandbox/DGCK81LNN/颜色描述

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索
Template-info.svg 模块文档  [查看] [编辑] [历史] [刷新]

描述传入的颜色。

颜色描述参考资料:http://www.procato.com/rgb+index/

2020年3月31日 (二) 10:05 (CST) 更新:现在颜色描述更详细了!

支持的颜色格式:

  • HEX色号(#ace#abcdef,不区分大小写)
  • RGB色号(rgb(123,45,67)rgb( 123 , 45 , 67 ),括号内空格随意,“rgb”必须小写)
  • HSL色号(hsl(123,45%,67%)hsl( 123% , 45% , 67% ),括号内空格随意,“hsl”必须小写)
  • RGBA、HSLA色号(rgba(123,45,67,89%)hsla( 123% , 45% , 67% , 89% ),Alpha值会被忽略)
  • CSS颜色名(aliceblue,必须小写)

User:DGCK81LNN/color block使用了此模块。栗子:

{{User:DGCK81LNN/color block|#ace}}

亮蔚蓝


{{User:DGCK81LNN/color block|#abcdef|rgb(123,45,67)}}

¤亮蔚蓝、暗猩红


{{User:DGCK81LNN/color block|hsl(123,45%,67%)|aliceblue}}

¤亮绿、蓝白


由于一些奇怪的特性,colour模板输出的颜色值不能直接传进来。

{{User:DGCK81LNN/color block|{{ColorOps|+50|#339988}}}}

这样会输出: Lua错误 第5行:attempt to index a nil value

--DGCK81LNN讨论

local p = {}
local Color = require('Module:color')
function p.main(frame)
	-- 第一步 hex -> rgb
	local hex = string.gsub(Color.create(frame.args[1]):toString('hex'),"#","",1)
	local rgb = {}
	rgb[1] = tonumber(string.sub(hex, 1, 2), 16) / 255
	rgb[2] = tonumber(string.sub(hex, 3, 4), 16) / 255
	rgb[3] = tonumber(string.sub(hex, 5, 6), 16) / 255
	-- 第二步 rgb -> hsv
	local h = 0
	local s = 0
	local v = rgb[1]
	local rgbmaxn = 1
	local rgbmin = v
	if rgb[2] > v then
		v = rgb[2]
		rgbmaxn = 2
	elseif rgb[2] < rgbmin then
		rgbmin = rgb[2]
	end
	if rgb[3] > v then
		v = rgb[3]
		rgbmaxn = 3
	elseif rgb[3] < rgbmin then
		rgbmin = rgb[3]
	end
	if v > 0 then
		s = (v - rgbmin) / v
		if s > 0 then
			if rgbmaxn == 1 then
				h = (rgb[2] - rgb[3]) / (v - rgbmin) * 60
			elseif rgbmaxn == 2 then
				h = (rgb[3] - rgb[1]) / (v - rgbmin) * 60 + 120
			else
				h = (rgb[1] - rgb[2]) / (v - rgbmin) * 60 + 240
			end
			if h < 0 then
				h = h + 360
			end
		end
	end
	-- 第三步 描述色调
	-- ...
	-- 等等,
	local commonColors = {
		{0.4, 0.8, 1, "天依蓝"},
		{0.22352941176471, 0.77254901960784, 0.73333333333333, "初音绿"},
	}
	for _, color in ipairs(commonColors) do
		if math.abs(color[1] - rgb[1]) < 0.01 and math.abs(color[2] - rgb[2]) < 0.01 and math.abs(color[2] - rgb[2]) < 0.01 then
			return color[4]
		end
	end
	-- 刚刚什么也没有发生
	-- 现在来描述色调
	local h_descs_detailed = {"红", "绯红", "朱红", "橘",
		"橙", "藤黄", "琥珀", "金黄",
		"黄", "苹果绿", "石灰绿", "春芽绿",
		"荨麻酒绿", "开心果绿", "柠檬绿", "草绿",
		"绿", "翠绿", "孔雀绿", "海绿",
		"春绿", "海蓝", "玉绿", "瓷蓝",
		"青", "北极蓝", "湛蓝", "矢车菊蓝",
		"蔚蓝", "钴蓝", "宝蓝", "酞青蓝",
		"蓝", "波斯蓝", "靛蓝", "品蓝",
		"鸢尾紫", "紫", "桑葚紫", "杜鹃花红",
		"品红", "兰花紫", "紫红", "樱桃红",
		"玫瑰红", "秋海棠粉", "猩红", "珊瑚红"}
	local h_descs_basic = {"红", "橙", "黄", "绿", "绿", "绿", "青", "蓝", "蓝", "紫", "粉", "红"}
	h_descs_detailed[49] = h_descs_detailed[1]
	h_descs_basic[13] = h_descs_basic[1]
	local h_desc_detailed = h_descs_detailed[math.floor(h / 7.5 + 0.5) + 1]
	local h_desc_basic = h_descs_basic[math.floor(h / 30 + 0.5) + 1]
	-- 第四步 描述饱和度和明度
	local descs = {
		{h_desc_basic .. "黑", h_desc_basic .. "黑", h_desc_basic .. "黑", h_desc_basic .. "黑", h_desc_basic .. "黑"},
		{"深" .. h_desc_basic .. "灰", "深灰" .. h_desc_detailed, "深" .. h_desc_detailed, "深" .. h_desc_detailed, "深" .. h_desc_detailed},
		{"中" .. h_desc_basic .. "灰", "灰" .. h_desc_detailed, "中" .. h_desc_detailed, "暗" .. h_desc_detailed, "暗" .. h_desc_detailed},
		{"亮" .. h_desc_basic .. "灰", "亮" .. h_desc_detailed, "浅" .. h_desc_detailed, "鲜" .. h_desc_detailed, "鲜" .. h_desc_detailed},
		{h_desc_basic .. "白", "亮" .. h_desc_detailed, "浅" .. h_desc_detailed, "鲜" .. h_desc_detailed, "纯" .. h_desc_detailed}
	}
	local desc
	local descs_grayscale = {"黑", "深灰", "中灰", "亮灰", "白"}
	if s < 0.05 or v < 0.05 then
		desc = descs_grayscale[math.ceil(v * 5)]
	else
		desc = descs[math.ceil(v * 5)][math.ceil(s * 5)]
	end
	--mw.logObject({rgb={r,g,b},hsv={h,s,v},h_desc_detailed=h_desc_detailed,h_desc_basic=h_desc_basic,descs=descs,desc=desc})
	return desc
end
return p