color_namer_ruby 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb8445c3407dff549f27e518515249fbe4dc5c3a5de0a7b388bc14968d8118f9
4
- data.tar.gz: 4f684c4a7fc9e8f01f301c538692252c893de978d0fafca53ac95604357c7759
3
+ metadata.gz: 99e55c151479cf0f2bdccedd02fe6e5632bf4cc2becc0c6bb35df9396429f53e
4
+ data.tar.gz: c1ed71d2712b45c0ec8174c8049995aceb5faeaf3a8552f108676e03c3b43156
5
5
  SHA512:
6
- metadata.gz: 3cfc740a65645606ce24fabf1b0fb583ee445953f5cbe64f7cdd1b4e08b0fc51321bb4d7a6156f64e65d90d260cb2a2ef10b89242a3340feab27b063d719baea
7
- data.tar.gz: c4cf7454c3a41dda0011cd2bf953a1a78503e115a79c507a9b9a91bb80a48620c1436e1724072111e050e9ad367508e5bfedd6793bbd106f63e4a36c7b74e062
6
+ metadata.gz: 598275d62f010ac8d7914644d284c7eee421509b8e3f38b98542adc0521037301ae092b87b986f11faac30cece71f2322ffcc9244d1ea29aa242061e1738e329
7
+ data.tar.gz: 29a288b098fa9de4f119b7a4b55a97bee14bf4a4cb47f7a18df9344ed801ed8df5859aa5b41f715edaad3fc59c54a394c1b5cc857c3bec97003fd73ce93bfd5c
data/README.md CHANGED
@@ -37,60 +37,110 @@ gem install color_name_ruby
37
37
 
38
38
  ## Usage
39
39
 
