Skip to content

Commit

Permalink
Fix transparent colors
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmetsait committed May 6, 2024
1 parent f2d6192 commit b023431
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 32 deletions.
37 changes: 35 additions & 2 deletions Scintilla.NET/HelperMethods.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.Linq;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

namespace ScintillaNET;

Expand All @@ -7,6 +10,36 @@ namespace ScintillaNET;
/// </summary>
public static class HelperMethods
{
static readonly Dictionary<int, Color> knownColorMap = [];

static HelperMethods()
{
foreach (var knownColor in Enum.GetValues(typeof(KnownColor)).Cast<KnownColor>().Where(k => k >= KnownColor.Transparent && k < KnownColor.ButtonFace))
{
Color color = Color.FromKnownColor(knownColor);
knownColorMap[ToWin32Color(color)] = color;
}
}

public static Color FromWin32Color(int color)
{
if (color == 0)
return Color.Transparent;

if (knownColorMap.TryGetValue(color, out Color result))
// We do all this nonsense because because Visual Studio designer
// does not mark raw colors as default if there exists a known color
// with the same value.
return result;

return Color.FromArgb((color >> 24) & 0xFF, (color >> 0) & 0xFF, (color >> 8) & 0xFF, (color >> 16) & 0xFF);
}

public static int ToWin32Color(Color color)
{
return (color.A << 24) | (color.R << 0) | (color.G << 8) | (color.B << 16);
}

/// <summary>
/// Gets the folding state of the control as a delimited string containing line indexes.
/// </summary>
Expand Down Expand Up @@ -37,4 +70,4 @@ public static class HelperMethods
scintilla.Lines[index].ToggleFold();
}
}
}
}
8 changes: 4 additions & 4 deletions Scintilla.NET/Indicator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ public Color ForeColor
get
{
var color = scintilla.DirectMessage(NativeMethods.SCI_INDICGETFORE, new IntPtr(Index)).ToInt32();
return ColorTranslator.FromWin32(color);
return HelperMethods.FromWin32Color(color);
}
set
{
var color = ColorTranslator.ToWin32(value);
var color = HelperMethods.ToWin32Color(value);
scintilla.DirectMessage(NativeMethods.SCI_INDICSETFORE, new IntPtr(Index), new IntPtr(color));
}
}
Expand All @@ -160,11 +160,11 @@ public Color HoverForeColor
get
{
var color = scintilla.DirectMessage(NativeMethods.SCI_INDICGETHOVERFORE, new IntPtr(Index)).ToInt32();
return ColorTranslator.FromWin32(color);
return HelperMethods.FromWin32Color(color);
}
set
{
var color = ColorTranslator.ToWin32(value);
var color = HelperMethods.ToWin32Color(value);
scintilla.DirectMessage(NativeMethods.SCI_INDICSETHOVERFORE, new IntPtr(Index), new IntPtr(color));
}
}
Expand Down
4 changes: 2 additions & 2 deletions Scintilla.NET/Margin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public Color BackColor
get
{
var color = scintilla.DirectMessage(NativeMethods.SCI_GETMARGINBACKN, new IntPtr(Index)).ToInt32();
return ColorTranslator.FromWin32(color);
return HelperMethods.FromWin32Color(color);
}
set
{
if (value.IsEmpty)
value = Color.Black;

var color = ColorTranslator.ToWin32(value);
var color = HelperMethods.ToWin32Color(value);
scintilla.DirectMessage(NativeMethods.SCI_SETMARGINBACKN, new IntPtr(Index), new IntPtr(color));
}
}
Expand Down
4 changes: 2 additions & 2 deletions Scintilla.NET/Marker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void SetAlpha(int alpha)
/// <seealso cref="SetAlpha" />
public void SetBackColor(Color color)
{
var colour = ColorTranslator.ToWin32(color);
var colour = HelperMethods.ToWin32Color(color);
scintilla.DirectMessage(NativeMethods.SCI_MARKERSETBACK, new IntPtr(Index), new IntPtr(colour));
}

Expand All @@ -115,7 +115,7 @@ public void SetBackColor(Color color)
/// <param name="color">The <see cref="Marker" /> foreground Color. The default is Black.</param>
public void SetForeColor(Color color)
{
var colour = ColorTranslator.ToWin32(color);
var colour = HelperMethods.ToWin32Color(color);
scintilla.DirectMessage(NativeMethods.SCI_MARKERSETFORE, new IntPtr(Index), new IntPtr(colour));
}

