Created
November 8, 2017 20:11
-
-
Save devhawk/ef4f3e7b3bf1aa3bd5cc8c7e24fcacc6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Set-ConsoleColors { | |
| param ([string]$theme = "campbell") | |
| # Following source adapted from https://github.com/Microsoft/console/tree/master/tools/ColorTool | |
| add-type -TypeDefinition @' | |
| using System; | |
| using System.Runtime.InteropServices; | |
| namespace ColorTool | |
| { | |
| public class ConsoleAPI | |
| { | |
| [StructLayout(LayoutKind.Sequential)] | |
| struct COORD | |
| { | |
| public short X; | |
| public short Y; | |
| } | |
| [StructLayout(LayoutKind.Sequential)] | |
| struct SMALL_RECT | |
| { | |
| public short Left; | |
| public short Top; | |
| public short Right; | |
| public short Bottom; | |
| } | |
| [StructLayout(LayoutKind.Sequential)] | |
| struct CONSOLE_SCREEN_BUFFER_INFO_EX | |
| { | |
| public uint cbSize; | |
| public COORD dwSize; | |
| public COORD dwCursorPosition; | |
| public short wAttributes; | |
| public SMALL_RECT srWindow; | |
| public COORD dwMaximumWindowSize; | |
| public ushort wPopupAttributes; | |
| public bool bFullscreenSupported; | |
| [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] | |
| public uint[] ColorTable; | |
| public static CONSOLE_SCREEN_BUFFER_INFO_EX Create() | |
| { | |
| return new CONSOLE_SCREEN_BUFFER_INFO_EX { cbSize = 96 }; | |
| } | |
| } | |
| [DllImport("kernel32.dll", SetLastError = true)] | |
| static extern IntPtr GetStdHandle(int nStdHandle); | |
| [DllImport("kernel32.dll", SetLastError = true)] | |
| static extern bool GetConsoleScreenBufferInfoEx(IntPtr hConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO_EX ConsoleScreenBufferInfo); | |
| [DllImport("kernel32.dll", SetLastError = true)] | |
| static extern bool SetConsoleScreenBufferInfoEx(IntPtr ConsoleOutput, ref CONSOLE_SCREEN_BUFFER_INFO_EX ConsoleScreenBufferInfoEx); | |
| public static uint RGB(int r, int g, int b) | |
| { | |
| return (uint)r + (((uint)g) << 8) + (((uint)b) << 16); | |
| } | |
| public static bool SetConsoleProperties(uint[] colorTable) | |
| { | |
| CONSOLE_SCREEN_BUFFER_INFO_EX csbiex = CONSOLE_SCREEN_BUFFER_INFO_EX.Create(); | |
| int STD_OUTPUT_HANDLE = -11; | |
| IntPtr hOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
| bool success = GetConsoleScreenBufferInfoEx(hOut, ref csbiex); | |
| if (success) | |
| { | |
| csbiex.srWindow.Bottom++; | |
| for (int i = 0; i < 16; i++) | |
| { | |
| csbiex.ColorTable[i] = colorTable[i]; | |
| } | |
| SetConsoleScreenBufferInfoEx(hOut, ref csbiex); | |
| } | |
| return success; | |
| } | |
| } | |
| } | |
| '@ | |
| # Campbell, RedAlert and SolarLight color schemes | |
| $campbell = 789516,14300928,958739,14521914,2035653,9967496,40129,13421772,7763574,16742459,837142,14079585,5654759,10354868,10875385,15921906 | |
| $redalert = 2303094,15571783,7061105,12107371,5058261,14055656,7059646,14079702,2500134,15772005,9171119,14540727,5448928,14661597,12049887,16777215 | |
| $solarlight = 14939901,13798182,39301,10002730,3093212,12874092,35253,10592659,8616805,13798182,39301,10002730,3093212,12874092,35253,7695960 | |
| $colors = $campbell | |
| if ([string]::Compare($theme, "redalert", $true) -eq 0) { | |
| $colors = $redalert | |
| } elseif ([string]::Compare($theme, "solarlight", $true) -eq 0) { | |
| $colors = $solarlight | |
| } | |
| [ColorTool.ConsoleAPI]::SetConsoleProperties($colors) | Out-Null | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment