BigPete
BigPete

Reputation: 779

converting ARGB to RGB in WPF

I have created a variable using System.Windows.Media.Color.

I can display the Hex value from this variable using ToString(); however that also gives me the alpha value. Is there anyway to get just the RGB values out? If I try using Color.R.ToString(); it only gives me the numerical value.

Do I have to change it to hex manually or is there a built in method for this?

Upvotes: 2

Views: 4414

Answers (2)

yuriks
yuriks

Reputation: 1094

From what I gather from your question you want to convert it to a hex-format color. You can just individually convert each of the color's members, leaving out the alpha:

string color_str = string.Format("#{0:X2}{1:X2}{2:X2}", Color.R, Color.G, Color.B);

Upvotes: 5

AbrahamJP
AbrahamJP

Reputation: 3440

I came to find the "ColorTranslator" also doing the Color to Hex conversion in a more concise manner.

 Color C = Color.Red;
 string HexVal = ColorTranslator.ToHtml(Color.FromArgb(C.R, C.G, C.B)));

Upvotes: 2

Related Questions