-
Integer to Hexadecimal
I am using a structure who has its three parameters declared as "COLOR16". I didn't find any documentation for it on MSDN but it says the value of COLOR16 can range from 0x0000 to 0xff00. This is hex but I have an integer that I want to pass it. Example:
PHP Code:
TRIVERTEX vert[2] ;
vert [0] .Red = GetRValue(mycolor);
vert [0] .Green = GetGValue(mycolor);
//.....
.Red and .Green are decalred as COLOR16. But GetRValue is either BYTE or int. It just doesn't work if I use the above method of typecast it like "(COLOR16)GetRValue(mycolor)". It only works if I provide a haxadecimal value.
How can I make it work WITH a variable instead of just passing it a non-declared hex value?
-
Hmmm, I just tried declaring the RGB values as COLOR16 but it still doesn't work.
PHP Code:
COLOR16 r1, g1, b1;
r1 = (COLOR16)GetRValue(crColour1);
g1 = (COLOR16)GetRValue(crColour1);
b1 = (COLOR16)GetGValue(crColour1);
TRIVERTEX vert[2] ;
vert [0] .x = 0;
vert [0] .y = 0;
vert [0] .Red = r1;
vert [0] .Green = g1;
vert [0] .Blue = b1;
vert [0] .Alpha = 0x0000;
//....continued....
-
This might help, I haved tested it and it work if you change the int x =1 to unsigned int x =1
http://planet-source-code.com/vb/scr...=4064&lngWId=3
-
-
Why ? You just want to convert the number isn't?
-
Yes, but I don't want to convert that number to a string which contains the hexdecimal value of it. Because there's no variable type of hold hexadecimal, I won't be able to store this number and then pass it as a colour.
It's just weird that the structure isn't accepting any integer although it's happily using its hexadecimal form (0x....).
-
COLOR16 is unsigned short (USHORT) - a two byte unsigned integer.
-
ya, it's USHORT after I looked at the wingdi.h header file. But how would I do the conversion?
-
Maybe what I will said it's idiot but you can not simply cast it?
-
Yes, that's what I did but it didn't work.
(COLOR16)GetRValue(crColour);
-
try using (USHORT)GetRValue(crColour);
-
What does GetRValue return?
What is the error?
-
GetRValue returns a BYTE which is the value of red colour in a colour. I don't get any error but the function just does draw the correct gradient (it doesn't use the values I provide) unless I provide it a hex value (0x****).
-
from MSDN
Code:
//declaraction of GetRValue
BYTE GetRValue(
DWORD rgb // RGB value
);
Just convert your parameter variables to DWORD will do.
DWORD is double word, meaning it is unsigned integer(32bit) alias.
For your FYI, hex, octal and based 10 representation are the same in binary so there is no reason that they cannot be interchangeable.
The reason 0x**** works is bcos it is a constant implicitly casted into DWORD.
When u put variables in, it couldn't work, meaning the variables u used cannot be cast into DWORD.
Another reason is GetRValue takes it that (assuming u put in 16bit variable) every colour component in the parameter is 8 bit, but in 16bit variable, every colour compnent is not 8 bit, rather than 565, so when u put in 16bit(implictly casted and zero-extended to 32bit), GetRValue() extracts 8 bits(5 of red + 3 of green), insteads of 5bits for red.
Of course, when u put in hex constants, it worked ,but in a wrong way for u.
Do I make myself clear?
-
If u previously had put in a DWORD, then I have nothing to say.:confused:
-
There is no problem with GetRValue(). I returns the correct red value and I can use this value with many many other functions which also works. The only problem is that I can't assign this value to the structure I just talked about above because it takes it as COLOR16.
Weird thing is that typecasting isn't work here either.
-
Why dun u just change the structure members, Red, Green and Blue to BYTE?
You doesn't need them to be COLOR16, right?
-
The structure is defined in Windows API - not by me.
-
You can't cast from different color systems just like that.
You need to recalculate the values. e.g. if you have the color value 4 when 16 are possible, it becomes 16 when 64 are possible. You can't simply change their type.