Black Friday is over; time for conditional coloring

amibroker%20conditional%20coloring

The below RGBcode function turns html tables in a colorful spectacle. The function supports both dynamic and static coloring combined with ranges going from darkred/red to darkgreen/green and vice versa, mimicking Excel's condintional cell coloring.

Enjoy!
JW

kpi%20table

// 0. Color Table.afl

// Location: C:\\Program Files\AmiBroker\Formulas\Report Charts

// AmiBroker implementation by TrendXplorer
// www.trendxplorer.info


EnableTextOutput( 3 ); // enable HTML output into report (Version 5.84 or higher!)


function RGBcode( curValue, minValue, maxValue, order, dynamicColoring )
{
    if ( dynamicColoring == False )
    {
        if ( order == 1 )	// highest to lowest : green to red
		{
			R = IIf( curValue < 0, 255,  0  );
			G = IIf( curValue < 0,  0 , 255 );
		}
		
		if ( order == 0 ) 	// lowest to highest: green to red
		{
			R = IIf( curValue < 0,  0 , 255 );
			G = IIf( curValue < 0, 255,  0  );
		}
		
		B = 0;
    }
    else
    {
		if ( order == 1 ) // highest to lowest : darkgreen to darkred
		{
			range = maxValue - minValue;
			value = ( curValue - minValue ) / range;
		}
	
		if ( order == 0 ) // lowest to highest: darkgreen to darkred
		{
			range = minValue - maxValue; 
			value  = ( curValue - maxValue ) / range;
		}
        
		fR = IIf( value <= 0.5  , 1 , ( 1 - value ) / 0.5                );
		fR = IIf( value >  0.25, fR, ( 0.8 + value / 0.25 ) * fR         );
		fG = IIf( value >= 0.5  , 1 , value / 0.5                        );
		fG = IIf( value <  0.75, fG, ( 0.8 + ( 1 - value ) / 0.25 ) * fG );
        
		R = fR * 255;
		G = fG * 240;
		B = 51;
	}
      
	RGB = "RGB(" + NumToStr( R, 0 ) + "," + NumToStr( G, 0 ) + "," + NumToStr( B, 0 ) + ")";
    
	return RGB;
}


valMax = -100;
valMin =  100;

for ( i = 0; i <= 10; i++ )
{
	value        = mtRandom();
	value        = round( 100 * value ) - 50;
	val[i]       = value;
	
	valMax       = Max( value, valMax );
	valMin       = Min( value, valMin );
}

numMax = -100;
numMin =  100;

for ( i = 0; i <= 10; i++ )
{
	value        = mtRandom();
	value        = round( 100 * value ) - 50;
	num[i]       = value;
	
	numMax       = Max( value, numMax );
	numMin       = Min( value, numMin );
}

function GenProfitTableHTML( )
{    
    printf( "<table border='0' bordercolor='#000000' cellspacing='0' cellpadding='3' style='border-collapse:collapse;'>\n" );    

    printf( "<tr>" );
    printf( "<td width='50'><center>Dynamic (order=1)</center></td><td width='25'>&nbsp;</td><td width='50'><center>Dynamic (order=1)</center></td><td width='25'>&nbsp;</td><td width='50'><center>Static (order=1)</center></td>" );
    printf( "<td width='100'>&nbsp;</td>" );
    printf( "<td width='50'><center>Dynamic (order=0)</center></td><td width='25'>&nbsp;</td><td width='50'><center>Dynamic (order=0)</center></td><td width='25'>&nbsp;</td><td width='50'><center>Static (order=0)</center></td>" );
    printf( "</tr>\n" );
    
    maxRow = 100;
    minRow = 0;

    for ( row = minRow; row <= maxRow; row += 10 )
    {
        i = row / 10;
        printf( "<tr>\n" ); // new row
        printf( "<td style='border:1px solid black;' bgcolor=" + RGBcode( row   , minRow, maxRow, 1, True )  + "><center>&nbsp;<b>" + row    + "</b>&nbsp;</center></td>" );
        printf( "<td style='border:0px solid black;' bgcolor=" + "'RGB( 255 , 255 , 255 )'"                  + "><center>&nbsp;<b>" + " "    + "</b>&nbsp;</center></td>" );
        printf( "<td style='border:1px solid black;' bgcolor=" + RGBcode( val[i], valMin, valMax, 1, True )  + "><center>&nbsp;<b>" + val[i] + "</b>&nbsp;</center></td>" );
        printf( "<td style='border:0px solid black;' bgcolor=" + "'RGB( 255 , 255 , 255 )'"                  + "><center>&nbsp;<b>" + " "    + "</b>&nbsp;</center></td>" );
        printf( "<td style='border:1px solid black;' bgcolor=" + RGBcode( val[i], valMin, valMax, 1, False ) + "><center>&nbsp;<b>" + val[i] + "</b>&nbsp;</center></td>" );
        printf( "<td style='border:0px solid black;' bgcolor=" + "'RGB( 255 , 255 , 255 )'"                  + "><center>&nbsp;<b>" + " "    + "</b>&nbsp;</center></td>" );
        printf( "<td style='border:1px solid black;' bgcolor=" + RGBcode( row   , minRow, maxRow, 0, True )  + "><center>&nbsp;<b>" + row    + "</b>&nbsp;</center></td>" );
        printf( "<td style='border:0px solid black;' bgcolor=" + "'RGB( 255 , 255 , 255 )'"                  + "><center>&nbsp;<b>" + " "    + "</b>&nbsp;</center></td>" );
        printf( "<td style='border:1px solid black;' bgcolor=" + RGBcode( num[i], numMin, numMax, 0, True )  + "><center>&nbsp;<b>" + num[i] + "</b>&nbsp;</center></td>" );
        printf( "<td style='border:0px solid black;' bgcolor=" + "'RGB( 255 , 255 , 255 )'"                  + "><center>&nbsp;<b>" + " "    + "</b>&nbsp;</center></td>" );
        printf( "<td style='border:1px solid black;' bgcolor=" + RGBcode( num[i], numMin, numMax, 0, False ) + "><center>&nbsp;<b>" + num[i] + "</b>&nbsp;</center></td>" );
        printf( "</tr>\n" ); // end row
    }

    printf( "</table>\n" );
}


GenProfitTableHTML();

13 Likes