Class: ColorConverters::NameConverter

Inherits:
BaseConverter show all
Defined in:
lib/color_converters/converters/name_converter.rb

Constant Summary

Constants inherited from BaseConverter

BaseConverter::IMPORT_DP, BaseConverter::OUTPUT_DP

Instance Attribute Summary

Attributes inherited from BaseConverter

#original_value, #rgba

Class Method Summary collapse

Methods inherited from BaseConverter

#alpha, #cielab, #cielch, #cmyk, factory, #hex, #hsb, #hsl, #hsv, inherited, #initialize, #name, #oklab, #oklch, #rgb, #xyz

Constructor Details

This class inherits a constructor from ColorConverters::BaseConverter

Class Method Details

.add_colour_distances_to_collection(collection_colours, source_colour) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/color_converters/converters/name_converter.rb', line 50

def self.add_colour_distances_to_collection(collection_colours, source_colour)
  collection_colours.map do |swatch|
    swatch[:distance] = self.distance_between_colours(ColorConverters::Color.new(swatch.dig(:hex)), source_colour)
  end

  collection_colours
end

.distance_between_colours(comparison_colour, source_colour) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/color_converters/converters/name_converter.rb', line 58

def self.distance_between_colours(comparison_colour, source_colour)
  # https://en.wikipedia.org/wiki/Euclidean_distance#Higher_dimensions
  # https://www.baeldung.com/cs/compute-similarity-of-colours
  # TODO: allow the type of matching to be set via config. Use HSL for now as it's far faster than CIELab
  conversion_1 = comparison_colour.hsl
  conversion_2 = source_colour.hsl

  keys = conversion_1.keys
  Math.sqrt((conversion_1[keys[0]] - conversion_2[keys[0]])**2 + (conversion_1[keys[1]] - conversion_2[keys[1]])**2 + (conversion_1[keys[2]] - conversion_2[keys[2]])**2)
end

.match_name_from_palettes(colour_name) ⇒ Object

this is a checking for a direct naming match against the ColorSwatchCollection



46
47
48
# File 'lib/color_converters/converters/name_converter.rb', line 46

def self.match_name_from_palettes(colour_name)
  ::ColorSwatchCollection.get_from_name(colour_name).dig(:hex)
end

.matches?(colour_input) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
# File 'lib/color_converters/converters/name_converter.rb', line 7

def self.matches?(colour_input)
  return false unless colour_input.is_a?(String)

  !colour_input.include?('#') && !colour_input.include?('rgb') && !colour_input.include?('hsl')
end

.rgb_to_name(rgb_array, fuzzy = false) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/color_converters/converters/name_converter.rb', line 13

def self.rgb_to_name(rgb_array, fuzzy = false)
  if fuzzy
    source_colour = ColorConverters::Color.new({ r: rgb_array[0], g: rgb_array[1], b: rgb_array[2] })

    collection_colours = []

    ::ColorSwatchCollection.list_collections.each do |collection_name|
      collection_colours += self.add_colour_distances_to_collection(Object.const_get("::ColorSwatchCollection::#{collection_name.capitalize}").colours, source_colour)
    end

    collection_colours.min_by { |swatch| swatch[:distance] }.dig(:name)
  else
    ::ColorSwatchCollection.get_from_hex(HexConverter.rgb_to_hex(rgb_array)).dig(:name)
  end
end