Convert RGB to hexadecimal

J

John

I want to convert the value of color.RGB of font object to hexadecimal
value.
How is it possible. Can some one help me.
I want to do it through my code internally without using any third party
tool.

John
 
D

Dennis Myrén

This is an example function:
<code>
public static string ToHtml ( System.Drawing.Color color )
{
return string.Concat("#", (color.ToArgb() & 0x00FFFFFF).ToString("X6"));
}
</code>

but also we should handle the transparent color, we could do that by
modifying the function a bit:
<code>
public static string ToHtml ( System.Drawing.Color color )
{
if (System.Drawing.Color.Transparent == color)
return "Transparent";
return string.Concat("#", (color.ToArgb() & 0x00FFFFFF).ToString("X6"));
}
</code>
 
J

John

Dennis Myrén said:
This is an example function:
<code>
public static string ToHtml ( System.Drawing.Color color )
{
return string.Concat("#", (color.ToArgb() & 0x00FFFFFF).ToString("X6"));
}
</code>

but also we should handle the transparent color, we could do that by
modifying the function a bit:
<code>
public static string ToHtml ( System.Drawing.Color color )
{
if (System.Drawing.Color.Transparent == color)
return "Transparent";
return string.Concat("#", (color.ToArgb() & 0x00FFFFFF).ToString("X6"));
}
</code>


I used your code and it works fine for some sample code, but in my
application I get the color as RGB long value and I try to convert it to
system color using this method

r=(System.Drawing.Color)System.ComponentModel.TypeDescriptor.GetConverter(typeof(System.Drawing.Color)).ConvertFromString(ppFont.Color.RGB.ToString());

this results in wrong color creation. i.e for blue color it gives red color.

Is there some other method through which I can convert the RGB long value to
color object.



Please help.
 
D

Dennis Myrén

You can use the static FromArgb:
System.Drawing.Color color = System.Drawing.Color.FromArgb(longRgb);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top