Module:Weather box/row: Difference between revisions

Content deleted Content added
No edit summary
update from sandbox: tweak to reduce duplication
 
(102 intermediate revisions by 9 users not shown)
Line 1:
local math_mod = require('Module:Math')
w = {};
convert = require( "Module:BaseConvert" );
math_mod = require( "Module:Math" );
 
local traceText
function hex( value )
local Value
return convert._convert( value, 16, 10, 0, 2, nil, nil, nil );
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 format_linecheckFlag( backgroundflag, text_color default)
if flag == nil then
return "background:#" .. background .. ";color:#" .. text_color .. ";font-size:85%;text-align:center;";
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 range_posmakeLine( valuelabel, startfirst_values, stopsecond_values, color_values)
local result = {'|- style="text-align: center;"\n! scope="row" style="height: 16px;" | ', label, "\n"}
if start < stop then
for i = 1, 13 do
if value < start then
table.insert(result,
return 0;
'|style="' .. color_values[i] ..
elseif value > stop then
(i == 13 and ' border-left-width:medium"' or '"') ..
return 1;
' class="notheme"| '
else
)
return (value - start) / (stop - start);
local display = first_values[i]:getDisplay()
end
if display then
else
table.insert(result, display)
if value < stop then
if second_values ~= nil then
return 1;
display = second_values[i]:getDisplay(true)
elseif value > start then
if display then
return 0;
table.insert(result, "<br />(" .. display .. ")")
else
end
return (start - value) / (start - stop);
end
else
end
table.insert(result, '—')
end
end
 
table.insert(result, "\n")
function w.color_d( frame )
end
local val = math_mod._cleanNumber( frame, frame.args[1] );
return wtable._days_colorconcat( val result);
end
function w.color_pastel( frame )
local val = math_mod._cleanNumber( frame, frame.args[1] );
return w._pastel_color( val );
end
function w.color_t( frame )
local val = math_mod._cleanNumber( frame, frame.args[1] );
return w._temperature_color( val );
end
function w.color_green( frame )
local val = math_mod._cleanNumber( frame, frame.args[1] );
return w._green_color( val );
end
function w.color_s( frame )
local val = math_mod._cleanNumber( frame, frame.args[1] );
return w._sunshine_color( val );
end
function w.color_h( frame )
local val = math_mod._cleanNumber( frame, frame.args[1] );
return w._humidity_color( val );
end
function w.color_p( frame )
local val = math_mod._cleanNumber( frame, frame.args[1] );
return w._precipitation_color( val );
end
 
