number_to_color 1.0.0 → 2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be64228ca77648ab10e5c239d226580f8854abd8a1dae15fdd0bbb4359c62c70
4
- data.tar.gz: 1a35483bb28f5432f19f4fbbdfecb57ed0a273f96c9943dc8bb461ff2e1b9225
3
+ metadata.gz: 4dc4be64719ba2808fd343499ee264d95c461160078d3399ae9df996fca16f2e
4
+ data.tar.gz: 54bca5fad8a30171842732d1f8d181c5eb334c5a39c88445aa726c7d87e8528d
5
5
  SHA512:
6
- metadata.gz: 226c9da9e7d3181d1c9741b543265619f2a9250b75dbea40defeae64dad93962ab51b5293d62833cbb8a0b096767248e17d6f365332371589101ccb61be7c2ab
7
- data.tar.gz: 8175102c43d3e21c12d59134e664884daa716bdf3f49beb49392a2a5535bd3d0e5f38e7b6b96502c3a8d4ffa8134813daadd36b3fa35c4f96b652992e30b9f8d
6
+ metadata.gz: 2d5310460412589e1455124d4775242b05b1a0195cc15181fd88c235f8d4e861368a3acce5ee04563dc12d7a4cfc31fa23a2e4a549619fa366686956daaa47df
7
+ data.tar.gz: 727bcb12d1bad08aa3fde47c71bd56ecadc4649a60daccc852d17eca65f26da64cacab6b2b2c9c1341eeb1cbd401b65409fdcee01e50d0f14bbde72e5bce9415
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- number_to_color (1.0.0)
4
+ number_to_color (2.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NumberToColor
4
- VERSION = "1.0.0"
4
+ VERSION = "2.0.0"
5
5
  end
@@ -6,24 +6,17 @@ require_relative "number_to_color/version"
6
6
  #
7
7
  # @author Luke Fair <fair@hey.com>
8
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
9
  # @param [Number] value The table cell's numerical value
17
10
  # @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.
11
+ # @param [String|Array] middle_color - The hex or rgb code to use for the middle color (default `white`).
12
+ # @param [String|Array] end_color - The hex or rgb code to use for the end color (default `blue`).
13
+ # @param [String|Array] start_color - The hex or rgb code to use for the start color (default `red`).
21
14
  def initialize(
22
15
  value:,
23
16
  domain: nil,
24
- middle_color: nil,
25
- end_color: nil,
26
- start_color: nil
17
+ middle_color: [255, 255, 255],
18
+ end_color: [14, 165, 233],
19
+ start_color: [248, 113, 113]
27
20
  )
28
21
  @value = value.to_f
29
22
  @domain = domain
@@ -46,9 +39,9 @@ class ColorCode
46
39
  :middle_rgb
47
40
 
48
41
  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
42
+ @end_rgb = format_color(end_color)
43
+ @start_rgb = format_color(start_color)
44
+ @middle_rgb = format_color(middle_color)
52
45
  end
53
46
 
54
47
  # Returns the hex code for any RGB color.
@@ -61,9 +54,9 @@ class ColorCode
61
54
  # @param [String|Array] color The hex or rgb code.
62
55
  # @return [Array].
63
56
  def format_color(color)
64
- return color.scan(/.{2}/).map(&:hex) if color.instance_of? String
57
+ return color.gsub("#", "").scan(/../).map(&:hex) if color.instance_of? String
65
58
 
66
- color
59
+ color unless color.nil?
67
60
  end
68
61
 
69
62
  # If the domain is inverted, switch its order.
@@ -115,9 +108,9 @@ class ColorCode
115
108
  # Returns the start color in RGB format.
116
109
  # @return [Array].
117
110
  def start_color_rgb
118
- return end_rgb if inverted_order? && value_is_start?
111
+ return end_rgb if inverted_order? && (value_is_in_starting_half? || two_item_domain?)
119
112
 
120
- return start_rgb if value_is_start?
113
+ return start_rgb if value_is_in_starting_half? || two_item_domain?
121
114
 
122
115
  middle_rgb
123
116
  end
@@ -125,31 +118,31 @@ class ColorCode
125
118
  # Returns the end color in RGB format.
126
119
  # @return [Array].
127
120
  def end_color_rgb
128
- return middle_rgb if value_is_start?
121
+ return middle_rgb if value_is_in_starting_half? && three_item_domain?
129
122
 
130
123
  return start_rgb if inverted_order?
131
124
 
132
125
  end_rgb
133
126
  end
134
127
 
135
- # Returns if the value is considered start.
128
+ # Returns if the value is considered in the first half of the domain.
136
129
  # @return [Boolean].
137
- def value_is_start?
138
- clamped_value.between?(normalized_domain.first, mean_value) || clamped_value == mean_value
130
+ def value_is_in_starting_half?
131
+ three_item_domain? && (clamped_value.between?(normalized_domain.first, mean_value) || clamped_value == mean_value)
139
132
  end
140
133
 
141
- # Returns the domain's min value, or what is considered 'start'.
134
+ # Returns the domain's min value.
142
135
  # @return [Number].
143
136
  def min_value
144
- return mean_value unless value_is_start?
137
+ return mean_value unless value_is_in_starting_half? || two_item_domain?
145
138
 
146
139
  normalized_domain.first
147
140
  end
148
141
 
149
- # Returns the domain's min value, or what is considered 'start'.
142
+ # Returns the domain's max value.
150
143
  # @return [Number].
151
144
  def max_value
152
- return mean_value if value_is_start?
145
+ return mean_value if value_is_in_starting_half? && three_item_domain?
153
146
 
154
147
  normalized_domain.last
155
148
  end
@@ -185,4 +178,16 @@ class ColorCode
185
178
  def distance_difference
186
179
  1 - distance_from_min_value
187
180
  end
181
+
182
+ # Returns if the domain has two values.
183
+ # @return [Boolean].
184
+ def two_item_domain?
185
+ domain.length == 2
186
+ end
187
+
188
+ # Returns if the domain has three values.
189
+ # @return [Boolean].
190
+ def three_item_domain?
191
+ domain.length == 3
192
+ end
188
193
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: number_to_color
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luke Fair
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-30 00:00:00.000000000 Z
11
+ date: 2023-05-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: