@@ -99,25 +99,53 @@ public static PantoneColor FindColor(string selection)
99
99
100
100
// If not found parse text as RGB input
101
101
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 )
103
117
{
104
- var input = selection . Split ( ',' ) ; // with comma separator
105
- if ( input . Count ( ) >= 3 )
118
+ try
106
119
{
107
120
// Create RGB
108
121
rgb = new RGB ( ( byte ) ( Int32 . Parse ( input [ 0 ] ) ) ,
109
122
( byte ) ( Int32 . Parse ( input [ 1 ] ) ) ,
110
- ( byte ) ( Int32 . Parse ( input [ 2 ] ) ) ) ;
123
+ ( byte ) ( Int32 . Parse ( input [ 2 ] ) ) ) ;
111
124
}
125
+ catch { }
112
126
}
113
- catch
114
- { }
115
127
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 )
117
145
try
118
146
{
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 )
121
149
{
122
150
// Create RGB
123
151
rgb = new RGB ( ( byte ) ( Int32 . Parse ( input [ 0 ] ) ) ,
@@ -128,7 +156,12 @@ public static PantoneColor FindColor(string selection)
128
156
catch { }
129
157
130
158
// 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 )
132
165
{
133
166
res = FindClosestColorByRGB ( rgb ) ;
134
167
if ( res != null ) return res . DeepCopy ( ) ;
@@ -137,6 +170,7 @@ public static PantoneColor FindColor(string selection)
137
170
// Nothing worked, return null
138
171
return null ;
139
172
}
173
+
140
174
public static PantoneColor FindColor ( double r , double g , double b )
141
175
{
142
176
// Tries to pull from catalog using reference name
@@ -178,6 +212,12 @@ private static PantoneColor FindClosestColorByRGB(double r, double g, double b)
178
212
return res ;
179
213
}
180
214
215
+ private static PantoneColor FindClosestColorByCMYK ( CMYK cmyk )
216
+ {
217
+ var res = PantoneCatalog . OrderBy ( u => u . CMYKdistance ( cmyk ) ) . ToList ( ) . FirstOrDefault ( ) ;
218
+ return res ;
219
+ }
220
+
181
221
/// <summary>
182
222
/// Returns the distance to another color in terms of RGB distance.
183
223
/// </summary>
@@ -200,6 +240,16 @@ private double RGBdistance(double r, double g, double b)
200
240
return Math . Sqrt ( ( rr * rr ) + ( gg * gg ) + ( bb * bb ) ) ;
201
241
}
202
242
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
+
203
253
#endregion
204
254
205
255
#region Static Loading Catalog
0 commit comments