61 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace DevComponents.DotNetBar.Layout
{
internal static class ColorHelpers
{
/// <summary>
/// Converts hex string to Color type.
/// </summary>
/// <param name="rgbHex">Hexadecimal color representation.</param>
/// <returns>Reference to Color object.</returns>
public static Color GetColor(string rgbHex)
{
if (string.IsNullOrEmpty(rgbHex))
return Color.Empty;
if (rgbHex.Length == 8)
return Color.FromArgb(Convert.ToInt32(rgbHex.Substring(0, 2), 16),
Convert.ToInt32(rgbHex.Substring(2, 2), 16),
Convert.ToInt32(rgbHex.Substring(4, 2), 16),
Convert.ToInt32(rgbHex.Substring(6, 2), 16));
return Color.FromArgb(Convert.ToInt32(rgbHex.Substring(0, 2), 16),
Convert.ToInt32(rgbHex.Substring(2, 2), 16),
Convert.ToInt32(rgbHex.Substring(4, 2), 16));
}
/// <summary>
/// Converts hex string to Color type.
/// </summary>
/// <param name="rgb">Color representation as 32-bit RGB value.</param>
/// <returns>Reference to Color object.</returns>
public static Color GetColor(int rgb)
{
if (rgb == -1) return Color.Empty;
return Color.FromArgb((rgb & 0xFF0000) >> 16, (rgb & 0xFF00) >> 8, rgb & 0xFF);
}
/// <summary>
/// Converts hex string to Color type.
/// </summary>
/// <param name="rgb">Color representation as 32-bit RGB value.</param>
/// <returns>Reference to Color object.</returns>
public static Color GetColor(int alpha, int rgb)
{
if (rgb == -1) return Color.Empty;
return Color.FromArgb(alpha, (rgb & 0xFF0000) >> 16, (rgb & 0xFF00) >> 8, rgb & 0xFF);
}
public static string ToArgbString(Color color)
{
if (color.IsEmpty) return "";
if (color.A == 255)
return color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2");
return color.A.ToString("X2") + color.R.ToString("X2") + color.G.ToString("X2") + color.B.ToString("X2");
}
}
}