local function getInputs(args, group_name, suffix, include_space)
function w._days_color( val )
local month_names = { 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
local item, background, text_color;
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'year' }
local str
if val == nil then
local values = {}
return format_line( "FFFFFF", "000000" );
if suffix == nil then
end
for i, mon in ipairs(month_names) do
if include_space then
background = '';
str = args[ mon .. ' ' .. group_name ] or ''
item = hex( range_pos( val, 20, 0 )*255 );
else
background = background .. item .. item;
str = args[ mon .. group_name ] or ''
end
item = hex( range_pos( val, 40, 20 )*255 );
values[i] = Value.new(str)
background = background .. item;
end
else
if val >= 12 then
for i, mon in ipairs(month_names) do
text_color = "FFFFFF";
local value, updated
else
for var, suf in ipairs(suffix) do
text_color = "000000";
if include_space then
end
str = args[ mon .. ' ' .. group_name .. ' ' .. suf ]
else
return format_line( background, text_color );
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 w._green_colorgetAnnualValue(values, val 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, end12 do
if not values[i]:isValid() then
return Value.new()
background = '';
end
item1 = hex( range_pos( val, 165.6, 0 )*255 );
if not variant then
item2 = hex( range_pos( val, 300, 165.61 )*207 + 48 );
local var = values[i].variant
background = background .. item1 .. item2 .. item1;
if var and var if val >~= 2000 then
variant = var
text_color = "FFFFFF";
end
else
end
text_color = "000000";
p2 = values[i]:getPrecision()
end
if p2 > p1 then
p1 = p2
return format_line( background, text_color );
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 w._temperature_colorreconcileTemperature(C_values, val F_values)
for i = 1, 13 do
local item, background, text_color;
local p
if valC_values[i].string == nil'' then
if F_values[i]:isValid() then
return format_line( "FFFFFF", "000000" );
p = F_values[i]:getPrecision()
end
C_values[i]:setNumberRounded((F_values[i].number - 32)*5/9, p)
end
background = '';
elseif F_values[i].string == '' then
if val < 4.5 then
if C_values[i]:isValid() then
item = range_pos( val, -42.75, 4.5 )*255;
p = C_values[i]:getPrecision()
background = background .. hex( item );
F_values[i]:setNumberRounded(C_values[i].number*9/5 + 32, p)
else
end
item = range_pos( val, 60, 41.5 )*255;
end
background = background .. hex( item );
end
if val <= 4.5 then
item = range_pos( val, -42.75, 4.5 )*255;
background = background .. hex( item );
else
item = range_pos( val, 41.5, 4.5 )*255;
background = background .. hex( item );
end
if val < -42.78 then
item = range_pos( val, -90, -42.78 )*255;
background = background .. hex( item );
else
item = range_pos( val, 23, 4.5 )*255;
background = background .. hex( item );
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
 
local function reconcilePrecipitation(M_values, I_values, prefer_cm)
function w._precipitation_color( val )
local v_class = 0
local item, background, text_color;
for i = 1, 13 do
if valM_values[i].variant == nil1 then
v_class = 1
return format_line( "FFFFFF", "000000" );
elseif M_values[i].variant == 2 then
end
v_class = 2
end
background = '';
end
item = hex( range_pos( val, 165.6, 0 )*255 );
if v_class == 0 then
background = background .. item .. item;
if prefer_cm then
v_class = 1
item = hex( range_pos( val, 300, 165.61 )*207 + 48 );
else
background = background .. item;
v_class = 2
end
if val > 90 then
end
text_color = "FFFFFF";
for i = 1, 13 do
else
local units
text_color = "000000";
if M_values[i].string == '' then
end
if I_values[i]:isValid() then
 
if v_class == 1 then
return format_line( background, text_color );
units = 'in2cm'
end
else
 
units = 'in2mm'
function w._humidity_color( val )
end
local item, background, text_color;
M_values[i]:setConvert(I_values[i], units)
M_values[i].variant = v_class
if val == nil then
end
return format_line( "FFFFFF", "000000" );
elseif I_values[i].string == '' then
end
if M_values[i]:isValid() then
if M_values[i].variant == 1 then
background = '';
units = 'cm2in'
item = hex( range_pos( val, 66.67, 0 )*255 );
else
background = background .. item .. item;
units = 'mm2in'
end
item = hex( range_pos( val, 133.33, 66.667 )*255 );
I_values[i]:setConvert(M_values[i], units)
background = background .. item;
end
end
if val >= 40 then
end
text_color = "FFFFFF";
else
text_color = "000000";
end
 
return format_line( background, text_color );
end
 
function w._sunshine_color( val )
local item, background, text_color;
if val == nil then
return format_line( "FFFFFF", "000000" );
end
background = '';
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
background = background .. item .. item;
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
background = background .. item;
if val < 80 then
text_color = "FFFFFF";
else
text_color = "000000";
end
 
return format_line( background, text_color );
end
 
function w._pastel_color( val )
local item, background, text_color;
if val == nil then
return format_line( "FFFFFF", "000000" );
end
 
if val < -15 or val >= 39 then
text_color = "FFFFFF";
else
text_color = "000000";
end
background = '';
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
background = 'AA00AA'
end
end
 
return format_line( background, text_color );
end
 
local function interpret_color_code_buildRow(definition, codeargs, options)
options = options or {}
code = code:lower();
local wbc = require('Module:Weather box/colors' .. (options.sandbox or ''))
if code == 't' then
local mode = (definition.mode or 'basic'):lower()
return w._temperature_color;
local group_name = definition.group_name
elseif color_scheme == 'pastel' then
local first_values, second_values
return w._pastel_color;
local color_values
elseif color_scheme == 'green' then
local color_scheme = definition.color_scheme or 't'
return w._green_color;
local scale_factor = math_mod._cleanNumber(definition.scale_factor) or 1
elseif color_scheme == 'h' then
local date_mode = checkFlag(definition.date_mode, false)
return w._humidity_color;
local label = definition.label or ''
elseif color_scheme == 's' then
local annual_mode = (definition.annual_mode or 'avg'):lower()
return w._sunshine_color;
local include_space = checkFlag(definition.include_space, true)
elseif color_scheme == 'p' then
local second_line = checkFlag(definition.second_line, false)
return w._precipitation_color;
local prefer_cm = checkFlag(definition.prefer_cm, false)
elseif color_scheme == 'd' then
local imperial_first = checkFlag(args['imperial first'])
return w._days_color;
local metric_first = checkFlag(args['metric first'])
else
local wantSingleLine = options.wantSingleLine or checkFlag(args['single line'])
error( 'Unknown color scheme option' );
local trace = args.trace
end
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 w.dual_linebuildRow( frame )
return _buildRow(frame.args, frame:getParent().args) -- row definition, template parameters
local color_scheme = frame.args.colors or 't';
local scale_factor = math_mod._cleanNumber( frame, frame.args.scale_factor) or 1;
local pframe = frame:getParent();
local show = pframe.args.show or '1'
local alt = pframe.args.alt or '';
local label = pframe.args.label or '';
local result = '';
color_scheme = interpret_color_code( color_scheme );
result = '|- \n! height="16" | ' .. label .. "\n"
months = { 'jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul',
'aug', 'sep', 'oct', 'nov', 'dec', 'yr' };
local m1, m_show, m_alt;
for _, k in ipairs( months ) do
m1 = math_mod._cleanNumber( frame, pframe.args[ k .. '1' ]);
if show == '1' then
m_show = m1;
else
m_show = math_mod._cleanNumber( frame, pframe.args[ k .. show ]);
end
if alt == '1' then
m_alt = m1;
elseif alt ~= '' then
m_alt = math_mod._cleanNumber( frame, pframe.args[ k .. alt ]);
else
m_alt = nil;
end
if k == 'yr' then
result = result .. '|style="' .. color_scheme( m1*scale_factor ) ..' border-left-width:medium" | '
else
result = result .. '|style="' .. color_scheme( m1*scale_factor ) ..'" | '
end
if m_alt ~= nil then
result = result .. m_show .. " <br /> (" .. m_alt .. ")\n";
else
result = result .. m_show .. "\n";
end
end
return result;
end
 
return w;{
buildRow = buildRow,
_buildRow = _buildRow,
}