Skip to content

Commit fb2ded0

Browse files
committed
Style fixes
1 parent 83d683b commit fb2ded0

File tree

5 files changed

+158
-96
lines changed

5 files changed

+158
-96
lines changed

Data/KMeans.cs

+31-21
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,20 @@ public static class KMeans
3131
public static string[] Cluster<T>(T[] data)
3232
where T : IColor, new()
3333
{
34-
Clusters = new int[data.Length];
35-
RandomiseClusters(data.Length);
34+
KMeans.Clusters = new int[data.Length];
35+
KMeans.RandomiseClusters(data.Length);
3636

3737
// average values of each cluster
3838
T[] means = new T[K];
3939
for (int i = 0; i < K; i++)
40+
{
4041
means[i] = new T();
42+
}
4143

42-
CentroidIds = new int[K];
44+
KMeans.CentroidIds = new int[K];
4345

44-
UpdateMeans(data, ref means);
45-
UpdateCentroids(data, means);
46+
KMeans.UpdateMeans(data, ref means);
47+
KMeans.UpdateCentroids(data, means);
4648

4749
// Console.WriteLine(String.Join(", ", Means));
4850
// Console.WriteLine(String.Join(", ", CentroidIds));
@@ -51,16 +53,18 @@ public static string[] Cluster<T>(T[] data)
5153
int iterations = 0;
5254
while (changed && iterations < MaxIterations)
5355
{
54-
changed = AssignClusters(data);
55-
UpdateMeans(data, ref means);
56-
UpdateCentroids(data, means);
56+
changed = KMeans.AssignClusters(data);
57+
KMeans.UpdateMeans(data, ref means);
58+
KMeans.UpdateCentroids(data, means);
5759

5860
iterations++;
5961
}
6062

6163
string[] centroids = new string[K];
6264
for (int i = 0; i < K; i++)
63-
centroids[i] = data[CentroidIds[i]].ToString();
65+
{
66+
centroids[i] = data[KMeans.CentroidIds[i]].ToString();
67+
}
6468

6569
return centroids;
6670
}
@@ -70,8 +74,8 @@ private static void RandomiseClusters(int n)
7074
{
7175
Random rnd = new Random();
7276

73-
Parallel.For(0, Clusters.Length, i => {
74-
Clusters[i] = rnd.Next(K);
77+
Parallel.For(0, KMeans.Clusters.Length, i => {
78+
KMeans.Clusters[i] = rnd.Next(K);
7579
});
7680
}
7781

@@ -82,18 +86,22 @@ private static void UpdateMeans<T>(T[] data, ref T[] means)
8286
where T : IColor
8387
{
8488
for (int i = 0; i < means.Length; i++)
89+
{
8590
means[i].Zero();
91+
}
8692

87-
int[] counts = new int[Clusters.Length];
88-
for (int i = 0; i < Clusters.Length; i++)
93+
int[] counts = new int[KMeans.Clusters.Length];
94+
for (int i = 0; i < KMeans.Clusters.Length; i++)
8995
{
90-
int cluster = Clusters[i];
96+
int cluster = KMeans.Clusters[i];
9197
counts[cluster]++;
9298
means[cluster].AddBy(data[i]);
9399
}
94100

95101
for (int i = 0; i < means.Length; i++)
102+
{
96103
means[i].DivideBy(counts[i]);
104+
}
97105
}
98106

99107
// for each cluster, find the data point that is
@@ -106,13 +114,13 @@ private static void UpdateCentroids<T>(T[] data, T[] means)
106114
Array.Fill(closestDistances, double.MaxValue);
107115

108116
Parallel.For(0, data.Length, i => {
109-
int cluster = Clusters[i];
117+
int cluster = KMeans.Clusters[i];
110118
double d = data[i].DistanceTo(means[cluster]);
111119

112120
if (d < closestDistances[cluster])
113121
{
114122
closestDistances[cluster] = d;
115-
CentroidIds[cluster] = i;
123+
KMeans.CentroidIds[cluster] = i;
116124
}
117125
});
118126
}
@@ -126,16 +134,18 @@ private static bool AssignClusters<T>(T[] data)
126134
bool changed = false;
127135

128136
Parallel.For(0, data.Length, i => {
129-
int closestIndex = Clusters[i];
137+
int closestIndex = KMeans.Clusters[i];
130138
double closestDistance = data[i]
131-
.DistanceTo(data[CentroidIds[closestIndex]]);
139+
.DistanceTo(data[KMeans.CentroidIds[closestIndex]]);
132140

133-
for (int j = 0; j < CentroidIds.Length; j++)
141+
for (int j = 0; j < KMeans.CentroidIds.Length; j++)
134142
{
135143
if (j == Clusters[i])
144+
{
136145
continue;
146+
}
137147

138-
double d = data[i].DistanceTo(data[CentroidIds[j]]);
148+
double d = data[i].DistanceTo(data[KMeans.CentroidIds[j]]);
139149

140150
// reassign if there is a closer centroid
141151
if (d < closestDistance)
@@ -146,7 +156,7 @@ private static bool AssignClusters<T>(T[] data)
146156
}
147157
}
148158

149-
Clusters[i] = closestIndex;
159+
KMeans.Clusters[i] = closestIndex;
150160
});
151161

