跳转到内容

模組:Infobox color

维基百科,自由的百科全书

这是本页的一个历史版本,由Wj654cj86留言 | 贡献2016年7月20日 (三) 14:28编辑。这可能和当前版本存在着巨大的差异。

--程式碼沒問題,但是維基百科lua出現不明原因錯誤,rgb轉hsv會出錯
local p = {}
function p.color(frame)
	function round(num)
		return math.floor(num + 0.5)
	end
	function roundarr(arr)
		local i = 1
		while arr[i] ~= nil do
			arr[i] = round(arr[i])
			i = i + 1
		end
		return arr
	end
 
	local color = {}
	color.setdata = {
		function(R, G, B)
			color.r = R
			color.g = G
			color.b = B
		end
		,
		function(C, M, Y)
			color.setdata[1](255 - C, 255 - M, 255 - Y)
		end
		,
		function(C, M, Y, K)
			function CMYKtoCMY(color)
				return color * (1 - K / 255) + K
			end
			color.setdata[2](CMYKtoCMY(C), CMYKtoCMY(M), CMYKtoCMY(Y))
		end
		,
		function(H, S, V)
			function SVFtoPQT(F)
				return V * 2.55 * (100 - F * S) / 100
			end
 
			H = H % 360
			local Hi = math.floor(H / 60)
			local F = H % 60 / 60
			local W = SVFtoPQT(0)
			local P = SVFtoPQT(1)
			local Q = SVFtoPQT(F)
			local T = SVFtoPQT(1 - F)
			if Hi == 0 then
				color.setdata[1](W, T, P)
			elseif Hi == 1 then
				color.setdata[1](Q, W, P)
			elseif Hi == 2 then
				color.setdata[1](P, W, T)
			elseif Hi == 3 then
				color.setdata[1](P, Q, W)
			elseif Hi == 4 then
				color.setdata[1](T, P, W)
			else
				color.setdata[1](W, P, Q)
			end
		end
		,
		function(HEX)
			HEX = string.format('%06x', '0X' .. HEX)
			function HEXtoDEC(h)
				return ('0X' .. h) * 1
			end
			color.setdata[1](HEXtoDEC(string.sub(HEX,1,2)), HEXtoDEC(string.sub(HEX,3,4)), HEXtoDEC(string.sub(HEX,5,6)))
		end		
	}
	color.changedata = {
		function()
			return {color.r, color.g, color.b}
		end
		,
		function()
			return {255 - color.r, 255 - color.g, 255 - color.b}
		end
		,
		function()
			local data = color.changedata[2]()
			local K = math.min(unpack(data))
			function CMYtoCMYK(color)
				return (data[color + 1] - K) / (255 - K) * 255
			end
			return (K == 255) and {0, 0, 0, 255} or {CMYtoCMYK(0), CMYtoCMYK(1), CMYtoCMYK(2), K}
		end
		,
		function()
			local data = color.changedata[1]()
			local max = math.max(unpack(data))
			local min = math.min(unpack(data))
			local H
			function RGBtoHSV(color)
				return ((data[(color + 1) % 3 + 1] - data[(color + 2) % 3 + 1]) / (max - min) * 60 + 120 * color + 360) % 360
			end
			if max == min then
				H = 0
			elseif max == color.r * 1 then
				H = RGBtoHSV(0)
			elseif max == color.g * 1 then
				H = RGBtoHSV(1)
			else
				H = RGBtoHSV(2)
			end
			return {H, (max == 0) and 0 or ((max - min) / max * 100), max / 2.55}
		end
		,
		function()
			return string.format('%02X%02X%02X',unpack(color.changedata[1]()))
		end
	}
	local args = frame.args
	if args[1] == nil then
 		args = frame:getParent().args
	end
	local arr = {args[1] or 255, args[2] or 255, args[3] or 255, args[4] or 0}
	local mod = frame.args.mod or frame:getParent().args.mod or 'rgb'
	local modint = string.find(mod, 'rgb') and 1 or string.find(mod, 'cmyk') and 3 or string.find(mod, 'hsv') and 4 or string.find(mod, 'hex') and 5 or 1
	color.setdata[modint](unpack(arr))
	local hex = color.changedata[5]()
	local textcolor = frame.args.textcolor or frame:getParent().args.textcolor or '#000000'
	local title = frame.args.title or frame:getParent().args.title or '(無標題)'
	local source = frame.args.source or frame:getParent().args.source or ''
	local r, g, b = unpack(roundarr(color.changedata[1]()))
	local c, m, y, k = unpack(roundarr(color.changedata[3]()))
	local h, s, v = unpack(roundarr(color.changedata[4]()))
	local ss = '{|class="toccolours" style="text-align:left;float:right;width:280px;clear:right;margin:0em 0em 1em 1em;"\n'
	ss = ss .. '|-\n!colspan=3 style="text-align:center;font-size:150%;background-color:#' .. hex .. ';color:' .. textcolor .. ';"|' .. title .. '\n'
	ss = ss .. '|-\n!colspan=2|[[網頁顏色]]||#' .. hex  .. '\n'
	ss = ss .. '|-\n![[RGB]]<sup>N</sup>||([[紅色|r]], [[綠色|g]], [[藍色|b]])||(' .. r .. ', ' .. g .. ', ' .. b .. ')\n'
	ss = ss .. '|-\n!nowrap|[[CMYK]]<sup>N</sup>||nowrap|([[青色|c]], [[品紅色|m]], [[黃色|y]], [[黑色|k]])||nowrap|(' .. c .. ', ' .. m .. ', ' .. y .. ', ' .. k .. ')\n'
	ss = ss .. '|-\n![[HSV色彩空間|HSV]]||([[色相|h]], [[色度|s]], [[明度|v]])||(' .. h .. '&deg;, ' .. s .. '%, ' .. v .. '%)\n'
	if source ~= '' then
		ss = ss .. '|-\n!colspan=2|資料來源||' .. source  .. '\n'
	end
	ss = ss .. '|-\n!align=center colspan=3|<sup>N</sup>:代表[[值域]]介於0~255之間\n'
	ss = ss .. '|-\n!align=center colspan=3|<div class="notice metadata NavFrame collapsed" id="spoiler" style="border: 0px;"><div class="NavHead" style="border: 0px; background: #E4E4E4">[[File:Nuvola_apps_kcoloredit.svg|20px]] \'\'\'注意事項\'\'\'</div><div class="NavContent"><ol type=1 style="margin-left: 2em;"><li>本條目的顏色值可能只是參考,並非共通標準。<li>本條目包含了使用[[顏色]]標示的內容,可能會因為不同的閱讀環境而產生差異。參見[[網頁親和力]]。[[色盲]]人士或黑白[[螢幕]]使用者需要非色盲人士或使用彩色螢幕來幫助,或是使用無障礙軟體幫助。</ol></div></div>\n'
	ss = ss .. '|}'
	return ss .. '\n'
end
return p