Module: ColorSwatchCollection

Defined in:
lib/color_swatch_collection/tailwind/tailwind_v4.rb,
lib/color_swatch_collection.rb,
lib/color_swatch_collection/ntc.rb,
lib/color_swatch_collection/x11.rb,
lib/color_swatch_collection/html.rb,
lib/color_swatch_collection/basic.rb,
lib/color_swatch_collection/pantone.rb,
lib/color_swatch_collection/roygbiv.rb,
lib/color_swatch_collection/version.rb,
lib/color_swatch_collection/configuration.rb,
lib/color_swatch_collection/tailwind/tailwind_v1.rb,
lib/color_swatch_collection/tailwind/tailwind_v2.rb,
lib/color_swatch_collection/tailwind/tailwind_v3.rb

Overview

converted from the OKLCh values that Tailwind uses for the colours, using ColorConverters which has been reliable for these types of conversions

Defined Under Namespace

Classes: Basic, Configuration, Error, Html, Ntc, Pantone, Roygbiv, TailwindV1, TailwindV2, TailwindV3, TailwindV4, X11

Constant Summary collapse

VERSION =
'0.1.2'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



21
22
23
# File 'lib/color_swatch_collection/configuration.rb', line 21

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.check_collection_use(pick, omit, collection_name) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/color_swatch_collection.rb', line 75

def self.check_collection_use(pick, omit, collection_name)
  pick = ColorSwatchCollection.configuration.default_collection_picks if pick.empty?
  omit = ColorSwatchCollection.configuration.default_collection_omits if omit.empty?

  pick = [] if pick == ['[]']
  omit = [] if omit == ['[]']

  (pick.empty? || pick.include?(collection_name)) && !omit.include?(collection_name)
end

.configure {|configuration| ... } ⇒ Object

Yields:



25
26
27
# File 'lib/color_swatch_collection/configuration.rb', line 25

def configure
  yield(configuration)
end

.get_colours(pick: [], omit: []) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/color_swatch_collection.rb', line 62

def self.get_colours(pick: [], omit: [])
  self.list_collections.collect do |collection_name|
    next unless self.check_collection_use(pick, omit, collection_name)

    Object.const_get("ColorSwatchCollection::#{collection_name.classify}").colours.collect do |swatch_hash|
      swatch_hash[:collection] = collection_name
      swatch_hash
    end
  end.compact.flatten
end

.get_from_hex(colour_hex, pick: [], omit: []) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/color_swatch_collection.rb', line 28

def self.get_from_hex(colour_hex, pick: [], omit: [])
  found_colour = nil

  self.list_collections.each do |collection_name|
    next unless self.check_collection_use(pick, omit, collection_name)

    found_colour = self.get_item_from_collection(colour_hex, 'hex', collection_name)

    if found_colour.present?
      found_colour[:collection] = collection_name
      break
    end
  end

  found_colour || {}
end

.get_from_name(colour_name, pick: [], omit: []) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/color_swatch_collection.rb', line 45

def self.get_from_name(colour_name, pick: [], omit: [])
  found_colour = nil

  self.list_collections.each do |collection_name|
    next unless self.check_collection_use(pick, omit, collection_name)

    found_colour = self.get_item_from_collection(colour_name, 'name', collection_name)

    if found_colour.present?
      found_colour[:collection] = collection_name
      break
    end
  end

  found_colour || {}
end

.get_item_from_collection(colour_input, input_type, collection_name) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/color_swatch_collection.rb', line 85

def self.get_item_from_collection(colour_input, input_type, collection_name)
  return nil if colour_input.blank?

  swatch_hash = nil
  list_collection_object = Object.const_get("ColorSwatchCollection::#{collection_name.classify}")

  if input_type == 'hex'
    swatch_hash = list_collection_object.colours.find { |swatch| swatch[:hex] == colour_input.upcase }
  elsif input_type == 'name'
    if ['ntc', 'pantone'].include?(collection_name)
      swatch_hash = list_collection_object.colours.find { |swatch| swatch[:name] == colour_input.downcase.strip }
    else
      swatch_hash = list_collection_object.colours.find { |swatch| swatch[:name] == colour_input.downcase.gsub(' ', '') }
    end
  end

  swatch_hash || {}
end

.list_collectionsObject



24
25
26
# File 'lib/color_swatch_collection.rb', line 24

def self.list_collections
  ['basic', 'html', 'ntc', 'pantone', 'roygbiv', 'x11', 'tailwind_v1', 'tailwind_v2', 'tailwind_v3', 'tailwind_v4']
end

.reset_configuration!Object



29
30
31
# File 'lib/color_swatch_collection/configuration.rb', line 29

def reset_configuration!
  @configuration = Configuration.new
end