@@ -227,12 +227,11 @@ def to_lab(color_space = :sRGB, reference_white = [95.047, 100.00, 108.883])
227
227
end
228
228
}
229
229
230
- Color ::LAB . new ( ( 116 * fy ) - 16 , 500 * ( fx - fy ) , 200 * ( fy - fz ) )
231
- # {
232
- # :L => ((116 * fy) - 16),
233
- # :a => (500 * (fx - fy)),
234
- # :b => (200 * (fy - fz))
235
- # }
230
+ Color ::LAB . new ( {
231
+ L : ( ( 116 * fy ) - 16 ) ,
232
+ a : ( 500 * ( fx - fy ) ) ,
233
+ b : ( 200 * ( fy - fz ) )
234
+ } )
236
235
end
237
236
238
237
# Mix the RGB hue with White so that the RGB hue is the specified
@@ -324,28 +323,25 @@ def adjust_hue(percent)
324
323
# provided colours. Returns +nil+ if +color_list+ is empty or if there is
325
324
# no colour within the +threshold_distance+.
326
325
#
327
- # +threshold_distance+ is used to determine the minimum colour distance
328
- # permitted. Uses the CIE Delta E 1994 algorithm (CIE94) to find near
329
- # matches based on perceived visual colour. The default value (1000.0) is
330
- # an arbitrarily large number. The values <tt>:jnd</tt> and
331
- # <tt>:just_noticeable</tt> may be passed as the +threshold_distance+ to
332
- # use the value <tt>2.3</tt>.
333
- def closest_match ( color_list , threshold_distance = 1000.0 )
326
+ # threshhold_distance removed to instead allow choice of algorithms used to calculate the contrast
327
+ # between each color.
328
+ def closest_match ( color_list , algorithm = :delta_e94 , options = { } )
334
329
color_list = [ color_list ] . flatten ( 1 )
335
330
return nil if color_list . empty?
336
331
337
- threshold_distance = case threshold_distance
338
- when :jnd , :just_noticeable
339
- 2.3
340
- else
341
- threshold_distance . to_f
342
- end
343
- lab = to_lab
344
- closest_distance = threshold_distance
332
+ # threshold_distance = case threshold_distance
333
+ # when :jnd, :just_noticeable
334
+ # 2.3
335
+ # else
336
+ # threshold_distance.to_f
337
+ # end
338
+ # lab = to_lab
339
+ closest_distance = 999_999.9
345
340
best_match = nil
346
341
347
342
color_list . each do |c |
348
- distance = Color ::LAB . delta_e94 ( lab , c . to_lab )
343
+ # distance = Color::LAB.delta_e94(lab, c.to_lab)
344
+ distance = contrast ( c , algorithm ) # delta_e94(lab, c.to_lab)
349
345
if distance < closest_distance
350
346
closest_distance = distance
351
347
best_match = c
@@ -694,3 +690,5 @@ def html_hexify(hex)
694
690
end
695
691
696
692
require "color/rgb/colors"
693
+ require "color/rgb/metallic"
694
+ require "color/rgb/contrast"
0 commit comments