コード例 #1
0
ファイル: HttpServer.cs プロジェクト: hjhong/ColorWanted
        public HttpServer(string host, int port)
        {
            Host = host;
            Port = port;

            Commands = new Dictionary <string, Func <string, string> >();

            RegisterCommand("Hi hyjican, tell me the color please. Thank you!", (data) =>
            {
                var pos   = Control.MousePosition;
                var color = ColorUtil.GetColor(pos);

                var hsl = new HSL(color.GetHue(), color.GetSaturation(), color.GetBrightness());
                var hsb = HSB.Parse(color);
                var hsi = HSI.Parse(color, Settings.Main.HsiAlgorithm);

                return(string.Format("It's my pleasure, the following lines shows the color at pixel ({0},{1}) in different formats.\n", pos.X, pos.Y) +
                       color.ToArgb() + "\n" +
                       string.Format("#{0:X2}{1:X2}{2:X2}\nRGB({0},{1},{2})\n", color.R, color.G, color.B) +
                       string.Format("HSL({0},{1},{2})\n", Math.Round(hsl.H), Util.Round(hsl.S * 100), Util.Round(hsl.L * 100)) +
                       string.Format("HSB({0},{1},{2})\n", Math.Round(hsb.H), Util.Round(hsb.S * 100), Util.Round(hsb.B * 100)) +
                       string.Format("HSI({0},{1},{2})", Math.Round(hsi.H), Util.Round(hsi.S * 100), Util.Round(hsi.I * 100)));
            });

            RegisterCommand("Hi hyjican, I would like to take a screenshot.", (data) =>
            {
                var rect = Util.GetCurrentScreen();
                var img  = ScreenShot.GetScreen(rect.X, rect.Y, rect.Width, rect.Height);
                using (var stream = new MemoryStream())
                {
                    img.Save(stream, ImageFormat.Jpeg);
                    return(string.Format("OK, the next line is the image data in encoding base64.\n{0}", Convert.ToBase64String(stream.ToArray())));
                }
            });
        }
コード例 #2
0
ファイル: MainForm.cs プロジェクト: vincent-deng/ColorWanted
        /// <summary>
        /// 更新当前获取的颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void colortimer_Tick(object sender, EventArgs e)
        {
            if (!stopDrawPreview && previewForm.MouseOnMe)
            {
                // 如果没有停止预览,并且鼠标在预览窗口上
                // 就不取色,这是为了防止因循环取色导致过高的资源占用
                return;
            }
            if (!settingLoaded)
            {
                // 不晓得为啥,在启动时加载Visible会被覆盖,所在放到这里来了
                SwitchDisplayMode(Settings.Main.Display);

                settingLoaded = true;
            }

            var color = ColorUtil.GetColor(MousePosition);

            // 如果光标位置不变,颜色也不变,就不绘制了
            if (MousePosition.Equals(lastPosition) && color.Equals(lastColor))
            {
                return;
            }

            lastPosition = MousePosition;
            lastColor    = color;

            colorBuffer.Clear();
            var val = colorBuffer.AppendFormat("{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B).ToString();

            lbHex.Tag = val;
            colorBuffer.Clear();

            lbHex.Text = colorBuffer.AppendFormat("#{0}", lbHex.Tag).ToString();
            colorBuffer.Clear();

            val       = colorBuffer.AppendFormat("{0},{1},{2}", color.R, color.G, color.B).ToString();
            lbRgb.Tag = val;
            colorBuffer.Clear();

            lbRgb.Text = colorBuffer.AppendFormat("RGB({0})", lbRgb.Tag).ToString();
            colorBuffer.Clear();

            if (trayMenuShowPreview.Checked && !stopDrawPreview)
            {
                DrawPreview(MousePosition);
            }

            if (FormatMode.Mini == currentFormatMode)
            {
                if (!lbColorPreview.Visible)
                {
                    lbColorPreview.Show();
                }
                lbColorPreview.BackColor = color;
                return;
            }

            if (lbColorPreview.Visible)
            {
                lbColorPreview.Hide();
            }

            var contrastColor = ColorUtil.GetContrastColor(color);

            if (FormatMode.Standard == currentFormatMode)
            {
                lbRgb.BackColor = color;
                lbRgb.ForeColor = contrastColor;
                return;
            }

            if (lbRgb.BackColor != BackColor)
            {
                lbRgb.BackColor = BackColor;
                lbRgb.ForeColor = ForeColor;
            }

            // var hsl = HSL.Parse(color);
            var hsl = new HSL(color.GetHue(), color.GetSaturation(), color.GetBrightness());

            lbHsl.Text = colorBuffer.AppendFormat("HSL({0},{1},{2})",
                                                  Math.Round(hsl.H),
                                                  Util.Round(hsl.S * 100),
                                                  Util.Round(hsl.L * 100)).ToString();
            colorBuffer.Clear();

            var hsb = HSB.Parse(color);

            lbHsb.Text = colorBuffer.AppendFormat("HSB({0},{1},{2})",
                                                  Math.Round(hsb.H),
                                                  Util.Round(hsb.S * 100),
                                                  Util.Round(hsb.B * 100)).ToString();
            colorBuffer.Clear();

            var hsi = HSI.Parse(color, currentHsiAlgorithm);

            lbHsi.Text = colorBuffer.AppendFormat("HSI({0},{1},{2})",
                                                  Math.Round(hsi.H),
                                                  Util.Round(hsi.S * 100),
                                                  Util.Round(hsi.I * 100)).ToString();
            colorBuffer.Clear();

            pnExt.BackColor = color;
            pnExt.ForeColor = contrastColor;
        }