Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2283: worked on contextual MCTS

File size: 6.9 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      var cIdx = Math.Max(0, (int)Math.Floor(d * 15) - 1);
112      return colors[cIdx];
113    }
114
115
116
117
118    [DllImport("kernel32.dll", SetLastError = true)]
119    public static extern bool GetConsoleScreenBufferInfoEx(IntPtr handle, ref CONSOLE_SCREEN_BUFFER_INFO_EX consoleScreenBufferInfoEx);
120    [DllImport("kernel32.dll", SetLastError = true)]
121    static extern bool SetConsoleScreenBufferInfoEx(IntPtr handle, ref CONSOLE_SCREEN_BUFFER_INFO_EX consoleScreenBufferInfoEx);
122
123    [StructLayout(LayoutKind.Sequential)]
124    public struct CONSOLE_SCREEN_BUFFER_INFO_EX {
125      public uint cbSize;  // 96
126      public short dwSizeX;
127      public short dwSizeY;
128      public short dwCursorPositionX;
129      public short dwCursorPositionY;
130      public short wAttributes;
131      public short srWindowLeft;
132      public short srWindowTop;
133      public short srWindowRight;
134      public short srWindowBottom;
135      public short dwMaximumWindowSizeX;
136      public short dwMaximumWindowSizeY;
137
138      public ushort wPopupAttributes;
139      public bool bFullscreenSupported;
140
141      public int Color0;
142      public int Color1;
143      public int Color2;
144      public int Color3;
145
146      public int Color4;
147      public int Color5;
148      public int Color6;
149      public int Color7;
150
151      public int Color8;
152      public int Color9;
153      public int ColorA;
154      public int ColorB;
155
156      public int ColorC;
157      public int ColorD;
158      public int ColorE;
159      public int ColorF;
160    }
161
162    [DllImport("kernel32.dll", SetLastError = true)]
163    private static extern IntPtr CreateFile(
164        string Filename,
165        int DesiredAccess,
166        int ShareMode,
167        [In][MarshalAs(UnmanagedType.LPStruct)]SecurityAttributes SecAttr,
168        int CreationDisposition,
169        int FlagsAndAttributes,
170        IntPtr TemplateFile);
171
172    private const int GENERIC_READ = unchecked((int)0x80000000);
173    private const int GENERIC_WRITE = 0x40000000;
174    private const int FILE_SHARE_READ = 1;
175    private const int FILE_SHARE_WRITE = 2;
176
177    [DllImport("kernel32.dll")]
178    private static extern bool CloseHandle(IntPtr handle);
179
180    [StructLayout(LayoutKind.Sequential)]
181    private class SecurityAttributes {
182      public int nLength;
183      public IntPtr lpSecurityDescriptor;
184      public bool bInheritHandle;
185    }
186  }
187}
Note: See TracBrowser for help on using the repository browser.