置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

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


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

User:DGCK81LNN/color block


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

User:DGCK81LNN/color block


由于一些奇怪的特性,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