2022-01-14 09:32:51 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2018-06-28 20:01:46 -04:00
|
|
|
|
|
|
|
|
|
namespace Vanara.PInvoke
|
|
|
|
|
{
|
|
|
|
|
/// <summary>The COLORREF value is used to specify an RGB color in the form <c>0x00bbggrr</c>.</summary>
|
2018-11-19 23:18:50 -05:00
|
|
|
|
// typedef DWORD COLORREF;typedef DWORD* LPCOLORREF; https://msdn.microsoft.com/en-us/library/windows/desktop/dd183449(v=vs.85).aspx
|
2018-06-28 20:01:46 -04:00
|
|
|
|
[PInvokeData("Windef.h", MSDNShortId = "dd183449")]
|
|
|
|
|
[StructLayout(LayoutKind.Explicit, Size = 4)]
|
2022-04-22 15:24:51 -04:00
|
|
|
|
public struct COLORREF : IEquatable<COLORREF>
|
2018-06-28 20:01:46 -04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>The DWORD value</summary>
|
|
|
|
|
[FieldOffset(0)]
|
|
|
|
|
private uint Value;
|
2018-11-19 23:18:50 -05:00
|
|
|
|
|
2018-06-28 20:01:46 -04:00
|
|
|
|
/// <summary>The intensity of the red color.</summary>
|
|
|
|
|
[FieldOffset(0)]
|
|
|
|
|
public byte R;
|
2018-11-19 23:18:50 -05:00
|
|
|
|
|
2018-06-28 20:01:46 -04:00
|
|
|
|
/// <summary>The intensity of the green color.</summary>
|
|
|
|
|
[FieldOffset(1)]
|
|
|
|
|
public byte G;
|
2018-11-19 23:18:50 -05:00
|
|
|
|
|
2018-06-28 20:01:46 -04:00
|
|
|
|
/// <summary>The intensity of the blue color.</summary>
|
|
|
|
|
[FieldOffset(2)]
|
|
|
|
|
public byte B;
|
|
|
|
|
|
2022-01-14 09:32:51 -05:00
|
|
|
|
/// <summary>The transparency.</summary>
|
|
|
|
|
[FieldOffset(3)]
|
|
|
|
|
public byte A;
|
|
|
|
|
|
2018-06-28 20:01:46 -04:00
|
|
|
|
private const uint CLR_NONE = 0xFFFFFFFF;
|
|
|
|
|
private const uint CLR_DEFAULT = 0xFF000000;
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="COLORREF"/> struct.</summary>
|
|
|
|
|
/// <param name="r">The intensity of the red color.</param>
|
|
|
|
|
/// <param name="g">The intensity of the green color.</param>
|
|
|
|
|
/// <param name="b">The intensity of the blue color.</param>
|
|
|
|
|
public COLORREF(byte r, byte g, byte b)
|
|
|
|
|
{
|
|
|
|
|
Value = 0;
|
2022-01-14 09:32:51 -05:00
|
|
|
|
A = 0;
|
2018-06-28 20:01:46 -04:00
|
|
|
|
R = r;
|
|
|
|
|
G = g;
|
|
|
|
|
B = b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="COLORREF"/> struct.</summary>
|
|
|
|
|
/// <param name="value">The packed DWORD value.</param>
|
|
|
|
|
public COLORREF(uint value)
|
|
|
|
|
{
|
|
|
|
|
R = 0;
|
|
|
|
|
G = 0;
|
|
|
|
|
B = 0;
|
2022-01-14 09:32:51 -05:00
|
|
|
|
A = 0;
|
2018-06-28 20:01:46 -04:00
|
|
|
|
Value = value & 0x00FFFFFF;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="COLORREF"/> struct.</summary>
|
|
|
|
|
/// <param name="color">The color.</param>
|
2022-01-14 09:32:51 -05:00
|
|
|
|
public COLORREF(Color color) : this(color.R, color.G, color.B)
|
2020-06-09 21:00:23 -04:00
|
|
|
|
{
|
2022-01-14 09:32:51 -05:00
|
|
|
|
if (color == Color.Transparent)
|
2020-06-09 21:00:23 -04:00
|
|
|
|
Value = CLR_NONE;
|
|
|
|
|
}
|
2018-06-28 20:01:46 -04:00
|
|
|
|
|
2022-01-14 09:32:51 -05:00
|
|
|
|
/// <summary>A method to darken a color by a percentage of the difference between the color and Black.</summary>
|
|
|
|
|
/// <param name="percent">The percentage by which to darken the original color.</param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// The return color's Alpha value will be unchanged, but the RGB content will have been increased by the specified percentage. If
|
|
|
|
|
/// percent is 100 then the returned Color will be Black with original Alpha.
|
|
|
|
|
/// </returns>
|
|
|
|
|
public COLORREF Darken(float percent)
|
|
|
|
|
{
|
|
|
|
|
if (percent < 0 || percent > 1.0)
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(percent));
|
|
|
|
|
|
|
|
|
|
return new(Conv(R), Conv(G), Conv(B)) { Value = Value };
|
|
|
|
|
|
|
|
|
|
byte Conv(byte c) => (byte)(c - (int)(c * percent));
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-22 15:24:51 -04:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override bool Equals(object obj) => obj is COLORREF q && Equals(q);
|
|
|
|
|
|
|
|
|
|
/// <summary>Determines whether the specified object is equal to the current object.</summary>
|
|
|
|
|
/// <param name="c">The object to compare with the current object.</param>
|
|
|
|
|
/// <returns><see langword="true"/> if the specified object is equal to the current object; otherwise, <see langword="false"/>.</returns>
|
|
|
|
|
public bool Equals(COLORREF c) => c.A == A && c.B == B && c.G == G && c.R == R;
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public override int GetHashCode() => ToArgb();
|
|
|
|
|
|
2022-01-14 09:32:51 -05:00
|
|
|
|
/// <summary>A method to lighten a color by a percentage of the difference between the color and Black.</summary>
|
|
|
|
|
/// <param name="percent">The percentage by which to lighten the original color.</param>
|
|
|
|
|
/// <returns>
|
|
|
|
|
/// The return color's Alpha value will be unchanged, but the RGB content will have been decreased by the specified percentage. If
|
|
|
|
|
/// percent is 100 then the returned Color will be White with original Alpha.
|
|
|
|
|
/// </returns>
|
|
|
|
|
public COLORREF Lighten(float percent)
|
|
|
|
|
{
|
|
|
|
|
if (percent < 0 || percent > 1.0)
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(percent));
|
|
|
|
|
|
|
|
|
|
return new(Conv(R), Conv(G), Conv(B)) { Value = Value };
|
|
|
|
|
|
|
|
|
|
byte Conv(byte c) => (byte)(c + (int)((255f - c) * percent));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets the 32-bit ARGB value of this <see cref="COLORREF"/> structure.</summary>
|
|
|
|
|
/// <returns>The 32-bit ARGB value of this <see cref="COLORREF"/> structure.</returns>
|
|
|
|
|
public int ToArgb() => unchecked((int)Value);
|
|
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="COLORREF"/> to <see cref="Color"/>.</summary>
|
2018-06-28 20:01:46 -04:00
|
|
|
|
/// <param name="cr">The <see cref="COLORREF"/> value.</param>
|
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
2022-01-14 09:32:51 -05:00
|
|
|
|
public static implicit operator Color(COLORREF cr) => cr.Value == CLR_NONE ? Color.Transparent : Color.FromArgb(cr.R, cr.G, cr.B);
|
2018-06-28 20:01:46 -04:00
|
|
|
|
|
2022-01-14 09:32:51 -05:00
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="Color"/> to <see cref="COLORREF"/>.</summary>
|
|
|
|
|
/// <param name="clr">The <see cref="Color"/> value.</param>
|
2018-06-28 20:01:46 -04:00
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
2022-01-14 09:32:51 -05:00
|
|
|
|
public static implicit operator COLORREF(Color clr) => new(clr);
|
2021-09-18 17:47:10 -04:00
|
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="System.ValueTuple{R, G, B}"/> to <see cref="COLORREF"/>.</summary>
|
|
|
|
|
/// <param name="clr">The tuple containing the R, G, and B values.</param>
|
|
|
|
|
/// <returns>The resulting <see cref="COLORREF"/> instance from the conversion.</returns>
|
|
|
|
|
public static implicit operator COLORREF((byte r, byte g, byte b) clr) => new(clr.r, clr.g, clr.b);
|
2018-06-28 20:01:46 -04:00
|
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="COLORREF"/> to <see cref="System.UInt32"/>.</summary>
|
|
|
|
|
/// <param name="cr">The <see cref="COLORREF"/> value.</param>
|
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
|
|
|
|
public static implicit operator uint(COLORREF cr) => cr.Value;
|
|
|
|
|
|
|
|
|
|
/// <summary>Performs an implicit conversion from <see cref="System.UInt32"/> to <see cref="COLORREF"/>.</summary>
|
|
|
|
|
/// <param name="value">The <see cref="uint"/> value.</param>
|
|
|
|
|
/// <returns>The result of the conversion.</returns>
|
2021-09-18 17:47:10 -04:00
|
|
|
|
public static implicit operator COLORREF(uint value) => new(value);
|
2018-06-28 20:01:46 -04:00
|
|
|
|
|
|
|
|
|
/// <summary>Static reference for CLR_NONE representing no color.</summary>
|
2021-09-18 17:47:10 -04:00
|
|
|
|
public static COLORREF None = new(CLR_NONE);
|
2018-06-28 20:01:46 -04:00
|
|
|
|
|
|
|
|
|
/// <summary>Static reference for CLR_DEFAULT representing the default color.</summary>
|
2021-09-18 17:47:10 -04:00
|
|
|
|
public static COLORREF Default = new(0xFF000000);
|
2019-04-17 13:43:19 -04:00
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2022-01-14 09:32:51 -05:00
|
|
|
|
public override string ToString() => ((Color)this).ToString();
|
2019-09-05 21:59:25 -04:00
|
|
|
|
}
|
2018-06-28 20:01:46 -04:00
|
|
|
|
}
|