Thursday, August 5, 2010

Create ambient color

There are times when you have to display charts with statistical data such as year to date sales, quarter sales, etc.
That's fine until your chart data changes every X period of time, let's say each 5 minutes, in this case the customer needs to somehow know that from last time he saw the statistical information has changed(or refreshed), you can do this by changing the background color of the chart, does not really matter if you choose to make the background gradient(it will look better tho) or not.
Changing the background color will make the user know the data has been changed(refreshed) without him having to think about it(the power of mind...).
We create a array of web colors because most users surf the web daily and they are very familiar with this colors...

Add Graphics and Math to uses clause, add Randomize to initialization section, copy-paste the following code:
const
  CAMBIENT_COLORS: array[0..WebColorsCount -1] of TColor = (
    clWebSnow, clWebFloralWhite, clWebLavenderBlush, clWebOldLace,
    clWebIvory, clWebCornSilk, clWebBeige, clWebAntiqueWhite,
    clWebWheat, clWebAliceBlue, clWebGhostWhite, clWebLavender,
    clWebSeashell, clWebLightYellow, clWebPapayaWhip, clWebNavajoWhite,
    clWebMoccasin, clWebBurlywood, clWebAzure, clWebMintcream,
    clWebHoneydew, clWebLinen, clWebLemonChiffon, clWebBlanchedAlmond,
    clWebBisque, clWebPeachPuff, clWebTan, clWebYellow,
    clWebDarkOrange, clWebRed, clWebDarkRed, clWebMaroon,
    clWebIndianRed, clWebSalmon, clWebCoral, clWebGold,
    clWebTomato, clWebCrimson, clWebBrown, clWebChocolate,
    clWebSandyBrown, clWebLightSalmon, clWebLightCoral, clWebOrange,
    clWebOrangeRed, clWebFirebrick, clWebSaddleBrown, clWebSienna,
    clWebPeru, clWebDarkSalmon, clWebRosyBrown, clWebPaleGoldenrod,
    clWebLightGoldenrodYellow, clWebOlive, clWebForestGreen, clWebGreenYellow,
    clWebChartreuse, clWebLightGreen, clWebAquamarine, clWebSeaGreen,
    clWebGoldenRod, clWebKhaki, clWebOliveDrab, clWebGreen,
    clWebYellowGreen, clWebLawnGreen, clWebPaleGreen, clWebMediumAquamarine,
    clWebMediumSeaGreen, clWebDarkGoldenRod, clWebDarkKhaki, clWebDarkOliveGreen,
    clWebDarkgreen, clWebLimeGreen, clWebLime, clWebSpringGreen,
    clWebMediumSpringGreen, clWebDarkSeaGreen, clWebLightSeaGreen, clWebPaleTurquoise,
    clWebLightCyan, clWebLightBlue, clWebLightSkyBlue, clWebCornFlowerBlue,
    clWebDarkBlue, clWebIndigo, clWebMediumTurquoise, clWebTurquoise,
    clWebCyan, clWebAqua, clWebPowderBlue, clWebSkyBlue,
    clWebRoyalBlue, clWebMediumBlue, clWebMidnightBlue, clWebDarkTurquoise,
    clWebCadetBlue, clWebDarkCyan, clWebTeal, clWebDeepskyBlue,
    clWebDodgerBlue, clWebBlue, clWebNavy, clWebDarkViolet,
    clWebDarkOrchid, clWebMagenta, clWebFuchsia, clWebDarkMagenta,
    clWebMediumVioletRed, clWebPaleVioletRed, clWebBlueViolet, clWebMediumOrchid,
    clWebMediumPurple, clWebPurple, clWebDeepPink, clWebLightPink,
    clWebViolet, clWebOrchid, clWebPlum, clWebThistle,
    clWebHotPink, clWebPink, clWebLightSteelBlue, clWebMediumSlateBlue,
    clWebLightSlateGray, clWebWhite, clWebLightgrey, clWebGray,
    clWebSteelBlue, clWebSlateBlue, clWebSlateGray, clWebWhiteSmoke,
    clWebSilver, clWebDimGray, clWebMistyRose, clWebDarkSlateBlue,
    clWebDarkSlategray, clWebGainsboro, clWebDarkGray, clWebBlack
  );

function GetAmbientRandomColor: TColor;

    // makes the color lighter by Percent
   // the higher the value of Percent the closer to white
    function LighterColor(Color: TColor; Percent: Byte): TColor;
    var
      vRed: Byte;
      vGreen: Byte;
      vBlue: Byte;
    begin
      Color := ColorToRGB(Color);
      vRed := GetRValue(Color);
      vGreen := GetGValue(Color);
      vBlue := GetBValue(Color);
      vRed := vRed + MulDiv(255 - vRed, Percent, 100);
      vGreen := vGreen + MulDiv(255 - vGreen, Percent, 100);
      vBlue := vBlue + MulDiv(255 - vBlue, Percent, 100);
      // generate the LighterColor color
      Result := RGB(vRed, vGreen, vBlue);
    end; // function LighterColor(Color: TColor; Percent: Byte): TColor;

var
  Index: Integer;
begin
  // generate a random Index
  Index := RandomRange(Low(CAMBIENT_COLORS), High(CAMBIENT_COLORS));
  Result := LighterColor(CAMBIENT_COLORS[Index], 70);
end;

No comments:

Post a Comment

Blogroll(General programming and Delphi feeds)