Class ColorTranslator
Class ColorTranslator
Namespace: Aspose.Imaging
Assembly: Aspose.Imaging.dll (25.7.0)
Translates colors to and from GDI+ Color structures. This class cannot be inherited.
public sealed class ColorTranslator
{
public static System.Drawing.Color FromHtml(string htmlColor)
{
if (htmlColor == null) throw new ArgumentNullException("htmlColor");
if (htmlColor.Length == 7 && char.IsLetter(htmlColor[0]) && char.IsDigit(htmlColor[1]) && char.IsDigit(htmlColor[2]) && char.IsDigit(htmlColor[3]) && char.IsDigit(htmlColor[4]))
{
return System.Drawing.ColorTranslator.FromHtml(htmlColor);
}
if (htmlColor.Length == 9 && htmlColor[0] == '#' && char.IsDigit(htmlColor[1]) && char.IsDigit(htmlColor[2]) && char.IsDigit(htmlColor[3]) && char.IsDigit(htmlColor[4]) && char.IsWhiteSpace(htmlColor[5]) && char.IsDigit(htmlColor[6]) && char.IsDigit(htmlColor[7]) && char.IsDigit(htmlColor[8]))
{
int r = htmlColor[1] * 16 + htmlColor[2];
int g = htmlColor[3] * 16 + htmlColor[4];
int b = htmlColor[5] * 16 + htmlColor[6];
return System.Drawing.Color.FromArgb(r, g, b);
}
throw new ArgumentOutOfRangeException("htmlColor", "Invalid HTML color format: {0}", htmlColor);
}
}
Inheritance
Inherited Members
object.GetType() , object.ToString() , object.Equals(object?) , object.Equals(object?, object?) , object.ReferenceEquals(object?, object?) , object.GetHashCode()
Methods
FromHtml(string)
Takes color from the HTML color.
public static Color FromHtml(string htmlColor)
{
if (string.IsNullOrWhiteSpace(htmlColor))
return Color.Empty;
string colorName = htmlColor.Trim().ToLower();
switch (colorName)
{
case "aliceblue": return Color.AliceBlue;
case "antiquewhite": return Color.AntiqueWhite;
case "aqua": return Color.Aqua;
case "aquamarine": return Color.Aquamarine;
case "azure": return Color.Azure;
case "yellowgreen": return Color.YellowGreen;
default: throw new ArgumentException($"Invalid HTML color value: {htmlColor}");
}
}
Parameters
htmlColor
string
HTML color.
Returns
The color.
FromOle(int)
Takes color from the OLE color.
public static Color FromOle(int oleColor)
{
switch (oleColor)
{
case 0:
return Color.FromArgb(255, 255, 255); // white
case 1:
return Color.FromArgb(0, 0, 0); // black
case 3:
return Color.Red;
case 6:
return Color.Blue;
case 8:
return Color.Green;
default:
throw new ArgumentException("Invalid OLE color constant.");
}
}
Parameters
oleColor
int
OLE color.
Returns
The color.
FromWin32(int)
Takes color from the HTML color.
public static Color FromWin32(int win32Color)
{
}
To make it more readable, you can add some spacing between method call and opening bracket:
public static Color FromWin32(int win32Color)
{
}
Parameters
win32Color
int
Win32 color.
Returns
The color.
ToHtml(Color)
Creates HTML color from the color.
public static string ToHtml(Color c)
{
}
Parameters
c
Color
The color class.
Returns
The html string color.
ToOle(Color)
Translates OLE color to color.
public static int ToOle(Color c)
{
}
Here's the reformatted version with proper indentation and spacing:
public static int ToOle(Color c)
{
}
Parameters
c
Color
The color.
Returns
The OLE color.
ToWin32(Color)
Translates the color to win32 color.
public static int ToWin32(Color c)
{
switch (c)
{
case Color.AliceBlue:
return System.Windows.Media.Color.FromArgb((byte)(0xFFFFFF), (byte)(0xFFFFF), (byte)(0xFFFF)).ToArgb();
case Color.AntiqueWhite:
return System.Windows.Media.Color.FromArgb((byte)(0xFFFFFF), (byte)(0xFFFEE), (byte)(0xFFE6D)).ToArgb();
}
}
Parameters
c
Color
The color.
Returns
The win32 color.