convert integer to color

convert integer to color

Anonymous
Not applicable
2,290 Views
4 Replies
Message 1 of 5

convert integer to color

Anonymous
Not applicable

I'm creating a WPF app for Revit and have some confusion about converting integers to colors. The code below works but I had to change the blue and red channels when creating the System.Windows.Media.Color for my window or the color was wrong.

 

System.Drawing.Color c = System.Drawing.Color.FromArgb(tnt.get_Parameter(BuiltInParameter.LINE_COLOR).AsInteger());
GraphicColour = System.Windows.Media.Color.FromRgb(c.B, c.G, c.R); //B and R need to be reversed

I saw Jeremy Tammik's post on "The Bullding Coder" blog  and tried to use his IntToColor function but had the same result, the channels had to be flipped.

 

Does anyone know why this is?

Accepted solutions (1)
2,291 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
I don't know why it's so, but I experienced the same issue and just accepted it 🙂
0 Likes
Message 3 of 5

Anonymous
Not applicable
Accepted solution

I found this works.

 

private Color ConvertInt(int rgb)
{
	byte[] bArray = BitConverter.GetBytes(rgb);
	if(BitConverter.IsLittleEndian)
		return new Color(bArray[0], bArray[1], bArray[2]);
	else
		return new Color(bArray[1], bArray[2], bArray[3]);
}

Seems it has to do with the architecture of the machine.

Message 4 of 5

Anonymous
Not applicable

"Seems it has to do with the architecture of the machine" ?

 

Does this means this function could behaive differently on each PC ?

0 Likes
Message 5 of 5

Anonymous
Not applicable

It should act the same on any machine. The Endian of a machine refers to where the machine stores the negative symbol on an integer. Some store it at the beginning (-100) and some at the end (100-). This function takes that into account.