Module:Weather box/row

This is an old revision of this page, as edited by Dragons flight (talk | contribs) at 05:37, 8 March 2013 (fixed). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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, item, background, text_color;
    
    val = math_mod._cleanNumber( frame, frame.args[1] );
    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.color_t( frame )
    local val, item, background, text_color;
    
    val = math_mod._cleanNumber( frame, frame.args[1] );
    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.color_p( frame )
    local val, item, background, text_color;
    
    val = math_mod._cleanNumber( frame, frame.args[1] );
    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

return w;