.NET FxColor
Explorations In Code Series June 4th, 2008Just a quick little post today regarding how you can do some fairly simple color transformations to produce some visual ‘charting’ of data using html. This example stems from an experiment to simulate the StatsPress module that can be included in WordPress. The outcome of this is a color transformation class that can be used to perform some nice color ranges in both HEX (HTML) and System.Drawing.Color. Below is a screenshot of a produced graph. Attached is FxColor Source Code and below is the core class for your review.
using System;
using System.Drawing;
using System.Collections.Generic;
/// <summary>
/// Provides a means to create a color mixer that shifts colors
/// from one color matrix to another.
/// </summary>
public class FxColorMixer
{
int red, green, blue;
int delta;
int iterations;
/// <summary>
/// Initializes a new instance of the <see cref="FxColorMixer"/> class.
/// </summary>
/// <param name="red">The starting red color.</param>
/// <param name="green">The starting green.</param>
/// <param name="blue">The starting blue.</param>
/// <param name="iterations">The number of iterations to produce.</param>
public FxColorMixer(int red, int green, int blue, int iterations)
{
if (iterations < = 0) throw new ArgumentOutOfRangeException("iterations must be greater than 0");
this.red = red;
this.green = green;
this.blue = blue;
this.delta = (int)Math.Round((double)250 / (double)iterations, 0);
this.iterations = iterations;
}
/// <summary>
/// Gets the color at the specified index from the number of iterations specified.
///
/// <param name="index">The index.</param>
/// <param name="sRed">The s red.</param>
/// <param name="sGreen">The s green.</param>
/// <param name="sBlue">The s blue.</param>
/// <returns></returns>
public FxColor GetColor(int index, double sRed, double sGreen, double sBlue)
{
if (index > iterations) throw new ArgumentOutOfRangeException("index is past number of iterations expected.");
double vBlue = (delta * index) * sBlue;
double cBlue = blue + vBlue;
double vGreen = (delta * index) * sGreen;
double cGreen = green + vGreen;
double vRed = (delta * index) * sRed;
double cRed = red + vRed;
cRed = Math.Max(0, Math.Min(255, cRed));
cGreen = Math.Max(0, Math.Min(255, cGreen));
cBlue = Math.Max(0, Math.Min(255, cBlue));
return new FxColor(Color.FromArgb((int)cRed, (int)cGreen, (int)cBlue));
}
}
/// <summary>
/// Represents a adapter over the <see cref="Color"/> class.
/// </summary>
public class FxColor
{
Color c;
/// <summary>
/// Initializes a new instance of the <see cref="FxColor"/> class.
/// </summary>
/// <param name="c">The c.</param>
internal FxColor(Color c)
{
this.c = c;
}
/// <summary>
/// Offsets the current color by the vectors specified.
/// </summary>
/// <param name="vRed">The displacement of red.</param>
/// <param name="vGreen">The displacement of green.</param>
/// <param name="vBlue">The displacement of blue.</param>
/// <returns>Returns a new color from the displacement.</returns>
public FxColor Offset(int vRed, int vGreen, int vBlue)
{
int r = Math.Max(0, Math.Min(255, c.R + vRed));
int g = Math.Max(0, Math.Min(255, c.G + vGreen));
int b = Math.Max(0, Math.Min(255, c.B + vBlue));
return new FxColor(Color.FromArgb(r, g, b));
}
/// <summary>
/// Gets the value.
/// </summary>
/// <value>The value.</value>
public Color Value
{
get
{
return c;
}
}
/// <summary>
/// Gets the HEX value of this color.
/// </summary>
/// <value>The HEX.</value>
public string HEX
{
get
{
return ColorTranslator.ToHtml(c);
}
}
}

June 5th, 2008 at 12:20 pm
Dude, this site is SOOOOO black. Light just falls into it and is never seen again
June 5th, 2008 at 8:06 pm
Work in progress, wordpress theme got me 80%, I have to carry the next 20%. Unless I wanted to write a wordpress port to .NET using WebParts. Glad to have my first official comment.