28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5 ■ An Introduction to Game Design: Concepts, Multimedia, and Using Scene Builder<br />

Representing Color and Alpha in <strong>Java</strong> 8 Game Code: Using Hexadecimal Notation<br />

Now that you know what color depth and alpha channels are, and that color and transparency are represented by<br />

using a combination of four different image channels (alpha, red, green, and blue [ARGB]) within any given digital<br />

image, it is important to understand how, as programmers, you are supposed to represent these four ARGB image<br />

color and transparency channel values in <strong>Java</strong> 8 and <strong>Java</strong>FX.<br />

In the <strong>Java</strong> programming language, color and alpha are used not only in 2D digital imagery (commonly referred<br />

to as bitmap imagery), but also in 2D illustration (commonly referred to as vector imagery). Colors and transparency<br />

values are also often used across a number of different color setting options. As you have already seen with the<br />

Stage object and the Scene object, you can set a background color (or transparency value) for a stage, scene, layout<br />

container (StackPane), or UI control, among other things.<br />

In <strong>Java</strong> (and thus <strong>Java</strong>FX) different levels of ARGB color intensity values are represented using hexadecimal<br />

notation. Hexadecimal (or hex) is based on the original Base16 computer notation. This was used long ago to<br />

represent 16 bits of data value. Unlike the more common Base10, which counts from 0 to 9, Base16 notation counts<br />

from 0 to F, where F represents the Base10 value of 15 (0 to 15 yields 16 data values).<br />

A hexadecimal value in <strong>Java</strong> always starts with a 0 and an x, like this: 0xFFFFFF. This hexadecimal color value<br />

represents the Color.WHITE constant and uses no alpha channel. Each of the six slots in this 24-bit hexadecimal<br />

representation stands for a single Base16 value, so to get the 256 values required for each RGB color will take two<br />

slots, as 16 × 16 = 256. Therefore, to represent a 24-bit image using hexadecimal notation, you would need to have<br />

six slots after the pound sign to hold each of the six hexadecimal data values (data pairs representing 256 levels of<br />

value each). If you multiply 16 × 16 × 16 × 16 × 16 × 16 you will get the 16,777,216 colors that are possible using 24-bit,<br />

truecolor image data.<br />

The hexadecimal data slots represent RGB values in the following format: 0xRRGGBB. For the <strong>Java</strong> constant<br />

Color.WHITE, all the red, green, and blue channels in the hexadecimal color data value representation are at the full<br />

(maximum color value) luminosity setting. If you add all these colors together, you will get white light.<br />

The color yellow would be represented by the red and green channels’ being on, and the blue channel’s being<br />

off, so a hexadecimal representation for Color.YELLOW would therefore be 0xFFFF00, where both the red and green<br />

channel slots are fully on (FF, or a 255 Base10 data value), and the blue channel slots are fully off (00, or a 0 value).<br />

The eight hexadecimal data slots for an ARGB value will hold data using the following format: 0xAARRGGBB.<br />

Thus, for the Color.WHITE, all alpha, red, green, and blue channels in the hexadecimal color data value<br />

representation would be at their maximum luminosity (or opacity), and the alpha channel would be fully opaque, that<br />

is, not transparent, as represented by an FF value. Therefore, the hexadecimal value for the Color.WHITE constant<br />

would be 0xFFFFFFFF.<br />

A 100 percent transparent alpha channel can be represented by setting an alpha slot to 0, as you observed when<br />

you created a windowless <strong>Java</strong> 8 application (see Chapter 4). Therefore, you would represent transparent image pixel<br />

values using any value between 0x00000000 and 0x00FFFFFF. It is important to note that if an alpha channel value<br />

equates to full transparency, it would follow that the 16,777,216 color values that could be contained in the other six<br />

(RGB) hexadecimal data value slots will not matter at all, because that pixel, being transparent, will be evaluated as<br />

not being there and thus will not be composited in the final image or animation composite image.<br />

Digital Image Masking: Using Alpha Channels to Create Game Sprites<br />

One of the primary applications for alpha channels in game design is to mask out areas of an image or animation<br />

(series of images) so that it can be used as a game sprite in a game play image-compositing scenario. Masking is the<br />

process of cutting subject matter out of a digital image so that the subject matter can be placed on its own virtual layer,<br />

using alpha channel transparency values. This is done using a digital imaging software package, such as GIMP.<br />

Digital image–compositing software packages, such as Photoshop and GIMP, feature tools that are included for use<br />

in masking and image compositing. You cannot do effective image compositing without doing effective masking, so this<br />

is an important area to master for game designers who wish to integrate graphics elements, such as image sprites and<br />

sprite animation, into their game designs. The art of digital image masking has been around for a very long time!<br />

www.it-ebooks.info<br />

109

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!