|
using System;
using System.Globalization;
using System.Text;
using System.Windows.Media;
namespace WpfColorConversionDemo
{
class Program
{
#region Structs
/// <summary>
/// An HSB color
/// </summary>
public struct HsbColor
{
// The Hue of the color [0,360]
public double H;
// The Saturation of the color [0,1]
public double S;
// The Brightness of the color [0,1]
public double B;
// The Alpha (opaqueness) of the color [0,1]
public double A;
}
#endregion
static void Main(string[] args)
{
var hexIn = "#FF7E188C";
var rgbColor = HexToRgb(hexIn);
Console.WriteLine("Hex in = {0}", hexIn);
Console.WriteLine();
Console.WriteLine("*** RGB IN ***");
Console.WriteLine("R={0}", rgbColor.R);
Console.WriteLine("G={0}", rgbColor.G);
Console.WriteLine("B={0}", rgbColor.B);
Console.WriteLine("A={0}", rgbColor.A);
Console.WriteLine();
var hsbColor = RgbToHsb(rgbColor);
Console.WriteLine("*** HSB OUT ***");
Console.WriteLine("H={0}", hsbColor.H.ToString("#"));
Console.WriteLine("S={0}", hsbColor.S.ToString("P0"));
Console.WriteLine("B={0}", hsbColor.B.ToString("P0"));
Console.WriteLine("A={0}", hsbColor.A.ToString("P0"));
Console.WriteLine();
rgbColor = HsbToRgb(hsbColor);
Console.WriteLine("*** RGB OUT ***");
Console.WriteLine("R={0}", rgbColor.R);
Console.WriteLine("G={0}", rgbColor.G);
Console.WriteLine("B={0}", rgbColor.B);
Console.WriteLine("A={0}", rgbColor.A);
Console.WriteLine();
var hexOut = RgbToHex(rgbColor);
Console.WriteLine("Hex out = {0}", hexOut);
Console.WriteLine();
bool b = (hexOut == hexIn);
Console.WriteLine("Hex out = hex in: {0}", b);
Console.ReadLine();
}
#region RGB-Hex Conversions
/// <summary>
/// Converts a hex-string representation of an RGB color to a WPF Color object.
/// </summary>
/// <param name="hexRgbColor">A hex-string representation of an RGB color.</param>
/// <returns>A WPF Color object.</returns>
public static Color HexToRgb(string hexRgbColor)
{
var rgbColor = new Color();
var s = hexRgbColor.Substring(1, 2);
var i = Int32.Parse(s, NumberStyles.HexNumber);
rgbColor.A = Convert.ToByte(i);
s = hexRgbColor.Substring(3, 2);
i = Int32.Parse(s, NumberStyles.HexNumber);
rgbColor.R = Convert.ToByte(i);
s = hexRgbColor.Substring(5, 2);
i = Int32.Parse(s, NumberStyles.HexNumber);
rgbColor.G = Convert.ToByte(i);
s = hexRgbColor.Substring(7, 2);
i = Int32.Parse(s, NumberStyles.HexNumber);
rgbColor.B = Convert.ToByte(i);
return rgbColor;
}
/// <summary>
/// Converts an RGB value to a hex-string equivalent.
/// </summary>
/// <param name="rgbColor">The WPF Color object to convert.</param>
/// <returns>A hex string that represents the RGB value.</returns>
public static string RgbToHex(Color rgbColor)
{
StringBuilder sb = new StringBuilder("#");
sb.Append(rgbColor.A.ToString("X"));
sb.Append(rgbColor.R.ToString("X"));
sb.Append(rgbColor.G.ToString("X"));
sb.Append(rgbColor.B.ToString("X"));
return sb.ToString();
}
#endregion
#region RGB-HSV Conversions
/// <summary>
/// Converts an RGB color to an HSB color.
/// </summary>
/// <param name="rgbColor">The RGB color to convert.</param>
/// <returns>The HSB color equivalent of the RGB color passed in.</returns>
/// <remarks>Source: http://msdn.microsoft.com/en-us/library/ms771620.aspx</remarks>
private static HsbColor RgbToHsb(Color rgbColor)
{
/* Hue values range between 0 and 360. All
* other values range between 0 and 1. */
// Create HSB color object
var hsbColor = new HsbColor();
// Get RGB color component values
var r = Convert.ToInt32(rgbColor.R);
var g = Convert.ToInt32(rgbColor.G);
var b = Convert.ToInt32(rgbColor.B);
var a = Convert.ToInt32(rgbColor.A);
// Get min, max, and delta values
double min = Math.Min(Math.Min(r, g), b);
double max = Math.Max(Math.Max(r, g), b);
double delta = max - min;
/* Black (max = 0) is a special case. We
* simply set HSB values to zero and exit. */
// Black: Set HSB and return
if (max == 0.0)
{
hsbColor.H = 0.0;
hsbColor.S = 0.0;
hsbColor.B = 0.0;
hsbColor.A = a;
return hsbColor;
}
/* Now we process the normal case. */
// Set HSB Alpha value
var alpha = Convert.ToDouble(a);
hsbColor.A = alpha / 255;
// Set HSB Hue value
if (r == max) hsbColor.H = (g - b) / delta;
else if (g == max) hsbColor.H = 2 + (b - r) / delta;
else if (b == max) hsbColor.H = 4 + (r - g) / delta;
hsbColor.H *= 60;
if (hsbColor.H < 0.0) hsbColor.H += 360;
// Set other HSB values
hsbColor.S = delta / max;
hsbColor.B = max / 255;
// Set return value
return hsbColor;
}
/// <summary>
/// Converts an HSB color to an RGB color.
/// </summary>
/// <param name="hsbColor">The HSBA color to convert.</param>
/// <returns>The RGBA color equivalent of the HSBA color passed in.</returns>
/// Source: http://msdn.microsoft.com/en-us/library/ms771620.aspx
private static Color HsbToRgb(HsbColor hsbColor)
{
// Initialize
var rgbColor = new Color();
/* Gray (zero saturation) is a special case.We simply
* set RGB values to HSB Brightness value and exit. */
// Gray: Set RGB and return
if (hsbColor.S == 0.0)
{
rgbColor.A = Convert.ToByte(hsbColor.A * 255);
rgbColor.R = Convert.ToByte(hsbColor.B * 255);
rgbColor.G = Convert.ToByte(hsbColor.B * 255);
rgbColor.B = Convert.ToByte(hsbColor.B * 255);
return rgbColor;
}
/* Now we process the normal case. */
var h = (hsbColor.H == 360) ? 0 : hsbColor.H / 60;
var i = Convert.ToInt32(Math.Truncate(h));
var f = h - i;
var p = hsbColor.B * (1.0 - hsbColor.S);
var q = hsbColor.B * (1.0 - (hsbColor.S * f));
var t = hsbColor.B * (1.0 - (hsbColor.S * (1.0 - f)));
double r, g, b;
switch (i)
{
case 0:
r = hsbColor.B;
g = t;
b = p;
break;
case 1:
r = q;
g = hsbColor.B;
b = p;
break;
case 2:
r = p;
g = hsbColor.B;
b = t;
break;
case 3:
r = p;
g = q;
b = hsbColor.B;
break;
case 4:
r = t;
g = p;
b = hsbColor.B;
break;
default:
r = hsbColor.B;
g = p;
b = q;
break;
}
// Set WPF Color object
rgbColor.A = Convert.ToByte(hsbColor.A * 255);
rgbColor.R = Convert.ToByte(r * 255);
rgbColor.G = Convert.ToByte(g * 255);
rgbColor.B = Convert.ToByte(b * 255);
// Set return value
return rgbColor;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
David Veeneman is a financial planner and software developer. He is the author of "The Fortune in Your Future" (McGraw-Hill 1998). His company, Foresight Systems, develops planning and financial software.