40
+ ## Methods
41
+
42
+ Possible methods:
43
+
44
+ ```ruby
45
+ ColorNamerRuby.list_collections
46
+ ColorNamerRuby.get_name
47
+ ColorNamerRuby.get_names
48
+ ColorNamerRuby.get_all_names
49
+ ColorNamerRuby.get_colour
50
+ ColorNamerRuby.get_colours
51
+ ColorNamerRuby.get_all_colours
52
+ ```
53
+
54
+ Get a list of the colour collections that can be checked against.
55
+
56
+ ```ruby
57
+ ColorNamerRuby.list_collections
58
+ => ['basic', 'html', 'ntc', 'pantone', 'roygbiv', 'x11']
59
+ ```
60
+
40
61
  Get a single name for a colour using `.get_name` by passing a colour in a way that [ColorConversion]((https://github.com/devrieda/color_conversion)) accepts.
41
62
  When passing colours with their properties assigned to keys, you need to pass nil or an empty string as the first parameter.
42
63
 
43
64
  ```ruby
44
- ColorNamerRuby::Namer.get_name('#3672b4')
45
- => 'Azure'
65
+ ColorNamerRuby.get_name('#3672b4')
66
+ => ['azure']
46
67
 
47
- ColorNamerRuby::Namer.get_name('', r: 70, g: 130, b: 180, a: 0.5)
48
- => 'steelblue'
68
+ ColorNamerRuby.get_name('', r: 70, g: 130, b: 180, a: 0.5)
69
+ => ['steelblue']
49
70
 
50
- ColorNamerRuby:Namer.get_name('', r: 130, g: 180, b: 70)
51
- => 'Sushi'
71
+ ColorNamerRuby.get_name('', r: 130, g: 180, b: 70)
72
+ => ['sushi']
52
73
 
53
- ColorNamerRuby:Namer.get_name('', h: 20, s: 73, l: 20)
54
- => 'Cioccolato'
74
+ ColorNamerRuby.get_name('', h: 20, s: 73, l: 20)
75
+ => ['cioccolato']
55
76
 
56
- ColorNamerRuby:Namer.get_name(nil, h: 107, s: 61, v: 71)
57
- => 'Apple'
77
+ ColorNamerRuby.get_name(nil, h: 107, s: 61, v: 71)
78
+ => ['apple']
58
79
 
59
- ColorNamerRuby::Namer.get_name(nil, h: 61, s: 71, b: 32)
60
- => 'Camouflage'
80
+ ColorNamerRuby.get_name(nil, h: 61, s: 71, b: 32)
81
+ => ['camouflage']
61
82
 
62
- ColorNamerRuby::Namer.get_name(nil, c: 71, m: 15, y: 5, k: 54)
63
- => 'Blue Dianne'
83
+ ColorNamerRuby.get_name(nil, c: 71, m: 15, y: 5, k: 54)
84
+ => ['blue dianne']
64
85
 
65
- ColorNamerRuby::Namer.get_name('rgb(51, 102, 204)')
66
- => 'Denim'
86
+ ColorNamerRuby.get_name('rgb(51, 102, 204)')
87
+ => ['denim']
67
88
 
68
- ColorNamerRuby::Namer.get_name('hsl(225, 73%, 57%)')
69
- => 'royalblue'
89
+ ColorNamerRuby.get_name('hsl(225, 73%, 57%)')
90
+ => ['royalblue']
70
91
  ```
71
92
 
72
93
  Get a list of colour names sorted by their perceptual similarity to the given color by using `.get_names`.
73
94
  Again, when passing colours with their properties assigned to keys, you need to pass nil or an empty string as the first parameter.
74
95
 
75
96
  ```ruby
76
- ColorNamerRuby::Namer.get_names('#3672b4')
97
+ ColorNamerRuby.get_names('#3672b4')
98
+ => ['azure', 'st tropaz', 'denim', 'steelblue', ...]
99
+ ```
100
+
101
+ Get a list of all the colour names across the lists that can be checked against.
102
+
103
+ ```ruby
104
+ ColorNamerRuby.get_all_names
105
+ => ['black', 'blue', 'cyan', 'green', 'teal', ...]
106
+ ```
107
+
108
+ You can also get the entire swatch object(s) to view the hex values and distance the colours are from the source colour you provided. These methods are all related/similar to their 'name' named method counterparts, except they provide the colour hash instead of just the name, and can be called in a similar way too.
109
+
110
+ When passing colours with their properties assigned to keys, you need to pass nil or an empty string as the first parameter.
111
+
112
+ ```ruby
113
+ ColorNamerRuby.get_colour('#3672b4')
114
+ => [{ distance: 8.660254037844387, hex: '#315BA1', name: 'azure' }]
115
+ ```
116
+
117
+ Get a list of colour hashes sorted by their perceptual similarity to the given color by using `.get_colours`.
118
+ Again, when passing colours with their properties assigned to keys, you need to pass nil or an empty string as the first parameter.
119
+
120
+ ```ruby
121
+ ColorNamerRuby.get_colours('#3672b4')
77
122
  => [
78
- => { name: 'Azure', hex: '#315BA1', distance: 8.660254037844387 },
79
- => { name: 'St Tropaz', hex: '#2D569B', distance: 9.9498743710662 },
80
- => { name: 'Denim', hex: '#2B6CC4', distance: 10.816653826391969 },
81
- => { name: 'steelblue', hex: '#4682B4', distance: 11.180339887498949 },
82
- => { name: 'Steel Blue', hex: '#4682B4', distance: 11.180339887498949 },
123
+ => { distance: 8.660254037844387, hex: '#315BA1', name: 'azure' },
124
+ => { distance: 9.9498743710662, hex: '#2D569B', name: 'st tropaz' },
125
+ => { distance: 10.81665382639196, name: 'denim' },
83
126
  => .
84
127
  => .
85
128
  => .
86
129
  => ]
87
130
  ```
88
131
 
89
- Get a list of the colour lists that can be checked against.
132
+ Get a list of all the colour hashes across the lists that can be checked against.
90
133
 
91
134
  ```ruby
92
- ColorNamerRuby::Namer.list_names
93
- => ['basic', 'html', 'ntc', 'pantone', 'roygbiv', 'x11']
135
+ ColorNamerRuby.get_all_colours
136
+ => [
137
+ => { name: 'black', hex: '#000000', collection: 'basic' },
138
+ => { name: 'blue', hex: '#0000FF', collection: 'basic' },
139
+ => { name: 'cyan', hex: '#00FFFF', collection: 'basic' },
140
+ => .
141
+ => .
142
+ => .
143
+ => ]
94
144
  ```
95
145
 
96
146
  ## Options
@@ -98,28 +148,28 @@ ColorNamerRuby::Namer.list_names
98
148
  ### pick
99
149
 
100
150
  This parameter allows you to filter names from the dedicated lists for faster computation.
101
- It can be used for both `get_name` and `get_names`.
151
+ It can be used for `get_name`, `get_names`, `get_all_names`, or `get_colour`, `get_colours`, `get_all_colours`.
102
152
 
103
153
  ```ruby
104
- ColorNamerRuby::Namer.get_names('#3672b4', pick: ['basic', 'x11'])
154
+ ColorNamerRuby.get_names('#3672b4', pick: ['basic', 'x11'])
105
155
  ```
106
156
 
107
157
  ### omit
108
158
 
109
159
  The opposite of `options.pick`.
110
- It can be used for both `get_name` and `get_names`.
160
+ It can be used for `get_name`, `get_names`, `get_all_names`, or `get_colour`, `get_colours`, `get_all_colours`.
111
161
 
112
162
  ```ruby
113
- ColorNamerRuby::Namer.get_names('#3672b4', omit: ['pantone', 'roygbiv'])
163
+ ColorNamerRuby.get_names('#3672b4', omit: ['pantone', 'roygbiv'])
114
164
  ```
115
165
 
116
166
  ### limit
117
167
 
118
168
  This option allows us to limit the number of names that are returned to keep the returned output manageable.
119
- It can be used for `get_names` (since `get_name` already has a limit of 1).
169
+ It can be used for `get_names`, or `get_colours` (since `get_name` and `get_colour` already have a limit of 1).
120
170
 
121
171
  ```ruby
122
- ColorNamerRuby::Namer.get_names('#3672b4', limit: 5)
172
+ ColorNamerRuby.get_names('#3672b4', limit: 5)
123
173
  ```
124
174
 
125
175
  ###
@@ -132,7 +182,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
132
182
 
133
183
  ## Contributing
134
184
 
135
- Bug reports and pull requests are welcome on GitHub at <https://github.com/[USERNAME]/color_namer_ruby>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/color_namer_ruby/blob/master/CODE_OF_CONDUCT.md).
185
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/louiswdavis/color_namer_ruby>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/louiswdavis/color_namer_ruby/blob/master/CODE_OF_CONDUCT.md).
136
186
 
137
187
  ## License
138
188
 
@@ -140,4 +190,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
140
190
 
141
191
  ## Code of Conduct
142
192
 
143
- Everyone interacting in the ColorNamerRuby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/color_namer_ruby/blob/master/CODE_OF_CONDUCT.md).
193
+ Everyone interacting in the ColorNamerRuby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/louiswdavis/color_namer_ruby/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile CHANGED
@@ -1,8 +1,8 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- task default: :spec
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -1,44 +1,44 @@
1
- require 'color_conversion'
2
- require 'active_support/core_ext/object/blank'
3
-
4
- module ColorNamerRuby
5
- class Distance
6
- def self.get_names_from_hex(hex, pick, omit, limit)
7
- @colour = ColorConversion::Color.new(hex)
8
-
9
- colour_lists = []
10
-
11
- ColorNamerRuby::Namer.list_names.each do |list_name|
12
- if (pick.empty? || pick.include?(list_name)) && !omit.include?(list_name)
13
- colour_lists += self.get_colour_distances_in_list(Object.const_get("ColorNamerRuby::#{list_name.capitalize}").colours)
14
- end
15
- end
16
-
17
- colour_lists = colour_lists.flatten.compact.reject(&:empty?)
18
-
19
- limit = colour_lists.length if limit < 0
20
-
21
- colour_lists.sort_by { |swatch| swatch[:distance] }.take(limit)
22
- end
23
-
24
- private
25
-
26
- def self.get_colour_distances_in_list(colour_list)
27
- colour_list.map do |swatch|
28
- swatch[:distance] = self.distance_between_colours(ColorConversion::Color.new(swatch.dig(:hex)))
29
- end
30
-
31
- colour_list
32
- end
33
-
34
- def self.distance_between_colours(comparison_colour)
35
- # https://en.wikipedia.org/wiki/Euclidean_distance#Higher_dimensions
36
- # closest colours across the different arrays
37
- # TODO use LAB or HCL instead of HSL
38
- hsl_1 = @colour.hsl
39
- hsl_2 = comparison_colour.hsl
40
-
41
- Math.sqrt((hsl_1[:h] - hsl_2[:h])**2 + (hsl_1[:s] - hsl_2[:s])**2 + (hsl_1[:l] - hsl_2[:l])**2)
42
- end
43
- end
44
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'color_conversion'
4
+ require 'active_support/core_ext/object/blank'
5
+
6
+ module ColorNamerRuby
7
+ class Distance
8
+ def self.get_names_from_hex(hex, pick, omit, limit)
9
+ @colour = ColorConversion::Color.new(hex)
10
+
11
+ colour_lists = []
12
+
13
+ ColorNamerRuby.list_collections.each do |collection_name|
14
+ next unless (pick.empty? || pick.include?(collection_name)) && !omit.include?(collection_name)
15
+
16
+ colour_lists += self.get_colour_distances_in_list(Object.const_get("ColorSwatchCollection::#{collection_name.capitalize}").colours)
17
+ end
18
+
19
+ colour_lists = colour_lists.flatten.compact.reject(&:empty?)
20
+
21
+ limit = colour_lists.length if limit.negative?
22
+
23
+ colour_lists.sort_by { |swatch| swatch[:distance] }.take(limit)
24
+ end
25
+
26
+ def self.get_colour_distances_in_list(colour_list)
27
+ colour_list.map do |swatch|
28
+ swatch[:distance] = self.distance_between_colours(ColorConversion::Color.new(swatch.dig(:hex)))
29
+ end
30
+
31
+ colour_list
32
+ end
33
+
34
+ def self.distance_between_colours(comparison_colour)
35
+ # https://en.wikipedia.org/wiki/Euclidean_distance#Higher_dimensions
36
+ # closest colours across the different arrays
37
+ # TODO use LAB or HCL instead of HSL
38
+ conversion_1 = @colour.hsl
39
+ conversion_2 = comparison_colour.hsl
40
+
41
+ Math.sqrt((conversion_1[:h] - conversion_2[:h])**2 + (conversion_1[:s] - conversion_2[:s])**2 + (conversion_1[:l] - conversion_2[:l])**2)
42
+ end
43
+ end
44
+ end
@@ -1,5 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
- module ColorNamerRuby
4
- VERSION = "0.1.0"
5
- end
1
+ # frozen_string_literal: true
2
+
3
+ module ColorNamerRuby
4
+ VERSION = '0.1.1'
5
+ end
@@ -1,18 +1,70 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'color_namer_ruby/version'
4
-
5
- require_relative 'color_namer_ruby/basic'
6
- require_relative 'color_namer_ruby/html'
7
- require_relative 'color_namer_ruby/ntc'
8
- require_relative 'color_namer_ruby/pantone'
9
- require_relative 'color_namer_ruby/roygbiv'
10
- require_relative 'color_namer_ruby/x11'
11
-
12
- require_relative 'color_namer_ruby/namer'
13
- require_relative 'color_namer_ruby/distance'
14
-
15
- module ColorNamerRuby
16
- class Error < StandardError; end
17
- # Your code goes here...
18
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'color_swatch_collection'
4
+
5
+ require_relative 'color_namer_ruby/version'
6
+ require_relative 'color_namer_ruby/distance'
7
+
8
+ module ColorNamerRuby
9
+ class Error < StandardError; end
10
+
11
+ def self.list_collections
12
+ ColorSwatchCollection.list_collections
13
+ end
14
+
15
+ def self.get_name(colour_value, pick: [], omit: [], **alt_colour_value)
16
+ self.get_colour(colour_value, pick: pick, omit: omit, **alt_colour_value).map { |h| h[:name] } || []
17
+ end
18
+
19
+ def self.get_names(colour_value, pick: [], omit: [], limit: -1, **alt_colour_value)
20
+ self.get_colours(colour_value, pick: pick, omit: omit, limit: limit, **alt_colour_value).map { |h| h[:name] } || []
21
+ end
22
+
23
+ def self.get_all_names(pick: [], omit: [])
24
+ self.get_all_colours(pick: pick, omit: omit).map { |h| h[:name] } || []
25
+ end
26
+
27
+ def self.get_colour(colour_value, pick: [], omit: [], **alt_colour_value)
28
+ # alt_colour_value are used to collect colour paramaters when they are passed in the key format
29
+ hex = ColorConversion::Color.new(colour_value.presence || alt_colour_value).hex
30
+
31
+ # if we get an exact match, return it
32
+ colour = self.get_colour_from_collections(hex, pick, omit)
33
+
34
+ if colour.present?
35
+ colour[:distance] = 0.0
36
+ return [colour]
37
+ end
38
+
39
+ # if we don't get an exact match, try to find the closest match
40
+ ColorNamerRuby::Distance.get_names_from_hex(hex, pick, omit, 1)
41
+ end
42
+
43
+ def self.get_colours(colour_value, pick: [], omit: [], limit: -1, **alt_colour_value)
44
+ hex = ColorConversion::Color.new(colour_value.presence || alt_colour_value).hex
45
+
46
+ # get the closest matches up to the count limit
47
+ ColorNamerRuby::Distance.get_names_from_hex(hex, pick, omit, limit)
48
+ end
49
+
50
+ # if the same names appear in multiple lists, they could still appear even if certain lists they are in are omitted
51
+ def self.get_all_colours(pick: [], omit: [])
52
+ ColorSwatchCollection.get_colours(pick: pick, omit: omit)
53
+ end
54
+
55
+ private
56
+
57
+ def self.get_colour_from_collections(hex, pick, omit)
58
+ colour = nil
59
+
60
+ self.list_collections.each do |collection_name|
61
+ next unless (pick.empty? || pick.include?(collection_name)) && !omit.include?(collection_name)
62
+
63
+ colour = ColorSwatchCollection.get_from_hex(hex, pick: [collection_name])
64
+
65
+ break if colour.present?
66
+ end
67
+
68
+ colour || {}
69
+ end
70
+ end
metadata CHANGED
@@ -1,14 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: color_namer_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Louis Davis
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-06-21 00:00:00.000000000 Z
10
+ date: 2025-08-19 00:00:00.000000000 Z
11
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activesupport
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: 6.1.3
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: 6.1.3
12
26
  - !ruby/object:Gem::Dependency
