Ejemplo n.º 1
0
        private void Initialize(HsbaColor hsbaColor)
        {
            // 绑定主题
            Utility.Refresh(this);

            // 设置当前初始颜色
            currentColor = hsbaColor;
            // defaultColor = currentColor;

            // 界面初始化
            viewDefColor.Background = defaultColor.SolidColorBrush;
            viewLine1.Offset        = 1.0 / 6.0 * 0.0;
            viewLine2.Offset        = 1.0 / 6.0 * 1.0;
            viewLine3.Offset        = 1.0 / 6.0 * 2.0;
            viewLine4.Offset        = 1.0 / 6.0 * 3.0;
            viewLine5.Offset        = 1.0 / 6.0 * 4.0;
            viewLine6.Offset        = 1.0 / 6.0 * 5.0;
            viewLine7.Offset        = 1.0 / 6.0 * 6.0;

            // 按钮事件
            bool start = true;

            button.Click += delegate
            {
                polygon.Margin = new Thickness(ActualWidth / 2 - 5, -5.0, 0.0, 0.0);
                popup.IsOpen   = true;
                if (start)
                {
                    ApplyColor(currentColor);
                    start = false;
                }
            };
            popup.Closed += delegate { if (ColorPickerClosed != null)
                                       {
                                           ColorPickerClosed();
                                       }
            };
            viewDefColor.Click += delegate { ApplyColor(new HsbaColor(viewDefColor.Background)); };
            hex.ButtonClick    += delegate { Clipboard.SetText(hex.Text); };

            // 视图被改变事件
            thumbSB.ValueChange += delegate { if (thumbSB.IsDragging)
                                              {
                                                  ViewChange();
                                              }
            };
            thumbH.ValueChange += delegate { if (thumbH.IsDragging)
                                             {
                                                 ViewChange();
                                             }
            };
            thumbA.ValueChange += delegate { if (thumbA.IsDragging)
                                             {
                                                 ViewChange();
                                             }
            };

            // RGBA操作事件
            rgbaR.TextChanged += delegate { if (rgbaR.IsSelectionActive)
                                            {
                                                RgbaChange();
                                            }
            };
            rgbaG.TextChanged += delegate { if (rgbaG.IsSelectionActive)
                                            {
                                                RgbaChange();
                                            }
            };
            rgbaB.TextChanged += delegate { if (rgbaB.IsSelectionActive)
                                            {
                                                RgbaChange();
                                            }
            };
            rgbaA.TextChanged += delegate { if (rgbaA.IsSelectionActive)
                                            {
                                                RgbaChange();
                                            }
            };

            // HSBA操作事件
            hsbaH.TextChanged += delegate { if (hsbaH.IsSelectionActive)
                                            {
                                                HsbaChange();
                                            }
            };
            hsbaS.TextChanged += delegate { if (hsbaS.IsSelectionActive)
                                            {
                                                HsbaChange();
                                            }
            };
            hsbaB.TextChanged += delegate { if (hsbaB.IsSelectionActive)
                                            {
                                                HsbaChange();
                                            }
            };
            hsbaA.TextChanged += delegate { if (hsbaA.IsSelectionActive)
                                            {
                                                HsbaChange();
                                            }
            };

            // HEX操作事件
            hex.TextChanged += delegate { if (hex.IsSelectionActive)
                                          {
                                              HexChange();
                                          }
            };
        }
Ejemplo n.º 2
0
        private void ApplyColor(HsbaColor hsba)
        {
            currentColor     = hsba;
            currentRgbaColor = currentColor.RgbaColor;

            if (!rgbaR.IsSelectionActive)
            {
                rgbaR.Text = currentRgbaColor.R.ToString();
            }
            if (!rgbaG.IsSelectionActive)
            {
                rgbaG.Text = currentRgbaColor.G.ToString();
            }
            if (!rgbaB.IsSelectionActive)
            {
                rgbaB.Text = currentRgbaColor.B.ToString();
            }
            if (!rgbaA.IsSelectionActive)
            {
                rgbaA.Text = currentRgbaColor.A.ToString();
            }

            if (!hsbaH.IsSelectionActive)
            {
                hsbaH.Text = ((int)(currentColor.H / 3.6)).ToString();
            }
            if (!hsbaS.IsSelectionActive)
            {
                hsbaS.Text = ((int)(currentColor.S * 100)).ToString();
            }
            if (!hsbaB.IsSelectionActive)
            {
                hsbaB.Text = ((int)(currentColor.B * 100)).ToString();
            }
            if (!hsbaA.IsSelectionActive)
            {
                hsbaA.Text = ((int)(currentColor.A * 100)).ToString();
            }

            if (!hex.IsSelectionActive)
            {
                if (canTransparent)
                {
                    hex.Text = currentColor.HexString;
                }
                else
                {
                    hex.Text = string.Format("#{0:X2}{1:X2}{2:X2}", currentRgbaColor.R, currentRgbaColor.G, currentRgbaColor.B);
                }
            }

            if (!thumbH.IsDragging)
            {
                thumbH.YPercent = currentColor.H / 360.0;
            }
            if (!thumbSB.IsDragging)
            {
                thumbSB.XPercent = currentColor.S; thumbSB.YPercent = 1 - currentColor.B;
            }
            if (!thumbA.IsDragging)
            {
                thumbA.YPercent = Math.Abs(1 - currentColor.A);
            }

            selectColor.H        = currentColor.H;
            selectColor.A        = currentColor.A;
            viewSelectColor.Fill = selectColor.OpaqueSolidColorBrush;
            if (canTransparent)
            {
                viewSelectColor.Opacity = viewSelectColor1.Opacity = viewSelectColor2.Opacity = 1 - thumbA.YPercent;
            }
            viewAlpha.Color = selectColor.OpaqueColor;
            if (canTransparent)
            {
                Background = currentColor.SolidColorBrush;
            }
            else
            {
                Background = currentColor.OpaqueSolidColorBrush;
            }

            ColorChange.Invoke(this, null);
        }