using SciColorMaps.Portable; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Txgy.Controls { /// /// ColorMapBar.xaml 的交互逻辑 /// public partial class ColorMapBar : UserControl { public static readonly DependencyProperty UcWidthProperty = DependencyProperty.Register("UcWidth", typeof(double), typeof(ColorMapBar), new PropertyMetadata(40.0)); public double UcWidth { get { return (double)GetValue(UcWidthProperty); } set { SetValue(UcWidthProperty, value); } } public static readonly DependencyProperty UcHeightProperty = DependencyProperty.Register("UcHeight", typeof(double), typeof(ColorMapBar), new PropertyMetadata(200.0)); public double UcHeight { get { return (double)GetValue(UcHeightProperty); } set { SetValue(UcHeightProperty, value); } } public static readonly DependencyProperty TopDepthProperty = DependencyProperty.Register("TopDepthProperty", typeof(double), typeof(ColorMapBar), new PropertyMetadata(200.0)); public double TopDepth { get { return (double)GetValue(TopDepthProperty); } set { SetValue(TopDepthProperty, value); } } public static readonly DependencyProperty BottomDepthProperty = DependencyProperty.Register("BottomDepthProperty", typeof(double), typeof(ColorMapBar), new PropertyMetadata(900.0)); public double BottomDepth { get { return (double)GetValue(BottomDepthProperty); } set { SetValue(BottomDepthProperty, value); } } public static readonly DependencyProperty ColorPaletteProperty = DependencyProperty.Register("ColorPaletteProperty", typeof(string), typeof(ColorMapBar), new PropertyMetadata("plasma")); public string ColorPalette { get { return (string)GetValue(ColorPaletteProperty); } set { SetValue(ColorPaletteProperty, value); UpdatePanels(); } } public static readonly DependencyProperty PaletteBrushProperty = DependencyProperty.Register("PaletteBrushProperty", typeof(LinearGradientBrush), typeof(ColorMapBar), new PropertyMetadata(null)); public LinearGradientBrush PaletteBrush { get { return (LinearGradientBrush)GetValue(PaletteBrushProperty); } set { SetValue(PaletteBrushProperty, value); //UpdatePanels(); } } public ColorMap colorMap; private const int ColorCountDefault = 256; public static List Palettes { get; } = ColorMap.Palettes.ToList(); //public LinearGradientBrush PaletteBrush { get; set; } private string _selectedPalette = Palettes.FirstOrDefault(); public string SelectedPalette { get { return _selectedPalette; } set { _selectedPalette = value; UpdatePanels(); } } public Color[] colors; public ColorMapBar() { InitializeComponent(); UpdatePanels(); } private void UpdatePanels() { //var colors = new[] //{ // new byte[] {0, 0, 0}, // new byte[] {255, 255, 0}, // new byte[] {255, 0, 0}, // new byte[] {255, 255, 255} //}; //var positions = new[] { 0, 0.3f, 0.7f, 1 }; //var cmap1 = ColorMap.CreateFromColors(colors, positions, colorCount: ColorCountDefault); colorMap = new ColorMap(ColorPalette, colorCount: ColorCountDefault); //var cmap3 = new MirrorColorMap(cmap2); //colors = CreateColors(colorMap); PaletteBrush = CreatePaletteBrush(colorMap); } private Color[] CreateColors(ColorMap cmap) { return cmap.Colors().Select((color)=>color.ToMediaColor()).ToArray(); } private LinearGradientBrush CreatePaletteBrush(ColorMap cmap) { return new LinearGradientBrush { StartPoint = new System.Windows.Point(0, 0), EndPoint = new System.Windows.Point(0, 1), GradientStops = new GradientStopCollection( cmap.Colors() .Select((color, i) => new GradientStop() { Color = color.ToMediaColor(), Offset = (float)(ColorCountDefault-i) / ColorCountDefault }) .ToList()) }; } } public static class ColorUtils { public static Color ToMediaColor(this byte[] rgb) { return Color.FromRgb(rgb[0], rgb[1], rgb[2]); } } }