views:

125

answers:

2

Hi, I've looked around, but don't see this question - maybe its too easy, but I don't have it.

If I have file containing color values like:

255, 65535, 65280 ( I think this is green)

is there a way to either:

  1. convert these to a System.Drawing.Color type or....better yet..
  2. have .NET accept them as is to represent the color?

thanks,

+1  A: 

From your comments on an earlier version of this answer it seems that each number in the file represents one colour, and that these are packed bytes:

65280 (= 0xFF00) -> (0, 255, 0)
65535 (= 0xFFFF) -> (255, 255, 0)

So you want the low byte in the first (red?) part of the triple, and the next higher byte in the second (blue?) part of the triple. I am guessing that values over 65535 would go into the third byte of the triple.

You can do this easily using bit masking and bit shift operators:

int r = n && 0xFF;
int g = (n >> 8) & 0xFF;
int b = (n >> 16) & 0xFF;

i.e. shift it right by 8 bits each time, and select out the bottom 8 bits.

NOTE: You can also do this directly using Color.FromArgb(Int32), which saves you from having to do the unpacking. But this will only work if the numbers in your file are packed in the right way. They would need to be in AARRGGBB format. For example, 255 (0x000000FF) = Blue, 65280 (0x0000FF00) = Green, 16711680 (0x00FF0000) = Red. I am not sure whether your numbers are in the right order which is why I have covered the explicit unpacking technique.

itowlson
Sorry, I didn't mean to say that the comma delimted string was my color, but rather: is there a way to turn 65535 into an rgb sequnce.Or 65280 into an rgb sequence. Is there any method for turning a number like 65535 into (0, 255, 0) or someting close. thx.
Chris
I mean turning 65820 into (0,255,0). 65535 would be turned into (255,255,0). I ask this because I want to go from a number like 65535 to System.Drawing.Color type. And if I can go from 65535 to (255,255,0) then I can take it from there ;) thx
Chris
I've updated my answer to describe how to extract the byte values from the numeric values.
itowlson
Excellent. Thanks very much!
Chris
A: 

This is how you use the color type:

dim someColor as Color = color.FromArgb(red, green, blue)
  • Be sure to use Imports System.Drawing
  • Change red, green and blue out for their respective rgb values (ie: 0-255)
Diakonia7