private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var map = d as ColorMap; map?.ColorChanged?.Invoke(map, new ColorChangedEventArgs((Color)e.NewValue)); if (map == null || map._settingColor) { return; } var col = (Color)e.NewValue; var hsl = ColorHelper.ToHSL(col); map._lightnessMid.Color = ColorHelper.FromHSL(new Vector4(hsl.X, 1, 0.5f, 1)); double angle = Math.PI * 2 * hsl.X; Vector2 normalized = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)); Vector2 halfSize = new Vector2( (float)map.ellipse.ActualWidth / 2, (float)map.ellipse.ActualHeight / 2); Vector2 pos = ((hsl.Y / 2) * normalized * halfSize * new Vector2(1, -1)) + halfSize; map._colorX = pos.X; map._colorY = pos.Y; map.UpdateThumb(); }
private Color CalcWheelColor(float x, float y, float lightness) { x = x - 0.5f; y = (1 - y) - 0.5f; float saturation = 2 * (float)Math.Sqrt((x * x) + (y * y)); float hue = y < 0 ? (float)Math.Atan2(-y, -x) + (float)Math.PI : (float)Math.Atan2(y, x); if (saturation > 1) { saturation = 1; } return(ColorHelper.FromHSL(new Vector4(hue / ((float)Math.PI * 2), saturation, lightness, 1))); }