Khác biệt giữa bản sửa đổi của “Mô đun:Weather box/colors”
Giao diện
Nội dung được xóa Nội dung được thêm vào
←Trang mới: “local w = {} local math_mod = require('Module:Math') local function hex( value ) return string.format("%02X", value) end local function format_line( background…” |
Không có tóm lược sửa đổi |
||
Dòng 1: | Dòng 1: | ||
local w = {} |
|||
local math_mod = require('Module:Math') |
local math_mod = require('Module:Math') |
||
local |
local traceText |
||
local Value |
|||
return string.format("%02X", value) |
|||
Value = { |
|||
end |
|||
lang = mw.getContentLanguage(), |
|||
getDisplay = function (self, second) |
|||
if not self:isValid() then |
|||
return nil |
|||
end |
|||
local display = self.string |
|||
if display == 'trace' then |
|||
if second then |
|||
-- If a cell displays "cm (inch)", show "trace" not "trace (trace)". |
|||
return nil |
|||
end |
|||
return traceText or 'trace' |
|||
end |
|||
if math.abs(self.number) >= 1000 then |
|||
display = self.lang:formatNum(math.abs(self.number)) |
|||
if self.number < 0 then |
|||
display = '−' .. display |
|||
end |
|||
elseif self.number < 0 then |
|||
display = '−' .. display:sub(2) |
|||
end |
|||
return display |
|||
end, |
|||
getPrecision = function (self) |
|||
local result = rawget(self, 'precision') |
|||
if not result then |
|||
if self:isValid() then |
|||
result = math.max(0, math_mod._precision(self.string)) |
|||
else |
|||
result = 0 |
|||
end |
|||
rawset(self, 'precision', result) |
|||
end |
|||
return result |
|||
end, |
|||
isValid = function (self) |
|||
return self.number ~= nil and self.number ~= -9999 |
|||
end, |
|||
new = function (v) |
|||
local val, str, precision |
|||
if type(v) == 'string' then |
|||
if v == 'trace' then |
|||
val, str, precision = 0, 'trace', 0 |
|||
else |
|||
val, str = math_mod._cleanNumber(v) |
|||
end |
|||
elseif type(v) == 'number' then |
|||
val, str = v, tostring(v) |
|||
end |
|||
if not val then |
|||
val, str = -9999, '' |
|||
end |
|||
return setmetatable({ |
|||
number = val, |
|||
string = str, |
|||
precision = precision, |
|||
}, Value) |
|||
end, |
|||
converts = { |
|||
in2cm = { factor = 2.54 }, |
|||
in2mm = { factor = 25.4 }, |
|||
cm2in = { factor = 1/2.54, p2max = 1 }, |
|||
mm2in = { factor = 1/25.4, p2max = 0 }, |
|||
}, |
|||
setConvert = function (self, invalue, units) |
|||
-- Use method modified from [[Module:Convert]] to determine precision. |
|||
if invalue.string == 'trace' then |
|||
self.number, self.string, self.precision = 0, 'trace', 0 |
|||
return |
|||
end |
|||
local convert = self.converts[units] or error('Unknown units') |
|||
local outnum = invalue.number * convert.factor |
|||
local precision = invalue:getPrecision() |
|||
if outnum > 0 then |
|||
local adjust = math.log10(1/convert.factor) + math.log10(2) |
|||
local p1 = math.floor(precision + adjust) |
|||
local p2 = 1 - math.floor(math.log10(outnum)) |
|||
if convert.p2max then |
|||
p2 = math.min(p2, convert.p2max) |
|||
end |
|||
precision = math.max(p1, p2) |
|||
end |
|||
self:setNumberRounded(outnum, precision) |
|||
end, |
|||
setNumberRounded = function (self, number, precision) |
|||
if precision > 2 then |
|||
precision = 2 |
|||
end |
|||
self.number = math_mod._round(number, precision) |
|||
if precision < 0 then |
|||
self.string = tostring(self.number) |
|||
else |
|||
local fmt = '%.' .. string.format('%d', precision) .. 'f' |
|||
self.string = string.format(fmt, self.number) |
|||
end |
|||
end, |
|||
} |
|||
Value.__index = Value |
|||
local function |
local function checkFlag(flag, default) |
||
if flag == nil then |
|||
return "background: #" .. background .. "; color:#" .. text_color .. ";" |
|||
return default |
|||
end |
|||
elseif type(flag) == 'boolean' then |
|||
return flag |
|||
local function range_pos( value, start, stop ) |
|||
elseif type(flag) == 'string' then |
|||
flag = flag:lower() |
|||
if value < start then |
|||
if flag == '0' or flag == 'false' or |
|||
return 0 |
|||
flag == '' or flag == 'no' or |
|||
elseif value > stop then |
|||
flag == 'n' then |
|||
return 1 |
|||
return false |
|||
else |
else |
||
return |
return true |
||
end |
end |
||
else |
else |
||
return error('Flag type not valid') |
|||
if value < stop then |
|||
end |
|||
return 1 |
|||
end |
|||
elseif value > start then |
|||
return 0 |
|||
local function makeLine(label, first_values, second_values, color_values) |
|||
local result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label, "\n"} |
|||
for i = 1, 13 do |
|||
table.insert(result, |
|||
'|style="' .. color_values[i] .. |
|||
(i == 13 and ' border-left-width:medium"' or '"') .. |
|||
' class="notheme"| ' |
|||
) |
|||
local display = first_values[i]:getDisplay() |
|||
if display then |
|||
table.insert(result, display) |
|||
if second_values ~= nil then |
|||
display = second_values[i]:getDisplay(true) |
|||
if display then |
|||
table.insert(result, "<br />(" .. display .. ")") |
|||
end |
|||
end |
|||
else |
else |
||
table.insert(result, '—') |
|||
return (start - value) / (start - stop) |
|||
end |
end |
||
table.insert(result, "\n") |
|||
end |
end |
||
return table.concat(result) |
|||
end |
end |
||
local function getInputs(args, group_name, suffix, include_space) |
|||
function w.color_d( frame ) |
|||
local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', |
|||
local val = math_mod._cleanNumber( frame.args[1] ) |
|||
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' } |
|||
return w._days_color( val ) |
|||
local str |
|||
end |
|||
local values = {} |
|||
function w.color_pastel( frame ) |
|||
if suffix == nil then |
|||
local val = math_mod._cleanNumber( frame.args[1] ) |
|||
for i, mon in ipairs(month_names) do |
|||
return w._pastel_color( val ) |
|||
if include_space then |
|||
end |
|||
str = args[ mon .. ' ' .. group_name ] or '' |
|||
function w.color_t( frame ) |
|||
else |
|||
local val = math_mod._cleanNumber( frame.args[1] ) |
|||
str = args[ mon .. group_name ] or '' |
|||
return w._temperature_color( val ) |
|||
end |
end |
||
values[i] = Value.new(str) |
|||
function w.color_green( frame ) |
|||
end |
|||
local val = math_mod._cleanNumber( frame.args[1] ) |
|||
return w._green_color( val ) |
|||
end |
|||
function w.color_s( frame ) |
|||
local val = math_mod._cleanNumber( frame.args[1] ) |
|||
return w._sunshine_color( val ) |
|||
end |
|||
function w.color_h( frame ) |
|||
local val = math_mod._cleanNumber( frame.args[1] ) |
|||
return w._humidity_color( val ) |
|||
end |
|||
function w.color_p( frame ) |
|||
local val = math_mod._cleanNumber( frame.args[1] ) |
|||
return w._precipitation_color( val ) |
|||
end |
|||
function w.color_u( frame ) |
|||
local val = math_mod._cleanNumber( frame.args[1] ) |
|||
return w._uv_color( val ) |
|||
end |
|||
function w._days_color( val ) |
|||
local item, background, text_color |
|||
if val == nil then |
|||
return format_line( "FFFFFF", "000000" ) |
|||
end |
|||
item = hex( range_pos( val, 20, 0 )*255 ) |
|||
background = item .. item |
|||
item = hex( range_pos( val, 40, 20 )*255 ) |
|||
background = background .. item |
|||
if val >= 12 then |
|||
text_color = "FFFFFF" |
|||
else |
else |
||
for i, mon in ipairs(month_names) do |
|||
text_color = "000000" |
|||
local value, updated |
|||
for var, suf in ipairs(suffix) do |
|||
if include_space then |
|||
str = args[ mon .. ' ' .. group_name .. ' ' .. suf ] |
|||
else |
|||
str = args[ mon .. group_name .. ' ' .. suf ] |
|||
end |
|||
if str ~= nil and str ~= '' then |
|||
value = Value.new(str) |
|||
value.variant = var |
|||
updated = true |
|||
break |
|||
end |
|||
end |
|||
if not updated then |
|||
value = Value.new() |
|||
value.variant = 0 |
|||
end |
|||
values[i] = value |
|||
end |
|||
end |
end |
||
return values |
|||
return format_line( background, text_color ) |
|||
end |
end |
||
function |
local function getAnnualValue(values, mode) |
||
if mode == 'avg' or mode == 'sum' then |
|||
local item1, item2, background, text_color |
|||
local total = 0 |
|||
local p1, p2, variant |
|||
if val == nil then |
|||
p1 = 0 |
|||
return format_line( "FFFFFF", "000000" ) |
|||
for i = 1, 12 do |
|||
end |
|||
if not values[i]:isValid() then |
|||
return Value.new() |
|||
item1 = hex( range_pos( val, 165.6, 0 )*255 ) |
|||
end |
|||
item2 = hex( range_pos( val, 300, 165.61 )*207 + 48 ) |
|||
if not variant then |
|||
background = item1 .. item2 .. item1 |
|||
local var = values[i].variant |
|||
if val >= 200 then |
|||
if var and var ~= 0 then |
|||
text_color = "FFFFFF" |
|||
variant = var |
|||
end |
|||
end |
|||
p2 = values[i]:getPrecision() |
|||
if p2 > p1 then |
|||
p1 = p2 |
|||
end |
|||
total = total + values[i].number |
|||
end |
|||
local value = Value.new(total) |
|||
if mode == 'avg' then |
|||
value:setNumberRounded(total / 12, p1) |
|||
end |
|||
value.variant = variant |
|||
return value |
|||
elseif mode == 'min' then |
|||
local target |
|||
for i = 1, 12 do |
|||
if values[i]:isValid() then |
|||
if target == nil or values[i].number < target.number then |
|||
target = values[i] |
|||
end |
|||
end |
|||
end |
|||
return target or Value.new() |
|||
elseif mode == 'max' then |
|||
local target |
|||
for i = 1, 12 do |
|||
if values[i]:isValid() then |
|||
if target == nil or values[i].number > target.number then |
|||
target = values[i] |
|||
end |
|||
end |
|||
end |
|||
return target or Value.new() |
|||
else |
else |
||
error('Unrecognized Annual Mode') |
|||
text_color = "000000" |
|||
end |
end |
||
return format_line( background, text_color ) |
|||
end |
end |
||
function |
local function reconcileTemperature(C_values, F_values) |
||
for i = 1, 13 do |
|||
local item, background, text_color |
|||
local p |
|||
if C_values[i].string == '' then |
|||
if F_values[i]:isValid() then |
|||
return format_line( "FFFFFF", "000000" ) |
|||
p = F_values[i]:getPrecision() |
|||
C_values[i]:setNumberRounded((F_values[i].number - 32)*5/9, p) |
|||
end |
|||
elseif F_values[i].string == '' then |
|||
if C_values[i]:isValid() then |
|||
p = C_values[i]:getPrecision() |
|||
F_values[i]:setNumberRounded(C_values[i].number*9/5 + 32, p) |
|||
end |
|||
end |
|||
end |
end |
||
end |
|||
local function reconcilePrecipitation(M_values, I_values, prefer_cm) |
|||
if val < 4.5 then |
|||
local v_class = 0 |
|||
item = range_pos( val, -42.75, 4.5 )*255 |
|||
for i = 1, 13 do |
|||
background = hex( item ) |
|||
if M_values[i].variant == 1 then |
|||
else |
|||
v_class = 1 |
|||
item = range_pos( val, 60, 41.5 )*255 |
|||
elseif M_values[i].variant == 2 then |
|||
background = hex( item ) |
|||
v_class = 2 |
|||
end |
|||
end |
end |
||
if v_class == 0 then |
|||
if prefer_cm then |
|||
v_class = 1 |
|||
item = range_pos( val, -42.75, 4.5 )*255 |
|||
else |
|||
background = background .. hex( item ) |
|||
v_class = 2 |
|||
else |
|||
end |
|||
item = range_pos( val, 41.5, 4.5 )*255 |
|||
background = background .. hex( item ) |
|||
end |
end |
||
for i = 1, 13 do |
|||
local units |
|||
if val < -42.78 then |
|||
if M_values[i].string == '' then |
|||
item = range_pos( val, -90, -42.78 )*255 |
|||
if I_values[i]:isValid() then |
|||
background = background .. hex( item ) |
|||
if v_class == 1 then |
|||
else |
|||
units = 'in2cm' |
|||
item = range_pos( val, 23, 4.5 )*255 |
|||
else |
|||
background = background .. hex( item ) |
|||
units = 'in2mm' |
|||
end |
|||
M_values[i]:setConvert(I_values[i], units) |
|||
M_values[i].variant = v_class |
|||
end |
|||
elseif I_values[i].string == '' then |
|||
if M_values[i]:isValid() then |
|||
if M_values[i].variant == 1 then |
|||
units = 'cm2in' |
|||
else |
|||
units = 'mm2in' |
|||
end |
|||
I_values[i]:setConvert(M_values[i], units) |
|||
end |
|||
end |
|||
end |
end |
||
if val < -23.3 or val >= 37.8 then |
|||
text_color = "FFFFFF" |
|||
else |
|||
text_color = "000000" |
|||
end |
|||
return format_line( background, text_color ) |
|||
end |
end |
||
function |
local function _buildRow(definition, args, options) |
||
options = options or {} |
|||
local item, background, text_color |
|||
local wbc = require('Module:Weather box/colors' .. (options.sandbox or '')) |
|||
local mode = (definition.mode or 'basic'):lower() |
|||
if val == nil then |
|||
local group_name = definition.group_name |
|||
return format_line( "FFFFFF", "000000" ) |
|||
local first_values, second_values |
|||
local color_values |
|||
local color_scheme = definition.color_scheme or 't' |
|||
local scale_factor = math_mod._cleanNumber(definition.scale_factor) or 1 |
|||
local date_mode = checkFlag(definition.date_mode, false) |
|||
local label = definition.label or '' |
|||
local annual_mode = (definition.annual_mode or 'avg'):lower() |
|||
local include_space = checkFlag(definition.include_space, true) |
|||
local second_line = checkFlag(definition.second_line, false) |
|||
local prefer_cm = checkFlag(definition.prefer_cm, false) |
|||
local imperial_first = checkFlag(args['imperial first']) |
|||
local metric_first = checkFlag(args['metric first']) |
|||
local wantSingleLine = options.wantSingleLine or checkFlag(args['single line']) |
|||
local trace = args.trace |
|||
if trace and trace ~= '' then |
|||
traceText = trace |
|||
end |
end |
||
if imperial_first == nil then |
|||
imperial_first = metric_first == nil and true or not metric_first |
|||
item = hex( range_pos( val, 165.6, 0 )*255 ) |
|||
background = item .. item |
|||
item = hex( range_pos( val, 300, 165.61 )*207 + 48 ) |
|||
background = background .. item |
|||
if val > 90 then |
|||
text_color = "FFFFFF" |
|||
else |
|||
text_color = "000000" |
|||
end |
end |
||
if mode == 'basic' then |
|||
first_values = getInputs(args, group_name, nil, include_space) |
|||
return format_line( background, text_color ) |
|||
second_values = nil |
|||
end |
|||
elseif mode == 'temperature' then |
|||
first_values = getInputs(args, group_name, {'C'}, include_space) |
|||
function w._humidity_color( val ) |
|||
second_values = getInputs(args, group_name, {'F'}, include_space) |
|||
local item, background, text_color |
|||
reconcileTemperature(first_values, second_values) |
|||
elseif mode == "precipitation" then |
|||
first_values = getInputs(args, group_name, {'cm', 'mm'}, include_space) |
|||
return format_line( "FFFFFF", "000000" ) |
|||
second_values = getInputs(args, group_name, {'inch'}, include_space) |
|||
end |
|||
reconcilePrecipitation(first_values, second_values, prefer_cm) |
|||
item = hex( range_pos( val, 66.67, 0 )*255 ) |
|||
background = item .. item |
|||
item = hex( range_pos( val, 133.33, 66.667 )*255 ) |
|||
background = background .. item |
|||
if val >= 40 then |
|||
text_color = "FFFFFF" |
|||
else |
else |
||
error('Requested mode not recognized') |
|||
text_color = "000000" |
|||
end |
end |
||
local good = false |
|||
for i = 1, 13 do |
|||
return format_line( background, text_color ) |
|||
if first_values[i].string ~= '' then |
|||
end |
|||
good = true |
|||
break |
|||
function w._sunshine_color( val ) |
|||
end |
|||
local item, background, text_color |
|||
if val == nil then |
|||
return format_line( "FFFFFF", "000000" ) |
|||
end |
end |
||
if not good then |
|||
return '' |
|||
if val < 90 then |
|||
item = hex( range_pos( val, 0, 90 )*170 ) |
|||
elseif val < 180 then |
|||
item = hex( range_pos( val, 90, 180 )*42.5 + 170 ) |
|||
else |
|||
item = hex( range_pos( val, 180, 360 )*42.5 + 212.5 ) |
|||
end |
end |
||
if first_values[13].string == '' then |
|||
background = item .. item |
|||
first_values[13] = getAnnualValue(first_values, annual_mode) |
|||
if val < 90 then |
|||
item = hex( range_pos( val, 0, 90 )*170 ) |
|||
elseif val < 270 then |
|||
item = hex( range_pos( val, 150, 90 )*170 ) |
|||
else |
|||
item = hex( range_pos( val, 270, 720 )*255 ) |
|||
end |
end |
||
if second_values ~= nil then |
|||
background = background .. item |
|||
if second_values[13].string == '' then |
|||
second_values[13] = getAnnualValue(second_values, annual_mode) |
|||
if val < 80 then |
|||
end |
|||
text_color = "FFFFFF" |
|||
else |
|||
text_color = "000000" |
|||
end |
end |
||
color_scheme = wbc.interpret_color_code(color_scheme) |
|||
color_values = {} |
|||
return format_line( background, text_color ) |
|||
local month_adj = { 31/30, 28.25/30, 31/30, 1, 31/30, 1, |
|||
end |
|||
31/30, 31/30, 1, 31/30, 1, 31/30, 365.25/30 } |
|||
for i = 1, 13 do |
|||
function w._pastel_color( val ) |
|||
if first_values[i]:isValid() then |
|||
local background, text_color |
|||
local adj = scale_factor |
|||
if date_mode then |
|||
adj = adj / month_adj[i] |
|||
return format_line( "FFFFFF", "000000" ) |
|||
end |
|||
if mode == "precipitation" then |
|||
if first_values[i].variant == 1 then |
|||
adj = adj * 10 |
|||
end |
|||
else |
|||
end |
|||
text_color = "000000" |
|||
table.insert(color_values, color_scheme(first_values[i].number * adj)) |
|||
end |
|||
if val >= 51 then |
|||
background = 'EE2200' |
|||
else |
|||
val = math_mod._round( (val + 25.5)/3, 0 ) |
|||
if val == 1 then |
|||
background = 'BB00CC' |
|||
elseif val == 2 then |
|||
background = 'CC00EE' |
|||
elseif val == 3 then |
|||
background = 'CC33EE' |
|||
elseif val == 4 then |
|||
background = 'CC55EE' |
|||
elseif val == 5 then |
|||
background = 'DD66EE' |
|||
elseif val == 6 then |
|||
background = 'DD77EE' |
|||
elseif val == 7 then |
|||
background = 'DD99EE' |
|||
elseif val == 8 then |
|||
background = 'DDAAEE' |
|||
elseif val == 9 then |
|||
background = 'DDBBEE' |
|||
elseif val == 10 then |
|||
background = 'EECCFF' |
|||
elseif val == 11 then |
|||
background = 'FFDDFF' |
|||
elseif val == 12 then |
|||
background = 'F1F1F1' |
|||
elseif val == 13 then |
|||
background = 'FFEEBB' |
|||
elseif val == 14 then |
|||
background = 'FFFFCC' |
|||
elseif val == 15 then |
|||
background = 'FFFFBB' |
|||
elseif val == 16 then |
|||
background = 'FFFFAA' |
|||
elseif val == 17 then |
|||
background = 'FFFF88' |
|||
elseif val == 18 then |
|||
background = 'FFCC33' |
|||
elseif val == 19 then |
|||
background = 'FFBB33' |
|||
elseif val == 20 then |
|||
background = 'FF9900' |
|||
elseif val == 21 then |
|||
background = 'FF8844' |
|||
elseif val == 22 then |
|||
background = 'FF6633' |
|||
elseif val == 23 then |
|||
background = 'FF5522' |
|||
elseif val == 24 then |
|||
background = 'FF4422' |
|||
elseif val == 25 then |
|||
background = 'EE4400' |
|||
else |
else |
||
table.insert(color_values, color_scheme(nil)) |
|||
background = 'AA00AA' |
|||
end |
end |
||
end |
end |
||
if imperial_first and second_values ~= nil then |
|||
first_values, second_values = second_values, first_values |
|||
return format_line( background, text_color ) |
|||
end |
|||
function w._uv_color( val ) |
|||
local background, text_color |
|||
if val == nil then |
|||
return format_line( "FFFFFF", "000000" ) |
|||
end |
end |
||
if not wantSingleLine then |
|||
if second_line and second_values ~= nil then |
|||
first_values = second_values |
|||
background = "3EA72D" |
|||
end |
|||
elseif val >= 3 and val < 6 then |
|||
second_values = nil |
|||
background = "FFF300" |
|||
elseif val >= 6 and val < 8 then |
|||
background = "F18B00" |
|||
elseif val >= 8 and val < 11 then |
|||
background = "E53210" |
|||
else |
|||
background = "B567A4" |
|||
end |
end |
||
return makeLine(label, first_values, second_values, color_values) |
|||
if val < 3 then |
|||
text_color = "FFFFFF" |
|||
elseif val >= 3 and val < 8 then |
|||
text_color = "000000" |
|||
else |
|||
text_color = "FFFFFF" |
|||
end |
|||
return format_line( background, text_color ) |
|||
end |
end |
||
function |
local function buildRow(frame) |
||
return _buildRow(frame.args, frame:getParent().args) -- row definition, template parameters |
|||
return format_line( "FAFAFA", "000000" ) |
|||
end |
|||
function w.interpret_color_code( code ) |
|||
code = code:lower() |
|||
if code == 't' then |
|||
return w._temperature_color |
|||
elseif code == 'pastel' then |
|||
return w._pastel_color |
|||
elseif code == 'green' then |
|||
return w._green_color |
|||
elseif code == 'h' then |
|||
return w._humidity_color |
|||
elseif code == 's' then |
|||
return w._sunshine_color |
|||
elseif code == 'p' then |
|||
return w._precipitation_color |
|||
elseif code == 'd' then |
|||
return w._days_color |
|||
elseif code == 'u' then |
|||
return w._uv_color |
|||
elseif code == 'none' then |
|||
return w._none_color |
|||
else |
|||
error( 'Unknown color scheme option' ) |
|||
end |
|||
end |
end |
||
return |
return { |
||
buildRow = buildRow, |
|||
_buildRow = _buildRow, |
|||
} |
Phiên bản lúc 09:14, ngày 25 tháng 6 năm 2024
![](http://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Test_Template_Info-Icon_-_Version_%282%29.svg/50px-Test_Template_Info-Icon_-_Version_%282%29.svg.png)
local math_mod = require('Module:Math')
local traceText
local Value
Value = {
lang = mw.getContentLanguage(),
getDisplay = function (self, second)
if not self:isValid() then
return nil
end
local display = self.string
if display == 'trace' then
if second then
-- If a cell displays "cm (inch)", show "trace" not "trace (trace)".
return nil
end
return traceText or 'trace'
end
if math.abs(self.number) >= 1000 then
display = self.lang:formatNum(math.abs(self.number))
if self.number < 0 then
display = '−' .. display
end
elseif self.number < 0 then
display = '−' .. display:sub(2)
end
return display
end,
getPrecision = function (self)
local result = rawget(self, 'precision')
if not result then
if self:isValid() then
result = math.max(0, math_mod._precision(self.string))
else
result = 0
end
rawset(self, 'precision', result)
end
return result
end,
isValid = function (self)
return self.number ~= nil and self.number ~= -9999
end,
new = function (v)
local val, str, precision
if type(v) == 'string' then
if v == 'trace' then
val, str, precision = 0, 'trace', 0
else
val, str = math_mod._cleanNumber(v)
end
elseif type(v) == 'number' then
val, str = v, tostring(v)
end
if not val then
val, str = -9999, ''
end
return setmetatable({
number = val,
string = str,
precision = precision,
}, Value)
end,
converts = {
in2cm = { factor = 2.54 },
in2mm = { factor = 25.4 },
cm2in = { factor = 1/2.54, p2max = 1 },
mm2in = { factor = 1/25.4, p2max = 0 },
},
setConvert = function (self, invalue, units)
-- Use method modified from [[Module:Convert]] to determine precision.
if invalue.string == 'trace' then
self.number, self.string, self.precision = 0, 'trace', 0
return
end
local convert = self.converts[units] or error('Unknown units')
local outnum = invalue.number * convert.factor
local precision = invalue:getPrecision()
if outnum > 0 then
local adjust = math.log10(1/convert.factor) + math.log10(2)
local p1 = math.floor(precision + adjust)
local p2 = 1 - math.floor(math.log10(outnum))
if convert.p2max then
p2 = math.min(p2, convert.p2max)
end
precision = math.max(p1, p2)
end
self:setNumberRounded(outnum, precision)
end,
setNumberRounded = function (self, number, precision)
if precision > 2 then
precision = 2
end
self.number = math_mod._round(number, precision)
if precision < 0 then
self.string = tostring(self.number)
else
local fmt = '%.' .. string.format('%d', precision) .. 'f'
self.string = string.format(fmt, self.number)
end
end,
}
Value.__index = Value
local function checkFlag(flag, default)
if flag == nil then
return default
elseif type(flag) == 'boolean' then
return flag
elseif type(flag) == 'string' then
flag = flag:lower()
if flag == '0' or flag == 'false' or
flag == '' or flag == 'no' or
flag == 'n' then
return false
else
return true
end
else
return error('Flag type not valid')
end
end
local function makeLine(label, first_values, second_values, color_values)
local result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label, "\n"}
for i = 1, 13 do
table.insert(result,
'|style="' .. color_values[i] ..
(i == 13 and ' border-left-width:medium"' or '"') ..
' class="notheme"| '
)
local display = first_values[i]:getDisplay()
if display then
table.insert(result, display)
if second_values ~= nil then
display = second_values[i]:getDisplay(true)
if display then
table.insert(result, "<br />(" .. display .. ")")
end
end
else
table.insert(result, '—')
end
table.insert(result, "\n")
end
return table.concat(result)
end
local function getInputs(args, group_name, suffix, include_space)
local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' }
local str
local values = {}
if suffix == nil then
for i, mon in ipairs(month_names) do
if include_space then
str = args[ mon .. ' ' .. group_name ] or ''
else
str = args[ mon .. group_name ] or ''
end
values[i] = Value.new(str)
end
else
for i, mon in ipairs(month_names) do
local value, updated
for var, suf in ipairs(suffix) do
if include_space then
str = args[ mon .. ' ' .. group_name .. ' ' .. suf ]
else
str = args[ mon .. group_name .. ' ' .. suf ]
end
if str ~= nil and str ~= '' then
value = Value.new(str)
value.variant = var
updated = true
break
end
end
if not updated then
value = Value.new()
value.variant = 0
end
values[i] = value
end
end
return values
end
local function getAnnualValue(values, mode)
if mode == 'avg' or mode == 'sum' then
local total = 0
local p1, p2, variant
p1 = 0
for i = 1, 12 do
if not values[i]:isValid() then
return Value.new()
end
if not variant then
local var = values[i].variant
if var and var ~= 0 then
variant = var
end
end
p2 = values[i]:getPrecision()
if p2 > p1 then
p1 = p2
end
total = total + values[i].number
end
local value = Value.new(total)
if mode == 'avg' then
value:setNumberRounded(total / 12, p1)
end
value.variant = variant
return value
elseif mode == 'min' then
local target
for i = 1, 12 do
if values[i]:isValid() then
if target == nil or values[i].number < target.number then
target = values[i]
end
end
end
return target or Value.new()
elseif mode == 'max' then
local target
for i = 1, 12 do
if values[i]:isValid() then
if target == nil or values[i].number > target.number then
target = values[i]
end
end
end
return target or Value.new()
else
error('Unrecognized Annual Mode')
end
end
local function reconcileTemperature(C_values, F_values)
for i = 1, 13 do
local p
if C_values[i].string == '' then
if F_values[i]:isValid() then
p = F_values[i]:getPrecision()
C_values[i]:setNumberRounded((F_values[i].number - 32)*5/9, p)
end
elseif F_values[i].string == '' then
if C_values[i]:isValid() then
p = C_values[i]:getPrecision()
F_values[i]:setNumberRounded(C_values[i].number*9/5 + 32, p)
end
end
end
end
local function reconcilePrecipitation(M_values, I_values, prefer_cm)
local v_class = 0
for i = 1, 13 do
if M_values[i].variant == 1 then
v_class = 1
elseif M_values[i].variant == 2 then
v_class = 2
end
end
if v_class == 0 then
if prefer_cm then
v_class = 1
else
v_class = 2
end
end
for i = 1, 13 do
local units
if M_values[i].string == '' then
if I_values[i]:isValid() then
if v_class == 1 then
units = 'in2cm'
else
units = 'in2mm'
end
M_values[i]:setConvert(I_values[i], units)
M_values[i].variant = v_class
end
elseif I_values[i].string == '' then
if M_values[i]:isValid() then
if M_values[i].variant == 1 then
units = 'cm2in'
else
units = 'mm2in'
end
I_values[i]:setConvert(M_values[i], units)
end
end
end
end
local function _buildRow(definition, args, options)
options = options or {}
local wbc = require('Module:Weather box/colors' .. (options.sandbox or ''))
local mode = (definition.mode or 'basic'):lower()
local group_name = definition.group_name
local first_values, second_values
local color_values
local color_scheme = definition.color_scheme or 't'
local scale_factor = math_mod._cleanNumber(definition.scale_factor) or 1
local date_mode = checkFlag(definition.date_mode, false)
local label = definition.label or ''
local annual_mode = (definition.annual_mode or 'avg'):lower()
local include_space = checkFlag(definition.include_space, true)
local second_line = checkFlag(definition.second_line, false)
local prefer_cm = checkFlag(definition.prefer_cm, false)
local imperial_first = checkFlag(args['imperial first'])
local metric_first = checkFlag(args['metric first'])
local wantSingleLine = options.wantSingleLine or checkFlag(args['single line'])
local trace = args.trace
if trace and trace ~= '' then
traceText = trace
end
if imperial_first == nil then
imperial_first = metric_first == nil and true or not metric_first
end
if mode == 'basic' then
first_values = getInputs(args, group_name, nil, include_space)
second_values = nil
elseif mode == 'temperature' then
first_values = getInputs(args, group_name, {'C'}, include_space)
second_values = getInputs(args, group_name, {'F'}, include_space)
reconcileTemperature(first_values, second_values)
elseif mode == "precipitation" then
first_values = getInputs(args, group_name, {'cm', 'mm'}, include_space)
second_values = getInputs(args, group_name, {'inch'}, include_space)
reconcilePrecipitation(first_values, second_values, prefer_cm)
else
error('Requested mode not recognized')
end
local good = false
for i = 1, 13 do
if first_values[i].string ~= '' then
good = true
break
end
end
if not good then
return ''
end
if first_values[13].string == '' then
first_values[13] = getAnnualValue(first_values, annual_mode)
end
if second_values ~= nil then
if second_values[13].string == '' then
second_values[13] = getAnnualValue(second_values, annual_mode)
end
end
color_scheme = wbc.interpret_color_code(color_scheme)
color_values = {}
local month_adj = { 31/30, 28.25/30, 31/30, 1, 31/30, 1,
31/30, 31/30, 1, 31/30, 1, 31/30, 365.25/30 }
for i = 1, 13 do
if first_values[i]:isValid() then
local adj = scale_factor
if date_mode then
adj = adj / month_adj[i]
end
if mode == "precipitation" then
if first_values[i].variant == 1 then
adj = adj * 10
end
end
table.insert(color_values, color_scheme(first_values[i].number * adj))
else
table.insert(color_values, color_scheme(nil))
end
end
if imperial_first and second_values ~= nil then
first_values, second_values = second_values, first_values
end
if not wantSingleLine then
if second_line and second_values ~= nil then
first_values = second_values
end
second_values = nil
end
return makeLine(label, first_values, second_values, color_values)
end
local function buildRow(frame)
return _buildRow(frame.args, frame:getParent().args) -- row definition, template parameters
end
return {
buildRow = buildRow,
_buildRow = _buildRow,
}