Modul:Vorlage:Standardfarbe

Vorlagenprogrammierung Diskussionen Lua Unterseiten
Modul Deutsch English

Modul: Dokumentation

Diese Seite enthält Code in der Programmiersprache Lua. Einbindungszahl Cirrus


-- module table
local Standardfarbe = {
	config = {
		colorJSON = "Vorlage:Standardfarbe/colors.json",
		errorOutput = "<span class=\"error\">Fehler in [[Vorlage:Standardfarbe]]: %s</span>"
	},
	colors = { },
	serial = ""
}
local Failsafe = Standardfarbe

Standardfarbe.formatError = function ( message )
	-- format error message
	-- parameters:
	--   message: (string) raw error message
	-- returns:
	--   (string) formatted error message
	return string.format( Standardfarbe.config.errorOutput, message )
end

Standardfarbe.iterateColorGroup = function ( group, key )
	-- return color value
	-- parameters:
	--   group: (table) color group
	--   key:   (string) key to find
	-- returns:
	--   (bool, table) color block found, color block table
	
	for _, block in ipairs( group ) do
		for _, k in ipairs( block.keys ) do
			if k == key then
				return true, block.colors
			end
		end
	end
	
	return false, {}
end

Standardfarbe.loadColorsJson = function ( page )
	-- load colors from json page
	-- parameters:
	--   page: (string) path to color json page
	-- returns:
	--   (bool) true or false by success/fault
	
	local success, c = pcall( mw.loadJsonData, page )
	if type( c ) == "table" then
		Standardfarbe.colors = c
	end
	
	return success
end

Standardfarbe.getColor = function ( group, key, mode, ucase )
	-- return color value
	-- parameters:
	--   group:  (string) color group
	--   key:    (string) color key
	--   mode:   (string) display mode
	--   ucase:  (bool)   uppercase color codes
	-- returns:
	--   (string) hex color code
	
	local blockColors -- colors in block
	local groupColors -- colors in group
	local found       -- color block found
	local color       -- color value

	-- load defined colors
	if Standardfarbe.loadColorsJson( Standardfarbe.config.colorJSON ) == false then
		return ""
	end

	-- find color group
	groupColors = Standardfarbe.colors[group]
	if type( groupColors ) ~= "table" then
		return Standardfarbe.formatError( "color group not found" )
	end
	
	-- find color block with key
	found, blockColors = Standardfarbe.iterateColorGroup( groupColors, key )
	if found == false then
		return Standardfarbe.formatError( "color key not found" )
	end
	
	-- find color in block
	if mode == "" then
		mode = "light"
	end
	color = blockColors[mode]
	if color == nil then
		color = blockColors["light"]
		if color == nil then
			return Standardfarbe.formatError( "no matching color mode" )
		end
	end
	
	-- uppercase, if wanted
	if ucase then
		color = string.upper( color )
	end
	
	-- return color value
	return color
end

-- export table
local p = { }

p.outputDefinedColors = function ( frame )
	-- return wikitable with defined colors
	-- parameters:
	--   frame: (table) wiki environment frame
	-- returns:
	--   (string) wikitable code
	
	local wtbmain -- main wikitable
	local wtbhead -- wikitable head
	local wtbrow  -- color row
	local wtbcol  -- color cell
	local keys    -- color keys
	local count   -- color count

	-- load defined colors
	if Standardfarbe.loadColorsJson( Standardfarbe.config.colorJSON ) == false then
		return ""
	end
	
	-- create wikitable
	wtbmain = mw.html.create( "table" ):addClass( "wikitable" )
	
	-- create headline
	wtbhead = mw.html.create( "tr" )
		:node( mw.html.create( "th" ):wikitext( "Gruppe" ):attr( "rowspan", 2 ) )
		:node( mw.html.create( "th" ):wikitext( "Schlüssel" ):attr( "rowspan", 2 ) )
		:node( mw.html.create( "th" ):wikitext( "Farbe/Modus" ):attr( "colspan", 4 ) )
	wtbmain:node( wtbhead )
	wtbhead = mw.html.create( "tr" )
		:node( mw.html.create( "th" ):wikitext( "Hell" ):attr( "colspan", 2 ) )
		:node( mw.html.create( "th" ):wikitext( "Dunkel" ):attr( "colspan", 2 ) )
	wtbmain:node( wtbhead )
	
	-- create lines for colors in groups
	for group, groupColors in pairs( Standardfarbe.colors ) do
		count = 0
		for _, _ in ipairs( groupColors ) do
			count = count + 1	
		end
		
		for i, color in ipairs( groupColors ) do
			wtbrow = mw.html.create( "tr" )

			-- create first cell that has the group name
			if i == 1 then
				wtbrow:node( mw.html.create( "td" ):attr( "rowspan", count ):wikitext( group ) )
			end
			
			-- color keys
			keys = ""
			for _, key in ipairs( color.keys ) do
				code = mw.html.create( "code" ):wikitext( key )
				keys = keys .. tostring( code ) .. " "
			end
			wtbrow:node( mw.html.create( "td" ):wikitext( keys ) )
			
			-- colors
			wtbcol = mw.html.create( "td" ):wikitext( "#" .. color.colors["light"] )
			if color.colors["dark"] == nil then
				wtbcol:attr( "colspan", 3 )
				wtbcol:css( "text-align", "center" )
			end
			wtbrow:node( wtbcol )
			wtbrow:node( mw.html.create( "td" ):css( "background-color", "#" .. color.colors["light"] ):wikitext("&nbsp;") )
			
			if color.colors["dark"] ~= nil then
				wtbrow:node( mw.html.create( "td" ):wikitext( "#" .. color.colors["dark"] ) )
				wtbrow:node( mw.html.create( "td" ):css( "background-color", "#" .. color.colors["dark"] ):wikitext("&nbsp;") )
			end
			
			-- add current row to table
			wtbmain:node( wtbrow )
		end
	end
	
	-- return wikitable
	return tostring( wtbmain )
end

p.f = function ( frame )
	-- return color value, access from templates
	-- parameters:
	--   frame: (table) wiki environment frame
	-- returns:
	--   (string) hex color code
	
	local args	 -- arguments of template call
	local group  -- color group
	local key    -- color key
	local mode   -- display mode
	local ucase  -- uppercase color values
	local nowiki -- enclose in nowiki tags
	
	-- get template parameters
	args   = frame:getParent().args
	group  = args[1] or ""
	key    = args[2] or ""
	mode   = args["mode"] or "light"
	ucase  = ( args["ucase"] == "1" )
	nowiki = ( args["nowiki"] == "1" )
	
	-- call main and return
	local success, r = pcall( Standardfarbe.getColor, group, key, mode, ucase )
	if not success then
		return Standardfarbe.formatError( "color call failed" )
	end
	
	-- apply nowiki
	if nowiki then
		r = frame:extensionTag( "nowiki", r )
	end
	
	-- return
	return r
end

setmetatable( p,  { __call = function ( func, ... )
                                 setmetatable( p, nil )
                                 return Failsafe
                             end } )

return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.