From f6dc4c4804ae827dc1e5e990b3c798922388103c Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Sat, 18 Jul 2026 03:20:52 -0600 Subject: [PATCH] use packed struct for COLORREF --- examples/testwindow.zig | 2 +- src/genzig.zig | 1 + src/static/win32/zig.zig | 27 ++++++++++++++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/examples/testwindow.zig b/examples/testwindow.zig index d90f3b2..5ee1a0d 100644 --- a/examples/testwindow.zig +++ b/examples/testwindow.zig @@ -121,7 +121,7 @@ fn WindowProc( } { - const brush = win32.createSolidBrush(0xff00ffff); + const brush = win32.createSolidBrush(.yellow); defer win32.deleteObject(brush); win32.fillRect(hdc, ps.rcPaint, brush); } diff --git a/src/genzig.zig b/src/genzig.zig index 38ae9ad..7f5a87c 100644 --- a/src/genzig.zig +++ b/src/genzig.zig @@ -1884,6 +1884,7 @@ const redirects = std.StaticStringMap(Redirect).initComptime(.{ .{ "VARIANT_BOOL", Redirect{ .native = "i16" } }, .{ "BSTR", Redirect{ .native = "*u16" } }, .{ "HRESULT", Redirect{ .import = "zig" } }, + .{ "COLORREF", Redirect{ .import = "zig" } }, }); const type_renames = std.StaticStringMap([]const u8).initComptime(.{ diff --git a/src/static/win32/zig.zig b/src/static/win32/zig.zig index cd78f72..64b679f 100644 --- a/src/static/win32/zig.zig +++ b/src/static/win32/zig.zig @@ -165,6 +165,31 @@ pub const HRESULT = packed struct (u32) { pub const S_OK: HRESULT = .fromInt(0); }; +pub const COLORREF = packed struct(u32) { + r: u8, + g: u8, + b: u8, + flags: u8, + pub fn fromInt(i: u32) COLORREF { + return @bitCast(i); + } + pub fn rgb(r: u8, g: u8, b: u8) COLORREF { + return .{ .r = r, .g = g, .b = b, .flags = 0 }; + } + pub const invalid: COLORREF = .fromInt(0xFFFFFFFF); + pub const none: COLORREF = .fromInt(0xFFFFFFFF); + pub const default: COLORREF = .fromInt(0xFF000000); + pub const black: COLORREF = .rgb(0, 0, 0); + pub const white: COLORREF = .rgb(255, 255, 255); + pub const red: COLORREF = .rgb(255, 0, 0); + pub const green: COLORREF = .rgb(0, 255, 0); + pub const blue: COLORREF = .rgb(0, 0, 255); + pub const yellow: COLORREF = .rgb(255, 255, 0); + pub const cyan: COLORREF = .rgb(0, 255, 255); + pub const magenta: COLORREF = .rgb(255, 0, 255); + pub const gray: COLORREF = .rgb(128, 128, 128); +}; + // These constants were removed from the metadata to allow each projection // to define them however they like (see https://github.com/microsoft/win32metadata/issues/530) pub const FALSE: win32.BOOL = 0; @@ -572,7 +597,7 @@ pub fn endPaint(hwnd: win32.HWND, paintstruct: *const win32.PAINTSTRUCT) void { } /// calls CreateSolidBrush, panics on failure -pub fn createSolidBrush(color: u32) win32.HBRUSH { +pub fn createSolidBrush(color: COLORREF) win32.HBRUSH { return win32.CreateSolidBrush(color) orelse panicWin32( "CreateSolidBrush", win32.GetLastError(),