public HsbaColor(string hexColor) { HsbaColor hsba = Utility.RgbaToHsba(new RgbaColor(hexColor)); H = hsba.H; S = hsba.S; B = hsba.B; A = hsba.A; }
public HsbaColor(Brush brush) { HsbaColor hsba = Utility.RgbaToHsba(new RgbaColor(brush)); H = hsba.H; S = hsba.S; B = hsba.B; A = hsba.A; }
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; }
/// <summary> /// Hsba转Rgba /// </summary> /// <param name="hsba"></param> /// <returns></returns> 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))); }
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(); } }; }
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); }