/*============================================================================= Project: SharpImage Module: siColorHelper.cs Language: C# Author: Dan Mueller Date: $Date: 2007-07-06 10:57:00 +1000 (Fri, 06 Jul 2007) $ Revision: $Revision: 2 $ Copyright (c) Queensland University of Technology (QUT) 2007. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =============================================================================*/ using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Windows.Forms; using System.Diagnostics; using SharpImage.Main; using SharpImage.Properties; namespace SharpImage.Rendering { public class siColorHelper { /// /// Converts a .NET managed color to an OpenGl RGB color array. /// /// /// public static float[] To3fv(Color managedColor) { return new float[]{ (float)managedColor.R/255.0f, (float)managedColor.G/255.0f, (float)managedColor.B/255.0f }; } /// /// Converts a .NET managed color to an OpenGl RGBA color array. /// /// /// public static float[] To4fv(Color managedColor) { return new float[]{ (float)managedColor.R/255.0f, (float)managedColor.G/255.0f, (float)managedColor.B/255.0f, (float)managedColor.A/255.0f }; } /// /// Converts an array of HSV to an array of RGB. /// /// An array of double (ranging from 0.0 to 1.0). /// This method converted from Common/vtkMath.cxx. public static double[] HSVtoRGB(double h, double s, double v) { return siColorHelper.HSVtoRGB(new double[] { h, s, v }); } /// /// Converts an array of HSV to an array of RGB. /// /// /// An array of double (ranging from 0.0 to 1.0). /// This method converted from Common/vtkMath.cxx. public static double[] HSVtoRGB(double[] hsv) { float[] fhsv = new float[] { (float)hsv[0], (float)hsv[1], (float)hsv[2] }; float[] frgb = siColorHelper.HSVtoRGB(fhsv); return new double[] { (double)frgb[0], (double)frgb[1], (double)frgb[2] }; } /// /// Converts an array of HSV to an array of RGB. /// /// An array of float (ranging from 0.0 to 1.0). /// This method converted from Common/vtkMath.cxx. public static float[] HSVtoRGB(float h, float s, float v) { return siColorHelper.HSVtoRGB(new float[] { h, s, v }); } /// /// Converts an array of HSV to an array of RGB. /// /// /// An array of float (ranging from 0.0 to 1.0). /// This method converted from Common/vtkMath.cxx. public static float[] HSVtoRGB(float[] hsv) { float[] rgb = new float[3]; const float onethird = 1.0f / 3.0f; const float onesixth = 1.0f / 6.0f; const float twothird = 2.0f / 3.0f; const float fivesixth = 5.0f / 6.0f; // compute RGB from HSV if (hsv[0] > onesixth && hsv[0] <= onethird) // green/red { rgb[1] = 1.0f; rgb[0] = (onethird - hsv[0]) / onesixth; rgb[2] = 0.0f; } else if (hsv[0] > onethird && hsv[0] <= 0.5f) // green/blue { rgb[1] = 1.0f; rgb[2] = (hsv[0] - onethird) / onesixth; rgb[0] = 0.0f; } else if (hsv[0] > 0.5f && hsv[0] <= twothird) // blue/green { rgb[2] = 1.0f; rgb[1] = (twothird - hsv[0]) / onesixth; rgb[0] = 0.0f; } else if (hsv[0] > twothird && hsv[0] <= fivesixth) // blue/red { rgb[2] = 1.0f; rgb[0] = (hsv[0] - twothird) / onesixth; rgb[1] = 0.0f; } else if (hsv[0] > fivesixth && hsv[0] <= 1.0f) // red/blue { rgb[0] = 1.0f; rgb[2] = (1.0f - hsv[0]) / onesixth; rgb[1] = 0.0f; } else // red/green { rgb[0] = 1.0f; rgb[1] = hsv[0] / onesixth; rgb[2] = 0.0f; } // add Saturation to the equation. rgb[0] = (hsv[1] * rgb[0] + (1.0f - hsv[1])); rgb[1] = (hsv[1] * rgb[1] + (1.0f - hsv[1])); rgb[2] = (hsv[1] * rgb[2] + (1.0f - hsv[1])); rgb[0] *= hsv[2]; rgb[1] *= hsv[2]; rgb[2] *= hsv[2]; return rgb; } /// /// Converts an array of RGB to an array of HSV. /// /// An array of double (ranging from 0.0 to 1.0). /// This method converted from Common/vtkMath.cxx. public static double[] RGBtoHSV(double r, double g, double b) { return siColorHelper.RGBtoHSV(new double[] { r, g, b }); } /// /// Converts an array of RGB to an array of HSV. /// /// /// An array of double (ranging from 0.0 to 1.0). /// This method converted from Common/vtkMath.cxx. public static double[] RGBtoHSV(double[] rgb) { float[] frgb = new float[] { (float)rgb[0], (float)rgb[1], (float)rgb[2] }; float[] fhsv = siColorHelper.RGBtoHSV(frgb); return new double[] { (double)fhsv[0], (double)fhsv[1], (double)fhsv[2] }; } /// /// Converts an array of RGB to an array of HSV. /// /// An array of float (ranging from 0.0 to 1.0). /// This method converted from Common/vtkMath.cxx. public static float[] RGBtoHSV(float r, float g, float b) { return siColorHelper.RGBtoHSV(new float[] { r, g, b }); } /// /// Converts an array of RGB to an array of HSV. /// /// /// An array of float (ranging from 0.0 to 1.0). /// This method converted from Common/vtkMath.cxx. public static float[] RGBtoHSV(float[] rgb) { float[] hsv = new float[3]; float onethird = 1.0f / 3.0f; float onesixth = 1.0f / 6.0f; float twothird = 2.0f / 3.0f; float cmax, cmin; cmax = rgb[0]; cmin = rgb[0]; if (rgb[1] > cmax) cmax = rgb[1]; else if (rgb[1] < cmin) cmin = rgb[1]; if (rgb[2] > cmax) cmax = rgb[2]; else if (rgb[2] < cmin) cmin = rgb[2]; hsv[2] = cmax; if (hsv[2] > 0.0f) hsv[1] = (cmax - cmin) / cmax; else hsv[1] = 0.0f; if (hsv[1] > 0.0f) { if (rgb[0] == cmax) hsv[0] = onesixth * (rgb[1] - rgb[2]) / (cmax - cmin); else if (rgb[1] == cmax) hsv[0] = onethird + onesixth * (rgb[2] - rgb[0]) / (cmax - cmin); else hsv[0] = twothird + onesixth * (rgb[0] - rgb[1]) / (cmax - cmin); if (hsv[0] < 0.0f) hsv[0] += 1.0f; } else hsv[0] = 0.0f; return hsv; } /// /// Create a bitmap consisting of alternating squares indicating a transparent /// background. The square size defaults to 10 x 10 pixels. /// /// /// /// public static Bitmap CreateTransparentBackground(int width, int height) { return CreateTransparentBackground(width, height, 10); } /// /// Create a bitmap consisting of alternating squares indicating a transparent /// background. /// /// /// /// The size of the square. eg. 10 = a 10 x 10 square. /// public static Bitmap CreateTransparentBackground(int width, int height, int squareSize) { // Create bitmap Bitmap result = new Bitmap(width, height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(result); // Create brushes SolidBrush brushBack = new SolidBrush(Color.White); SolidBrush brushWhite = new SolidBrush(Color.FromArgb(128, Color.White)); SolidBrush brushBlack = new SolidBrush(Color.FromArgb(48, Color.Black)); // Draw background g.FillRectangle(brushBack, 0, 0, width, height); // Draw squares Brush brushCurrent = brushWhite; Brush brushLastY = brushWhite; int count = 0; int y = 0; int x = 0; while (y <= height) { brushLastY = brushCurrent; while (x <= width) { // Draw rectangle g.FillRectangle(brushCurrent, x, y, squareSize, squareSize); // Toggle brush brushCurrent = ToggleBrush(brushBlack, brushWhite, brushCurrent); // Update counters count++; x += squareSize; } // Update counters y += squareSize; x = 0; // Toggle brush brushCurrent = ToggleBrush(brushBlack, brushWhite, brushLastY); } // Clean up and return g.Flush(); g.Dispose(); return result; } /// /// Toggles the current brush between the two sources. /// /// /// /// /// private static Brush ToggleBrush(Brush first, Brush second, Brush current) { // Change brush if (current == first) return second; else if (current == second) return first; else return current; } /// /// Returns a color constructed from the RGB components of the given color. /// /// /// public static Color ColorFromRgb(Color color) { return Color.FromArgb(color.R, color.G, color.B); } /// /// Returns a color constructed from the RGB components of the given color. /// /// /// public static Color ColorFromRgb(object oColor) { Color color = (Color)oColor; return Color.FromArgb(color.R, color.G, color.B); } /// /// Converts a color to a string for serialization. /// /// /// public static string SerializeColor(Color color) { return String.Format("{0}, {1}, {2}, {3}", color.A, color.R, color.G, color.B); } /// /// Deserializes a color from a string. /// /// /// public static Color DeserializeColor(string strcolor) { string[] split = strcolor.Split(new char[] { ',' }); byte a = byte.Parse(split[0]); byte r = byte.Parse(split[1]); byte g = byte.Parse(split[2]); byte b = byte.Parse(split[3]); return Color.FromArgb(a, r, g, b); } } }