Module:Weather/sandbox
![]() | This is the module sandbox page for Module:Weather (diff). |
This module can be used to display temperatures in a table. It is under development and is intended to be efficient so a page can hold many tables.
- Input numbers must use a hyphen if negative (Unicode minus "−" would give an error).
- All displayed numbers (including inputs) use Unicode minus if negative.
- The input consists of 13 values, separated by any number of spaces.
- Each of the 13 values must be a number. Any invalid value results in the corresponding cell being blank with no error message or tracking category.
The following functions are available:
Function | Input | Output |
---|---|---|
CtoF |
°C | °C (°F) |
FfromC |
°C | °F (°C) |
CfromF |
°F | °C (°F) |
FtoC |
°F | °F (°C) |
The following templates use the module:
An example using the above templates and the module is at:
The output can be examined by entering the following example at Special:ExpandTemplates. The output from each of the following lines is identical.
{{#invoke:weather|CtoF|-10 -5 0 5 10 15 20 25 30 35 40 45 50}} ---- {{#invoke:weather|CfromF|14 23 32 41 50 59 68 77 86 95 104 113 122}}
Optional parameter
|palette=
name- Where name is the built-in name of a palette:
- cool (default)
- cool2 (modified)
- cool2avg (modified and intended for average temperatures)
- Where name is the built-in name of a palette:
Testing
The function show
provides a way to test the color schemes. It generates a graph of how the red, green, and blue portions of the color vary with temperature, and a table of the full range of temperatures in °C.
Optional parameters
- Two unnamed parameters may be entered to specify the first and last Celsius temperatures (
|-90|59
by default). - The palette can be specified with
|palette=
name as above.
The following codes produce the same result:
{{#invoke:weather|show}}
{{#invoke:weather|show|palette=cool}}
−90 | −89 | −88 | −87 | −86 | −85 | −84 | −83 | −82 | −81 |
−80 | −79 | −78 | −77 | −76 | −75 | −74 | −73 | −72 | −71 |
−70 | −69 | −68 | −67 | −66 | −65 | −64 | −63 | −62 | −61 |
−60 | −59 | −58 | −57 | −56 | −55 | −54 | −53 | −52 | −51 |
−50 | −49 | −48 | −47 | −46 | −45 | −44 | −43 | −42 | −41 |
−40 | −39 | −38 | −37 | −36 | −35 | −34 | −33 | −32 | −31 |
−30 | −29 | −28 | −27 | −26 | −25 | −24 | −23 | −22 | −21 |
−20 | −19 | −18 | −17 | −16 | −15 | −14 | −13 | −12 | −11 |
−10 | −9 | −8 | −7 | −6 | −5 | −4 | −3 | −2 | −1 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 |
![]() | This graph was using the legacy Graph extension, which is no longer supported. It needs to be converted to the new Chart extension. |
![]() | This graph was using the legacy Graph extension, which is no longer supported. It needs to be converted to the new Chart extension. |
![]() | This graph was using the legacy Graph extension, which is no longer supported. It needs to be converted to the new Chart extension. |
Modified palette:
{{#invoke:weather|show|palette=cool2}}
−90 | −89 | −88 | −87 | −86 | −85 | −84 | −83 | −82 | −81 |
−80 | −79 | −78 | −77 | −76 | −75 | −74 | −73 | −72 | −71 |
−70 | −69 | −68 | −67 | −66 | −65 | −64 | −63 | −62 | −61 |
−60 | −59 | −58 | −57 | −56 | −55 | −54 | −53 | −52 | −51 |
−50 | −49 | −48 | −47 | −46 | −45 | −44 | −43 | −42 | −41 |
−40 | −39 | −38 | −37 | −36 | −35 | −34 | −33 | −32 | −31 |
−30 | −29 | −28 | −27 | −26 | −25 | −24 | −23 | −22 | −21 |
−20 | −19 | −18 | −17 | −16 | −15 | −14 | −13 | −12 | −11 |
−10 | −9 | −8 | −7 | −6 | −5 | −4 | −3 | −2 | −1 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 |
![]() | This graph was using the legacy Graph extension, which is no longer supported. It needs to be converted to the new Chart extension. |
![]() | This graph was using the legacy Graph extension, which is no longer supported. It needs to be converted to the new Chart extension. |
![]() | This graph was using the legacy Graph extension, which is no longer supported. It needs to be converted to the new Chart extension. |
This modified palette is intended for average temperatures. It results in good colors between the extreme highest average monthly temperature of +39 °C (102 °F) in Death Valley, California, and −68 °C (−90 °F), the extreme lowest average monthly temperature at Vostok Station, at a high elevation on the Antarctic ice sheet. These are the highest and lowest known average temperatures recorded on Earth, not to be confused with the highest and lowest records, which are quite a bit hotter and colder.
{{#invoke:weather|show|palette=cool2avg}}
−90 | −89 | −88 | −87 | −86 | −85 | −84 | −83 | −82 | −81 |
−80 | −79 | −78 | −77 | −76 | −75 | −74 | −73 | −72 | −71 |
−70 | −69 | −68 | −67 | −66 | −65 | −64 | −63 | −62 | −61 |
−60 | −59 | −58 | −57 | −56 | −55 | −54 | −53 | −52 | −51 |
−50 | −49 | −48 | −47 | −46 | −45 | −44 | −43 | −42 | −41 |
−40 | −39 | −38 | −37 | −36 | −35 | −34 | −33 | −32 | −31 |
−30 | −29 | −28 | −27 | −26 | −25 | −24 | −23 | −22 | −21 |
−20 | −19 | −18 | −17 | −16 | −15 | −14 | −13 | −12 | −11 |
−10 | −9 | −8 | −7 | −6 | −5 | −4 | −3 | −2 | −1 |
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 |
![]() | This graph was using the legacy Graph extension, which is no longer supported. It needs to be converted to the new Chart extension. |
![]() | This graph was using the legacy Graph extension, which is no longer supported. It needs to be converted to the new Chart extension. |
![]() | This graph was using the legacy Graph extension, which is no longer supported. It needs to be converted to the new Chart extension. |
local p = {}
require('Module:No globals')
local degree = "°" -- used by addUnitNames()
local minus = "−" -- used by makeRow() and makeTable()
local thinSpace = mw.ustring.char(0x2009) -- used by makeCell()
local length, inputUnit, outputUnit, palette, show, cellFormat, precision, decimals
-- String-handling function
local function checkForString(var)
var = tostring(var)
if var == "" then
return nil
else
return var
end
end
-- Error message handling
local message = ""
local function addMessage(newMessage)
if show then
if checkForString(message) then
message = message .. " " .. newMessage
else
message = "Notices: " .. newMessage
end
end
end
-- Input and output parameters
local function getFormat (frame)
local inputParameter = frame.args.input
local outputParameter = frame.args.output
if inputParameter == nil then
error('Please provide the number of values and a unit in the input parameter')
else
-- Find as many as two digits in the input parameter.
length = tonumber(string.match(inputParameter, "(%d%d?)")) or 13 and addMessage('getFormat has not found a length value in the input parameter; length defaults to "13"')
-- Find C or F, but not both
inputUnit = (string.find(inputParameter, "C") and string.find(inputParameter, "F") ) and error("Input unit must be either C (Celsius) or F (Fahrenheit)") or string.match(inputParameter, "([CF])") or error("Please provide an input unit in the input parameter: F for Fahrenheit or C for Celsius", 0)
-- Make sure nothing except C, F, numbers, or spaces is in the input parameter.
if string.find(inputParameter, "[^CF%d%s]") then
addMessage('There are extraneous characters in the <span style="background-color: #EEE; font-family: monospace;">output</span> parameter.')
end
end
if outputParameter == nil then
addMessage('No output format has been provided in the <span style="background-color: #EEE; font-family: monospace;">output</span> parameter, so default values will be used.') -- Since there are default values, the module will still generate output with an empty output parameter.
else
cellFormat = {}
local n = 1
for unit in outputParameter:gmatch("[CF]") do
cellFormat[n] = unit
n = n + 1
if n > 2 then
break
end
end
local function setFormat(key, formatVariable, formatValue1, formatValue2)
if string.find(outputParameter, key) then
cellFormat[formatVariable] = formatValue1
else
cellFormat[formatVariable] = formatValue2
end
end
if cellFormat[1] then
cellFormat.first = cellFormat[1]
else
error('C or F not found in output parameter')
end
if cellFormat[2] == nil then
cellFormat["convertUnits"] = "no"
else
if cellFormat[2] == cellFormat[1] then
error('There should not be two of the same unit name in the output parameter.')
else
cellFormat["convertUnits"] = "yes"
end
end
setFormat("unit", "unitNames", "yes", "no")
setFormat("no ?color", "color", "no", "yes")
setFormat("sort", "sortable", "yes", "no")
setFormat("full ?size", "smallFont", "no", "yes")
setFormat("no ?brackets", "brackets", "no", "yes")
setFormat("round", "decimals", "0", "")
if string.find(outputParameter, "line break") then
cellFormat["lineBreak"] = "yes"
elseif string.find(outputParameter, "one line") then
cellFormat["lineBreak"] = "no"
else
cellFormat["lineBreak"] = "auto"
end
if string.find(outputParameter, "one line") and string.find(outputParameter, "line break") then
error('Place either "one line" or "line break" in the output parameter, not both')
end
end
if frame.args.palette == nil then
palette = "cool2avg"
else
palette = frame.args.palette
end
if frame.args.messages == "show" then
show = true
else
show = false
end
return length, inputUnit, outputUnit, cellFormat, show
end
-- Math functions
local function round(value, decimals)
value = tonumber(value)
if type(value) == "number" then
return string.format("%." .. decimals .. "f", value)
else
addMessage("Format was asked to operate on " .. tostring(value) .. ", which cannot be converted to a number.", 2)
return ""
end
end
local function convert(value, decimals, unit) -- Unit is the unit being converted from. It defaults to inputUnit.
if not unit then
unit = inputUnit
end
if tonumber(value) then
local value = tonumber(value)
if unit == "C" then
addMessage(value .. " " .. degree .. unit .. " was converted.")
return round(value * 9/5 + 32, decimals)
elseif unit == "F" then
addMessage(value .. " " .. degree .. unit .. " was converted.")
return round((value - 32) * 5/9, decimals)
else
error("Input unit not recognized", 2)
end
else
-- to avoid concatenation errors
return ""
end
end
-- Input parsing
local function makeArray(parameter, array, frame)
getFormat(frame)
local array = {}
local hasDecimals = false
local numbers = mw.text.split(parameter, "%s+")
if not numbers[length] then
addMessage('There are not ' .. length .. ' values in the ' .. parameter .. ' parameter.')
end
for i, number in ipairs(numbers) do
if number:match("^%-?%d%d?%d?.?(%d?)$") then
hasDecimals = true
else
error('The number "' .. number .. '" does not fit the expected pattern.')
end
table.insert(array, number)
end
-- If a value with a decimal point was found somewhere, set precision to 1.
-- The possibility of two decimal points will be excluded, at least for now.
if hasDecimals then
precision = "1"
else
precision = "0"
end
return array, precision
end
local a, b, c
local function makeArrays(frame)
getFormat(frame)
local parameter_a = frame.args.a
local parameter_b = frame.args.b
local parameter_c = frame.args.c
if parameter_a then
a = makeArray(parameter_a, a, frame)
else
error('Please provide a set of numbers in parameter a')
end
if parameter_b then
b = makeArray(parameter_b, b, frame)
else
addMessage('There is no content in parameter <span style="background-color: #EEE; font-family: monospace;">b</span>.')
end
if parameter_c then
c = makeArray(parameter_c, c, frame)
else
addMessage('There is no content in parameter <span style="background-color: #EEE; font-family: monospace;">c</span>.')
end
return a, b, c
end
-- Color generation
p.palettes = {
-- The first three arrays in each palette defines background color using a table of four numbers,
-- say { 11, 22, 33, 44 } (values in °C).
-- That means the color is 0 below 11 and above 44, and is 255 from 22 to 33.
-- The color rises from 0 to 255 between 11 and 22, and falls between 33 and 44.
cool = {
{ -42.75, 4.47, 41.5, 60 },
{ -42.75, 4.47, 4.5, 41.5 },
{ -90 , -42.78, 4.5, 23 },
white = { -23.3, 37.8 },
},
cool2 = {
{ -42.75, 4.5 , 41.5, 56 },
{ -42.75, 4.5 , 4.5, 41.5 },
{ -90 , -42.78, 4.5, 23 },
white = { -23.3, 35 },
},
cool2avg = {
{ -38, 4.5, 25 , 45 },
{ -38, 4.5, 4.5, 30 },
{ -70, -38 , 4.5, 23 },
white = { -23.3, 25 },
},
}
local function temperatureColor(palette, value, outRGB)
--[[ Return style for a table cell based on the given value which
should be a temperature in °C. ]]
local backgroundColor, textColor
value = tonumber(value)
if value == nil then
backgroundColor, textColor = 'FFF', '000'
addMessage('Value supplied to <span style="background-color: #EEE; font-family: monospace;">temperatureColor</span> is not recognized.')
else
local min, max = unpack(palette.white or { -23, 35 })
if value < min or value >= max then
textColor = 'FFF'
else
textColor = '' -- This assumes that black text color is the default for most readers.
end
local backgroundRGB = outRGB or {}
for i, v in ipairs(palette) do
local a, b, c, d = unpack(v)
if value <= a then
backgroundRGB[i] = 0
elseif value < b then
backgroundRGB[i] = (value - a) * 255 / (b - a)
elseif value <= c then
backgroundRGB[i] = 255
elseif value < d then
backgroundRGB[i] = 255 - ( (value - c) * 255 / (d - c) )
else
backgroundRGB[i] = 0
end
end
backgroundColor = string.format('%02X%02X%02X', backgroundRGB[1], backgroundRGB[2], backgroundRGB[3])
end
if textColor == "" then
return backgroundColor
else
return backgroundColor, textColor
end
end
local function colorCSS(backgroundColor, textColor)
if backgroundColor and textColor then
return 'background: #' .. backgroundColor .. '; color: #' .. textColor .. ';'
elseif backgroundColor then
return 'background: #' .. backgroundColor .. ';'
else
return ''
end
end
local function temperatureColorCSS(palette, value, outRGB)
return colorCSS(temperatureColor(palette, value, outRGB))
end
local function temperatureCSS(value, unit, palette)
local palette = p.palettes[palette] or p.palettes.cool
local value = tonumber(value)
if value == nil then
error('The function <span style="background-color: #EEE; font-family: monospace;">temperatureCSS</span> is receiving a nil value')
else
if unit == 'C' then
return colorCSS(temperatureColor(palette, value))
elseif unit == 'F' then
return colorCSS(temperatureColor(palette, convert(value, decimals, 'F')))
else
unitError(unit or "nil")
end
end
end
local function styleAttribute(palette, value, outRGB)
local fontSize = "font-size: 85%;"
local color = temperatureColorCSS(palette, value, outRGB)
return 'style=\"' .. color .. ' ' .. fontSize .. '\"'
end
local style_attribute = styleAttribute
--[=[
Used by {{Average temperature table/row/C/sandbox}},
{{Average temperature table/row/F/sandbox}},
{{Average temperature table/row/C/sandbox}},
{{Template:Avg temp row F/sandbox2}},
{{Template:Avg temp row C/sandbox2}}.
]=]
function p.temperatureStyle(frame)
local palette = p.palettes[frame.args.palette] or p.palettes.cool
local unit = frame.args.unit or 'C'
local value = tonumber(frame.args[1])
if unit == 'C' then
return styleAttribute(palette, value)
elseif unit == 'F' then
return styleAttribute(palette, convert(value, 1, 'F'))
else
unitError(unit)
end
end
p.temperature_style = p.temperatureStyle
--[[ ==== Cell, row, table generation ==== ]]
local outputFormats = {
high_low_average_F =
{ first = "F",
convertUnits = "yes",
unitNames = "no",
color = "yes",
smallFont = "yes",
sortable = "yes",
decimals = "0",
brackets = "yes",
lineBreak = "auto", },
high_low_average_C =
{ first = "C",
convertUnits = "yes",
unitNames = "no",
color = "yes",
smallFont = "yes",
sortable = "yes",
decimals = "0",
brackets = "yes",
lineBreak = "auto", },
high_low_F =
{ first = "F",
convertUnits = "yes",
unitNames = "no",
color = "no",
smallFont = "yes",
sortable = "no",
decimals = "",
brackets = "yes",
lineBreak = "auto", },
high_low_C =
{ first = "C",
convertUnits = "yes",
unitNames = "no",
color = "no",
smallFont = "yes",
sortable = "no",
decimals = "0",
brackets = "yes",
lineBreak = "auto", },
average_F =
{ first = "F",
convertUnits = "yes",
unitNames = "no",
color = "yes",
smallFont = "yes",
sortable = "no",
decimals = "0",
brackets = "yes",
lineBreak = "auto", },
average_C =
{ first = "C",
convertUnits = "yes",
unitNames = "no",
color = "yes",
smallFont = "yes",
sortable = "no",
decimals = "0",
brackets = "yes",
lineBreak = "auto", },
}
local outputFormat
local function addUnitNames(value, yesOrNo, unit)
if not unit then unit = inputUnit end
-- Don't add a unit name to an empty string
value = yesOrNo == "yes" and checkForString(value) and value .. " " .. degree .. unit or value
return value
end
local function ifYes(parameter, realization1, realization2)
local result
if realization1 then
if realization2 then
result = parameter == "yes" and { realization1, realization2 } or { "", "" }
else
result = parameter == "yes" and realization1 or ""
end
else
result = ""
addMessage('<span style="background-color: #EEE; font-family: monospace;">ifYes</span> needs at least one realization')
end
return result
end
local function makeCell(outputFormat, a, b, c)
local cell, cellContent = "", ""
local colorCSS, otherCSS, titleAttribute, sortkey, attributeSeparator, convertedUnitsSeparator = "", "", "", "", "", "", ""
local styleAttribute, highLowSeparator, brackets, values, convertedUnits = {"", ""}, {"", ""}, {"", ""}, {"", ""}, {"", ""}
-- Distinguish styleAttribute variable from styleAttribute function above.
decimals = ( tonumber(outputFormat.decimals) and outputFormat.decimals ) or precision
--[[ Precision is the number of decimals in the first number of the last array.
This may be a problem for data from Weatherbase,
which seems to inappropriately remove .0 from numbers that have it. ]]
if tonumber(b) and tonumber(a) then
values, highLowSeparator = { round(a, decimals), round(b, decimals) }, { thinSpace .. "/" .. thinSpace, ifYes(outputFormat.convertUnits, thinSpace .. "/" .. thinSpace) }
elseif tonumber(a) then
values = { round(a, decimals), "" }
elseif tonumber(c) then
values = { round(c, decimals), "" }
end
if outputFormat.first == inputUnit then
if outputFormat.convertUnits == "yes" then
convertedUnits = { addUnitNames(convert(values[1], decimals), outputFormat.unitNames, outputUnit), addUnitNames(convert(values[2], decimals), outputFormat.unitNames, outputUnit) }
end
values = { addUnitNames(values[1], outputFormat.unitNames), addUnitNames(values[2], outputFormat.unitNames) }
elseif outputFormat.first == "C" or outputFormat.first == "F" then
if outputFormat.convertUnits == "yes" then
convertedUnits = { addUnitNames(values[1]), addUnitNames(values[2], outputFormat.unitNames) }
end
values = { addUnitNames(convert(values[1], decimals), outputUnit), addUnitNames(convert(values[2], decimals), outputFormat.unitNames, outputUnit) }
else
if outputFormat.first == nil then
outputFormat.first = "nil"
end
addMessage('<span style="background-color: #EEE; font-family: monospace;">' .. outputFormat.first .. '</span>, the value for <span style="background-color: #EEE; font-family: monospace;">first</span> in <span style="background-color: #EEE; font-family: monospace;">outputFormat</span> is not recognized.')
end
--[[
Regarding line breaks:
If there are two values, there will be at least three characters: 9/1.
If there is one decimal, numbers will be three to five characters long
and there will be 3 to 10 characters total even without unit conversion:
1.1, 116.5/88.0.
If there are units, that adds three characters per number: 25 °C/20 °C.
In each of these cases, a line break is needed so that table cells are not too wide;
even more so when more than one of these things are true.
]]
if outputFormat.convertUnits == "yes" then
brackets = outputFormat.brackets == "yes" and { "(", ")" } or { "", "" }
if outputFormat.lineBreak == "auto" then
convertedUnitsSeparator = ( checkForString(values[2]) or decimals ~= "0" or outputFormat.showUnits == "yes" ) and "<br>" or " "
else
convertedUnitsSeparator = outputFormat.lineBreak == "yes" and "<br>" or outputFormat.lineBreak == "no" and " " or error('Value for lineBreak not recognized')
end
end
cellContent = values[1] .. highLowSeparator[1] .. values[2] .. convertedUnitsSeparator .. brackets[1] .. convertedUnits[1] .. highLowSeparator[2] .. convertedUnits[2] .. brackets[2]
if tonumber(c) then
colorCSS = outputFormat.color == "yes" and temperatureCSS(c, inputUnit, palette) or ""
if tonumber(b) and tonumber(a) then
local attributeValue = outputFormat.first == inputUnit and c or convert(c, decimals)
sortkey = outputFormat.sortable == "yes" and " data-sort-value=\"" .. attributeValue .. "\"" or ""
titleAttribute = " title=\"Average temperature: " .. attributeValue .. " " .. degree .. outputFormat.first .. "\""
end
elseif tonumber(b) then
colorCSS = ""
elseif tonumber(a) then
colorCSS = outputFormat.color == "yes" and temperatureCSS(a, inputUnit, palette) or ""
else
addMessage('Neither a nor b nor c are strings.')
end
otherCSS = outputFormat.smallFont == "yes" and "font-size: 85%;" or ""
if checkForString(colorCSS) or checkForString(otherCSS) then
styleAttribute = { "style=\"", "\"" }
end
if checkForString(otherCSS) or checkForString(colorCSS) or checkForString(titleAttribute) or checkForString(sortkey) then
attributeSeparator = " | "
end
cell = "\n| " .. styleAttribute[1] .. colorCSS .. otherCSS .. styleAttribute[2] .. titleAttribute .. sortkey .. attributeSeparator .. cellContent
return cell
end
function p.makeRow(frame)
makeArrays(frame)
local output = ""
if frame.args[1] then
output = "\n|-"
output = output .. "\n! " .. frame.args[1]
if frame.args[2] then
output = output .. " !! " .. frame.args[2]
end
end
if cellFormat then
outputFormat = cellFormat
end
if a and b and c then
for i = 1, length do
if not outputFormat then
outputFormat = outputFormats.high_low_average_F
end
output = output .. makeCell(outputFormat, a[i], b[i], c[i])
end
elseif a and b then
for i = 1, length do
if not outputFormat then
outputFormat = outputFormats.high_low_F
end
output = output .. makeCell(outputFormat, a[i], b[i])
end
elseif a then
for i = 1, length do
if not outputFormat then
outputFormat = outputFormats.average_F
end
output = output .. makeCell(outputFormat, a[i])
end
end
output = mw.ustring.gsub(output, "([%p%s])-(%d)", "%1" .. minus .. "%2")
return output
end
function p.makeTable(frame)
makeArrays(frame)
local output = "{| class=\"wikitable center nowrap\""
if cellFormat then
outputFormat = cellFormat
end
if a and b and c then
for i = 1, length do
if not outputFormat then
outputFormat = outputFormats.high_low_average_F
end
output = output .. makeCell(outputFormat, a[i], b[i], c[i])
end
elseif a and b then
for i = 1, length do
if not outputFormat then
outputFormat = outputFormats.high_low_F
end
output = output .. makeCell(outputFormat, a[i], b[i])
end
elseif a then
for i = 1, length do
if not outputFormat then
outputFormat = outputFormats.average_F
end
output = output .. makeCell(outputFormat, a[i])
end
end
output = mw.ustring.gsub(output, "([%p%s])-(%d)", "%1" .. minus .. "%2")
--[[ Replaces hyphens that have a punctuation or space character before them and a number after them,
making sure that hyphens in "data-sort-type" are not replaced with minuses.
If Lua had (?<=), a capture would not be necessary. ]]
output = output .. "\n|}"
if show then
output = output .. "\n\n<span style=\"color: red; font-size: 80%; line-height: 100%;\">" .. message .. "</span>"
end
return output
end
local chart = [[
{{Graph:Chart
|width=600
|height=180
|xAxisTitle=Celsius
|yAxisTitle=__COLOR
|type=line
|x=__XVALUES
|y=__YVALUES
|colors=__COLOR
}}
]]
function p.show(frame)
-- For testing, return wikitext to show graphs of how the red/green/blue colors
-- vary with temperature, and a table of the resulting colors.
local function collection()
-- Return a table to hold items.
return {
n = 0,
add = function (self, item)
if item then
self.n = self.n + 1
self[self.n] = item
end
end,
join = function (self, sep)
return table.concat(self, sep)
end,
}
end
local function make_chart(result, color, xvalues, yvalues)
result:add('\n')
result:add(frame:preprocess((chart:gsub('__[A-Z]+', {
__COLOR = color,
__XVALUES = xvalues:join(','),
__YVALUES = yvalues:join(','),
}))))
end
local function with_minus(value)
if value < 0 then
return minus .. tostring(-value)
end
return tostring(value)
end
local args = frame.args
local first = args[1] or -90
local last = args[2] or 59
local palette = p.palettes[args.palette] or p.palettes.cool
local xvals, reds, greens, blues = collection(), collection(), collection(), collection()
local wikitext = collection()
wikitext:add('{| class="wikitable"\n|-\n')
local columns = 0
for celsius = first, last do
local backgroundRGB = {}
local style = styleAttribute(palette, celsius, backgroundRGB)
local R = math.floor(backgroundRGB[1])
local G = math.floor(backgroundRGB[2])
local B = math.floor(backgroundRGB[3])
xvals:add(celsius)
reds:add(R)
greens:add(G)
blues:add(B)
wikitext:add('| ' .. style .. ' | ' .. with_minus(celsius) .. '\n')
columns = columns + 1
if columns >= 10 then
columns = 0
wikitext:add('|-\n')
end
end
wikitext:add('|}\n')
make_chart(wikitext, 'Red', xvals, reds)
make_chart(wikitext, 'Green', xvals, greens)
make_chart(wikitext, 'Blue', xvals, blues)
return wikitext:join()
end
return p