13
27
  name: color_conversion
14
28
  requirement: !ruby/object:Gem::Requirement
@@ -24,19 +38,19 @@ dependencies:
24
38
  - !ruby/object:Gem::Version
25
39
  version: 0.1.2
26
40
  - !ruby/object:Gem::Dependency
27
- name: activesupport
41
+ name: color_swatch_collection
28
42
  requirement: !ruby/object:Gem::Requirement
29
43
  requirements:
30
- - - ">="
44
+ - - "~>"
31
45
  - !ruby/object:Gem::Version
32
- version: '0'
46
+ version: 0.1.0
33
47
  type: :runtime
34
48
  prerelease: false
35
49
  version_requirements: !ruby/object:Gem::Requirement
36
50
  requirements:
37
- - - ">="
51
+ - - "~>"
38
52
  - !ruby/object:Gem::Version
39
- version: '0'
53
+ version: 0.1.0
40
54
  description: Provide a color in a variety of formats and get a name back based on
41
55
  a range of colour options and see how closely they match your color.
42
56
  email:
@@ -48,15 +62,8 @@ files:
48
62
  - README.md
49
63
  - Rakefile
50
64
  - lib/color_namer_ruby.rb
51
- - lib/color_namer_ruby/basic.rb
52
65
  - lib/color_namer_ruby/distance.rb
