convenience method for win32

This commit is contained in:
Adam D. Ruppe 2021-11-27 22:20:04 -05:00
parent a7d045f632
commit 234da9fe86
1 changed files with 32 additions and 0 deletions

32
color.d
View File

@ -162,6 +162,38 @@ struct Color {
uint asUint; /// The components as a single 32 bit value (beware of endian issues!)
}
/++
Returns a value compatible with [https://docs.microsoft.com/en-us/windows/win32/gdi/colorref|a Win32 COLORREF].
Please note that the alpha value is lost in translation.
History:
Added November 27, 2021 (dub v10.4)
See_Also:
[fromWindowsColorRef]
+/
nothrow pure @nogc
uint asWindowsColorRef() {
uint cr;
cr |= b << 16;
cr |= g << 8;
cr |= r;
return cr;
}
/++
Constructs a Color from [https://docs.microsoft.com/en-us/windows/win32/gdi/colorref|a Win32 COLORREF].
History:
Added November 27, 2021 (dub v10.4)
See_Also:
[asWindowsColorRef]
+/
nothrow pure @nogc
static Color fromWindowsColorRef(uint cr) {
return Color(cr & 0xff, (cr >> 8) & 0xff, (cr >> 16) & 0xff);
}
/++
Like the constructor, but this makes sure they are in range before casting. If they are out of range, it saturates: anything less than zero becomes zero and anything greater than 255 becomes 255.
+/