I am doing some graphics in C# and I need to convert a 6-digit rgb hexadecimal such as 0xaabbcc (rr gg bb) into 3 RGB values. I don't want to use Color
. I am not developing for Windows so I don't want to use the Microsoft.CSharp
library. Even if there is some way around that I'm not very fond of the .NET framework due to all the fancy fluff, I prefer to make my own class libraries and such.
I was able to convert 3 RGB values into a single Hex number but I don't know how to do the opposite.
private static long MakeRgb(byte red, byte green, byte blue)
{
return ((red*0x10000) + (green*0x100) + blue);
}
There is my code for the original conversion.
Anyone know a good way to separate a 6 digit hex number into 3 separate bytes?
EDIT:
I am not using the .NET framework, not using Mono, and I do not have access to System.Drawing.Color.
This should not be marked as a duplicate because it has nothing to do with .NET.
You could use bitmasking
Both
System.Drawing.Color
andMicrosoft.CSharp
are available on Mono (which I assume you're using if you're not using .NET)In any case, this was already a good answer, but if you're really not going to use
System.Drawing.Color
, then you should probably write your own class.Old fashion way that'll work in most languages: