Code: Select all
static constexpr Color MakeRGB( unsigned int r,unsigned int g,unsigned int b )
{
return ( r << 16u ) | ( g << 8u ) | b;
}
Code: Select all
static constexpr Color MakeRGB( unsigned char r, unsigned char g, unsigned char b )
{
return ( r << 16 ) | ( g << 8 ) | b;
}
The operator<< bit shifting inside the function are int based since the literals aren't postfixed with the letter 'u', so depending on the promotion rule here (r, g, and b are all unsigned, but are unsigned chars so I'm not sure if they get promoted to unsigned ints when doing operations with signed int ) there is another implicit conversion either to signed or unsigned int.
Not sure about anyone else, but VS2017 seems to want to promote to int as the intellisense message I get is "Value exceeds range of (int)".
The only benefit to having the parameters as unsigned char would be to let programmers know that the max acceptable value is 255. However, this isn't necessarily enforced as the compiler would probably truncate passed in values over 255 causing a value of say 256 to end up as 0, 257 as 1 and so on. Changing the parameters to unsigned int isn't clear that the values passed in need to be between 0u and 255u, but it makes the compiler happy. A solution would be to use asserts
Code: Select all
assert(r <= 255u);
assert(g <= 255u);
assert(b <= 255u);