Expand Down
36 changes: 18 additions & 18 deletions Scintilla.NET/Scintilla.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ public void CallTipCancel()
/// <param name="color">The new highlight text Color. The default is dark blue.</param>
public void CallTipSetForeHlt(Color color)
{
var colour = ColorTranslator.ToWin32(color);
var colour = HelperMethods.ToWin32Color(color);
DirectMessage(NativeMethods.SCI_CALLTIPSETFOREHLT, new IntPtr(colour));
}

Expand Down Expand Up @@ -1574,7 +1574,7 @@ public int MarkerLineFromHandle(MarkerHandle markerHandle)
public void MultiEdgeAddLine(int column, Color edgeColor)
{
column = Helpers.ClampMin(column, 0);
var colour = ColorTranslator.ToWin32(edgeColor);
var colour = HelperMethods.ToWin32Color(edgeColor);

DirectMessage(NativeMethods.SCI_MULTIEDGEADDLINE, new IntPtr(column), new IntPtr(colour));
}
Expand Down Expand Up @@ -2416,7 +2416,7 @@ public void SelectAll()
/// <remarks>Calling <see cref="SetSelectionBackColor" /> will reset the <paramref name="color" /> specified.</remarks>
public void SetAdditionalSelBack(Color color)
{
var colour = ColorTranslator.ToWin32(color);
var colour = HelperMethods.ToWin32Color(color);
DirectMessage(NativeMethods.SCI_SETADDITIONALSELBACK, new IntPtr(colour));
}

Expand All @@ -2427,7 +2427,7 @@ public void SetAdditionalSelBack(Color color)
/// <remarks>Calling <see cref="SetSelectionForeColor" /> will reset the <paramref name="color" /> specified.</remarks>
public void SetAdditionalSelFore(Color color)
{
var colour = ColorTranslator.ToWin32(color);
var colour = HelperMethods.ToWin32Color(color);
DirectMessage(NativeMethods.SCI_SETADDITIONALSELFORE, new IntPtr(colour));
}

