Heat Map background colour calculations : Color « 2D Graphics « C# / C Sharp






Heat Map background colour calculations

        
using System;
using System.Collections.Generic;
using System.Text;

namespace TfsScorecard.Utilities
{
    /// <summary>
    /// Heat Map background colour calculations
    /// Derived from: http://blogs.technet.com/andrew/archive/2007/12/06/heat-maps-in-sql-server-reporting-services-2005.aspx
    /// </summary>
    public class HeatMap
    {
        #region Public Method
        /// <summary>
        /// 
        /// </summary>
        /// <param name="redStartVal"></param>
        /// <param name="yellowStartVal"></param>
        /// <param name="greenStartVal"></param>
        /// <param name="val"></param>
        /// <returns>The RGB hex color string</returns>
        public static string GetColor(decimal redStartVal, decimal yellowStartVal, decimal greenStartVal, decimal val)
        {
            // color points
            int[] Red = new int[] { 255, 255, 255 }; // #FCBF7B
            int[] Yellow = new int[] { 254, 255, 132 }; // #FEEB84
            int[] Green = new int[] { 99, 190, 123 };  // #63BE7B
            int[] White = new int[] { 255, 255, 255 }; // #FFFFFF

            // value that corresponds to the color that represents the tier above the value - determined later
            Decimal highValue = 0.0M;
            // value that corresponds to the color that represents the tier below the value
            Decimal lowValue = 0.0M;
            // next higher and lower color tiers (set to corresponding member variable values)
            int[] highColor = null;
            int[] lowColor = null;

            // 3-integer array of color values (r,g,b) that will ultimately be converted to hex
            int[] rgb = null;


            // If value lower than green start value, it must be green.
            if (val <= greenStartVal)
            {
                rgb = Green;
            }
            // determine if value lower than the baseline of the red tier
            else if (val >= redStartVal)
            {
                rgb = Red;
            }

            // if not, then determine if value is between the red and yellow tiers
            else if (val > yellowStartVal)
            {
                highValue = redStartVal;
                lowValue = yellowStartVal;
                highColor = Red;
                lowColor = Yellow;
            }

            // if not, then determine if value is between the yellow and green tiers
            else if (val > greenStartVal)
            {
                highValue = yellowStartVal;
                lowValue = greenStartVal;
                highColor =Yellow;
                lowColor = Green;
            }
            // must be green
            else
            {
                rgb = Green;
            }

            // get correct color values for values between dark red and green
            if (rgb == null)
            {
                rgb = GetColorValues(highValue, lowValue, highColor, lowColor, val);
            }

            // return the hex string
            return String.Format("#{0:x}{1:x}{2:x}", rgb[0], rgb[1], rgb[2]);
        }
        #endregion

        /// <summary>
        /// Looks at the passed in value and high and low boundary values and determines the
        /// relative color values (rgb) in proportion to the relative distance the value is found 
        /// between the boundaries. The rgb color string is calculated proportionately between the 
        /// highColor/lowColor commensurate with the proportions the passed-in value exists within 
        /// the boundary values
        /// </summary>
        /// <param name="highBound">closest decimal boundary above the value (val)</param>
        /// <param name="lowBound">closest decimal boundary above the value (val)</param>
        /// <param name="highColor">color (int[] {r,g,b}) that corresponds to the highBound</param>
        /// <param name="lowColor">color (int[] {r,g,b}) that corresponds to the lowBound</param>
        /// <param name="val">pass rate</param>
        /// <returns>a 3-member int[] array (rgb)</returns>
        private static int[] GetColorValues(decimal highBound, decimal lowBound, int[] highColor, int[] lowColor, decimal val)
        {
            // proportion the val is between the high and low bounds
            decimal ratio = (val - lowBound) / (highBound - lowBound);
            int[] rgb = new int[3];
            // step through each color and find the value that represents the approriate proportional value 
            // between the high and low colors
            for (int i = 0; i < 3; i++)
            {
                int hc = (int)highColor[i];
                int lc = (int)lowColor[i];
                // high color is lower than low color - reverse the subtracted vals
                bool reverse = hc < lc;
                // difference between the high and low values
                int diff = reverse ? lc - hc : hc - lc;
                // lowest value of the two
                int baseVal = reverse ? hc : lc;
                rgb[i] = (int)Math.Round((decimal)diff * ratio) + baseVal;
            }
            return rgb;
        }
    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.Transparent colorTransparent color
2.List all known color in a systemList all known color in a system
3.Draw each of 100 cells with randomly chosen colorsDraw each of 100 cells with randomly chosen colors
4.Filled with the semi transparent and transparent colorFilled with the semi transparent and transparent color
5.All the colors that are supported in C# according
6.Color ChangerColor Changer
7.Known ColorsKnown Colors
8.Five yellow squares with different alpha values(Transparensy)
9.Create two color instances with different alpha components
10.Color.Chocolate
11.Use Color.FromArgb to create Color
12.Get all known color
13.Color representation in r,g,b with values [0.0 - 1.0].
14.Color representation in h,s,v with h = [0 - 360], s,v = [0.0 - 1.0].
15.Return a randomly-generated color
16.Color to RGB value
17.Get color outof String
18.Parse Color
19.Color To Rgb
20.Rgb To Color
21.Returns an HTML #XXXXXX format for a color.
22.Convert color name to Color object
23.Hex Color Util
24.Helper method to get a color based on it's string value
25.Converts a color string to a hex value string
26.Color to Hue,Lightness,Saturation value
27.Get Color From String
28.Convert String value To Color
29.Color to String
30.Masks an image and returns a two color bitmap.
31.Convert Hex To Color
32.Create Color From HLS value
33.Parse Color with Color.FromArgb
34.Create Color Object from RGB value
35.Get Rainbow Colors
36.Convert RGB to HSL
37.HSL to RGB conversion.
38.Hsv To Rgb
39.Rgb Linear Interpolate
40.Get Luminance
41.To Grey scale