From 013ee74453f8d94f75598f9126e0453a5bf07ab7 Mon Sep 17 00:00:00 2001 From: dahall Date: Fri, 22 Apr 2022 13:24:51 -0600 Subject: [PATCH] Updates to hopefully fix PInvoke.Shared load problems --- PInvoke/Shared/Vanara.PInvoke.Shared.csproj | 2 +- PInvoke/Shared/WinDef/COLORREF.cs | 13 +++++++- PInvoke/Shared/WinGdi/WinGdi.RGBQUAD.cs | 46 ++++++++++++++++++++++++----- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/PInvoke/Shared/Vanara.PInvoke.Shared.csproj b/PInvoke/Shared/Vanara.PInvoke.Shared.csproj index 83b1a9f3..674dd908 100644 --- a/PInvoke/Shared/Vanara.PInvoke.Shared.csproj +++ b/PInvoke/Shared/Vanara.PInvoke.Shared.csproj @@ -30,7 +30,7 @@ CharacterSet, CM_DEVCAP, CM_FILE, CM_INSTALL_STATE, CM_REMOVAL_POLICY, CM_RESOUR - + \ No newline at end of file diff --git a/PInvoke/Shared/WinDef/COLORREF.cs b/PInvoke/Shared/WinDef/COLORREF.cs index bccffc09..e60aa007 100644 --- a/PInvoke/Shared/WinDef/COLORREF.cs +++ b/PInvoke/Shared/WinDef/COLORREF.cs @@ -8,7 +8,7 @@ namespace Vanara.PInvoke // typedef DWORD COLORREF;typedef DWORD* LPCOLORREF; https://msdn.microsoft.com/en-us/library/windows/desktop/dd183449(v=vs.85).aspx [PInvokeData("Windef.h", MSDNShortId = "dd183449")] [StructLayout(LayoutKind.Explicit, Size = 4)] - public struct COLORREF + public struct COLORREF : IEquatable { /// The DWORD value [FieldOffset(0)] @@ -81,6 +81,17 @@ namespace Vanara.PInvoke byte Conv(byte c) => (byte)(c - (int)(c * percent)); } + /// + public override bool Equals(object obj) => obj is COLORREF q && Equals(q); + + /// Determines whether the specified object is equal to the current object. + /// The object to compare with the current object. + /// if the specified object is equal to the current object; otherwise, . + public bool Equals(COLORREF c) => c.A == A && c.B == B && c.G == G && c.R == R; + + /// + public override int GetHashCode() => ToArgb(); + /// A method to lighten a color by a percentage of the difference between the color and Black. /// The percentage by which to lighten the original color. /// diff --git a/PInvoke/Shared/WinGdi/WinGdi.RGBQUAD.cs b/PInvoke/Shared/WinGdi/WinGdi.RGBQUAD.cs index dcef56d2..a291d6e6 100644 --- a/PInvoke/Shared/WinGdi/WinGdi.RGBQUAD.cs +++ b/PInvoke/Shared/WinGdi/WinGdi.RGBQUAD.cs @@ -1,11 +1,12 @@ -using System.Runtime.InteropServices; +using System; +using System.Runtime.InteropServices; namespace Vanara.PInvoke { /// The RGBQUAD structure describes a color consisting of relative intensities of red, green, and blue. - [StructLayout(LayoutKind.Sequential)] [PInvokeData("Wingdi.h", MSDNShortId = "dd162938")] - public struct RGBQUAD + [StructLayout(LayoutKind.Sequential, Size = 4)] + public struct RGBQUAD : IEquatable { /// The intensity of blue in the color. public byte rgbBlue; @@ -19,6 +20,18 @@ namespace Vanara.PInvoke /// This member is reserved and must be zero. public byte rgbReserved; + /// Initializes a new instance of the struct. + /// The intensity of the red color. + /// The intensity of the green color. + /// The intensity of the blue color. + public RGBQUAD(byte r, byte g, byte b) + { + rgbRed = r; + rgbGreen = g; + rgbBlue = b; + rgbReserved = 0; + } + /// Gets a value indicating whether any transparency is defined. /// if this value is transparent; otherwise, . public bool IsTransparent => rgbReserved == 0; @@ -31,23 +44,42 @@ namespace Vanara.PInvoke set { rgbReserved = value.A; rgbBlue = value.B; rgbGreen = value.G; rgbRed = value.R; } } + /// + public override bool Equals(object obj) => obj is RGBQUAD q && Equals(q); + + /// Determines whether the specified object is equal to the current object. + /// The object to compare with the current object. + /// if the specified object is equal to the current object; otherwise, . + public bool Equals(RGBQUAD q) => q.rgbBlue == rgbBlue && q.rgbGreen == rgbGreen && q.rgbRed == rgbRed && q.rgbReserved == rgbReserved; + + /// + public override int GetHashCode() => unchecked((int)(uint)this); + + /// + public override string ToString() => $"{{R={rgbRed},G={rgbGreen},B={rgbBlue}}}"; + + /// Performs an implicit conversion from to . + /// The value. + /// The result of the conversion. + public static implicit operator uint(RGBQUAD c) { unsafe { return *(uint*)&c; } } + /// Performs an implicit conversion from to . - /// The c. + /// The value. /// The result of the conversion. public static implicit operator RGBQUAD(COLORREF c) => new() { Color = c }; /// Performs an implicit conversion from to . - /// The c. + /// The value. /// The result of the conversion. public static implicit operator COLORREF(RGBQUAD c) => c.Color; /// Performs an implicit conversion from to . - /// The c. + /// The value. /// The result of the conversion. public static implicit operator RGBQUAD(System.Drawing.Color c) => new() { Color = c }; /// Performs an implicit conversion from to . - /// The c. + /// The value. /// The result of the conversion. public static implicit operator System.Drawing.Color(RGBQUAD c) => c.Color; }