Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Common/ConsoleEx.cs @ 13847

Last change on this file since 13847 was 11981, checked in by gkronber, 10 years ago

#2283: cleanup and included HeuristicLab.dlls to create a self-contained branch

File size: 7.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Drawing;
5using System.Linq;
6using System.Runtime.InteropServices;
7using System.Text;
8using System.Threading.Tasks;
9
10namespace HeuristicLab.Common {
11  // for controlling console colors
12  // see output of Solvers (PrintStats)
13  // http://stackoverflow.com/questions/25274019/strange-setconsolescreenbufferinfoex-behavior
14  public class ConsoleEx {
15
16    private static readonly ConsoleColor[] colors = (ConsoleColor[])Enum.GetValues(typeof(ConsoleColor));
17
18    static ConsoleEx() {
19      // init colors
20      /*
21IntPtr handle = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, null, 3, 0, IntPtr.Zero);
22
23CONSOLE_SCREEN_BUFFER_INFO_EX currentScreen = new CONSOLE_SCREEN_BUFFER_INFO_EX();
24currentScreen.cbSize = (uint)Marshal.SizeOf(currentScreen);
25
26GetConsoleScreenBufferInfoEx(handle, ref currentScreen);
27
28currentScreen.ColorF = currentScreen.Color0;
29currentScreen.dwSizeX = 80;
30currentScreen.dwSizeY = 30;
31
32SetConsoleScreenBufferInfoEx(handle, ref currentScreen);
33CloseHandle(handle);*/
34      // Initializing Screen with Testcase
35
36      IntPtr handle = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, null, 3, 0, IntPtr.Zero);
37      CONSOLE_SCREEN_BUFFER_INFO_EX currentScreen = new CONSOLE_SCREEN_BUFFER_INFO_EX();
38      currentScreen.cbSize = (uint)Marshal.SizeOf(currentScreen);
39      GetConsoleScreenBufferInfoEx(handle, ref currentScreen);
40
41
42      CONSOLE_SCREEN_BUFFER_INFO_EX newData = currentScreen;
43
44      // newData.srWindowRight = 300;
45      // newData.srWindowBottom = 300;
46
47      double startRed = 0;
48      double endRed = 1.41;
49
50      double startGreen = -1.41;
51      double endGreen = 1.41;
52
53      double startBlue = -3;
54      double endBlue = 1;
55
56      double startAlpha = 0;
57      double endAlpha = 0;
58
59      // fire
60      //var red = new int[] { 1, 49, 98, 146, 173, 195, 217, 240, 255, 255, 255, 255, 255, 255, 255 };
61      //var green = new int[] { 0, 0, 0, 0, 0, 0, 35, 79, 117, 147, 175, 205, 234, 255, 255 };
62      //var blue = new int[] { 96, 165, 220, 210, 151, 93, 35, 0, 0, 0, 0, 0, 0, 98, 223 };
63      var red = new int[16];
64      var green = new int[16];
65      var blue = new int[16];
66      var alpha = new int[16];
67
68      var r = startRed;
69      var g = startGreen;
70      var b = startBlue;
71      var a = startAlpha;
72      var n = 15;
73      for (int i = 0; i < n; i++) {
74        red[i] = (int)Math.Round(Math.Max(Math.Min(1.0, r), 0) * 255);
75        green[i] = (int)Math.Round(Math.Max(Math.Min(1.0, g), 0) * 255);
76        blue[i] = (int)Math.Round(Math.Max(Math.Min(1.0, b), 0) * 255);
77        alpha[i] = (int)Math.Round(Math.Max(Math.Min(1.0, a), 0) * 255);
78
79        r += (endRed - startRed) / n;
80        g += (endGreen - startGreen) / n;
81        b += (endBlue - startBlue) / n;
82        a += (endAlpha - startAlpha) / n;
83      }
84
85      newData.ColorF = newData.Color7;
86      int h = 0;
87      newData.Color0 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++;
88      newData.Color1 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++;
89      newData.Color2 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++;
90      newData.Color3 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++;
91      newData.Color4 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++;
92      newData.Color5 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++;
93      newData.Color6 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++;
94      newData.Color7 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++;
95      newData.Color8 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++;
96      newData.Color9 = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++;
97      newData.ColorA = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++;
98      newData.ColorB = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++;
99      newData.ColorC = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++;
100      newData.ColorD = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h]; h++;
101      newData.ColorE = (alpha[h] << 24) + (blue[h] << 16) + (green[h] << 8) + red[h];
102      // newData.ColorF = (r << 16) + (g << 8) + b;
103
104      SetConsoleScreenBufferInfoEx(handle, ref newData);
105      Console.ForegroundColor = ConsoleColor.White;
106
107      CloseHandle(handle);
108    }
109
110    public static ConsoleColor ColorForValue(double d) {
111      //Debug.Assert(d >= 0 && d <= 1.0);
112      d = Math.Min(1.0, Math.Max(0.0, d));
113      var cIdx = Math.Max(0, (int)Math.Floor(d * 15) - 1);
114      return colors[cIdx];
115    }
116
117
118
119
120    [DllImport("kernel32.dll", SetLastError = true)]
121    public static extern bool GetConsoleScreenBufferInfoEx(IntPtr handle, ref CONSOLE_SCREEN_BUFFER_INFO_EX consoleScreenBufferInfoEx);
122    [DllImport("kernel32.dll", SetLastError = true)]
123    static extern bool SetConsoleScreenBufferInfoEx(IntPtr handle, ref CONSOLE_SCREEN_BUFFER_INFO_EX consoleScreenBufferInfoEx);
124
125    [StructLayout(LayoutKind.Sequential)]
126    public struct CONSOLE_SCREEN_BUFFER_INFO_EX {
127      public uint cbSize;  // 96
128      public short dwSizeX;
129      public short dwSizeY;
130      public short dwCursorPositionX;
131      public short dwCursorPositionY;
132      public short wAttributes;
133      public short srWindowLeft;
134      public short srWindowTop;
135      public short srWindowRight;
136      public short srWindowBottom;
137      public short dwMaximumWindowSizeX;
138      public short dwMaximumWindowSizeY;
139
140      public ushort wPopupAttributes;
141      public bool bFullscreenSupported;
142
143      public int Color0;
144      public int Color1;
145      public int Color2;
146      public int Color3;
147
148      public int Color4;
149      public int Color5;
150      public int Color6;
151      public int Color7;
152
153      public int Color8;
154      public int Color9;
155      public int ColorA;
156      public int ColorB;
157
158      public int ColorC;
159      public int ColorD;
160      public int ColorE;
161      public int ColorF;
162    }
163
164    [DllImport("kernel32.dll", SetLastError = true)]
165    private static extern IntPtr CreateFile(
166        string Filename,
167        int DesiredAccess,
168        int ShareMode,
169        [In][MarshalAs(UnmanagedType.LPStruct)]SecurityAttributes SecAttr,
170        int CreationDisposition,
171        int FlagsAndAttributes,
172        IntPtr TemplateFile);
173
174    private const int GENERIC_READ = unchecked((int)0x80000000);
175    private const int GENERIC_WRITE = 0x40000000;
176    private const int FILE_SHARE_READ = 1;
177    private const int FILE_SHARE_WRITE = 2;
178
179    [DllImport("kernel32.dll")]
180    private static extern bool CloseHandle(IntPtr handle);
181
182    [StructLayout(LayoutKind.Sequential)]
183    private class SecurityAttributes {
184      public int nLength;
185      public IntPtr lpSecurityDescriptor;
186      public bool bInheritHandle;
187    }
188  }
189}
Note: See TracBrowser for help on using the repository browser.