Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for colorselectbox.cs

(view source code of colorselectbox.cs as plain text)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Windows.Forms;
  6.  
  7.  
  8. namespace RobvanderWoude
  9. {
  10. 	class ColorSelectBox
  11. 	{
  12. 		#region Global Variables
  13.  
  14. 		static string progver = "1.00";
  15. 		static string defaultcaption = "Pick a Console Color";
  16. 		static string caption = defaultcaption;
  17. 		static bool usergb = false;
  18. 		public static Form colordialog;
  19. 		public static int selectedcolor = -1;
  20. 		// Available standard console colors;
  21. 		// do NOT change the order of these values,
  22. 		// even if it seems illogical at index 7 and 8
  23. 		static int[] consolecolors = new int[] {
  24. 				0x000000, // 0 Black
  25. 				0x000080, // 1 DarkBlue
  26. 				0x008000, // 2 DarkGreen
  27. 				0x008080, // 3 DarkCyan
  28. 				0x800000, // 4 DarkRed
  29. 				0x800080, // 5 DarkMagenta
  30. 				0x808000, // 6 DarkYellow
  31. 				0xC0C0C0, // 7 Gray
  32. 				0x808080, // 8 DarkGray
  33. 				0x0000FF, // 9 Blue
  34. 				0x00FF00, // A Green
  35. 				0x00FFFF, // B Cyan
  36. 				0xFF0000, // C Red
  37. 				0xFF00FF, // D Magenta
  38. 				0xFFFF00, // E Yellow
  39. 				0xFFFFFF  // F White
  40. 		};
  41.  
  42. 		#endregion Global Variables
  43.  
  44.  
  45. 		static int Main( string[] args )
  46. 		{
  47. 			#region Command Line Parsing
  48.  
  49. 			if ( args.Length == 1 && args[0] == "/?" )
  50. 			{
  51. 				return ShowHelp( );
  52. 			}
  53. 			foreach(string arg in args )
  54. 			{
  55. 				if ( arg[0] == '/' )
  56. 				{
  57. 					switch ( arg.ToUpper( )[1] )
  58. 					{
  59. 						case 'R':
  60. 							if ( usergb )
  61. 							{
  62. 								return ShowHelp( "Duplicate command line switch /R" );
  63. 							}
  64. 							usergb = true;
  65. 							break;
  66. 						case 'T':
  67. 							TestColors( );
  68. 							return -1;
  69. 						case '?':
  70. 							return ShowHelp( );
  71. 						default:
  72. 							return ShowHelp( "Invalid command line switch {0}", arg );
  73. 					}
  74. 				}
  75. 				else
  76. 				{
  77. 					if ( caption != defaultcaption )
  78. 					{
  79. 						return ShowHelp( "Duplicate command line argument" );
  80. 					}
  81. 					caption = args[0];
  82. 				}
  83. 			}
  84.  
  85. 			#endregion Command Line Parsing
  86.  
  87.  
  88. 			// Define dialog window
  89. 			colordialog = new Form( );
  90. 			colordialog.Size = new Size( 230, 110 );
  91. 			colordialog.SizeGripStyle = SizeGripStyle.Hide;
  92. 			colordialog.Text = caption;
  93. 			//colordialog.MinimumSize = new Size( colordialog.Width, colordialog.Height );
  94. 			//colordialog.MaximumSize = new Size( colordialog.Width, colordialog.Height );
  95. 			colordialog.MinimizeBox = false;
  96. 			colordialog.MaximizeBox = false;
  97. 			int[] cols = new int[8];
  98. 			int[] rows = new int[2];
  99. 			// Fill dialog with color boxes
  100. 			for ( int col = 0; col < cols.Length; col++ )
  101. 			{
  102. 				for ( int row = 0; row < rows.Length; row++ )
  103. 				{
  104. 					TextBox colorbox = new TextBox( );
  105. 					colorbox.Size = new Size( 20, 20 );
  106. 					colorbox.Location = new Point( 10 + col * 25, 10 + row * 30 );
  107. 					int[] rgb = Color2RGB( consolecolors[8 * row + col] );
  108. 					colorbox.BackColor = Color.FromArgb( rgb[0], rgb[1], rgb[2] );
  109. 					colorbox.Click += Colorbox_Click;
  110. 					colordialog.Controls.Add( colorbox );
  111. 				}
  112. 			}
  113. 			// Make sure the cursor won't be visible in one of the color boxes
  114. 			colordialog.Select( );
  115. 			// Show the dialog window
  116. 			colordialog.ShowDialog( );
  117. 			return selectedcolor;
  118. 		}
  119.  
  120.  
  121. 		static int[] Color2RGB( int color )
  122. 		{
  123. 			int red = Convert.ToInt32( Math.Floor( ( (decimal) color ) / 65536 ) );
  124. 			int green = Convert.ToInt32( Math.Floor( ( ( (decimal) color ) - red * 65536 ) / 256 ) );
  125. 			int blue = color - red * 65536 - green * 256;
  126. 			return new int[3] { red, green, blue };
  127. 		}
  128.  
  129.  
  130. 		private static void Colorbox_Click( object sender, EventArgs e )
  131. 		{
  132. 			foreach ( Control colorbox in colordialog.Controls )
  133. 			{
  134. 				if ( colorbox.GetType( ) == typeof( TextBox ) )
  135. 				{
  136. 					// If the box has focus, it is the one that was clicked
  137. 					if ( colorbox.Focused )
  138. 					{
  139. 						selectedcolor = ( colorbox.BackColor.R * 256 + colorbox.BackColor.G ) * 256 + colorbox.BackColor.B;
  140. 						if ( usergb )
  141. 						{
  142. 							// Return RGB value of the selected color, e.g. 255,0,0 for bright red
  143. 							Console.Write( "{0},{1},{2}", colorbox.BackColor.R, colorbox.BackColor.G, colorbox.BackColor.B );
  144. 						}
  145. 						else
  146. 						{
  147. 							// Return the "index" of the selected color, e.g. 12 decimal / C hexadecimal for bright red
  148. 							selectedcolor = consolecolors.ToList<int>( ).IndexOf( selectedcolor );
  149. 							Console.Write( "{0:X}", selectedcolor );
  150. 						}
  151. 						colordialog.Close( );
  152. 						return;
  153. 					}
  154. 				}
  155. 			}
  156. 		}
  157.  
  158.  
  159. 		public static void TestColors( )
  160. 		{
  161. 			int width = Console.WindowWidth * 2;
  162. 			string text;
  163. 			for ( int i = 0; i < 16; i++ )
  164. 			{
  165. 				Console.BackgroundColor = (ConsoleColor) i;
  166. 				Console.ForegroundColor = ConsoleColor.White;
  167. 				int[] rgb = Color2RGB( consolecolors[i] );
  168. 				text = String.Format( "{0,-21} Index = {1,2} (0x{1:X})           RGB = {2,11} (0x{3:X6})", ( (ConsoleColor) i ).ToString( ), i, String.Join( ",", rgb ), consolecolors[i] );
  169. 				if ( i > 9 )
  170. 				{
  171. 					Console.ForegroundColor = ConsoleColor.Black;
  172. 				}
  173. 				Console.Write( "{0,-" + width + "}", text );
  174. 				Console.ResetColor( );
  175. 			}
  176. 		}
  177.  
  178.  
  179. 		public static int ShowHelp( params string[] errmsg )
  180. 		{
  181. 			/*
  182. 			ColorSelectBox,  Version 1.00
  183. 			Batch tool to present a color picker and return the selected (console) color
  184.  
  185. 			Usage:    COLORSELECTBOX  [ "title" ]  [ /R ]  [ /T ]
  186.  
  187. 			Where:    "title"     is the optional custom caption in the title bar
  188. 			                      (default: Pick a Console Color)
  189. 			          /R          return RGB value (R,G,B) of the selected color
  190. 			                      (default: single hexadcimal digit)
  191. 			          /T          Test: show console colors and their return values
  192.  
  193. 			Notes:    Only the 16 standard console colors are available.
  194. 			          By default, the hexadecimal "index" of the selected
  195. 			          console color is shown on screen (e.g. "C" for bright red)
  196. 			          and returned as return code (e.g. 12 for bright red).
  197. 			          With /R however, the RGB value of the selected color
  198. 			          is shown (e.g. "255,0,0" for bright red) and returned
  199. 			          as return code (i.e. 65536 x red + 256 x green + blue).
  200. 			          With /T any other command line arguments will be ignored.
  201. 			          With /T or if no selection is made or in case of
  202. 			          (command line) errors, return code is -1.
  203.  
  204. 			Written by Rob van der Woude
  205. 			http://www.robvanderwoude.com
  206. 			*/
  207.  
  208. 			if ( errmsg.Length > 0 )
  209. 			{
  210. 				List<string> errargs = new List<string>( errmsg );
  211. 				errargs.RemoveAt( 0 );
  212. 				Console.Error.WriteLine( );
  213. 				Console.ForegroundColor = ConsoleColor.Red;
  214. 				Console.Error.Write( "ERROR:\t" );
  215. 				Console.ForegroundColor = ConsoleColor.White;
  216. 				Console.Error.WriteLine( errmsg[0], errargs.ToArray( ) );
  217. 				Console.ResetColor( );
  218. 			}
  219.  
  220. 			Console.Error.WriteLine( );
  221.  
  222. 			Console.Error.WriteLine( "ColorSelectBox,  Version {0}", progver );
  223.  
  224. 			Console.Error.WriteLine( "Batch tool to present a color picker and return the selected (console) color" );
  225.  
  226. 			Console.Error.WriteLine( );
  227.  
  228. 			Console.Error.Write( "Usage:    " );
  229. 			Console.ForegroundColor = ConsoleColor.White;
  230. 			Console.Error.WriteLine( "COLORSELECTBOX  [ \"title\" ]  [ /R ]  [ /T ]" );
  231. 			Console.ResetColor( );
  232.  
  233. 			Console.Error.WriteLine( );
  234.  
  235. 			Console.Error.Write( "Where:    " );
  236. 			Console.ForegroundColor = ConsoleColor.White;
  237. 			Console.Error.Write( "\"title\"" );
  238. 			Console.ResetColor( );
  239. 			Console.Error.WriteLine( "     is the optional custom caption in the title bar" );
  240.  
  241. 			Console.Error.WriteLine( "                      (default: {0})", caption );
  242.  
  243. 			Console.ForegroundColor = ConsoleColor.White;
  244. 			Console.Error.Write( "          /R" );
  245. 			Console.ResetColor( );
  246. 			Console.Error.Write( "          return " );
  247. 			Console.ForegroundColor = ConsoleColor.White;
  248. 			Console.Error.Write( "R" );
  249. 			Console.ResetColor( );
  250. 			Console.Error.WriteLine( "GB value (R,G,B) of the selected color" );
  251.  
  252. 			Console.Error.WriteLine( "                      (default: single hexadcimal digit)" );
  253.  
  254. 			Console.ForegroundColor = ConsoleColor.White;
  255. 			Console.Error.Write( "          /T          T" );
  256. 			Console.ResetColor( );
  257. 			Console.Error.WriteLine( "est: show console colors and their return values" );
  258.  
  259. 			Console.Error.WriteLine( );
  260.  
  261. 			Console.Error.WriteLine( "Notes:    Only the 16 standard console colors are available." );
  262.  
  263. 			Console.Error.WriteLine( "          By default, the hexadecimal \"index\" of the selected" );
  264.  
  265. 			Console.Error.WriteLine( "          console color is shown on screen (e.g. \"C\" for bright red)" );
  266.  
  267. 			Console.Error.WriteLine( "          and returned as return code (e.g. 12 for bright red)." );
  268.  
  269. 			Console.Error.Write( "          With " );
  270. 			Console.ForegroundColor = ConsoleColor.White;
  271. 			Console.Error.Write( "/R" );
  272. 			Console.ResetColor( );
  273. 			Console.Error.WriteLine( " however, the RGB value of the selected color" );
  274.  
  275. 			Console.Error.WriteLine( "          is shown (e.g. \"255,0,0\" for bright red) and returned" );
  276.  
  277. 			Console.Error.WriteLine( "          as return code (i.e. 65536 x red + 256 x green + blue)." );
  278.  
  279. 			Console.Error.Write( "          With " );
  280. 			Console.ForegroundColor = ConsoleColor.White;
  281. 			Console.Error.Write( "/T" );
  282. 			Console.ResetColor( );
  283. 			Console.Error.WriteLine( " any other command line arguments will be ignored." );
  284.  
  285. 			Console.Error.Write( "          With " );
  286. 			Console.ForegroundColor = ConsoleColor.White;
  287. 			Console.Error.Write( "/T" );
  288. 			Console.ResetColor( );
  289. 			Console.Error.WriteLine( " or if no selection is made or in case of" );
  290.  
  291. 			Console.Error.WriteLine( "          (command line) errors, return code is -1." );
  292.  
  293. 			Console.Error.WriteLine( );
  294.  
  295. 			Console.Error.WriteLine( "Written by Rob van der Woude" );
  296.  
  297. 			Console.Error.WriteLine( "http://www.robvanderwoude.com" );
  298.  
  299. 			return -1;
  300. 		}
  301. 	}
  302. }
  303.  

page last uploaded: 2018-12-04, 10:47