-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEnumHasFlag.cs
60 lines (47 loc) · 1.41 KB
/
EnumHasFlag.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
class Example
{
[Flags]
enum Color
{
None = 1,
Blue = 2,
Green = 4,
Yellow = 8,
Purple = 16,
Red = 32,
Orange = 64,
Pink = 128,
Cyan = 256,
Magenta = 512
}
private static readonly Color[] colors = Enum.GetValues<Color>();
static void Main()
{
var randomColors = Color.Red | Color.Magenta | Color.Cyan | Color.Blue | Color.Orange | Color.Purple;
var (primaryColors, secondaryColors, otherColors) = Filter(randomColors);
Console.WriteLine();
}
static (Color[] primary, Color[] secondary, Color[] other) Filter(Color color)
{
var primaryList = new Color[3];
var secondaryList = new Color[3];
var mixList = new Color[3];
int i = 0, j = 0, k = 0;
var primaryColors = Color.Yellow | Color.Red | Color.Blue;
var secondaryColors = Color.Purple | Color.Orange | Color.Green;
foreach (var item in colors)
{
if (color.HasFlag(item))
{
if (primaryColors.HasFlag(item))
primaryList[i++] = item;
else if (secondaryColors.HasFlag(item))
secondaryList[j++] = item;
else
mixList[k++] = item;
}
}
return (primaryList, secondaryList, mixList);
}
}