Expand Down Expand Up @@ -2460,7 +2460,7 @@ public void SetFoldFlags(FoldFlags flags)
/// <seealso cref="SetFoldMarginHighlightColor" />
public void SetFoldMarginColor(bool use, Color color)
{
var colour = ColorTranslator.ToWin32(color);
var colour = HelperMethods.ToWin32Color(color);
var useFoldMarginColour = (use ? new IntPtr(1) : IntPtr.Zero);

DirectMessage(NativeMethods.SCI_SETFOLDMARGINCOLOUR, useFoldMarginColour, new IntPtr(colour));
Expand All @@ -2474,7 +2474,7 @@ public void SetFoldMarginColor(bool use, Color color)
/// <seealso cref="SetFoldMarginColor" />
public void SetFoldMarginHighlightColor(bool use, Color color)
{
var colour = ColorTranslator.ToWin32(color);
var colour = HelperMethods.ToWin32Color(color);
var useFoldMarginHighlightColour = (use ? new IntPtr(1) : IntPtr.Zero);

DirectMessage(NativeMethods.SCI_SETFOLDMARGINHICOLOUR, useFoldMarginHighlightColour, new IntPtr(colour));
Expand Down Expand Up @@ -2647,7 +2647,7 @@ public void SetSelection(int caret, int anchor)
/// <seealso cref="SetSelectionForeColor" />
public void SetSelectionBackColor(bool use, Color color)
{
var colour = ColorTranslator.ToWin32(color);
var colour = HelperMethods.ToWin32Color(color);
var useSelectionForeColour = (use ? new IntPtr(1) : IntPtr.Zero);

DirectMessage(NativeMethods.SCI_SETSELBACK, useSelectionForeColour, new IntPtr(colour));
Expand All @@ -2661,7 +2661,7 @@ public void SetSelectionBackColor(bool use, Color color)
/// <seealso cref="SetSelectionBackColor" />
public void SetSelectionForeColor(bool use, Color color)
{
var colour = ColorTranslator.ToWin32(color);
var colour = HelperMethods.ToWin32Color(color);
var useSelectionForeColour = (use ? new IntPtr(1) : IntPtr.Zero);

DirectMessage(NativeMethods.SCI_SETSELFORE, useSelectionForeColour, new IntPtr(colour));
Expand Down Expand Up @@ -2750,7 +2750,7 @@ public void SetTargetRange(int start, int end)
/// <seealso cref="SetWhitespaceForeColor" />
public void SetWhitespaceBackColor(bool use, Color color)
{
var colour = ColorTranslator.ToWin32(color);
var colour = HelperMethods.ToWin32Color(color);
var useWhitespaceBackColour = (use ? new IntPtr(1) : IntPtr.Zero);

DirectMessage(NativeMethods.SCI_SETWHITESPACEBACK, useWhitespaceBackColour, new IntPtr(colour));
Expand All @@ -2766,7 +2766,7 @@ public void SetWhitespaceBackColor(bool use, Color color)
/// <seealso cref="SetWhitespaceBackColor" />
public void SetWhitespaceForeColor(bool use, Color color)
{
var colour = ColorTranslator.ToWin32(color);
var colour = HelperMethods.ToWin32Color(color);
var useWhitespaceForeColour = (use ? new IntPtr(1) : IntPtr.Zero);

DirectMessage(NativeMethods.SCI_SETWHITESPACEFORE, useWhitespaceForeColour, new IntPtr(colour));
Expand Down Expand Up @@ -3369,11 +3369,11 @@ public Color AdditionalCaretForeColor
get
{
var color = DirectMessage(NativeMethods.SCI_GETADDITIONALCARETFORE).ToInt32();
return ColorTranslator.FromWin32(color);
return HelperMethods.FromWin32Color(color);
}
set
{
var color = ColorTranslator.ToWin32(value);
int color = HelperMethods.ToWin32Color(value);
DirectMessage(NativeMethods.SCI_SETADDITIONALCARETFORE, new IntPtr(color));
}
}
Expand Down Expand Up @@ -4024,11 +4024,11 @@ public Color CaretForeColor
get
{
var color = DirectMessage(NativeMethods.SCI_GETCARETFORE).ToInt32();
return ColorTranslator.FromWin32(color);
return HelperMethods.FromWin32Color(color);
}
set
{
var color = ColorTranslator.ToWin32(value);
int color = HelperMethods.ToWin32Color(value);
DirectMessage(NativeMethods.SCI_SETCARETFORE, new IntPtr(color));
}
}
Expand All @@ -4045,11 +4045,11 @@ public Color CaretLineBackColor
get
{
var color = DirectMessage(NativeMethods.SCI_GETCARETLINEBACK).ToInt32();
return ColorTranslator.FromWin32(color);
return HelperMethods.FromWin32Color(color);
}
set
{
var color = ColorTranslator.ToWin32(value);
int color = HelperMethods.ToWin32Color(value);
DirectMessage(NativeMethods.SCI_SETCARETLINEBACK, new IntPtr(color));
}
}
Expand Down Expand Up @@ -4445,11 +4445,11 @@ public Color EdgeColor
get
{
var color = DirectMessage(NativeMethods.SCI_GETEDGECOLOUR).ToInt32();
return ColorTranslator.FromWin32(color);
return HelperMethods.FromWin32Color(color);
}
set
{
var color = ColorTranslator.ToWin32(value);
var color = HelperMethods.ToWin32Color(value);
DirectMessage(NativeMethods.SCI_SETEDGECOLOUR, new IntPtr(color));
}
}
Expand Down
8 changes: 4 additions & 4 deletions Scintilla.NET/Style.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ public Color BackColor
get
{
var color = scintilla.DirectMessage(NativeMethods.SCI_STYLEGETBACK, new IntPtr(Index), IntPtr.Zero).ToInt32();
return ColorTranslator.FromWin32(color);
return HelperMethods.FromWin32Color(color);
}
set
{
if (value.IsEmpty)
value = Color.White;

var color = ColorTranslator.ToWin32(value);
var color = HelperMethods.ToWin32Color(value);
scintilla.DirectMessage(NativeMethods.SCI_STYLESETBACK, new IntPtr(Index), new IntPtr(color));
}
}
Expand Down Expand Up @@ -231,14 +231,14 @@ public Color ForeColor
get
{
var color = scintilla.DirectMessage(NativeMethods.SCI_STYLEGETFORE, new IntPtr(Index), IntPtr.Zero).ToInt32();
return ColorTranslator.FromWin32(color);
return HelperMethods.FromWin32Color(color);
}
set
{
if (value.IsEmpty)
value = Color.Black;

var color = ColorTranslator.ToWin32(value);
var color = HelperMethods.ToWin32Color(value);
scintilla.DirectMessage(NativeMethods.SCI_STYLESETFORE, new IntPtr(Index), new IntPtr(color));
}
}
Expand Down

0 comments on commit b023431

Please sign in to comment.