using System; using System.Windows.Media; using System.Diagnostics.CodeAnalysis; namespace Microsoft.Research.DynamicDataDisplay { public static class ColorHelper { private readonly static Random random = new Random(); /// /// Creates color from HSB color space with random hue and saturation and brighness equal to 1. /// /// public static Color CreateColorWithRandomHue() { double hue = random.NextDouble() * 360; HsbColor hsbColor = new HsbColor(hue, 1, 1); return hsbColor.ToArgbColor(); } public static Color[] CreateRandomColors(int colorNum) { double startHue = random.NextDouble() * 360; Color[] res = new Color[colorNum]; double hueStep = 360.0 / colorNum; for (int i = 0; i < res.Length; i++) { double hue = startHue + i * hueStep; res[i] = new HsbColor(hue, 1, 1).ToArgbColor(); } return res; } /// /// Creates color with fully random hue and slightly random saturation and brightness. /// /// public static Color CreateRandomHsbColor() { double h = random.NextDouble() * 360; double s = random.NextDouble() * 0.5 + 0.5; double b = random.NextDouble() * 0.25 + 0.75; return new HsbColor(h, s, b).ToArgbColor(); } /// /// Creates color with random hue. /// /// The saturation, [0..1]. /// The brightness, [0..1] /// public static Color CreateColorWithRandomHue(double saturation, double brightness) { double h = random.NextDouble() * 360; return new HsbColor(h, saturation, brightness).ToArgbColor(); } /// /// Creates brush with random hue. /// /// The saturation, [0..1]. /// The brightness, [0..1]. /// public static Brush CreateBrushWithRandomHue(double saturation, double brightness) { Color color = CreateColorWithRandomHue(saturation, brightness); return new SolidColorBrush(color); } /// /// Gets the random color (this property is created to use it from Xaml). /// /// The random color. public static Color RandomColor { get { return CreateRandomHsbColor(); } } /// /// Gets the random brush. /// /// The random brush. public static SolidColorBrush RandomBrush { get { return new SolidColorBrush(CreateRandomHsbColor()); } } public static int ToArgb(this Color color) { int result = color.A << 24 | color.R << 16 | color.G << 8 | color.B; return result; } } }