HdcExtensions
using System.Runtime.CompilerServices;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.GdiPlus;
namespace Windows.Win32.Graphics.Gdi
{
    internal static class HdcExtensions
    {
        private static HPALETTE s_halftonePalette;
        public static SelectPaletteScope HalftonePalette(this GetDcScope hdc, bool forceBackground, bool realizePalette)
        {
            return hdc.HDC.HalftonePalette(forceBackground, realizePalette);
        }
        [NullableContext(1)]
        public static SelectPaletteScope HalftonePalette<[Nullable(0)] T>(this T hdc, bool forceBackground, bool realizePalette) where T : IHandle<HDC>
        {
            if (PInvokeCore.GetDeviceCaps(((IHandle<HDC>)hdc).Handle, GET_DEVICE_CAPS_INDEX.BITSPIXEL) > 8)
                return default(SelectPaletteScope);
            if (s_halftonePalette.IsNull) {
                GdiPlusInitialization.EnsureInitialized();
                s_halftonePalette = PInvokeGdiPlus.GdipCreateHalftonePalette();
            }
            return new SelectPaletteScope(((IHandle<HDC>)hdc).Handle, s_halftonePalette, forceBackground, realizePalette);
        }
    }
}