Game Development Reference
In-Depth Information
3.9C OLOR
Color can be represented using different data types, depending on the target platform some
color formats may be more appropriate. For simplicity and compatibility, we will describe
and operate on colors internally using floating point values in the range that represent
thecontribution ofeachcolorchannel towardsthefinalcolor,thismakesthedifferentmath-
ematical operations more intuitive to work with. We will then provide the means to convert
this representation into an integer format that is useable by the graphics device. It is import-
ant to understand the color format our target platform requires to make sure we provide the
corresponding conversion facilities.
The example code is built on Direct3D, so we will focus primarily on using colors in an
ARGB 8-bit format (and in some situations in ABGR), this means that for each color chan-
nel we will use 8 bits of data, this will allow us to represent a color in an unsigned 32-bit
type.
Figure 37 - Color represented by four 8-bit channels.
An unsigned 8 bit value has a range from {0..255} if we represent an unsigned 8 bit value
in hexadecimal, the value ranges from {0..FF} which allows us to represent colors in the fa-
miliar hexadecimal notation used across the web.
It's useful to understand how to convert from one color format to another, particularly when
working in multiple platforms, if our yellows have become cyan, and our red have become
blue, then we may need to be certain we are sending the colors in the right format to the
graphics hardware.
The first thing we need to do is bring the floating point representation of a color into the 8
bit range, we do this by multiplying it by 255. Once we have the 8 bit value for each color
channel, we will need to combine them into a single 32-bit type, an unsigned int. We do this
with bit manipulation, we have 4 8-bit values that we need to bit shift into place into the
Search WWH ::




Custom Search