152162
return changed;

Data/models/CMYK.cs

+25-15
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ namespace Palette
55
{
66
public struct CMYK : IColor
77
{
8-
public CMYK() =>
9-
Values = new double[]{ 0.0, 0.0, 0.0, 0.0 };
8+
public CMYK()
9+
{
10+
this.Values = new double[] { 0.0, 0.0, 0.0, 0.0 };
11+
}
1012

1113
public CMYK(byte r, byte g, byte b)
1214
{
1315
// edge case for black
1416
if (r == 0 && g == 0 && b == 0)
1517
{
16-
Values = new double[]{ 0.0, 0.0, 0.0, 1.0 };
18+
this.Values = new double[] { 0.0, 0.0, 0.0, 1.0 };
1719
return;
1820
}
1921

2022
// find reciprocals of RGB values
21-
double[] reciprocals = new double[]{ r / 255.0,
23+
double[] reciprocals = new double[] { r / 255.0,
2224
g / 255.0, b / 255.0 };
2325

2426
double k = 1 - reciprocals.Max();
@@ -27,31 +29,39 @@ public CMYK(byte r, byte g, byte b)
2729
double m = (1 - reciprocals[1] - k) / (1 - k);
2830
double y = (1 - reciprocals[2] - k) / (1 - k);
2931

30-
Values = new double[]{ c, m, y, k };
32+
this.Values = new double[]{ c, m, y, k };
3133
}
3234

3335
public double[] Values { get; set; }
3436

35-
public void Zero() =>
36-
Values = Values.Select(v => 0.0)
37+
public void Zero()
38+
{
39+
this.Values = this.Values.Select(v => 0.0)
3740
.ToArray();
41+
}
3842

39-
public void AddBy(IColor other) =>
40-
Values = Values.Zip(other.Values, (v0, v1) => v0 + v1)
43+
public void AddBy(IColor other)
44+
{
45+
this.Values = this.Values.Zip(other.Values, (v0, v1) => v0 + v1)
4146
.ToArray();
47+
}
4248

43-
public void DivideBy(int n) =>
44-
Values = Values.Select(v => v / n)
49+
public void DivideBy(int n)
50+
{
51+
this.Values = this.Values.Select(v => v / n)
4552
.ToArray();
53+
}
4654

47-
public double DistanceTo(IColor other) =>
48-
Values.Zip(other.Values, (v0, v1) => Math.Pow(v0 - v1, 2))
55+
public double DistanceTo(IColor other)
56+
{
57+
return this.Values.Zip(other.Values, (v0, v1) => Math.Pow(v0 - v1, 2))
4958
.Aggregate(0.0, (acc, v2) => acc + v2);
59+
}
5060

5161
public override string ToString()
5262
{
53-
double k = Values[3];
54-
return '#' + String.Join("", Values.Take(3).Select(v =>
63+
double k = this.Values[3];
64+
return '#' + String.Join("", this.Values.Take(3).Select(v =>
5565
Convert.ToInt32(255 * (1 - v) * (1 - k)).ToString("X").PadLeft(2, '0')));
5666
}
5767
}

Data/models/HSL.cs

+36-22
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ namespace Palette
55
{
66
public struct HSL : IColor
77
{
8-
public HSL() =>
9-
Values = new double[]{ 0.0, 0.0, 0.0 };
8+
public HSL()
9+
{
10+
this.Values = new double[] { 0.0, 0.0, 0.0 };
11+
}
1012

1113
public HSL(byte r, byte g, byte b)
1214
{
1315
// find reciprocals of RGB values
14-
double[] reciprocals = new double[]{ r / 255.0,
16+
double[] reciprocals = new double[] { r / 255.0,
1517
g / 255.0, b / 255.0 };
1618

1719
// edge case when all RGB values are the same
1820
if (reciprocals[0] == reciprocals[1] &&
1921
reciprocals[1] == reciprocals[2])
2022
{
21-
Values = new double[]{ 0.0, 0.0, reciprocals[0] };
23+
this.Values = new double[] { 0.0, 0.0, reciprocals[0] };
2224
return;
2325
}
2426

@@ -28,10 +30,14 @@ public HSL(byte r, byte g, byte b)
2830
for (int i = 1; i < 3; i++)
2931
{
3032
if (reciprocals[i] > reciprocals[cMaxIndex])
33+
{
3134
cMaxIndex = i;
35+
}
3236

3337
if (reciprocals[i] < reciprocals[cMinIndex])
38+
{
3439
cMinIndex = i;
40+
}
3541
}
3642

3743
double d = reciprocals[cMaxIndex] - reciprocals[cMinIndex];
@@ -61,58 +67,66 @@ public HSL(byte r, byte g, byte b)
6167
double l = (reciprocals[cMaxIndex] + reciprocals[cMinIndex]) * 0.5;
6268
double s = d / (1 - Math.Abs(2 * l - 1));
6369

64-
Values = new double[]{ h, s, l };
70+
this.Values = new double[]{ h, s, l };
6571
}
6672

6773
public double[] Values { get; set; }
6874

69-
public void Zero() =>
70-
Values = Values.Select(v => 0.0)
75+
public void Zero()
76+
{
77+
this.Values = this.Values.Select(v => 0.0)
7178
.ToArray();
79+
}
7280

73-
public void AddBy(IColor other) =>
74-
Values = Values.Zip(other.Values, (v0, v1) => v0 + v1)
81+
public void AddBy(IColor other)
82+
{
83+
this.Values = this.Values.Zip(other.Values, (v0, v1) => v0 + v1)
7584
.ToArray();
85+
}
7686

77-
public void DivideBy(int n) =>
78-
Values = Values.Select(v => v / n)
87+
public void DivideBy(int n)
88+
{
89+
this.Values = this.Values.Select(v => v / n)
7990
.ToArray();
91+
}
8092

81-
public double DistanceTo(IColor other) =>
82-
Values.Zip(other.Values, (v0, v1) => Math.Pow(v0 - v1, 2))
93+
public double DistanceTo(IColor other)
94+
{
95+
return this.Values.Zip(other.Values, (v0, v1) => Math.Pow(v0 - v1, 2))
8396
.Aggregate(0.0, (acc, v2) => acc + v2);
97+
}
8498

8599
public override string ToString()
86100
{
87-
double c = (1 - Math.Abs(2 * Values[2] - 1)) * Values[1];
88-
double x = (1 - Math.Abs((Values[0] / 60) % 2 - 1)) * c;
89-
double m = Values[2] - c / 2;
101+
double c = (1 - Math.Abs(2 * this.Values[2] - 1)) * this.Values[1];
102+
double x = (1 - Math.Abs((this.Values[0] / 60) % 2 - 1)) * c;
103+
double m = this.Values[2] - c / 2;
90104

91105
double[] reciprocals;
92-
switch (Values[0])
106+
switch (this.Values[0])
93107
{
94108
case >= 0 and < 60:
95-
reciprocals = new double[]{ c, x, 0.0 };
109+
reciprocals = new double[] { c, x, 0.0 };
96110
break;
97111

98112
case >= 60 and < 120:
99113
reciprocals = new double[]{ x, c, 0.0 };
100114
break;
101115

102116
case >= 120 and < 180:
103-
reciprocals = new double[]{ 0.0, c, x };
117+
reciprocals = new double[] { 0.0, c, x };
104118
break;
105119

106120
case >= 180 and < 240:
107-
reciprocals = new double[]{ 0.0, x, c };
121+
reciprocals = new double[] { 0.0, x, c };
108122
break;
109123

110124
case >= 240 and < 300:
111-
reciprocals = new double[]{ x, 0.0, c };
125+
reciprocals = new double[] { x, 0.0, c };
112126
break;
113127

114128
case >= 300 and < 360:
115-
reciprocals = new double[]{ c, 0.0, x };
129+
reciprocals = new double[] { c, 0.0, x };
116130
break;
117131

118132
default:

0 commit comments

Comments
 (0)