HEX - RGB cinsinden renk dönüşümleri yapmaya yarayan csharp kodu alternatifidir.
#region VSKicks HEX - RGB Dönüştürücü
/// <summary>
/// Hex cinsinden verilmiş rengi
/// RGB cinsine dönüştürüyor.
/// http://www.vcskicks.com/hex-to-rgb.php
/// </summary>
/// <param name="hexColor"></param>
/// <returns>Color</returns>
public static Color HexToColor(string hexColor)
{
//Remove # if present
if (hexColor.IndexOf('#') != -1)
hexColor = hexColor.Replace("#", "");
int red = 0;
int green = 0;
int blue = 0;
if (hexColor.Length == 6)
{
//#RRGGBB
red = int.Parse(hexColor.Substring(0, 2), NumberStyles.AllowHexSpecifier);
green = int.Parse(hexColor.Substring(2, 2), NumberStyles.AllowHexSpecifier);
blue = int.Parse(hexColor.Substring(4, 2), NumberStyles.AllowHexSpecifier);
}
else if (hexColor.Length == 3)
{
//#RGB
red = int.Parse(hexColor[0].ToString() + hexColor[0].ToString(), NumberStyles.AllowHexSpecifier);
green = int.Parse(hexColor[1].ToString() + hexColor[1].ToString(), NumberStyles.AllowHexSpecifier);
blue = int.Parse(hexColor[2].ToString() + hexColor[2].ToString(), NumberStyles.AllowHexSpecifier);
}
return Color.FromArgb(red, green, blue);
}
/// <summary>
/// RGB rengi Hex cinsinden yazdırıyor.
/// </summary>
/// <param name="rgb">Color.fromArgb renk.</param>
/// <returns>string</returns>
public static string ColorToHex(Color rgb)
{
return rgb.R.ToString("X2") + rgb.G.ToString("X2") + rgb.B.ToString("X2");
}
/// <summary>
/// Hex cinsinden verilen renk kodunun
/// geçreli olup olmadığını kontrol etmeye yarıyor.
/// http://www.vcskicks.com/hex-to-rgb.php
/// </summary>
/// <param name="hexColor">Hex cinsinden renk kodu.</param>
/// <returns>bool</returns>
public static bool IsValidHex(string hexColor)
{
if (hexColor.StartsWith("#"))
return hexColor.Length == 7 || hexColor.Length == 4;
else
return hexColor.Length == 6 || hexColor.Length == 3;
}
#endregion