using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace HeuristicLab.Common { // for controlling console colors // http://stackoverflow.com/questions/25274019/strange-setconsolescreenbufferinfoex-behavior public class ConsoleEx { private static readonly ConsoleColor[] colors = (ConsoleColor[])Enum.GetValues(typeof(ConsoleColor)); static ConsoleEx() { // init colors /* IntPtr handle = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, null, 3, 0, IntPtr.Zero); CONSOLE_SCREEN_BUFFER_INFO_EX currentScreen = new CONSOLE_SCREEN_BUFFER_INFO_EX(); currentScreen.cbSize = (uint)Marshal.SizeOf(currentScreen); GetConsoleScreenBufferInfoEx(handle, ref currentScreen); currentScreen.ColorF = currentScreen.Color0; currentScreen.dwSizeX = 80; currentScreen.dwSizeY = 30; SetConsoleScreenBufferInfoEx(handle, ref currentScreen); CloseHandle(handle);*/ // Initializing Screen with Testcase IntPtr handle = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, null, 3, 0, IntPtr.Zero); CONSOLE_SCREEN_BUFFER_INFO_EX currentScreen = new CONSOLE_SCREEN_BUFFER_INFO_EX(); currentScreen.cbSize = (uint)Marshal.SizeOf(currentScreen); GetConsoleScreenBufferInfoEx(handle, ref currentScreen); CONSOLE_SCREEN_BUFFER_INFO_EX newData = currentScreen; // newData.srWindowRight = 300; // newData.srWindowBottom = 300; double startRed = 0; double endRed = 1.41; double startGreen = -1.41; double endGreen = 1.41; double startBlue = -3; double endBlue = 1; double startAlpha = 0; double endAlpha = 0; // fire //var red = new int[] { 1, 49, 98, 146, 173, 195, 217, 240, 255, 255, 255, 255, 255, 255, 255 }; //var green = new int[] { 0, 0, 0, 0, 0, 0, 35, 79, 117, 147, 175, 205, 234, 255, 255 }; //var blue = new int[] { 96, 165, 220, 210, 151, 93, 35, 0, 0, 0, 0, 0, 0, 98, 223 }; var red = new int[16]; var green = new int[16]; var blue = new int[16]; var alpha = new int[16]; var r = startRed; var g = startGreen; var b = startBlue; var a = startAlpha; var n = 15; for (int i = 0; i < n; i++) { red[i] = (int)Math.Round(Math.Max(Math.Min(1.0, r), 0) * 255); green[i] = (int)Math.Round(Math.Max(Math.Min(1.0, g), 0) * 255); blue[i] = (int)Math.Round(Math.Max(Math.Min(1.0, b), 0) * 255); alpha[i] = (int)Math.Round(Math.Max(Math.Min(1.0, a), 0) * 255); r += (endRed - startRed) / n; g += (endGreen - startGreen) / n; b += (endBlue - startBlue) / n; a += (endAlpha - startAlpha) / n; } newData.ColorF = newData.Color7; int h = 0; newData.Color0 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++; newData.Color1 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++; newData.Color2 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++; newData.Color3 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++; newData.Color4 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++; newData.Color5 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++; newData.Color6 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++; newData.Color7 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++; newData.Color8 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++; newData.Color9 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++; newData.ColorA = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++; newData.ColorB = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++; newData.ColorC = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++; newData.ColorD = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++; newData.ColorE = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; // newData.ColorF = (r << 16) + (g << 8) + b; SetConsoleScreenBufferInfoEx(handle, ref newData); Console.ForegroundColor = ConsoleColor.White; CloseHandle(handle); } public static ConsoleColor ColorForValue(double d) { //Debug.Assert(d >= 0 && d <= 1.0); d = Math.Min(1.0, Math.Max(0.0, d)); var cIdx = Math.Max(0, (int)Math.Floor(d * 15) - 1); return colors[cIdx]; } [DllImport("kernel32.dll", SetLastError = true)] public static extern bool GetConsoleScreenBufferInfoEx(IntPtr handle, ref CONSOLE_SCREEN_BUFFER_INFO_EX consoleScreenBufferInfoEx); [DllImport("kernel32.dll", SetLastError = true)] static extern bool SetConsoleScreenBufferInfoEx(IntPtr handle, ref CONSOLE_SCREEN_BUFFER_INFO_EX consoleScreenBufferInfoEx); [StructLayout(LayoutKind.Sequential)] public struct CONSOLE_SCREEN_BUFFER_INFO_EX { public uint cbSize; // 96 public short dwSizeX; public short dwSizeY; public short dwCursorPositionX; public short dwCursorPositionY; public short wAttributes; public short srWindowLeft; public short srWindowTop; public short srWindowRight; public short srWindowBottom; public short dwMaximumWindowSizeX; public short dwMaximumWindowSizeY; public ushort wPopupAttributes; public bool bFullscreenSupported; public int Color0; public int Color1; public int Color2; public int Color3; public int Color4; public int Color5; public int Color6; public int Color7; public int Color8; public int Color9; public int ColorA; public int ColorB; public int ColorC; public int ColorD; public int ColorE; public int ColorF; } [DllImport("kernel32.dll", SetLastError = true)] private static extern IntPtr CreateFile( string Filename, int DesiredAccess, int ShareMode, [In][MarshalAs(UnmanagedType.LPStruct)]SecurityAttributes SecAttr, int CreationDisposition, int FlagsAndAttributes, IntPtr TemplateFile); private const int GENERIC_READ = unchecked((int)0x80000000); private const int GENERIC_WRITE = 0x40000000; private const int FILE_SHARE_READ = 1; private const int FILE_SHARE_WRITE = 2; [DllImport("kernel32.dll")] private static extern bool CloseHandle(IntPtr handle); [StructLayout(LayoutKind.Sequential)] private class SecurityAttributes { public int nLength; public IntPtr lpSecurityDescriptor; public bool bInheritHandle; } } }