1 | using System;
|
---|
2 | using System.Windows.Media;
|
---|
3 | using System.Diagnostics.CodeAnalysis;
|
---|
4 |
|
---|
5 | namespace Microsoft.Research.DynamicDataDisplay
|
---|
6 | {
|
---|
7 | public static class ColorHelper
|
---|
8 | {
|
---|
9 | private readonly static Random random = new Random();
|
---|
10 |
|
---|
11 | /// <summary>
|
---|
12 | /// Creates color from HSB color space with random hue and saturation and brighness equal to 1.
|
---|
13 | /// </summary>
|
---|
14 | /// <returns></returns>
|
---|
15 | public static Color CreateColorWithRandomHue()
|
---|
16 | {
|
---|
17 | double hue = random.NextDouble() * 360;
|
---|
18 | HsbColor hsbColor = new HsbColor(hue, 1, 1);
|
---|
19 | return hsbColor.ToArgbColor();
|
---|
20 | }
|
---|
21 |
|
---|
22 | public static Color[] CreateRandomColors(int colorNum)
|
---|
23 | {
|
---|
24 | double startHue = random.NextDouble() * 360;
|
---|
25 |
|
---|
26 | Color[] res = new Color[colorNum];
|
---|
27 | double hueStep = 360.0 / colorNum;
|
---|
28 | for (int i = 0; i < res.Length; i++)
|
---|
29 | {
|
---|
30 | double hue = startHue + i * hueStep;
|
---|
31 | res[i] = new HsbColor(hue, 1, 1).ToArgbColor();
|
---|
32 | }
|
---|
33 |
|
---|
34 | return res;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /// <summary>
|
---|
38 | /// Creates color with fully random hue and slightly random saturation and brightness.
|
---|
39 | /// </summary>
|
---|
40 | /// <returns></returns>
|
---|
41 | public static Color CreateRandomHsbColor()
|
---|
42 | {
|
---|
43 | double h = random.NextDouble() * 360;
|
---|
44 | double s = random.NextDouble() * 0.5 + 0.5;
|
---|
45 | double b = random.NextDouble() * 0.25 + 0.75;
|
---|
46 | return new HsbColor(h, s, b).ToArgbColor();
|
---|
47 | }
|
---|
48 |
|
---|
49 | /// <summary>
|
---|
50 | /// Creates color with random hue.
|
---|
51 | /// </summary>
|
---|
52 | /// <param name="saturation">The saturation, [0..1].</param>
|
---|
53 | /// <param name="brightness">The brightness, [0..1]</param>
|
---|
54 | /// <returns></returns>
|
---|
55 | public static Color CreateColorWithRandomHue(double saturation, double brightness)
|
---|
56 | {
|
---|
57 | double h = random.NextDouble() * 360;
|
---|
58 |
|
---|
59 | return new HsbColor(h, saturation, brightness).ToArgbColor();
|
---|
60 | }
|
---|
61 |
|
---|
62 | /// <summary>
|
---|
63 | /// Creates brush with random hue.
|
---|
64 | /// </summary>
|
---|
65 | /// <param name="saturation">The saturation, [0..1].</param>
|
---|
66 | /// <param name="brightness">The brightness, [0..1].</param>
|
---|
67 | /// <returns></returns>
|
---|
68 | public static Brush CreateBrushWithRandomHue(double saturation, double brightness)
|
---|
69 | {
|
---|
70 | Color color = CreateColorWithRandomHue(saturation, brightness);
|
---|
71 |
|
---|
72 | return new SolidColorBrush(color);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /// <summary>
|
---|
76 | /// Gets the random color (this property is created to use it from Xaml).
|
---|
77 | /// </summary>
|
---|
78 | /// <value>The random color.</value>
|
---|
79 | public static Color RandomColor
|
---|
80 | {
|
---|
81 | get { return CreateRandomHsbColor(); }
|
---|
82 | }
|
---|
83 |
|
---|
84 | /// <summary>
|
---|
85 | /// Gets the random brush.
|
---|
86 | /// </summary>
|
---|
87 | /// <value>The random brush.</value>
|
---|
88 | public static SolidColorBrush RandomBrush
|
---|
89 | {
|
---|
90 | get
|
---|
91 | {
|
---|
92 | Color color = CreateColorWithRandomHue();
|
---|
93 | SolidColorBrush brush = new SolidColorBrush(color);
|
---|
94 | return brush;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | public static int ToArgb(this Color color)
|
---|
99 | {
|
---|
100 | int result =
|
---|
101 | color.A << 24 |
|
---|
102 | color.R << 16 |
|
---|
103 | color.G << 8 |
|
---|
104 | color.B;
|
---|
105 |
|
---|
106 | return result;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|