53
- - lib/color_namer_ruby/html.rb
54
- - lib/color_namer_ruby/namer.rb
55
- - lib/color_namer_ruby/ntc.rb
56
- - lib/color_namer_ruby/pantone.rb
57
- - lib/color_namer_ruby/roygbiv.rb
58
66
  - lib/color_namer_ruby/version.rb
59
- - lib/color_namer_ruby/x11.rb
60
67
  homepage: https://github.com/louiswdavis/color_namer_ruby
61
68
  licenses:
62
69
  - MIT
@@ -1,36 +0,0 @@
1
- module ColorNamerRuby
2
- class Basic
3
- def self.get_name_from_hex(hex)
4
- self.colours.select { |swatch| swatch[:hex] == hex.upcase }.first&.dig(:name)
5
- end
6
-
7
- private
8
-
9
- def self.colours
10
- [
11
- { name: 'black', hex: '#000000' },
12
- { name: 'blue', hex: '#0000FF' },
13
- { name: 'cyan', hex: '#00FFFF' },
14
- { name: 'green', hex: '#008000' },
15
- { name: 'teal', hex: '#008080' },
16
- { name: 'turquoise', hex: '#40E0D0' },
17
- { name: 'indigo', hex: '#4B0082' },
18
- { name: 'gray', hex: '#808080' },
19
- { name: 'purple', hex: '#800080' },
20
- { name: 'brown', hex: '#A52A2A' },
21
- { name: 'tan', hex: '#D2B48C' },
22
- { name: 'violet', hex: '#EE82EE' },
23
- { name: 'beige', hex: '#F5F5DC' },
24
- { name: 'fuchsia', hex: '#FF00FF' },
25
- { name: 'gold', hex: '#FFD700' },
26
- { name: 'magenta', hex: '#FF00FF' },
27
- { name: 'orange', hex: '#FFA500' },
28
- { name: 'pink', hex: '#FFC0CB' },
29
- { name: 'red', hex: '#FF0000' },
30
- { name: 'white', hex: '#FFFFFF' },
31
- { name: 'yellow', hex: '#FFFF00' },
32
- ]
33
- end
34
- end
35
- end
36
-