コード例 #1
0
        public HsbaColor(string hexColor)
        {
            HsbaColor hsba = Utility.RgbaToHsba(new RgbaColor(hexColor));

            H = hsba.H;
            S = hsba.S;
            B = hsba.B;
            A = hsba.A;
        }
コード例 #2
0
        public HsbaColor(Brush brush)
        {
            HsbaColor hsba = Utility.RgbaToHsba(new RgbaColor(brush));

            H = hsba.H;
            S = hsba.S;
            B = hsba.B;
            A = hsba.A;
        }
コード例 #3
0
        public HsbaColor(int r, int g, int b, int a = 255)
        {
            HsbaColor hsba = Utility.RgbaToHsba(new RgbaColor(r, g, b, a));

            H = hsba.H;
            S = hsba.S;
            B = hsba.B;
            A = hsba.A;
        }
コード例 #4
0
        /// <summary> Hsba转Rgba </summary>
        internal static RgbaColor HsbaToRgba(HsbaColor hsba)
        {
            double r = 0, g = 0, b = 0;
            int    i = (int)((hsba.H / 60) % 6);
            double f = (hsba.H / 60) - i;
            double p = hsba.B * (1 - hsba.S);
            double q = hsba.B * (1 - f * hsba.S);
            double t = hsba.B * (1 - (1 - f) * hsba.S);

            switch (i)
            {
            case 0:
                r = hsba.B;
                g = t;
                b = p;
                break;

            case 1:
                r = q;
                g = hsba.B;
                b = p;
                break;

            case 2:
                r = p;
                g = hsba.B;
                b = t;
                break;

            case 3:
                r = p;
                g = q;
                b = hsba.B;
                break;

            case 4:
                r = t;
                g = p;
                b = hsba.B;
                break;

            case 5:
                r = hsba.B;
                g = p;
                b = q;
                break;

            default:
                break;
            }
            return(new RgbaColor((int)(255.0 * r), (int)(255.0 * g), (int)(255.0 * b), (int)(255.0 * hsba.A)));
        }
コード例 #5
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;

                Background = CurrentColor.SolidColorBrush;
            }
            else
            {
                //Background = CurrentColor.OpaqueSolidColorBrush;

                Background = CurrentColor.OpaqueSolidColorBrush;
            }

            ColorChange?.Invoke(this, null);
        }
コード例 #6
0
        private void Initialize(HsbaColor hsbaColor)
        {
            // 设置当前初始颜色
            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 (CurrentColor.Color != ((SolidColorBrush)this.Background).Color)
                {
                    ApplyColor(new HsbaColor(this.Background));

                    //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();
                                          }
            };
        }