Numeric representation of a color
What would be the best format to numerically represent a color in .NET so that I wouldn't have to use the Color object? Right now I am saving the color as a the HTML representation, but in order to use it I have to parse it out.
I am dealing with a 800x600 canvas that stores a color value for each pixel and I need to be able to rende开发者_运维问答r the color out as quick as possible without bloating my application out to storing 500k+ color objects.
You can use a standard Int32 to store a color.
This is particularly nice because the Color types in the framework support this directly via Color.ToArgb() and Color.FromArgb() and similar methods.
A colour is a 24 bit RGB or 32 bit RGBA value - easily represented in a 32-bit int. The Color struct just wraps this to make access more convenient, but if you go low-level (pixel or palette data in a bitmap pixel) that's all it is.
If you're storing a large array of colours, then use a Bitmap and simply set/read the pixel value for the location you're interested in.
An instance of the color structure only has 4 members (Alpha, Red, Green, Blue), each of type Byte
. So you could just store each color using a 32-bit integer instead, but you may be underwhelmed at how much memory you end up saving, if any...
Of course, if you do not need all 4 color channels, you can use a smaller structure to store only the channels you need (EG, just use a Byte to store Red), and save a fair chunk of space.
Is this what you need?
- http://www.w3.org/TR/REC-html40/types.html#h-6.5
- CSS Colour Module Level 3
You will only need to use Color.FromArgb() and Color.ToArgb() in order to get your colour.
精彩评论