Typically when you cast a variable to another it runs a procedure to convert it to the given type, since most types differ in their underlying bit representation. For instance 10.3.int the int(float) conversion truncates the decimals. Now you also have a bit for bit cast which is cast[int](float), but that takes the bit signature of the float and converts it directly to an int, so it should only be used when that's the behaviour that you want.
10.3.int == 10
cast[int](10.3) == 4621988002574997914
I have a section about cast and type conversion in the Nim for kids book:
http://ssalewski.de/nimprogramming.html#_casts_and_type_conversion
Have you missed it, is it unclear?
converting a tuple with 3 int elements to COLORREF
Have you managed it?
Google tells me that COLORREF is a Windows 32 bit entity which encodes each color in 8 bits. So if you have a tuple with 3 ints, you may have to shift them to the right position and then join them into one 32 bit value. For shift you can use shl() or multiply with 256. (well maybe better not multiply, as that may do sign extend.) And for joining you may use "or" operator or plain addition. For that to work I assume that in your tuple all 3 components contains values which all are smaller than 256. I hope you solved the task already yourself, if not let us know the exact content of your initial tuple.
Once you're comfortable with doing things manually, you can use an object and let the compiler figure out the bit shifts for you:
type
Colorref{.packed.} = object
r,g,b,a:uint8
assert sizeof(Colorref) == sizeof(uint32)
let c = Colorref(b:255,g:0xCC,r:0xAA)
import strutils
echo cast[uint32](c).toHex #0x00ffcaa
@Stefan_Salewski > Have you managed it? No, That was just my thought. Actually, i tried this.
type
RgbColor* = tuple[red: int, green : int, blue : int]
This is my Color type. But, after reading your reply, i think it's good to change it to int8. And this is my conversion code. I've used RGB macro from win32 API.
proc toColorRef(clr : RgbColor) : COLORREF = RGB(clr.red, clr.green, clr.blue)
This is working perfectly. Thanks for the code sample and guidance. I will sure check that out.