number_to_color 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: be64228ca77648ab10e5c239d226580f8854abd8a1dae15fdd0bbb4359c62c70
4
+ data.tar.gz: 1a35483bb28f5432f19f4fbbdfecb57ed0a273f96c9943dc8bb461ff2e1b9225
5
+ SHA512:
6
+ metadata.gz: 226c9da9e7d3181d1c9741b543265619f2a9250b75dbea40defeae64dad93962ab51b5293d62833cbb8a0b096767248e17d6f365332371589101ccb61be7c2ab
7
+ data.tar.gz: 8175102c43d3e21c12d59134e664884daa716bdf3f49beb49392a2a5535bd3d0e5f38e7b6b96502c3a8d4ffa8134813daadd36b3fa35c4f96b652992e30b9f8d
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-04-28
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in number_to_color.gemspec
6
+ gemspec
7
+
8
+ gem "minitest"
9
+ gem "rake", "~> 13.0"
10
+ gem "rspec"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,59 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ number_to_color (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.5.0)
11
+ json (2.6.3)
12
+ minitest (5.18.0)
13
+ parallel (1.23.0)
14
+ parser (3.2.2.1)
15
+ ast (~> 2.4.1)
16
+ rainbow (3.1.1)
17
+ rake (13.0.6)
18
+ regexp_parser (2.8.0)
19
+ rexml (3.2.5)
20
+ rspec (3.12.0)
21
+ rspec-core (~> 3.12.0)
22
+ rspec-expectations (~> 3.12.0)
23
+ rspec-mocks (~> 3.12.0)
24
+ rspec-core (3.12.2)
25
+ rspec-support (~> 3.12.0)
26
+ rspec-expectations (3.12.3)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.12.0)
29
+ rspec-mocks (3.12.5)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.12.0)
32
+ rspec-support (3.12.0)
33
+ rubocop (1.50.2)
34
+ json (~> 2.3)
35
+ parallel (~> 1.10)
36
+ parser (>= 3.2.0.0)
37
+ rainbow (>= 2.2.2, < 4.0)
38
+ regexp_parser (>= 1.8, < 3.0)
39
+ rexml (>= 3.2.5, < 4.0)
40
+ rubocop-ast (>= 1.28.0, < 2.0)
41
+ ruby-progressbar (~> 1.7)
42
+ unicode-display_width (>= 2.4.0, < 3.0)
43
+ rubocop-ast (1.28.0)
44
+ parser (>= 3.2.1.0)
45
+ ruby-progressbar (1.13.0)
46
+ unicode-display_width (2.4.2)
47
+
48
+ PLATFORMS
49
+ x86_64-darwin-22
50
+
51
+ DEPENDENCIES
52
+ minitest
53
+ number_to_color!
54
+ rake (~> 13.0)
55
+ rspec
56
+ rubocop (~> 1.21)
57
+
58
+ BUNDLED WITH
59
+ 2.4.12
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Luke Fair
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+
2
+ # NumberToColor
3
+ Color code a value within a numerical range.
4
+
5
+ All colors are customizable, but defaults to a linear gradient between red (![#ef4444](https://placehold.co/15x15/ef4444/ef4444.png) "negative") and blue (![#0ea5e9](https://placehold.co/15x15/0ea5e9/0ea5e9.png) "positive"), with white as the midpoint for "neutral" or average.
6
+
7
+
8
+
9
+ ## Usage
10
+
11
+ ### Params
12
+ * `value`: The numerical value to color code for.
13
+
14
+ * `domain`: The numerical domain, or "range", for the color gradient. This can be a 2 or 3-item array. In a 2-item array, the midpoint is automatically set as the "neutral" value/color. In a 3-item array, you can set the midpoint to be anywhere between the first and last item (e.g., `[0, 5, 7]`).
15
+
16
+ * `neutral_color` / `positive_color` / `negative_color`: (optional) Custom colors that can be passed in to override the defaults. These can be hex or RGB codes.
17
+ <br>
18
+
19
+ To get started, require the gem in your file.
20
+ ```
21
+
22
+ require 'number_to_color'
23
+
24
+ ```
25
+
26
+
27
+ ### Examples
28
+ #### Simple linear range between two numbers
29
+ ```
30
+ ColorCode.new(value: 2, domain: [1, 3]).to_hex
31
+
32
+ // #ffffff (neutral color)
33
+ ```
34
+
35
+
36
+
37
+ #### Non-linear range
38
+ ```
39
+ ColorCode.new(value: 5, domain: [1, 5, 7]).to_hex
40
+
41
+ // #ffffff (neutral color)
42
+ ```
43
+
44
+
45
+
46
+ #### Inverted domain
47
+ ```
48
+ ColorCode.new(value: 1, domain: [7, 1]).to_hex
49
+
50
+ // #0ea5e9 ("good" color)
51
+ ```
52
+
53
+
54
+
55
+ #### Custom "positive" color
56
+ ```
57
+ ColorCode.new(value: 1, domain: [0, 1], positive_color: '#ffffff").to_hex
58
+
59
+ // #ffffff (our new "positive" color)
60
+ ```
61
+
62
+ ## Development
63
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
64
+
65
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
66
+
67
+ ## Contributing
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nonlocalize/number_to_color.
69
+
70
+ ## License
71
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,21 @@
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
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
13
+
14
+ require "rake/testtask"
15
+
16
+ Rake::TestTask.new(:test) do |t|
17
+ t.libs << "test"
18
+ t.pattern = "test/**/*_test.rb"
19
+ end
20
+
21
+ task default: :test
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module NumberToColor
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,188 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "number_to_color/version"
4
+
5
+ # Class to generate a hex color code based on a value and domain.
6
+ #
7
+ # @author Luke Fair <fair@hey.com>
8
+ class ColorCode
9
+ # '#0ea5e9'
10
+ DEFAULT_END = [14, 165, 233].freeze
11
+ # 'f87171'
12
+ DEFAULT_START = [248, 113, 113].freeze
13
+ # 'fff'
14
+ DEFAULT_MIDDLE = [255, 255, 255].freeze
15
+
16
+ # @param [Number] value The table cell's numerical value
17
+ # @param [Array] domain - Two or three-element arrays (e.g., [0, 20], [-20, 0, 20])
18
+ # @param [String|Array] middle_color - The hex or rgb code to use for the middle color.
19
+ # @param [String|Array] end_color - The hex or rgb code to use for the end color.
20
+ # @param [String|Array] start_color - The hex or rgb code to use for the start color.
21
+ def initialize(
22
+ value:,
23
+ domain: nil,
24
+ middle_color: nil,
25
+ end_color: nil,
26
+ start_color: nil
27
+ )
28
+ @value = value.to_f
29
+ @domain = domain
30
+ @middle_color = middle_color
31
+ @start_color = start_color
32
+ @end_color = end_color
33
+
34
+ set_colors
35
+ end
36
+
37
+ # The public method to return the hex code.
38
+ # @return [String].
39
+ def hex_color
40
+ rgb_to_hex(red: final_rgb[:r], green: final_rgb[:g], blue: final_rgb[:b])
41
+ end
42
+
43
+ private
44
+
45
+ attr_reader :value, :domain, :end_color, :start_color, :middle_color, :end_rgb, :start_rgb,
46
+ :middle_rgb
47
+
48
+ def set_colors
49
+ @end_rgb = end_color ? format_color(end_color) : DEFAULT_END
50
+ @start_rgb = start_color ? format_color(start_color) : DEFAULT_START
51
+ @middle_rgb = middle_color ? format_color(middle_color) : DEFAULT_MIDDLE
52
+ end
53
+
54
+ # Returns the hex code for any RGB color.
55
+ # @return [String].
56
+ def rgb_to_hex(red:, green:, blue:)
57
+ "##{Kernel.format("%02x%02x%02x", red, green, blue)}"
58
+ end
59
+
60
+ # Returns the hex code for any RGB color.
61
+ # @param [String|Array] color The hex or rgb code.
62
+ # @return [Array].
63
+ def format_color(color)
64
+ return color.scan(/.{2}/).map(&:hex) if color.instance_of? String
65
+
66
+ color
67
+ end
68
+
69
+ # If the domain is inverted, switch its order.
70
+ # @return [Array].
71
+ def normalized_domain
72
+ return domain.reverse if inverted_order?
73
+
74
+ domain
75
+ end
76
+
77
+ # Calcluate the final RGB value.
78
+ # @return [Hash].
79
+ def final_rgb
80
+ r_start, g_start, b_start = start_color_rgb
81
+
82
+ r_end, g_end, b_end = end_color_rgb
83
+
84
+ {
85
+ r: calculate_rgb_level(r_start, r_end),
86
+ g: calculate_rgb_level(g_start, g_end),
87
+ b: calculate_rgb_level(b_start, b_end)
88
+ }
89
+ end
90
+
91
+ # Returns if the domain is reversed (lower number = good).
92
+ # @return [Boolean].
93
+ def inverted_order?
94
+ domain.first > domain.last
95
+ end
96
+
97
+ # Calculate the specific level (R, G, or B) based on the start and end value.
98
+ # @return [Integer].
99
+ def calculate_rgb_level(start_level, end_level)
100
+ r = ((end_level * distance_from_min_value) + (start_level * distance_difference)).to_i
101
+
102
+ [[0, r].max, 255].min
103
+ end
104
+
105
+ # Clamp the value if it falls above or below the min.
106
+ # @return [Number].
107
+ def clamped_value
108
+ return normalized_domain.first if value < normalized_domain.first
109
+
110
+ return normalized_domain.last if value > normalized_domain.last
111
+
112
+ value
113
+ end
114
+
115
+ # Returns the start color in RGB format.
116
+ # @return [Array].
117
+ def start_color_rgb
118
+ return end_rgb if inverted_order? && value_is_start?
119
+
120
+ return start_rgb if value_is_start?
121
+
122
+ middle_rgb
123
+ end
124
+
125
+ # Returns the end color in RGB format.
126
+ # @return [Array].
127
+ def end_color_rgb
128
+ return middle_rgb if value_is_start?
129
+
130
+ return start_rgb if inverted_order?
131
+
132
+ end_rgb
133
+ end
134
+
135
+ # Returns if the value is considered start.
136
+ # @return [Boolean].
137
+ def value_is_start?
138
+ clamped_value.between?(normalized_domain.first, mean_value) || clamped_value == mean_value
139
+ end
140
+
141
+ # Returns the domain's min value, or what is considered 'start'.
142
+ # @return [Number].
143
+ def min_value
144
+ return mean_value unless value_is_start?
145
+
146
+ normalized_domain.first
147
+ end
148
+
149
+ # Returns the domain's min value, or what is considered 'start'.
150
+ # @return [Number].
151
+ def max_value
152
+ return mean_value if value_is_start?
153
+
154
+ normalized_domain.last
155
+ end
156
+
157
+ # Returns the range's mean. This will be the median of a two-value domain,
158
+ # or the middle value of a three-value domain.
159
+ # @return [Number].
160
+ def mean_value
161
+ return normalized_domain[1] if normalized_domain.length == 3
162
+
163
+ normalized_domain.sum(0.0) / normalized_domain.size
164
+ end
165
+
166
+ # Normalize the range in end values [0, a_end_value].
167
+ # @return [Number].
168
+ def normalized_max_value
169
+ max_value - min_value
170
+ end
171
+
172
+ # Normalize the range in end values [0, a_end_value].
173
+ # This ensures the value is end.
174
+ # @return [Number].
175
+ def normalized_value
176
+ clamped_value - min_value
177
+ end
178
+
179
+ # Calculate distance to min value on a 0,1 scale.
180
+ # @return [Number].
181
+ def distance_from_min_value
182
+ normalized_value / normalized_max_value
183
+ end
184
+
185
+ def distance_difference
186
+ 1 - distance_from_min_value
187
+ end
188
+ end
@@ -0,0 +1,4 @@
1
+ module NumberToColor
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: number_to_color
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Luke Fair
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - fair@hey.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - lib/number_to_color.rb
29
+ - lib/number_to_color/version.rb
30
+ - sig/number_to_color.rbs
31
+ homepage: https://github.com/nonlocalize/number_to_color
32
+ licenses:
33
+ - MIT
34
+ metadata:
35
+ homepage_uri: https://github.com/nonlocalize/number_to_color
36
+ source_code_uri: https://github.com/nonlocalize/number_to_color
37
+ changelog_uri: https://github.com/nonlocalize/number_to_color/CHANGELOG.md
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 2.6.0
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubygems_version: 3.4.12
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: Color code a numerical value based on a range.
57
+ test_files: []