Skip to content

Commit 75d338d

Browse files
daveheitzmanhalostatue
authored andcommitted
Putting lab stuff into its own area, and correcting references to lab objects to reflect
1 parent 988806d commit 75d338d

File tree

4 files changed

+25
-29
lines changed

4 files changed

+25
-29
lines changed

lib/color.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ def normalize_word(value)
138138
alias_method :normalize_16bit, :normalize_word
139139
end
140140

141-
require "color/rgb"
142141
require "color/cmyk"
142+
require "color/css"
143143
require "color/grayscale"
144144
require "color/hsl"
145-
require "color/yiq"
146-
require "color/css"
147145
require "color/lab"
146+
require "color/rgb"
147+
require "color/yiq"

lib/color/rgb.rb

+20-22
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,11 @@ def to_lab(color_space = :sRGB, reference_white = [95.047, 100.00, 108.883])
227227
end
228228
}
229229

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+
})
236235
end
237236

238237
# Mix the RGB hue with White so that the RGB hue is the specified
@@ -324,28 +323,25 @@ def adjust_hue(percent)
324323
# provided colours. Returns +nil+ if +color_list+ is empty or if there is
325324
# no colour within the +threshold_distance+.
326325
#
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 = {})
334329
color_list = [color_list].flatten(1)
335330
return nil if color_list.empty?
336331

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
345340
best_match = nil
346341

347342
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)
349345
if distance < closest_distance
350346
closest_distance = distance
351347
best_match = c
@@ -694,3 +690,5 @@ def html_hexify(hex)
694690
end
695691

696692
require "color/rgb/colors"
693+
require "color/rgb/metallic"
694+
require "color/rgb/contrast"

lib/color/rgb/contrast.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# -*- ruby encoding: utf-8 -*-
1+
# frozen_string_literal: true
22

33
class Color::RGB
44
# Outputs how much contrast this color has with another RGB color.
@@ -22,8 +22,6 @@ def contrast(other, algorithm = nil)
2222
end
2323
end
2424

25-
private
26-
2725
# Provides the luminosity difference between two rbg vals
2826
def diff_luminosity(other)
2927
other = coerce(other)

test/test_rgb.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def test_inspect
414414
assert_equal("RGB [#ffffff]", Color::RGB::White.inspect)
415415
end
416416

417-
def test_delta_e2000_lab
417+
def test_delta_e2000
418418
# test data:
419419
# http://www.ece.rochester.edu/~gsharma/ciede2000/
420420
# http://www.ece.rochester.edu/~gsharma/ciede2000/dataNprograms/CIEDE2000.xls

0 commit comments

Comments
 (0)