sysperfana/heapanalyser/Libraries/UI/HeapCtrlLib/Renderers/Utilities/HeapCellRendererColour.cs
changeset 8 15296fd0af4a
equal deleted inserted replaced
7:8e12a575a9b5 8:15296fd0af4a
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * Redistribution and use in source and binary forms, with or without
       
     6 * modification, are permitted provided that the following conditions are met:
       
     7 *
       
     8 * - Redistributions of source code must retain the above copyright notice,
       
     9 *   this list of conditions and the following disclaimer.
       
    10 * - Redistributions in binary form must reproduce the above copyright notice,
       
    11 *   this list of conditions and the following disclaimer in the documentation
       
    12 *   and/or other materials provided with the distribution.
       
    13 * - Neither the name of Nokia Corporation nor the names of its contributors
       
    14 *   may be used to endorse or promote products derived from this software
       
    15 *   without specific prior written permission.
       
    16 *
       
    17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
       
    18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
       
    20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
       
    21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
       
    27 * POSSIBILITY OF SUCH DAMAGE.
       
    28 * 
       
    29 * Initial Contributors:
       
    30 * Nokia Corporation - initial contribution.
       
    31 *
       
    32 * Contributors:
       
    33 *
       
    34 * Description: 
       
    35 *
       
    36 */
       
    37 
       
    38 using System;
       
    39 using System.Drawing;
       
    40 using HeapCtrlLib.Interfaces;
       
    41 using HeapCtrlLib.Utilities;
       
    42 using HeapCtrlLib.Renderers.Utilities;
       
    43 using HeapLib;
       
    44 using HeapLib.Array;
       
    45 using HeapLib.Cells;
       
    46 using SymbianUtils.Graphics;
       
    47 using SymbianUtils.Colour;
       
    48 
       
    49 namespace HeapCtrlLib.Renderers.Utilities
       
    50 {
       
    51 	public class HeapCellRendererColour
       
    52 	{
       
    53         #region Constructors & destructor
       
    54 		public HeapCellRendererColour()
       
    55 		{
       
    56         }
       
    57         #endregion
       
    58 
       
    59         #region Drawing
       
    60         protected virtual void PaintBorders( HeapCellBorderInfo aBorders, Graphics aGraphics, SymRect aRect, Pen aPen )
       
    61         {
       
    62             if ( aBorders[ THeapCellBorderType.ELeft ] )
       
    63             {
       
    64                 aGraphics.DrawLine( aPen, aRect.TopLeft, aRect.BottomLeft );
       
    65             }
       
    66             if ( aBorders[ THeapCellBorderType.ERight ] )
       
    67             {
       
    68                 aGraphics.DrawLine( aPen, aRect.TopRight, aRect.BottomRight );
       
    69             }
       
    70             if ( aBorders[ THeapCellBorderType.ETop ] )
       
    71             {
       
    72                 aGraphics.DrawLine( aPen, aRect.TopLeft, aRect.TopRight );
       
    73             }
       
    74             if ( aBorders[ THeapCellBorderType.EBottom ] )
       
    75             {
       
    76                 aGraphics.DrawLine( aPen, aRect.BottomLeft, aRect.BottomRight );
       
    77             }
       
    78         }
       
    79         #endregion
       
    80 
       
    81         #region Colouring
       
    82         public static Color ColourByCellType( HeapCell aCell )
       
    83         {
       
    84             bool isUnknown = false;
       
    85             return ColourByCellType( aCell, out isUnknown );
       
    86         }
       
    87 
       
    88         public static Color ColourByCellType( HeapCell aCell, out bool aIsUnknown )
       
    89         {
       
    90             aIsUnknown = false;
       
    91             Color ret = Color.WhiteSmoke;
       
    92             //
       
    93             switch ( aCell.Type )
       
    94             {
       
    95                 case HeapCell.TType.EAllocated:
       
    96                     {
       
    97                         if ( aCell.IsDescriptor )
       
    98                         {
       
    99                             ret = CellAllocatedDescriptor;
       
   100                         }
       
   101                         else if ( aCell.Symbol != null )
       
   102                         {
       
   103                             ret = CellAllocatedWithSymbol;
       
   104                         }
       
   105                         else
       
   106                         {
       
   107                             aIsUnknown = true;
       
   108                             ret = CellAllocatedUnknown;
       
   109                         }
       
   110                         break;
       
   111                     }
       
   112                 case HeapCell.TType.EFree:
       
   113                     ret = CellFree;
       
   114                     break;
       
   115             }
       
   116             //
       
   117             return ret;
       
   118         }
       
   119 
       
   120         public static Color CellFillColourByRegion( HeapCell aCell, uint aAddress, out HeapCell.TRegion aRegion )
       
   121         {
       
   122             // Cell coloring
       
   123             aRegion = aCell.RegionForAddress( aAddress );
       
   124 
       
   125             Color fillColour = Color.White;
       
   126             switch( aRegion )
       
   127             {
       
   128             case HeapCell.TRegion.EHeader:
       
   129                 fillColour = Color.GhostWhite;
       
   130                 break;
       
   131             case HeapCell.TRegion.EPayload:
       
   132                 fillColour = ColourByCellType( aCell );
       
   133                 break;
       
   134             default:
       
   135                 System.Diagnostics.Debug.Assert( false );
       
   136                 break;
       
   137             }
       
   138 
       
   139             return fillColour;
       
   140         }
       
   141 
       
   142         public static Color RampedCellFillColourByRegion( HeapCell aCell, int aCellBoxCount, uint aAddress, out HeapCell.TRegion aRegion )
       
   143         {
       
   144             // Get the cell colour to use and also the region associated with
       
   145             // the cell address.
       
   146             Color fillColour = CellFillColourByRegion( aCell, aAddress, out aRegion );
       
   147 
       
   148             // If we're rendering the payload we'll want to ramp the cell colour
       
   149             // to make it easier to read.
       
   150             if  ( aRegion == HeapCell.TRegion.EPayload )
       
   151             {
       
   152                 fillColour = RampedColourByBoxNumber( fillColour, aCellBoxCount, aCell.Address, aAddress );
       
   153             }
       
   154 
       
   155             return fillColour;
       
   156         }
       
   157 
       
   158         public static Color RampedColourByBoxNumber( Color aBaseline, int aBoxCount, uint aCellBaseAddress, uint aAddress )
       
   159         {
       
   160             float cellAddressOffset = (float) ( ( aAddress - aCellBaseAddress ) / 4 );
       
   161 
       
   162             Color dark = ColourUtils.Darken( aBaseline, 0.30f );
       
   163             Color light = ColourUtils.Lighten( aBaseline, 0.30f );
       
   164 
       
   165             // This is the baseline brightness for the colour
       
   166             float brightnessBaseline = light.GetBrightness() - dark.GetBrightness();
       
   167     
       
   168             // This is how much brightness we can apply to each box.
       
   169             float brightnessPerBox = ( brightnessBaseline / aBoxCount );
       
   170           
       
   171             // This is the brightness of the target box
       
   172             float brightnessPercentage = brightnessPerBox * cellAddressOffset; 
       
   173 
       
   174             Color ret = ColourUtils.Lighten( aBaseline, brightnessPercentage );
       
   175             return ret;
       
   176         }
       
   177 
       
   178         public static Color RampedColourByIntensityRange( Color aMaxIntensity, long aValue, long aMin, long aMax )
       
   179         {
       
   180             // Calculate the difference between the maximum and minimum. This tells us how many different
       
   181             // "intensity" values we have to work with.
       
   182             long valueSpan = ( aMax - aMin );
       
   183 
       
   184             // Get the brightness of the baseline colour and then scale it based upon the number of different
       
   185             // value intensities we must cope with.
       
   186             float brightness = aMaxIntensity.GetBrightness();
       
   187             float brightnessPerValue = ( brightness / (float) valueSpan );
       
   188 
       
   189             // Ramp the cell colour based upon the index
       
   190             float percentage = brightness - ( brightnessPerValue * ( aValue - aMin ) );
       
   191 
       
   192             Color ret = ColourUtils.Darken( aMaxIntensity, percentage );
       
   193             return ret;
       
   194         }
       
   195         #endregion
       
   196 
       
   197         #region Properties
       
   198         public static Color CellAllocatedDescriptor
       
   199         {
       
   200             get { return Color.FromKnownColor( KCellAllocatedDescriptor ); }
       
   201         }
       
   202 
       
   203         public static Color CellAllocatedUnknown
       
   204         {
       
   205             get { return Color.FromKnownColor( KCellAllocatedUnknown ); }
       
   206         }
       
   207 
       
   208         public static Color CellAllocatedWithSymbol
       
   209         {
       
   210             get { return Color.FromKnownColor( KCellAllocatedWithSymbol ); }
       
   211         }
       
   212 
       
   213         public static Color CellFree
       
   214         {
       
   215             get { return Color.FromKnownColor( KCellFree ); }
       
   216         }
       
   217         #endregion
       
   218 
       
   219         #region Constants
       
   220         public const KnownColor KCellAllocatedDescriptor = KnownColor.Gold;
       
   221         public const KnownColor KCellAllocatedUnknown = KnownColor.Firebrick;
       
   222         public const KnownColor KCellAllocatedWithSymbol = KnownColor.OrangeRed;
       
   223         public const KnownColor KCellFree = KnownColor.RoyalBlue;
       
   224         #endregion
       
   225     }
       
   226 }