Initial Commit
This commit is contained in:
66
Common/Palettes/DecoratorPaletteBase.cs
Normal file
66
Common/Palettes/DecoratorPaletteBase.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay.Common.Palettes
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a base class for decorating palette, which wraps another palette and intercepts calls to it.
|
||||
/// </summary>
|
||||
public abstract class DecoratorPaletteBase : PaletteBase
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DecoratorPaletteBase"/> class.
|
||||
/// </summary>
|
||||
protected DecoratorPaletteBase() { }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DecoratorPaletteBase"/> class.
|
||||
/// </summary>
|
||||
/// <param name="palette">The palette.</param>
|
||||
public DecoratorPaletteBase(IPalette palette)
|
||||
{
|
||||
Palette = palette;
|
||||
}
|
||||
|
||||
private IPalette palette = null;
|
||||
/// <summary>
|
||||
/// Gets or sets the palette being decorated.
|
||||
/// </summary>
|
||||
/// <value>The palette.</value>
|
||||
public IPalette Palette
|
||||
{
|
||||
get { return palette; }
|
||||
set
|
||||
{
|
||||
if (value == null)
|
||||
throw new ArgumentNullException("value");
|
||||
|
||||
if (palette != null)
|
||||
palette.Changed -= OnChildPaletteChanged;
|
||||
|
||||
palette = value;
|
||||
palette.Changed += OnChildPaletteChanged;
|
||||
|
||||
RaiseChanged();
|
||||
}
|
||||
}
|
||||
|
||||
void OnChildPaletteChanged(object sender, EventArgs e)
|
||||
{
|
||||
RaiseChanged();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the color by interpolation coefficient.
|
||||
/// </summary>
|
||||
/// <param name="t">Interpolation coefficient, should belong to [0..1].</param>
|
||||
/// <returns>Color.</returns>
|
||||
public override Color GetColor(double t)
|
||||
{
|
||||
return palette.GetColor(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
26
Common/Palettes/DelegatePalette.cs
Normal file
26
Common/Palettes/DelegatePalette.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay.Common.Palettes
|
||||
{
|
||||
public sealed class DelegatePalette : PaletteBase
|
||||
{
|
||||
public DelegatePalette(Func<double, Color> func)
|
||||
{
|
||||
if (func == null)
|
||||
throw new ArgumentNullException("func");
|
||||
|
||||
this.func = func;
|
||||
}
|
||||
|
||||
private readonly Func<double, Color> func;
|
||||
|
||||
public override Color GetColor(double t)
|
||||
{
|
||||
return func(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
61
Common/Palettes/HsbPalette.cs
Normal file
61
Common/Palettes/HsbPalette.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
|
||||
using System.ComponentModel;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay.Common.Palettes
|
||||
{
|
||||
public sealed class HSBPalette : IPalette
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HSBPalette"/> class.
|
||||
/// </summary>
|
||||
public HSBPalette() { }
|
||||
|
||||
private double start = 0;
|
||||
[DefaultValue(0.0)]
|
||||
public double Start
|
||||
{
|
||||
get { return start; }
|
||||
set
|
||||
{
|
||||
if (start != value)
|
||||
{
|
||||
start = value;
|
||||
Changed.Raise(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private double width = 360;
|
||||
[DefaultValue(360.0)]
|
||||
public double Width
|
||||
{
|
||||
get { return width; }
|
||||
set
|
||||
{
|
||||
if (width != value)
|
||||
{
|
||||
width = value;
|
||||
Changed.Raise(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region IPalette Members
|
||||
|
||||
public Color GetColor(double t)
|
||||
{
|
||||
Verify.IsTrue(0 <= t && t <= 1);
|
||||
|
||||
return new HsbColor(start + t * width, 1, 1).ToArgbColor();
|
||||
}
|
||||
|
||||
public event EventHandler Changed;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
26
Common/Palettes/IPalette.cs
Normal file
26
Common/Palettes/IPalette.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay.Common.Palettes
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a color palette, which can generate color by interpolation coefficient.
|
||||
/// </summary>
|
||||
public interface IPalette
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the color by interpolation coefficient.
|
||||
/// </summary>
|
||||
/// <param name="t">Interpolation coefficient, should belong to [0..1].</param>
|
||||
/// <returns>Color.</returns>
|
||||
Color GetColor(double t);
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when palette changes.
|
||||
/// </summary>
|
||||
event EventHandler Changed;
|
||||
}
|
||||
}
|
||||
99
Common/Palettes/LinearPalette.cs
Normal file
99
Common/Palettes/LinearPalette.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay.Common.Palettes
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a color step with its offset in limits [0..1].
|
||||
/// </summary>
|
||||
[DebuggerDisplay("Color={Color}, Offset={Offset}")]
|
||||
public class LinearPaletteColorStep
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LinearPaletteColorStep"/> class.
|
||||
/// </summary>
|
||||
public LinearPaletteColorStep() { }
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LinearPaletteColorStep"/> class.
|
||||
/// </summary>
|
||||
/// <param name="color">The color.</param>
|
||||
/// <param name="offset">The offset.</param>
|
||||
public LinearPaletteColorStep(Color color, double offset)
|
||||
{
|
||||
this.Color = color;
|
||||
this.Offset = offset;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the color.
|
||||
/// </summary>
|
||||
/// <value>The color.</value>
|
||||
public Color Color { get; set; }
|
||||
/// <summary>
|
||||
/// Gets or sets the offset.
|
||||
/// </summary>
|
||||
/// <value>The offset.</value>
|
||||
public double Offset { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a palette with start and stop colors and intermediate colors with their custom offsets.
|
||||
/// </summary>
|
||||
public class LinearPalette : IPalette
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LinearPalette"/> class.
|
||||
/// </summary>
|
||||
/// <param name="startColor">The start color.</param>
|
||||
/// <param name="endColor">The end color.</param>
|
||||
/// <param name="steps">The steps.</param>
|
||||
public LinearPalette(Color startColor, Color endColor, params LinearPaletteColorStep[] steps)
|
||||
{
|
||||
this.steps.Add(new LinearPaletteColorStep(startColor, 0));
|
||||
if (steps != null)
|
||||
this.steps.AddMany(steps);
|
||||
this.steps.Add(new LinearPaletteColorStep(endColor, 1));
|
||||
}
|
||||
|
||||
private readonly List<LinearPaletteColorStep> steps = new List<LinearPaletteColorStep>();
|
||||
|
||||
#region IPalette Members
|
||||
|
||||
/// <summary>
|
||||
/// Gets the color by interpolation coefficient.
|
||||
/// </summary>
|
||||
/// <param name="t">Interpolation coefficient, should belong to [0..1].</param>
|
||||
/// <returns>Color.</returns>
|
||||
public Color GetColor(double t)
|
||||
{
|
||||
if (t < 0) return steps[0].Color;
|
||||
if (t > 1) return steps[steps.Count - 1].Color;
|
||||
|
||||
int i = 0;
|
||||
double x = 0;
|
||||
while (x <= t)
|
||||
{
|
||||
x = steps[i + 1].Offset;
|
||||
i++;
|
||||
}
|
||||
|
||||
Color c0 = steps[i - 1].Color;
|
||||
Color c1 = steps[i].Color;
|
||||
double ratio = (t - steps[i - 1].Offset) / (steps[i].Offset - steps[i - 1].Offset);
|
||||
|
||||
Color result = Color.FromRgb(
|
||||
(byte)((1 - ratio) * c0.R + ratio * c1.R),
|
||||
(byte)((1 - ratio) * c0.G + ratio * c1.G),
|
||||
(byte)((1 - ratio) * c0.B + ratio * c1.B));
|
||||
return result;
|
||||
}
|
||||
#pragma warning disable 0067
|
||||
public event EventHandler Changed;
|
||||
#pragma warning restore 0067
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
30
Common/Palettes/LinearPalettes.cs
Normal file
30
Common/Palettes/LinearPalettes.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay.Common.Palettes
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains some predefined linear palettes.
|
||||
/// </summary>
|
||||
public static class LinearPalettes
|
||||
{
|
||||
private const double geoHeight = 8845 + 400;
|
||||
private static LinearPalette geoHeightsPalette = new LinearPalette(
|
||||
Color.FromRgb(1, 99, 69), Colors.White,
|
||||
new LinearPaletteColorStep(Color.FromRgb(28, 128, 52), (50 + 400) / geoHeight),
|
||||
new LinearPaletteColorStep(Color.FromRgb(229, 209, 119), (200 + 400) / geoHeight),
|
||||
new LinearPaletteColorStep(Color.FromRgb(160, 66, 1), (1000 + 400) / geoHeight),
|
||||
new LinearPaletteColorStep(Color.FromRgb(129, 32, 32), (2000 + 400) / geoHeight),
|
||||
new LinearPaletteColorStep(Color.FromRgb(119, 119, 119), (4000 + 400) / geoHeight),
|
||||
new LinearPaletteColorStep(Color.FromRgb(244, 244, 244), (6000 + 400) / geoHeight));
|
||||
|
||||
/// <summary>
|
||||
/// Gets the palette for geo height map visualization.
|
||||
/// </summary>
|
||||
/// <value>The geo heights palette.</value>
|
||||
public static LinearPalette GeoHeightsPalette { get { return geoHeightsPalette; } }
|
||||
}
|
||||
}
|
||||
33
Common/Palettes/MinMaxLoggingPalette.cs
Normal file
33
Common/Palettes/MinMaxLoggingPalette.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay.Common.Palettes
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a palette that calculates minimal and maximal values of interpolation coefficient and every 100000 calls writes these values
|
||||
/// to DEBUG console.
|
||||
/// </summary>
|
||||
public class MinMaxLoggingPalete : DecoratorPaletteBase
|
||||
{
|
||||
double min = Double.MaxValue;
|
||||
double max = Double.MinValue;
|
||||
int counter = 0;
|
||||
public override Color GetColor(double t)
|
||||
{
|
||||
if (t < min) min = t;
|
||||
if (t > max) max = t;
|
||||
counter++;
|
||||
|
||||
if (counter % 100000 == 0)
|
||||
{
|
||||
Debug.WriteLine("Palette: Min = " + min + ", max = " + max);
|
||||
}
|
||||
|
||||
return base.GetColor(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Common/Palettes/PaletteBase.cs
Normal file
35
Common/Palettes/PaletteBase.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay.Common.Palettes
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a simple base class for a palette. Contains an abstract merhod for creation of color and method to raise changed event.
|
||||
/// </summary>
|
||||
public abstract class PaletteBase : IPalette
|
||||
{
|
||||
#region IPalette Members
|
||||
|
||||
/// <summary>
|
||||
/// Gets the color by interpolation coefficient.
|
||||
/// </summary>
|
||||
/// <param name="t">Interpolation coefficient, should belong to [0..1].</param>
|
||||
/// <returns>Color.</returns>
|
||||
public abstract Color GetColor(double t);
|
||||
|
||||
protected void RaiseChanged()
|
||||
{
|
||||
Changed.Raise(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Occurs when palette changes.
|
||||
/// </summary>
|
||||
public event EventHandler Changed;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
24
Common/Palettes/TransparentLimitsPalette.cs
Normal file
24
Common/Palettes/TransparentLimitsPalette.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay.Common.Palettes
|
||||
{
|
||||
public class TransparentLimitsPalette : DecoratorPaletteBase
|
||||
{
|
||||
public TransparentLimitsPalette()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public TransparentLimitsPalette(IPalette palette) : base(palette) { }
|
||||
|
||||
public override Color GetColor(double t)
|
||||
{
|
||||
if (t < 0 || t > 1) return Colors.Transparent;
|
||||
return Palette.GetColor(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
133
Common/Palettes/UniformLinearPalette.cs
Normal file
133
Common/Palettes/UniformLinearPalette.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
using System.ComponentModel;
|
||||
using System.Windows.Markup;
|
||||
using Microsoft.Research.DynamicDataDisplay.Common.Auxiliary;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay.Common.Palettes
|
||||
{
|
||||
public class UniformPaletteColorStep
|
||||
{
|
||||
public Color Color { get; set; }
|
||||
}
|
||||
|
||||
[ContentProperty("ColorSteps_XAML")]
|
||||
public sealed class UniformLinearPalette : IPalette, ISupportInitialize
|
||||
{
|
||||
private double[] points;
|
||||
|
||||
private ObservableCollection<Color> colors = new ObservableCollection<Color>();
|
||||
public ObservableCollection<Color> ColorSteps
|
||||
{
|
||||
get { return colors; }
|
||||
}
|
||||
|
||||
private List<UniformPaletteColorStep> colorSteps_XAML = new List<UniformPaletteColorStep>();
|
||||
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public List<UniformPaletteColorStep> ColorSteps_XAML
|
||||
{
|
||||
get { return colorSteps_XAML; }
|
||||
}
|
||||
|
||||
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
private void RaiseChanged()
|
||||
{
|
||||
Changed.Raise(this);
|
||||
}
|
||||
public event EventHandler Changed;
|
||||
|
||||
public UniformLinearPalette() { }
|
||||
|
||||
public UniformLinearPalette(params Color[] colors)
|
||||
{
|
||||
if (colors == null) throw new ArgumentNullException("colors");
|
||||
if (colors.Length < 2) throw new ArgumentException(Strings.Exceptions.PaletteTooFewColors);
|
||||
|
||||
this.colors = new ObservableCollection<Color>(colors);
|
||||
FillPoints(colors.Length);
|
||||
}
|
||||
|
||||
private void FillPoints(int size)
|
||||
{
|
||||
points = new double[size];
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
points[i] = i / (double)(size - 1);
|
||||
}
|
||||
}
|
||||
|
||||
private bool increaseBrightness = true;
|
||||
[DefaultValue(true)]
|
||||
public bool IncreaseBrightness
|
||||
{
|
||||
get { return increaseBrightness; }
|
||||
set { increaseBrightness = value; }
|
||||
}
|
||||
|
||||
public Color GetColor(double t)
|
||||
{
|
||||
Verify.AssertIsFinite(t);
|
||||
Verify.IsTrue(0 <= t && t <= 1);
|
||||
|
||||
if (t <= 0)
|
||||
return colors[0];
|
||||
else if (t >= 1)
|
||||
return colors[colors.Count - 1];
|
||||
else
|
||||
{
|
||||
int i = 0;
|
||||
while (points[i] < t)
|
||||
i++;
|
||||
|
||||
double ratio = (points[i] - t) / (points[i] - points[i - 1]);
|
||||
|
||||
Verify.IsTrue(0 <= ratio && ratio <= 1);
|
||||
|
||||
Color c0 = colors[i - 1];
|
||||
Color c1 = colors[i];
|
||||
Color res = Color.FromRgb(
|
||||
(byte)(c0.R * ratio + c1.R * (1 - ratio)),
|
||||
(byte)(c0.G * ratio + c1.G * (1 - ratio)),
|
||||
(byte)(c0.B * ratio + c1.B * (1 - ratio)));
|
||||
|
||||
// Increasing saturation and brightness
|
||||
if (increaseBrightness)
|
||||
{
|
||||
HsbColor hsb = res.ToHsbColor();
|
||||
//hsb.Saturation = 0.5 * (1 + hsb.Saturation);
|
||||
hsb.Brightness = 0.5 * (1 + hsb.Brightness);
|
||||
return hsb.ToArgbColor();
|
||||
}
|
||||
else
|
||||
{
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#region ISupportInitialize Members
|
||||
|
||||
bool beganInit = false;
|
||||
public void BeginInit()
|
||||
{
|
||||
beganInit = true;
|
||||
}
|
||||
|
||||
public void EndInit()
|
||||
{
|
||||
if (beganInit)
|
||||
{
|
||||
colors = new ObservableCollection<Color>(colorSteps_XAML.Select(step => step.Color));
|
||||
FillPoints(colors.Count);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
45
Common/Palettes/UniformLinearPalettes.cs
Normal file
45
Common/Palettes/UniformLinearPalettes.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Microsoft.Research.DynamicDataDisplay.Common.Palettes
|
||||
{
|
||||
public static class UniformLinearPalettes
|
||||
{
|
||||
static UniformLinearPalettes()
|
||||
{
|
||||
blackAndWhitePalette.IncreaseBrightness = false;
|
||||
rgbPalette.IncreaseBrightness = false;
|
||||
blueOrangePalette.IncreaseBrightness = false;
|
||||
}
|
||||
|
||||
private static readonly UniformLinearPalette blackAndWhitePalette =
|
||||
new UniformLinearPalette(Colors.Black, Colors.White);
|
||||
|
||||
public static UniformLinearPalette BlackAndWhitePalette
|
||||
{
|
||||
get { return blackAndWhitePalette; }
|
||||
}
|
||||
|
||||
private static readonly UniformLinearPalette rgbPalette =
|
||||
new UniformLinearPalette(Colors.Blue, Color.FromRgb(0, 255, 0), Colors.Red);
|
||||
|
||||
public static UniformLinearPalette RedGreenBluePalette
|
||||
{
|
||||
get { return rgbPalette; }
|
||||
}
|
||||
|
||||
private static readonly UniformLinearPalette blueOrangePalette = new UniformLinearPalette(
|
||||
Colors.Blue,
|
||||
Colors.Cyan,
|
||||
Colors.Yellow,
|
||||
Colors.Orange);
|
||||
|
||||
public static UniformLinearPalette BlueOrangePalette
|
||||
{
|
||||
get { return blueOrangePalette; }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user