• 16
• 15
• 11
• 9
• 10

# Question about Win32 and c++ data types

This topic is 3074 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I'm trying to make a Gradient Fill of a rectangle, I looked in the documentation, and understand how it's done, but I can't convert my values to get my desired results. Here is the source on MSDN of how to do this.
// Create an array of TRIVERTEX structures that describe
// positional and color values for each vertex. For a rectangle,
// only two vertices need to be defined: upper-left and lower-right.
TRIVERTEX vertex[2] ;
vertex[0].x     = 0;
vertex[0].y     = 0;
vertex[0].Red   = 0x0000;
vertex[0].Green = 0x8000;
vertex[0].Blue  = 0x8000;
vertex[0].Alpha = 0x0000;

vertex[1].x     = 300;
vertex[1].y     = 80;
vertex[1].Red   = 0x0000;
vertex[1].Green = 0xd000;
vertex[1].Blue  = 0xd000;
vertex[1].Alpha = 0x0000;

// Create a GRADIENT_RECT structure that
// references the TRIVERTEX vertices.
gRect.UpperLeft  = 0;
gRect.LowerRight = 1;

// Draw a shaded rectangle.


I'm wondering why I have to use hex (i think that's what it is) in the TRIVERTEX.Alpha, TRIVERTEX.Red, TRIVERTEX.Green, and TRIVERTEX.Blue fields? I looked up the TRIVERTEX structure, and it turns out they are COLOR16 variables, which are actually unsigned shorts. So my question is why can't i use regular numbers to give them their value. Like this
//for the color white
vertex[0].Alpha = 255;
vertex[0].Red = 255;
vertex[0].Green = 255;
vertex[0].Blue = 255;

yeah sure it compiles fine, but it doesn't work as intended.
Quote:
 The color information of each channel is specified as a value from 0x0000 to 0xff00
I don't really understand how those values represent colors. 0 to 65280 in decimal...so to convert to the 0-255 system i'm used to
COLOR16 theirValue = 65280/255 * myValue;

That's really not how i'm expected to do it is it? Should I be reading up on bits and bytes and how they're stored (I think there's some kind of shift or something) Thanks, ArchG

##### Share on other sites
You don't have to use hex. Use values from 0 to 65280 decimal if you like. It appears the expected values are 0 to 0xff00 or 0 to 65280. Your values of 255 are just 255/65280 or 0.0039 (a fraction of a percent) of the full range.

If you want to use values from 0 to 255, and you'd like to see something other than black with alpha = 0.0039, you could use a function like Color16Value(x) = 256*x. That way Color16Value(255) would be 256*255 or 0xff00, giving you full range.

##### Share on other sites
There's no difference in meaning between specifying integer constants in decimal, or in hex (apart from the base, obviously). You could write, say,
int playerAge = 28;
or..
int playerAge = 0x1C;

The two constants, while they appear differently textually in the source code (obviously due to the prefix and different base), both represent the same value (28 in decimal).

In some cases it can easier to represent values using hex, like in this case. The docs most likely say that the range is '0x0 - 0xff00' instead of '0 - 65280' because 0xff00 is a 'tidier' way of representing the upper bound than 65280.

This also makes it obvious that you can easily represent your normal 8-bit range (0-255, 0x0-0xff) in this extended range simply by multiplying the values by 256 (as mentioned by Buckeye), which is the same as shifting left by 8, and is therefore the same as adding '00' to the end of your value in a hex textual representation.
I.e., 134 = 0x86 as an 8-bit colour can be extended to 0x8600 for this TRIVERTEX colour range.

As an aside, you probably saw it as you mentioned you looked at them, but the justification for this extended range is given in the docs:
Quote:
 The color information of each channel is specified as a value from 0x0000 to 0xff00. This allows higher color resolution for an object that has been split into small triangles for display.