Advertisement
Blizzardo1

Colours

Jan 25th, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 34.21 KB | None | 0 0
  1. /**
  2.     Copyright © 2015 Adonis S. Deliannis (Blizzardo1)
  3. */
  4. #region Include Directives
  5.  
  6. using System;
  7. using System.Diagnostics;
  8. using System.Drawing;
  9. using System.Runtime.InteropServices;
  10.  
  11. #endregion Include Directives
  12.  
  13.     using static NativeMethods;
  14.  
  15.     /// <summary>
  16.     /// </summary>
  17.     public enum Colors : int
  18.     {
  19.         /// <summary>
  20.         ///     The color white
  21.         /// </summary>
  22.         White = 0,
  23.  
  24.         /// <summary>
  25.         ///     The color black
  26.         /// </summary>
  27.         Black = 1,
  28.  
  29.         /// <summary>
  30.         ///     The color blue
  31.         /// </summary>
  32.         Blue = 2,
  33.  
  34.         /// <summary>
  35.         ///     The color green
  36.         /// </summary>
  37.         Green = 3,
  38.  
  39.         /// <summary>
  40.         ///     The color red
  41.         /// </summary>
  42.         Red = 4,
  43.  
  44.         /// <summary>
  45.         ///     The color maroon
  46.         /// </summary>
  47.         Maroon = 5,
  48.  
  49.         /// <summary>
  50.         ///     The color purple
  51.         /// </summary>
  52.         Purple = 6,
  53.  
  54.         /// <summary>
  55.         ///     The color orange
  56.         /// </summary>
  57.         Orange = 7,
  58.  
  59.         /// <summary>
  60.         ///     The color yellow
  61.         /// </summary>
  62.         Yellow = 8,
  63.  
  64.         /// <summary>
  65.         ///     The color light green
  66.         /// </summary>
  67.         LightGreen = 9,
  68.  
  69.         /// <summary>
  70.         ///     The color dark cyan
  71.         /// </summary>
  72.         DarkCyan = 10,
  73.  
  74.         /// <summary>
  75.         ///     The color cyan
  76.         /// </summary>
  77.         Cyan = 11,
  78.  
  79.         /// <summary>
  80.         ///     The color royale blue
  81.         /// </summary>
  82.         Royale = 12,
  83.  
  84.         /// <summary>
  85.         ///     The color magenta
  86.         /// </summary>
  87.         Magenta = 13,
  88.  
  89.         /// <summary>
  90.         ///     The color gray
  91.         /// </summary>
  92.         Gray = 14,
  93.  
  94.         /// <summary>
  95.         ///     The color light gray
  96.         /// </summary>
  97.         LightGray = 15,
  98.  
  99.         /// <summary>
  100.         /// Color is overridden
  101.         /// </summary>
  102.         Overridden = -1
  103.     };
  104.  
  105.     [JsonObject ( Title = "Theme", Description = "Global Theme for the Client" )]
  106.     public struct Theme
  107.     {
  108.         private IrcPalette palette;
  109.         private IrcColor action;
  110.         private IrcColor ctcp;
  111.         private IrcColor error;
  112.         private IrcColor highlight;
  113.         private IrcColor info;
  114.         private IrcColor info2;
  115.         private IrcColor invite;
  116.         private IrcColor join;
  117.         private IrcColor kick;
  118.         private IrcColor mode;
  119.         private IrcColor nick;
  120.         private IrcColor normal;
  121.         private IrcColor notice;
  122.         private IrcColor notify;
  123.         private IrcColor other;
  124.         private IrcColor own;
  125.         private IrcColor part;
  126.         private IrcColor quit;
  127.         private IrcColor topic;
  128.         private IrcColor wallops;
  129.         private IrcColor whois;
  130.  
  131.         private IrcColor fore;
  132.         private IrcColor back;
  133.  
  134.         private string name;
  135.  
  136.         [JsonProperty ( PropertyName = "Palette" )]
  137.         public IrcPalette Palette => palette;
  138.  
  139.         [JsonProperty ( PropertyName = "Action" )]
  140.         public IrcColor Action => action;
  141.  
  142.         [JsonProperty ( PropertyName = "Back" )]
  143.         public IrcColor Background => back;
  144.  
  145.         [JsonProperty ( PropertyName = "Ctcp" )]
  146.         public IrcColor Ctcp => ctcp;
  147.  
  148.         [JsonProperty ( PropertyName = "Error" )]
  149.         public IrcColor Error => error;
  150.  
  151.         [JsonProperty ( PropertyName = "Foreground" )]
  152.         public IrcColor Foreground => fore;
  153.  
  154.         [JsonProperty ( PropertyName = "Highlight" )]
  155.         public IrcColor Highlight => highlight;
  156.  
  157.         [JsonProperty ( PropertyName = "Info" )]
  158.         public IrcColor Info => info;
  159.  
  160.         [JsonProperty ( PropertyName = "Info2" )]
  161.         public IrcColor Info2 => info2;
  162.  
  163.         [JsonProperty ( PropertyName = "Invite" )]
  164.         public IrcColor Invite => invite;
  165.  
  166.         [JsonProperty ( PropertyName = "Join" )]
  167.         public IrcColor Join => join;
  168.  
  169.         [JsonProperty ( PropertyName = "Kick" )]
  170.         public IrcColor Kick => kick;
  171.  
  172.         [JsonProperty ( PropertyName = "Mode" )]
  173.         public IrcColor Mode => mode;
  174.  
  175.         [JsonProperty ( PropertyName = "Nick" )]
  176.         public IrcColor Nick => nick;
  177.  
  178.         [JsonProperty ( PropertyName = "Normal" )]
  179.         public IrcColor Normal => normal;
  180.  
  181.         [JsonProperty ( PropertyName = "Notice" )]
  182.         public IrcColor Notice => notice;
  183.  
  184.         [JsonProperty ( PropertyName = "Notify" )]
  185.         public IrcColor Notify => notify;
  186.  
  187.         [JsonProperty ( PropertyName = "other" )]
  188.         public IrcColor Other => other;
  189.  
  190.         [JsonProperty ( PropertyName = "Own" )]
  191.         public IrcColor Own => own;
  192.  
  193.         [JsonProperty ( PropertyName = "Part" )]
  194.         public IrcColor Part => part;
  195.  
  196.         [JsonProperty ( PropertyName = "Quit" )]
  197.         public IrcColor Quit => quit;
  198.  
  199.         [JsonProperty ( PropertyName = "Topic" )]
  200.         public IrcColor Topic => topic;
  201.  
  202.         [JsonProperty ( PropertyName = "Wallops" )]
  203.         public IrcColor Wallops => wallops;
  204.  
  205.         [JsonProperty ( PropertyName = "Whois" )]
  206.         public IrcColor Whois => whois;
  207.  
  208.         [JsonProperty ( PropertyName = "Name" )]
  209.         public string Name => name;
  210.  
  211.         // [JsonConstructor]
  212.         public Theme ( string name,
  213.             IrcPalette palette,
  214.             IrcColor action,
  215.             IrcColor background,
  216.             IrcColor ctcp,
  217.             IrcColor error,
  218.             IrcColor foreground,
  219.             IrcColor highlight,
  220.             IrcColor info,
  221.             IrcColor info2,
  222.             IrcColor invite,
  223.             IrcColor join,
  224.             IrcColor kick,
  225.             IrcColor mode,
  226.             IrcColor nick,
  227.             IrcColor normal,
  228.             IrcColor notice,
  229.             IrcColor notify,
  230.             IrcColor other,
  231.             IrcColor own,
  232.             IrcColor part,
  233.             IrcColor quit,
  234.             IrcColor topic,
  235.             IrcColor wallops,
  236.             IrcColor whois
  237.             )
  238.         {
  239.             this.name = name;
  240.             this.palette = palette;
  241.             this.action = action;
  242.             this.ctcp = ctcp;
  243.             this.error = error;
  244.             this.highlight = highlight;
  245.             this.info = info;
  246.             this.info2 = info2;
  247.             this.invite = invite;
  248.             this.join = join;
  249.             this.kick = kick;
  250.             this.mode = mode;
  251.             this.nick = nick;
  252.             this.normal = normal;
  253.             this.notice = notice;
  254.             this.notify = notify;
  255.             this.other = other;
  256.             this.own = own;
  257.             this.part = part;
  258.             this.quit = quit;
  259.             this.topic = topic;
  260.             this.wallops = wallops;
  261.             this.whois = whois;
  262.             fore = foreground;
  263.             back = background;
  264.         }
  265.  
  266.         private static Theme _blizzy = new Theme
  267.         {
  268.             name = "Blizzy",
  269.             palette = IrcPalette.Blizzy,
  270.             action = IrcColor.FromCode ( 10 ),
  271.             back = IrcColor.FromCode ( 1 ),
  272.             ctcp = IrcColor.FromCode ( 5 ),
  273.             error = IrcColor.FromCode ( 4 ),
  274.             fore = IrcColor.FromCode ( 0 ),
  275.             highlight = IrcColor.FromCode ( 7 ),
  276.             info = IrcColor.FromCode ( 5 ),
  277.             info2 = IrcColor.FromCode ( 5 ),
  278.             invite = IrcColor.FromCode ( 3 ),
  279.             join = IrcColor.FromCode ( 11 ),
  280.             kick = IrcColor.FromCode ( 4 ),
  281.             mode = IrcColor.FromCode ( 10 ),
  282.             nick = IrcColor.FromCode ( 14 ),
  283.             normal = IrcColor.FromCode ( 0 ),
  284.             notice = IrcColor.FromCode ( 6 ),
  285.             notify = IrcColor.FromCode ( 2 ),
  286.             other = IrcColor.FromCode ( 15 ),
  287.             own = IrcColor.FromCode ( 0 ),
  288.             part = IrcColor.FromCode ( 10 ),
  289.             quit = IrcColor.FromCode ( 13 ),
  290.             topic = IrcColor.FromCode ( 12 ),
  291.             wallops = IrcColor.FromCode ( 12 ),
  292.             whois = IrcColor.FromCode ( 12 ),
  293.         };
  294.  
  295.         public static Theme Blizzy => _blizzy;
  296.  
  297.         private static Theme _mircClassic = new Theme
  298.         {
  299.             name = "mIRC Classic",
  300.             palette = IrcPalette.Default,
  301.             action = IrcColor.FromCode ( 6 ),
  302.             back = IrcColor.FromCode ( 0 ),
  303.             ctcp = IrcColor.FromCode ( 4 ),
  304.             error = IrcColor.FromCode ( 9 ),
  305.             fore = IrcColor.FromCode ( 1 ),
  306.             highlight = IrcColor.FromCode ( 5 ),
  307.             info = IrcColor.FromCode ( 2 ),
  308.             info2 = IrcColor.FromCode ( 3 ),
  309.             invite = IrcColor.FromCode ( 3 ),
  310.             join = IrcColor.FromCode ( 3 ),
  311.             kick = IrcColor.FromCode ( 3 ),
  312.             mode = IrcColor.FromCode ( 3 ),
  313.             nick = IrcColor.FromCode ( 3 ),
  314.             normal = IrcColor.FromCode ( 8 ),
  315.             notice = IrcColor.FromCode ( 5 ),
  316.             notify = IrcColor.FromCode ( 7 ),
  317.             other = IrcColor.FromCode ( 6 ),
  318.             own = IrcColor.FromCode ( 8 ),
  319.             part = IrcColor.FromCode ( 3 ),
  320.             quit = IrcColor.FromCode ( 2 ),
  321.             topic = IrcColor.FromCode ( 3 ),
  322.             wallops = IrcColor.FromCode ( 5 ),
  323.             whois = IrcColor.FromCode ( 8 )
  324.         };
  325.  
  326.         public static Theme mIrcClassic => _mircClassic;
  327.  
  328.         private static Theme _mircModern = new Theme
  329.         {
  330.             name = "mIRC Modern",
  331.             palette = IrcPalette.Modern,
  332.             action = IrcColor.FromCode ( 6 ),
  333.             back = IrcColor.FromCode ( 0 ),
  334.             ctcp = IrcColor.FromCode ( 4 ),
  335.             error = IrcColor.FromCode ( 8 ),
  336.             fore = IrcColor.FromCode ( 1 ),
  337.             highlight = IrcColor.FromCode ( 7 ),
  338.             info = IrcColor.FromCode ( 3 ),
  339.             info2 = IrcColor.FromCode ( 3 ),
  340.             invite = IrcColor.FromCode ( 3 ),
  341.             join = IrcColor.FromCode ( 3 ),
  342.             kick = IrcColor.FromCode ( 3 ),
  343.             mode = IrcColor.FromCode ( 3 ),
  344.             nick = IrcColor.FromCode ( 3 ),
  345.             normal = IrcColor.FromCode ( 0 ),
  346.             notice = IrcColor.FromCode ( 5 ),
  347.             notify = IrcColor.FromCode ( 7 ),
  348.             other = IrcColor.FromCode ( 6 ),
  349.             own = IrcColor.FromCode ( 0 ),
  350.             part = IrcColor.FromCode ( 3 ),
  351.             quit = IrcColor.FromCode ( 2 ),
  352.             topic = IrcColor.FromCode ( 3 ),
  353.             wallops = IrcColor.FromCode ( 5 ),
  354.             whois = IrcColor.FromCode ( 0 )
  355.         };
  356.  
  357.         public static Theme mIrcModern => _mircModern;
  358.     }
  359.  
  360.     public struct IrcPalette
  361.     {
  362.         private Color [ ] pal;
  363.         private bool defaultPalette;
  364.         private const int max = 16;
  365.  
  366.         [JsonProperty ( PropertyName = "DefaultPalette" )]
  367.         public bool DefaultPalette => defaultPalette;
  368.  
  369.         private string name;
  370.  
  371.         [JsonProperty ( PropertyName = "Name" )]
  372.         public string Name => name;
  373.  
  374.         private bool _isok;
  375.  
  376.         [JsonIgnore]
  377.         public bool IsOk => _isok;
  378.  
  379.         private static Dictionary<string, IrcPalette> _cache = new Dictionary<string, IrcPalette> ( );
  380.         public static Dictionary<string, IrcPalette> AvailablePalettes => _cache;
  381.  
  382.         [JsonConstructor]
  383.         private IrcPalette ( string name, bool defaultPalette = false )
  384.         {
  385.             this.defaultPalette = defaultPalette;
  386.             pal = new Color [ max ];
  387.             _isok = true;
  388.             this.name = name;
  389.  
  390.             if ( !_cache.ContainsKey ( name ) )
  391.             {
  392.                 // Console.WriteLine ( "Adding key {0}", name );
  393.                 _cache.Add ( name, this );
  394.             }
  395.         }
  396.  
  397.         public override bool Equals ( object obj )
  398.         {
  399.             return base.Equals ( obj );
  400.         }
  401.  
  402.         public override int GetHashCode ( )
  403.         {
  404.             return base.GetHashCode ( );
  405.         }
  406.  
  407.         public void OverrideConsolePalette ( )
  408.         {
  409.             // Console.WriteLine ( "Overriding Console Palette" );
  410.             ConsoleScreenBufferInfoEx info = new ConsoleScreenBufferInfoEx ( );
  411.             info.cbSize = 96;
  412.             IntPtr hConsole = ConsoleHandle;
  413.             GetConsoleScreenBufferInfoEx ( hConsole, ref info );
  414.             for ( int i = 0; i < pal.Length; i++ )
  415.             {
  416.                 info.ColorTable [ i ] = new ColorRef ( pal [ i ] );
  417.                 SetConsoleScreenBufferInfoEx ( hConsole, ref info );
  418.                 SetConsoleTextAttribute ( hConsole, ( ushort ) i );
  419.             }
  420.         }
  421.  
  422.         public Color GetColor ( int code )
  423.         {
  424.             if ( code >= 0 && code < max )
  425.                 return ( pal != null ) ? pal [ code ] : Color.White;
  426.             throw new IndexOutOfRangeException ( "code" );
  427.         }
  428.  
  429.         public Color GetColor ( Colors code )
  430.         {
  431.             return GetColor ( ( int ) code );
  432.         }
  433.  
  434.         public static bool operator == ( IrcPalette left, IrcPalette right )
  435.         {
  436.             return ( left.Equals ( right ) );
  437.         }
  438.  
  439.         public static bool operator != ( IrcPalette left, IrcPalette right )
  440.         {
  441.             return !( left.Equals ( right ) );
  442.         }
  443.  
  444.         public static IrcPalette Default => _cache.ContainsKey ( "Default" ) ? _cache [ "Default" ] : _default;
  445.         public static IrcPalette Blizzy => _cache.ContainsKey ( "Blizzy" ) ? _cache [ "Blizzy" ] : _blizzy;
  446.         public static IrcPalette Modern => _cache.ContainsKey ( "Modern" ) ? _cache [ "Modern" ] : _modern;
  447.         public static IrcPalette Monochrome => _cache.ContainsKey ( "Monochrome" ) ? _cache [ "Monochrome" ] : _monochrome;
  448.         public static IrcPalette PlacidHues => _cache.ContainsKey ( "PlacidHues" ) ? _cache [ "PlacidHues" ] : _placidHues;
  449.         public static IrcPalette RainbowSky => _cache.ContainsKey ( "RainbowSky" ) ? _cache [ "RainbowSky" ] : _rainbowSky;
  450.  
  451.         private static IrcPalette _default => new IrcPalette ( "Default", true )
  452.         {
  453.             pal = new Color [ max ]
  454.             {
  455.                 IrcColor.White, // White
  456.                 IrcColor.Black, // Black
  457.                 IrcColor.Blue,
  458.                 IrcColor.Green,
  459.                 IrcColor.Red,
  460.                 IrcColor.Maroon,
  461.                 IrcColor.Purple,
  462.                 IrcColor.Orange,
  463.                 IrcColor.Yellow,
  464.                 IrcColor.LightGreen,
  465.                 IrcColor.DarkCyan,
  466.                 IrcColor.Cyan,
  467.                 IrcColor.Royale,
  468.                 IrcColor.Magenta,
  469.                 IrcColor.Gray,
  470.                 IrcColor.LightGray
  471.             }
  472.         };
  473.  
  474.         private static IrcPalette _blizzy => new IrcPalette ( "Blizzy" )
  475.         {
  476.             pal = new Color [ max ]
  477.             {
  478.                 IrcColor.White, // White
  479.                 IrcColor.Black, // Black
  480.                 IrcColor.Blue,
  481.                 IrcColor.Green,
  482.                 IrcColor.Red,
  483.                 IrcColor.Maroon,
  484.                 IrcColor.Purple,
  485.                 IrcColor.Orange,
  486.                 IrcColor.Yellow,
  487.                 IrcColor.LightGreen,
  488.                 IrcColor.DarkCyan,
  489.                 IrcColor.Cyan,
  490.                 IrcColor.Royale,
  491.                 new IrcColor( Rgb ( 128, 255, 255 ), (Colors)13, "#80ffff" ),
  492.                 IrcColor.Gray,
  493.                 IrcColor.LightGray
  494.             }
  495.         };
  496.  
  497.         private static IrcPalette _modern => new IrcPalette ( "Modern" )
  498.         {
  499.             pal = new Color [ max ]
  500.             {
  501.                 IrcColor.White, // White
  502.                 IrcColor.Black, // Black
  503.                 new IrcColor(Rgb(0  ,0,167), (Colors)2, "#0000a7"),
  504.                 new IrcColor(Rgb(43 ,89,50) ,(Colors)3, "#2b5932"),
  505.                 new IrcColor(Rgb(240,0,0), (Colors)4, "#f00000" ),
  506.                 new IrcColor(Rgb(136,72,72), (Colors)5, "#884848"),
  507.                 new IrcColor(Rgb(112,0,128), (Colors)6, "#700080"),
  508.                 new IrcColor(Rgb(208,104,0), (Colors)7, "#d06800"),
  509.                 new IrcColor(Rgb(248,232,24), (Colors)8, "#f8e818"),
  510.                 new IrcColor(Rgb(0  ,224,0), (Colors)9, "#00e000"),
  511.                 new IrcColor(Rgb(20,114,1), (Colors)10, "#147201"),
  512.                 IrcColor.Cyan,
  513.                 new IrcColor(Rgb(0,0,252), (Colors)12, "#0000fc"),
  514.                 IrcColor.Magenta,
  515.                 IrcColor.Gray,
  516.                 IrcColor.LightGray
  517.             }
  518.         };
  519.  
  520.         private static IrcPalette _monochrome => new IrcPalette ( "Monochrome" )
  521.         {
  522.             pal = new Color [ max ]
  523.     {
  524.                 IrcColor.White, // White
  525.                 IrcColor.Black, // Black
  526.                 IrcColor.Gray,
  527.                 IrcColor.Gray,
  528.                 IrcColor.Gray,
  529.                 IrcColor.Gray,
  530.                 IrcColor.Gray,
  531.                 IrcColor.Gray,
  532.                 IrcColor.Gray,
  533.                 IrcColor.Gray,
  534.                 IrcColor.Gray,
  535.                 IrcColor.Gray,
  536.                 IrcColor.Gray,
  537.                 IrcColor.Gray,
  538.                 IrcColor.Gray,
  539.                 IrcColor.Gray
  540.     }
  541.         };
  542.  
  543.         private static IrcPalette _placidHues => new IrcPalette ( "PlacidHues" )
  544.         {
  545.             pal = new Color [ max ]
  546. {
  547.                 IrcColor.White,
  548.                 IrcColor.Black,
  549.                 new IrcColor(Rgb(108,47,0), Colors.Orange, "#6c2f00"),
  550.                 new IrcColor(Rgb(198,2,23), Colors.Red, "#c60217"),
  551.                 IrcColor.Red,
  552.                 new IrcColor(Rgb(67,78,167), Colors.Blue, "#434ea7"),
  553.                 new IrcColor(Rgb(62,100,100), Colors.DarkCyan, "#3e6464"),
  554.                 new IrcColor(Rgb(255,128,0), Colors.Orange, "#ff8000"),
  555.                 IrcColor.Yellow,
  556.                 new IrcColor(Rgb(0,128,64), Colors.Green, "#008040"),
  557.                 new IrcColor(Rgb(0,144,144), Colors.DarkCyan, "#009090"),
  558.                 new IrcColor(Rgb(25,96,151), Colors.Blue, "#196097"),
  559.                 IrcColor.Blue,
  560.                 IrcColor.Magenta,
  561.                 new IrcColor(Rgb(100,100,100), Colors.Gray, "#646464"),
  562.                 new IrcColor(Rgb(130,130,130), Colors.Gray, "#828282")
  563. }
  564.         };
  565.  
  566.         private static IrcPalette _rainbowSky => new IrcPalette ( "RainbowSky" )
  567.         {
  568.             pal = new Color [ max ]
  569. {
  570.                 IrcColor.White,
  571.                 new IrcColor (Rgb(55,55,55), Colors.Gray, "#373737"),
  572.                 new IrcColor(Rgb(112,112,184), Colors.Blue, "#7070b8"),
  573.                 new IrcColor(Rgb(0,128,255), Colors.Blue, "#0080ff"),
  574.                 IrcColor.Red,
  575.                 IrcColor.Orange,
  576.                 IrcColor.Yellow,
  577.                 new IrcColor(Rgb (0,168,0), Colors.Green, "#00a800"),
  578.                 IrcColor.DarkCyan,
  579.                 IrcColor.Cyan,
  580.                 new IrcColor (Rgb(0,0,252), Colors.Blue, "#0000fc"),
  581.                 IrcColor.Magenta,
  582.                 IrcColor.Gray,
  583.                 IrcColor.Purple,
  584.                 IrcColor.Gray,
  585.                 IrcColor.LightGray
  586. }
  587.         };
  588.     }
  589.  
  590.     public struct IrcColor
  591.     {
  592.         private static IrcPalette iPalette = IrcPalette.Default;
  593.         public static IrcPalette IrcColorPalette => iPalette;
  594.         private Colors clr;
  595.         private Color xlr;
  596.         private string name;
  597.  
  598.         public Colors IColor => clr;
  599.         public string Name => name;
  600.  
  601.         [JsonConstructor]
  602.         public IrcColor ( Colors color, string name )
  603.         {
  604.             clr = color;
  605.             xlr = iPalette.IsOk ? iPalette.GetColor ( clr ) : ( IrcPalette.Default.IsOk ? IrcPalette.Default.GetColor ( clr ) : Color.White );
  606.             this.name = name;
  607.         }
  608.  
  609.         public IrcColor ( Color color, Colors type, string name )
  610.         {
  611.             clr = type;
  612.             xlr = color;
  613.             this.name = name;
  614.         }
  615.  
  616.         public static void SetPalette ( IrcPalette palette )
  617.         {
  618.             iPalette = palette;
  619.         }
  620.  
  621.         //public IrcColor (Color color )
  622.         //{
  623.         //    clr = Colors.Overridden;
  624.         //    xlr = color;
  625.         //    iPalette = IrcPalette.Default;
  626.         //}
  627.  
  628.         public static IrcColor Black = new IrcColor ( Color.Black, Colors.Black, "Black" );
  629.         public static IrcColor Blue = new IrcColor ( Color.Blue, Colors.Blue, "Blue" );
  630.         public static IrcColor Cyan = new IrcColor ( Color.Cyan, Colors.Cyan, "Cyan" );
  631.         public static IrcColor DarkCyan = new IrcColor ( Color.DarkCyan, Colors.DarkCyan, "DarkCyan" );
  632.         public static IrcColor Gray = new IrcColor ( Color.Gray, Colors.Gray, "Gray" );
  633.         public static IrcColor LightGray = new IrcColor ( Color.LightGray, Colors.LightGray, "LightGray" );
  634.         public static IrcColor Green = new IrcColor ( Color.Green, Colors.Green, "Green" );
  635.         public static IrcColor LightGreen = new IrcColor ( Color.LightGreen, Colors.LightGreen, "LightGreen" );
  636.         public static IrcColor Magenta = new IrcColor ( Color.Magenta, Colors.Magenta, "Magenta" );
  637.         public static IrcColor Maroon = new IrcColor ( Color.Maroon, Colors.Maroon, "Maroon" );
  638.         public static IrcColor Orange = new IrcColor ( Color.Orange, Colors.Orange, "Orange" );
  639.         public static IrcColor Purple = new IrcColor ( Color.Purple, Colors.Purple, "Purple" );
  640.         public static IrcColor Red = new IrcColor ( Color.Red, Colors.Red, "Red" );
  641.         public static IrcColor Royale = new IrcColor ( Rgb ( 174, 174, 255 ), Colors.Royale, "Royale" );
  642.         public static IrcColor White = new IrcColor ( Color.White, Colors.White, "White" );
  643.         public static IrcColor Yellow = new IrcColor ( Color.Yellow, Colors.Yellow, "Yellow" );
  644.  
  645.         public static IrcColor FromCode ( int code )
  646.         {
  647.             // Console.WriteLine ( "Code = {0}", ( Colors ) code );
  648.             if ( code < -1 || code > MAX )
  649.                 throw new ArgumentOutOfRangeException ( "code" );
  650.             switch ( ( Colors ) code )
  651.             {
  652.                 case Colors.Black:
  653.                     return Black;
  654.  
  655.                 case Colors.Blue:
  656.                     return Blue;
  657.  
  658.                 case Colors.Cyan:
  659.                     return Cyan;
  660.  
  661.                 case Colors.DarkCyan:
  662.                     return DarkCyan;
  663.  
  664.                 case Colors.Gray:
  665.                     return Gray;
  666.  
  667.                 case Colors.Green:
  668.                     return Green;
  669.  
  670.                 case Colors.LightGray:
  671.                     return LightGray;
  672.  
  673.                 case Colors.LightGreen:
  674.                     return LightGreen;
  675.  
  676.                 case Colors.Magenta:
  677.                     return Magenta;
  678.  
  679.                 case Colors.Maroon:
  680.                     return Maroon;
  681.  
  682.                 case Colors.Orange:
  683.                     return Orange;
  684.  
  685.                 case Colors.Purple:
  686.                     return Purple;
  687.  
  688.                 case Colors.Red:
  689.                     return Red;
  690.  
  691.                 case Colors.Royale:
  692.                     return Royale;
  693.  
  694.                 case Colors.White:
  695.                     return White;
  696.  
  697.                 case Colors.Yellow:
  698.                     return Yellow;
  699.  
  700.                 case Colors.Overridden:
  701.                 default:
  702.                     return new IrcColor ( iPalette.IsOk ? iPalette.GetColor ( ( Colors ) code ) : Color.White, Colors.Overridden, "Custom" );
  703.             }
  704.         }
  705.  
  706.         public ConsoleColor ToConsoleColor ( )
  707.         {
  708.             return ( ConsoleColor ) ( ( int ) clr );
  709.         }
  710.  
  711.         public IrcColor Override ( string hex )
  712.         {
  713.             if ( !hex.StartsWith ( "#" ) )
  714.             {
  715.                 hex = hex.Insert ( 0, "#" );
  716.             }
  717.  
  718.             return new IrcColor { xlr = Color.FromName ( hex ) };
  719.         }
  720.  
  721.         public IrcColor Override ( byte r, byte g, byte b )
  722.         {
  723.             return ( Override ( ToHex ( r, g, b, 255 ) ) );
  724.         }
  725.  
  726.         public static implicit operator Color ( IrcColor iclr )
  727.         {
  728.             //if ( iPalette == IrcPalette.Default )
  729.             //{
  730.             //
  731.             //    if(iclr.clr == Colors.Overridden)
  732.             //    {
  733.             //        return iclr.xlr;
  734.             //    }
  735.             //    return FromCode ( (int)iclr.clr );
  736.             //
  737.             //    #region Old
  738.             switch ( iclr.clr )
  739.             {
  740.                 case Colors.Black:
  741.                     return Color.Black;
  742.  
  743.                 case Colors.Blue:
  744.                     return Color.Blue;
  745.  
  746.                 case Colors.Cyan:
  747.                     return Color.Cyan;
  748.  
  749.                 case Colors.DarkCyan:
  750.                     return Color.DarkCyan;
  751.  
  752.                 case Colors.Gray:
  753.                     return Color.Gray;
  754.  
  755.                 case Colors.LightGray:
  756.                     return Color.LightGray;
  757.  
  758.                 case Colors.Green:
  759.                     return Color.Green;
  760.  
  761.                 case Colors.LightGreen:
  762.                     return Color.LightGreen;
  763.  
  764.                 case Colors.Magenta:
  765.                     return Color.Magenta;
  766.  
  767.                 case Colors.Maroon:
  768.                     return Color.Maroon;
  769.  
  770.                 case Colors.Orange:
  771.                     return Color.Orange;
  772.  
  773.                 case Colors.Purple:
  774.                     return Color.Purple;
  775.  
  776.                 case Colors.Red:
  777.                     return Color.Red;
  778.  
  779.                 case Colors.Royale:
  780.                     return Color.RoyalBlue;
  781.  
  782.                 case Colors.White:
  783.                     return Color.White;
  784.  
  785.                 case Colors.Yellow:
  786.                     return Color.Yellow;
  787.  
  788.                 case Colors.Overridden:
  789.                     return iclr.xlr;
  790.             }
  791.             //    #endregion
  792.             //
  793.             //} else
  794.             if ( !iPalette.IsOk )
  795.                 SetPalette ( IrcPalette.Default );
  796.  
  797.             return iPalette.GetColor ( iclr.clr );
  798.         }
  799.  
  800.         public override string ToString ( )
  801.         {
  802.             return name;
  803.         }
  804.     }
  805.  
  806. internal class NativeMethods
  807.     {
  808.         public const int ForegroundBlue = 0x01;
  809.         public const int ForegroundGreen = 0x02;
  810.         public const int ForegroundRed = 0x04;
  811.         public const int ForegroundIntensity = 0x08;
  812.         public const int MAX = ForegroundIntensity | ForegroundRed | ForegroundGreen | ForegroundBlue;
  813.  
  814.         [StructLayout ( LayoutKind.Sequential )]
  815.         public struct ColorRef
  816.         {
  817.             public uint ColorDWORD;
  818.  
  819.             public ColorRef ( System.Drawing.Color color )
  820.             {
  821.                 ColorDWORD = ( uint ) color.R + ( ( ( uint ) color.G ) << 8 ) + ( ( ( uint ) color.B ) << 16 );
  822.             }
  823.  
  824.             public System.Drawing.Color GetColor ( )
  825.             {
  826.                 return System.Drawing.Color.FromArgb ( ( int ) ( 0x000000FFU & ColorDWORD ),
  827.                    ( int ) ( 0x0000FF00U & ColorDWORD ) >> 8, ( int ) ( 0x00FF0000U & ColorDWORD ) >> 16 );
  828.             }
  829.  
  830.             public void SetColor ( System.Drawing.Color color )
  831.             {
  832.                 ColorDWORD = ( uint ) color.R + ( ( ( uint ) color.G ) << 8 ) + ( ( ( uint ) color.B ) << 16 );
  833.             }
  834.         }
  835.  
  836.         [StructLayout ( LayoutKind.Sequential )]
  837.         public struct Coord
  838.         {
  839.             public short X;
  840.             public short Y;
  841.         }
  842.  
  843.         public struct SmallRect
  844.         {
  845.             public short Left;
  846.             public short Top;
  847.             public short Right;
  848.             public short Bottom;
  849.         }
  850.  
  851.         [StructLayout ( LayoutKind.Sequential )]
  852.         public struct ConsoleScreenBufferInfoEx
  853.         {
  854.             public uint cbSize;
  855.             public Coord dwSize;
  856.             public Coord dwCursorPosition;
  857.             public short wAttributes;
  858.             public SmallRect srWindow;
  859.             public Coord dwMaximumWindowSize;
  860.  
  861.             public ushort wPopupAttributes;
  862.             public bool bFullscreenSupported;
  863.  
  864.             [MarshalAs ( UnmanagedType.ByValArray, SizeConst = 16 )]
  865.             public ColorRef [ ] ColorTable;
  866.  
  867.             public static ConsoleScreenBufferInfoEx Create ( )
  868.             {
  869.                 return new ConsoleScreenBufferInfoEx { cbSize = 96 };
  870.             }
  871.         }
  872.  
  873.         [StructLayout ( LayoutKind.Sequential )]
  874.         public struct Rgb
  875.         {
  876.             private byte byRed, byGreen, byBlue, RESERVED;
  877.  
  878.             public Rgb ( Color colorIn )
  879.             {
  880.                 byRed = colorIn.R;
  881.                 byGreen = colorIn.G;
  882.                 byBlue = colorIn.B;
  883.                 RESERVED = 0;
  884.             }
  885.  
  886.             public Rgb ( byte R, byte G, byte B )
  887.             {
  888.                 byRed = R;
  889.                 byGreen = G;
  890.                 byBlue = B;
  891.                 RESERVED = 0;
  892.             }
  893.  
  894.             public static implicit operator Color ( Rgb rgb )
  895.             {
  896.                 return Color.FromArgb ( rgb.byRed, rgb.byGreen, rgb.byBlue );
  897.             }
  898.  
  899.             public int ToInt32 ( )
  900.             {
  901.                 byte [ ] rgbcolors = new byte [ 4 ];
  902.                 rgbcolors [ 0 ] = byRed;
  903.                 rgbcolors [ 1 ] = byGreen;
  904.                 rgbcolors [ 2 ] = byBlue;
  905.                 rgbcolors [ 3 ] = RESERVED;
  906.                 return BitConverter.ToInt32 ( rgbcolors, 0 );
  907.             }
  908.         }
  909.  
  910.         public enum ShowWindowCommands
  911.         {
  912.             /// <summary>
  913.             /// Hides the window and activates another window.
  914.             /// </summary>
  915.             Hide = 0,
  916.  
  917.             /// <summary>
  918.             /// Activates and displays a window. If the window is minimized or
  919.             /// maximized, the system restores it to its original size and position.
  920.             /// An application should specify this flag when displaying the window
  921.             /// for the first time.
  922.             /// </summary>
  923.             Normal = 1,
  924.  
  925.             /// <summary>
  926.             /// Activates the window and displays it as a minimized window.
  927.             /// </summary>
  928.             ShowMinimized = 2,
  929.  
  930.             /// <summary>
  931.             /// Maximizes the specified window.
  932.             /// </summary>
  933.             Maximize = 3, // is this the right value?
  934.  
  935.             /// <summary>
  936.             /// Activates the window and displays it as a maximized window.
  937.             /// </summary>
  938.             ShowMaximized = 3,
  939.  
  940.             /// <summary>
  941.             /// Displays a window in its most recent size and position. This value
  942.             /// is similar to <see cref="Win32.ShowWindowCommand.Normal"/>, except
  943.             /// the window is not activated.
  944.             /// </summary>
  945.             ShowNoActivate = 4,
  946.  
  947.             /// <summary>
  948.             /// Activates the window and displays it in its current size and position.
  949.             /// </summary>
  950.             Show = 5,
  951.  
  952.             /// <summary>
  953.             /// Minimizes the specified window and activates the next top-level
  954.             /// window in the Z order.
  955.             /// </summary>
  956.             Minimize = 6,
  957.  
  958.             /// <summary>
  959.             /// Displays the window as a minimized window. This value is similar to
  960.             /// <see cref="Win32.ShowWindowCommand.ShowMinimized"/>, except the
  961.             /// window is not activated.
  962.             /// </summary>
  963.             ShowMinNoActive = 7,
  964.  
  965.             /// <summary>
  966.             /// Displays the window in its current size and position. This value is
  967.             /// similar to <see cref="Win32.ShowWindowCommand.Show"/>, except the
  968.             /// window is not activated.
  969.             /// </summary>
  970.             ShowNA = 8,
  971.  
  972.             /// <summary>
  973.             /// Activates and displays the window. If the window is minimized or
  974.             /// maximized, the system restores it to its original size and position.
  975.             /// An application should specify this flag when restoring a minimized window.
  976.             /// </summary>
  977.             Restore = 9,
  978.  
  979.             /// <summary>
  980.             /// Sets the show state based on the SW_* value specified in the
  981.             /// STARTUPINFO structure passed to the CreateProcess function by the
  982.             /// program that started the application.
  983.             /// </summary>
  984.             ShowDefault = 10,
  985.  
  986.             /// <summary>
  987.             ///  <b>Windows 2000/XP:</b> Minimizes a window, even if the thread
  988.             /// that owns the window is not responding. This flag should only be
  989.             /// used when minimizing windows from a different thread.
  990.             /// </summary>
  991.             ForceMinimize = 11
  992.         }
  993.  
  994.         public static IntPtr ConsoleHandle { get; private set; }
  995.         public const int StdOutputHandle = -11;
  996.  
  997.         [DllImport ( "kernel32.dll", SetLastError = true )]
  998.         private static extern IntPtr GetStdHandle ( int nStdHandle );
  999.  
  1000.         public static void SetConsoleHandle ( )
  1001.         {
  1002.             ConsoleHandle = GetStdHandle ( StdOutputHandle );
  1003.         }
  1004.  
  1005.         [DllImport ( "kernel32.dll", SetLastError = true )]
  1006.         public static extern bool GetConsoleScreenBufferInfoEx (
  1007.            IntPtr hConsoleOutput,
  1008.            ref ConsoleScreenBufferInfoEx consoleScreenBufferInfo
  1009.            );
  1010.  
  1011.         [DllImport ( "kernel32.dll", SetLastError = true )]
  1012.         public static extern bool SetConsoleScreenBufferInfoEx (
  1013.            IntPtr consoleOutput,
  1014.            ref ConsoleScreenBufferInfoEx consoleScreenBufferInfoEx
  1015.            );
  1016.  
  1017.         [DllImport ( "kernel32.dll" )]
  1018.         public static extern bool SetConsoleTextAttribute ( IntPtr hConsoleOutput,
  1019.             ushort wAttributes );
  1020.  
  1021.         [Conditional ( "DEBUG" ), DllImport ( "Kernel32.dll" )]
  1022.         public static extern void AllocConsole ( );
  1023.  
  1024.         [Conditional ( "DEBUG" ), DllImport ( "Kernel32.dll" )]
  1025.         public static extern void FreeConsole ( );
  1026.  
  1027.         [DllImport ( "user32.dll" )] // import lockwindow to remove flashing
  1028.         public static extern bool LockWindowUpdate ( IntPtr hWndLock );
  1029.  
  1030.         [DllImport ( "user32.dll", CharSet = CharSet.Unicode )]
  1031.         public static extern int MessageBox ( IntPtr hWnd, string caption, string title, int flags );
  1032.  
  1033.         [DllImport ( "user32.dll" )]
  1034.         public static extern bool ShowWindowAsync ( IntPtr hWnd, ShowWindowCommands nCmdShow );
  1035.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement