Skip to content

Commit 481596c

Browse files
Finding colors by CMYK
1 parent ff03762 commit 481596c

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

PantoneColorPicker/Models/PantoneColor.cs

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,25 +99,53 @@ public static PantoneColor FindColor(string selection)
9999

100100
// If not found parse text as RGB input
101101
RGB rgb = null;
102-
try
102+
CMYK cmyk = null;
103+
var input = selection.Split(','); // with comma separator
104+
if(input.Count() >= 4)
105+
{
106+
try
107+
{
108+
// Create CYMK
109+
cmyk = new CMYK((byte)Int32.Parse(input[0]),
110+
(byte)(Int32.Parse(input[1])),
111+
(byte)(Int32.Parse(input[2])),
112+
(byte)(Int32.Parse(input[3])));
113+
}
114+
catch { }
115+
}
116+
else if (input.Count() == 3)
103117
{
104-
var input = selection.Split(','); // with comma separator
105-
if (input.Count() >= 3)
118+
try
106119
{
107120
// Create RGB
108121
rgb = new RGB((byte)(Int32.Parse(input[0])),
109122
(byte)(Int32.Parse(input[1])),
110-
(byte)(Int32.Parse(input[2])));
123+
(byte)(Int32.Parse(input[2])));
111124
}
125+
catch { }
112126
}
113-
catch
114-
{}
115127

116-
if(rgb == null)
128+
if(cmyk == null)
129+
{
130+
try
131+
{
132+
input = selection.Split(' '); // with space separator
133+
if (input.Count() >= 4)
134+
{
135+
// Create CYMK
136+
cmyk = new CMYK((byte)Int32.Parse(input[0]),
137+
(byte)(Int32.Parse(input[1])),
138+
(byte)(Int32.Parse(input[2])),
139+
(byte)(Int32.Parse(input[3])));
140+
}
141+
}
142+
catch { }
143+
}
144+
else if(rgb == null)
117145
try
118146
{
119-
var input = selection.Split(' '); // with space separator
120-
if (input.Count() >= 3)
147+
input = selection.Split(' '); // with space separator
148+
if (input.Count() == 3)
121149
{
122150
// Create RGB
123151
rgb = new RGB((byte)(Int32.Parse(input[0])),
@@ -128,7 +156,12 @@ public static PantoneColor FindColor(string selection)
128156
catch { }
129157

130158
// Try to pull the closest
131-
if (rgb != null)
159+
if(cmyk != null)
160+
{
161+
res = FindClosestColorByCMYK(cmyk);
162+
if (res != null) return res.DeepCopy();
163+
}
164+
else if (rgb != null)
132165
{
133166
res = FindClosestColorByRGB(rgb);
134167
if (res != null) return res.DeepCopy();
@@ -137,6 +170,7 @@ public static PantoneColor FindColor(string selection)
137170
// Nothing worked, return null
138171
return null;
139172
}
173+
140174
public static PantoneColor FindColor(double r, double g, double b)
141175
{
142176
// Tries to pull from catalog using reference name
@@ -178,6 +212,12 @@ private static PantoneColor FindClosestColorByRGB(double r, double g, double b)
178212
return res;
179213
}
180214

215+
private static PantoneColor FindClosestColorByCMYK(CMYK cmyk)
216+
{
217+
var res = PantoneCatalog.OrderBy(u => u.CMYKdistance(cmyk)).ToList().FirstOrDefault();
218+
return res;
219+
}
220+
181221
/// <summary>
182222
/// Returns the distance to another color in terms of RGB distance.
183223
/// </summary>
@@ -200,6 +240,16 @@ private double RGBdistance(double r, double g, double b)
200240
return Math.Sqrt((rr * rr) + (gg * gg) + (bb * bb));
201241
}
202242

243+
private double CMYKdistance(CMYK cmyk)
244+
{
245+
var c = ((double)(this.CMYK.C) - (double)(cmyk.C));
246+
var m = ((double)(this.CMYK.M) - (double)(cmyk.M));
247+
var y = ((double)(this.CMYK.Y) - (double)(cmyk.Y));
248+
var k = ((double)(this.CMYK.K) - (double)(cmyk.K));
249+
250+
return Math.Sqrt((c * c) + (m * m) + (y * y) + (k * k));
251+
}
252+
203253
#endregion
204254

205255
#region Static Loading Catalog

0 commit comments

Comments
 (0)