w = {};
convert = require( "Module:BaseConvert" );
math_mod = require( "Module:Math" );
function hex( value )
return convert._convert( value, 16, 10, 0, 2, nil, nil, nil );
end
function format_line( background, text_color )
return "background:#" .. background .. ";color:#" .. text_color .. ";font-size:85%;text-align:center;";
end
function range_pos( value, start, stop )
if start < stop then
if value < start then
return 0;
elseif value > stop then
return 1;
else
return (value - start) / (stop - start);
end
else
if value < stop then
return 1;
elseif value > start then
return 0;
else
return (start - value) / (start - stop);
end
end
end
function w.color_d( frame )
local val = math_mod._cleanNumber( frame, frame.args[1] );
return w._days_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
function w._days_color( val )
local item, background, text_color;
if val == nil then
return format_line( "FFFFFF", "000000" );
end
background = '';
item = hex( range_pos( val, 20, 0 )*255 );
background = background .. item .. item;
item = hex( range_pos( val, 40, 20 )*255 );
background = background .. item;
if val >= 12 then
text_color = "FFFFFF";
else
text_color = "000000";
end
return format_line( background, text_color );
end
function w._green_color( val )
local item1, item2, background, text_color;
if val == nil then
return format_line( "FFFFFF", "000000" );
end
background = '';
item1 = hex( range_pos( val, 165.6, 0 )*255 );
item2 = hex( range_pos( val, 300, 165.61 )*207 + 48 );
background = background .. item1 .. item2 .. item1;
if val >= 200 then
text_color = "FFFFFF";
else
text_color = "000000";
end
return format_line( background, text_color );
end
function w._temperature_color( val )
local item, background, text_color;
if val == nil then
return format_line( "FFFFFF", "000000" );
end
background = '';
if val < 4.5 then
item = range_pos( val, -42.75, 4.5 )*255;
background = background .. hex( item );
else
item = range_pos( val, 60, 41.5 )*255;
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
function w._precipitation_color( val )
local item, background, text_color;
if val == nil then
return format_line( "FFFFFF", "000000" );
end
background = '';
item = hex( range_pos( val, 165.6, 0 )*255 );
background = 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
return format_line( background, text_color );
end
function w._humidity_color( val )
local item, background, text_color;
if val == nil then
return format_line( "FFFFFF", "000000" );
end
background = '';
item = hex( range_pos( val, 66.67, 0 )*255 );
background = background .. item .. item;
item = hex( range_pos( val, 133.33, 66.667 )*255 );
background = background .. item;
if val >= 40 then
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
return w;