0
ARGB(alpha-RGB)値からCGColor
を作成します。それは可能ですか?どうやって?Xamarin.iOSの{alpha、red、green、blue}の値からCGColorを作成します。
ARGB(alpha-RGB)値からCGColor
を作成します。それは可能ですか?どうやって?Xamarin.iOSの{alpha、red、green、blue}の値からCGColorを作成します。
あなたが渡す値を持っている場合は、単純に次の操作を行うことができます。
UIColor.FromRGBA(red, green, blue, alpha).CGColor;
私たちは、あなたがそれを必要とするだけで包み、同様UIColorsにハッシュ値を変換するための小さなヘルパークラスを使用します。単純にハッシュ値( "#ffffff")を渡します。その後、それが返すUIColor上で.CGColorを呼び出すか、あるいはそれに応じてクラスを調整します。
class Colors
{
public static UIColor FromHexString(string hexValue, float alpha = 1.0f)
{
try
{
string colorString = hexValue.Replace("#", "");
float red, green, blue;
switch (colorString.Length)
{
case 3: // #RGB
{
red = Convert.ToInt32(string.Format("{0}{0}", colorString.Substring(0, 1)), 16)/255f;
green = Convert.ToInt32(string.Format("{0}{0}", colorString.Substring(1, 1)), 16)/255f;
blue = Convert.ToInt32(string.Format("{0}{0}", colorString.Substring(2, 1)), 16)/255f;
return UIColor.FromRGBA(red, green, blue, alpha);
}
case 6: // #RRGGBB
{
red = Convert.ToInt32(colorString.Substring(0, 2), 16)/255f;
green = Convert.ToInt32(colorString.Substring(2, 2), 16)/255f;
blue = Convert.ToInt32(colorString.Substring(4, 2), 16)/255f;
return UIColor.FromRGBA(red, green, blue, alpha);
}
case 8: // #RRGGBBAA
{
red = Convert.ToInt32(colorString.Substring(0, 2), 16)/255f;
green = Convert.ToInt32(colorString.Substring(2, 2), 16)/255f;
blue = Convert.ToInt32(colorString.Substring(4, 2), 16)/255f;
alpha = Convert.ToInt32(colorString.Substring(6, 2), 16)/255f;
return UIColor.FromRGBA(red, green, blue, alpha);
}
default:
throw new ArgumentOutOfRangeException(string.Format("Invalid color value {0} is invalid. It should be a hex value of the form #RBG, #RRGGBB", hexValue));
}
}
catch (Exception genEx)
{
return null;
}
}
}