Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add COLORREF helper class and move COLORREF methods there #2218

Merged
merged 1 commit into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/Common/src/Interop/Interop.COLORREF.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Drawing;

internal partial class Interop
{
/// <summary>
/// Helpers for color conversion.
/// </summary>
internal static class COLORREF
gpetrou marked this conversation as resolved.
Show resolved Hide resolved
{
public static int RgbToCOLORREF(int rgbValue)
{
// Clear the A value, swap R & B values.
int bValue = (rgbValue & 0xFF) << 16;

rgbValue &= 0xFFFF00;
rgbValue |= (rgbValue >> 16) & 0xFF;
rgbValue &= 0x00FFFF;
rgbValue |= bValue;
return rgbValue;
}

public static Color COLORREFToColor(int colorref)
RussKie marked this conversation as resolved.
Show resolved Hide resolved
{
int r = colorref & 0xFF;
int g = (colorref >> 8) & 0xFF;
int b = (colorref >> 16) & 0xFF;
return Color.FromArgb(r, g, b);
}

public static int ColorToCOLORREF(Color color)
RussKie marked this conversation as resolved.
Show resolved Hide resolved
=> color.R | (color.G << 8) | (color.B << 16);
}
}
26 changes: 0 additions & 26 deletions src/Common/src/SafeNativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,32 +214,6 @@ public static class VisualStyleSystemProperty

[DllImport(ExternDll.Uxtheme, CharSet = CharSet.Auto)]
public extern static int SetWindowTheme(IntPtr hWnd, string subAppName, string subIdList);

// Color conversion
public static int RGBToCOLORREF(int rgbValue)
{
// clear the A value, swap R & B values
int bValue = (rgbValue & 0xFF) << 16;

rgbValue &= 0xFFFF00;
rgbValue |= ((rgbValue >> 16) & 0xFF);
rgbValue &= 0x00FFFF;
rgbValue |= bValue;
return rgbValue;
}

public static Color ColorFromCOLORREF(int colorref)
{
int r = colorref & 0xFF;
int g = (colorref >> 8) & 0xFF;
int b = (colorref >> 16) & 0xFF;
return Color.FromArgb(r, g, b);
}

public static int ColorToCOLORREF(Color color)
{
return (int)color.R | ((int)color.G << 8) | ((int)color.B << 16);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public Color Color
{
if (color.IsEmpty)
{
color = SafeNativeMethods.ColorFromCOLORREF((int)listView.SendMessage((int)LVM.GETINSERTMARKCOLOR, 0, 0));
color = COLORREF.COLORREFToColor((int)listView.SendMessage((int)LVM.GETINSERTMARKCOLOR, 0, 0));
}
return color;
}
Expand All @@ -86,7 +86,7 @@ public Color Color
color = value;
if (listView.IsHandleCreated)
{
listView.SendMessage((int)LVM.SETINSERTMARKCOLOR, 0, SafeNativeMethods.ColorToCOLORREF(color));
listView.SendMessage((int)LVM.SETINSERTMARKCOLOR, 0, COLORREF.ColorToCOLORREF(color));
}
}
}
Expand Down Expand Up @@ -139,7 +139,7 @@ internal void UpdateListView()

if (!color.IsEmpty)
{
listView.SendMessage((int)LVM.SETINSERTMARKCOLOR, 0, SafeNativeMethods.ColorToCOLORREF(color));
listView.SendMessage((int)LVM.SETINSERTMARKCOLOR, 0, COLORREF.ColorToCOLORREF(color));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2485,8 +2485,8 @@ public virtual void PaintValue(object val, Graphics g, Rectangle rect, Rectangle

try
{
oldTextColor = Gdi32.SetTextColor(new HandleRef(g, hdc), SafeNativeMethods.RGBToCOLORREF(textColor.ToArgb()));
oldBkColor = Gdi32.SetBkColor(new HandleRef(g, hdc), SafeNativeMethods.RGBToCOLORREF(bkColor.ToArgb()));
oldTextColor = Gdi32.SetTextColor(new HandleRef(g, hdc), COLORREF.RgbToCOLORREF(textColor.ToArgb()));
oldBkColor = Gdi32.SetBkColor(new HandleRef(g, hdc), COLORREF.RgbToCOLORREF(bkColor.ToArgb()));
hfont = Gdi32.SelectObject(hdc, hfont);
User32.DT format = User32.DT.EDITCONTROL | User32.DT.EXPANDTABS | User32.DT.NOCLIP | User32.DT.SINGLELINE | User32.DT.NOPREFIX;
if (gridHost.DrawValuesRightToLeft)
Expand Down