Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Common/ConsoleEx.cs @ 11842

Last change on this file since 11842 was 11806, checked in by gkronber, 9 years ago

#2283: separated value-states from done-states in GenericGrammarPolicy and removed disabling of actions from bandit policies

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