Skip to content

Commit 1011768

Browse files
committed
Respond to feedback
1 parent 4800f19 commit 1011768

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

src/libraries/System.Drawing.Primitives/src/System/Drawing/KnownColorTable.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,11 @@ internal static class KnownColorTable
427427
KnownColorKindWeb, // RebeccaPurple
428428
];
429429

430+
// These values were based on manual investigation of dark mode themes in the
431+
// Win32 Common Controls and WinUI. There aren't direct mappings published by
432+
// Windows, these may change slightly when this feature is finalized to make
433+
// sure we have the best experience in hybrid dark mode scenarios (mixing
434+
// WPF, WinForms, and WinUI).
430435
private static ReadOnlySpan<uint> AlternateSystemColors =>
431436
[
432437
0, // To align with KnownColor.ActiveBorder = 1
@@ -494,18 +499,24 @@ public static uint KnownColorToArgb(KnownColor color)
494499
: ColorValueTable[(int)color];
495500
}
496501

502+
private static uint GetAlternateSystemColorArgb(KnownColor color)
503+
{
504+
// Shift the original (split) index to fit the alternate color map.
505+
int index = color <= KnownColor.WindowText
506+
? (int)color
507+
: (int)color - (int)KnownColor.ButtonFace + (int)KnownColor.WindowText + 1;
508+
509+
return AlternateSystemColors[index];
510+
}
511+
497512
#if FEATURE_WINDOWS_SYSTEM_COLORS
498513
public static uint GetSystemColorArgb(KnownColor color)
499514
{
500515
Debug.Assert(Color.IsKnownColorSystem(color));
501516

502-
if (!SystemColors.s_useAlternativeColorSet || HighContrastEnabled())
503-
{
504-
return ColorTranslator.COLORREFToARGB(Interop.User32.GetSysColor((byte)ColorValueTable[(int)color]));
505-
}
506-
507-
int index = color <= KnownColor.WindowText ? (int)color : (int)color - (int)KnownColor.ButtonFace + (int)KnownColor.WindowText + 1;
508-
return AlternateSystemColors[index];
517+
return !SystemColors.s_useAlternativeColorSet || HighContrastEnabled()
518+
? ColorTranslator.COLORREFToARGB(Interop.User32.GetSysColor((byte)ColorValueTable[(int)color]))
519+
: GetAlternateSystemColorArgb(color);
509520
}
510521

511522
private static unsafe bool HighContrastEnabled()
@@ -529,13 +540,9 @@ public static uint GetSystemColorArgb(KnownColor color)
529540
{
530541
Debug.Assert(Color.IsKnownColorSystem(color));
531542

532-
if (!SystemColors.s_useAlternativeColorSet)
533-
{
534-
return ColorValueTable[(int)color];
535-
}
536-
537-
int index = color <= KnownColor.WindowText ? (int)color : (int)color - (int)KnownColor.ButtonFace + (int)KnownColor.WindowText + 1;
538-
return AlternateSystemColors[index];
543+
return (!SystemColors.s_useAlternativeColorSet)
544+
? ColorValueTable[(int)color]
545+
: GetAlternateSystemColorArgb(color);
539546
}
540547
#endif
541548
}

src/libraries/System.Drawing.Primitives/src/System/Drawing/SystemColors.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public static class SystemColors
6060
/// </summary>
6161
/// <remarks>
6262
/// <para>
63+
/// <see cref="KnownColor"/> <see cref="Color"/> values are always looked up every
64+
/// time you use them and do not retain any other context. As such, existing
65+
/// <see cref="Color"/> values will change when this property is set.
66+
/// </para>
67+
/// <para>
6368
/// On Windows, system <see cref="KnownColor"/> values will always return the current
6469
/// Windows color when the OS has a high contrast theme enabled.
6570
/// </para>

0 commit comments

Comments
 (0)