math2d 1.4.1 → 1.4.5

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/math2d/utils2d.rb +131 -1
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 34a1c8daaf45027dc42765be7a12a631038b42bfb3454d68da26eb99e763cacd
4
- data.tar.gz: 0b3e5533e79a80877806416dffb8c055f6164d5b391eea3ba0764589eb59bbcd
3
+ metadata.gz: 7fe67e25a8057e6e11bd7cd602d37c67eb7cab78ed029c83928638144d1d590d
4
+ data.tar.gz: 2bf6bf8012cba83df542190d30980234e70b74fe1bad00f46e1694a48a9b42fe
5
5
  SHA512:
6
- metadata.gz: fd195200c4e96cac60591821c73fa96b73e3cf3902dfff3f77191c4fd70ecad42c590dd92df7d9b90250647c6a02bf847f6a0b9f951f4def7544fe3bb9d8457e
7
- data.tar.gz: b578724ee4a68f9951a7fb5d31f0e10dcb9700d3b9b70496f23afc6c4ef11c0cc674a28c54fa83d89fe72745219b3f1b74b9cc685a1bdae5f16d62417eec1415
6
+ metadata.gz: 9d445a9bf01b379884e8b59508960b10ba51a82866ab8402c6dc44b44370532ff64e15cad3492afe279bdcfa3788c732f68ca5b5214d5356c716692197755369
7
+ data.tar.gz: 0e88cea36fa423ab36138ab6140d4ca2e87d9987f95898c5fe95667c2826077099bc0bbf28c4a3d003eed69e561ca7b47e6d297485e08d1ec8c4578e97f1ea1a
@@ -48,7 +48,7 @@ module Math2D
48
48
  end
49
49
 
50
50
  # Calculates a number between two numbers at a specific increment.
51
- # The amt parameter is the amount to interpolate between the two
51
+ # The +amt+ parameter is the amount to interpolate between the two
52
52
  # values where 0.0 equal to the first point, 0.1 is very near the
53
53
  # first point, 0.5 is half-way in between, and 1.0 is equal to the
54
54
  # second point.
@@ -160,6 +160,136 @@ module Math2D
160
160
  alias greyscale grayscale
161
161
  end
162
162
 
163
+ # Interpolates two RGB (r,g,b,a) colors +a+ and +b+ by an amount +amt+.
164
+ #
165
+ # @param [Array<Numeric>] a
166
+ # @param [Array<Numeric>] b
167
+ # @param [Numeric] amt
168
+ # @return [Array<Float>]
169
+ def self.lerp_rgb(a, b, amt)
170
+ [
171
+ lerp(a[0], b[0], amt),
172
+ lerp(a[1], b[1], amt),
173
+ lerp(a[2], b[2], amt),
174
+ lerp(a[3], b[3], amt)
175
+ ]
176
+ end
177
+
178
+ # Interpolates two RGB (r,g,b,a) colors +a+ and +b+ in the HSV color space by an amount +amt+.
179
+ # This method is preferred over +lerp_rgb+ when working with intensity graphs (i.e. temperature, probability density
180
+ # etc.)
181
+ #
182
+ # @note Both colors are converted internally to HSV and then converted back to RGB 0-1 at the end.
183
+ #
184
+ # @param [Array<Numeric>] a
185
+ # @param [Array<Numeric>] b
186
+ # @param [Numeric] amt
187
+ # @return [Array<Float>]
188
+ def self.lerp_hue(a, b, amt)
189
+ a = rgb_to_hsv(a)
190
+ b = rgb_to_hsv(b)
191
+ d = (b[0] - a[0]).abs
192
+ if a[0] > b[0]
193
+ a, b = b, a
194
+ amt = 1 - amt
195
+ end
196
+ if d > 0.5
197
+ a[0] += 1
198
+ h = (a[0] + amt * (b[0] - a[0])) % 1
199
+ else
200
+ h = a[0] + amt * d
201
+ end
202
+ hsv_to_rgb(
203
+ [
204
+ h,
205
+ lerp(a[1], b[1], amt),
206
+ lerp(a[2], b[2], amt),
207
+ lerp(a[3], b[3], amt)
208
+ ]
209
+ )
210
+ end
211
+
212
+ # Converts an RGB color array (r,g,b,a) +color+ to the HSV (h,s,v,a) color space.
213
+ #
214
+ # @note This method assumes that both the RGB and HSV color arrays are in Linear RGB/HSV (also called RBG/HSV 0-1),
215
+ # where each component ranges from 0.0 to 1.0.
216
+ #
217
+ # @param [Array<Numeric>]
218
+ # @return [Array<Float>]
219
+ def self.rgb_to_hsv(color)
220
+ r = color[0]
221
+ g = color[1]
222
+ b = color[2]
223
+ a = color[3]
224
+ max = [r, g, b].max
225
+ min = [r, g, b].min
226
+ v = max
227
+ d = max - min
228
+ s = max.zero? ? 0 : d / max
229
+ if max == min
230
+ h = 0
231
+ else
232
+ case max
233
+ when r
234
+ h = (g - b) / d + (g < b ? 6 : 0)
235
+ when g
236
+ h = (b - r) / d + 2
237
+ when b
238
+ h = (r - g) / d + 4
239
+ end
240
+ h /= 6
241
+ end
242
+ [h, s, v, a]
243
+ end
244
+
245
+ # Converts an HSV color array (h,s,v,a) +color+ to the RGB (r,g,b,a) color space.
246
+ #
247
+ # @note This method assumes that both the RGB and HSV color arrays are in Linear RGB/HSV (also called RBG/HSV 0-1),
248
+ # where each component ranges from 0.0 to 1.0.
249
+ #
250
+ # @param [Array<Numeric>]
251
+ # @return [Array<Float>]
252
+ def self.hsv_to_rgb(color)
253
+ h = color[0]
254
+ s = color[1]
255
+ v = color[2]
256
+ a = color[3]
257
+ i = (h * 6).floor
258
+ f = h * 6 - i
259
+ pp = v * (1 - s)
260
+ q = v * (1 - f * s)
261
+ t = v * (1 - (1 - f) * s)
262
+
263
+ case i % 6
264
+ when 0
265
+ r = v
266
+ g = t
267
+ b = pp
268
+ when 1
269
+ r = q
270
+ g = v
271
+ b = pp
272
+ when 2
273
+ r = pp
274
+ g = v
275
+ b = t
276
+ when 3
277
+ r = pp
278
+ g = q
279
+ b = v
280
+ when 4
281
+ r = t
282
+ g = pp
283
+ b = v
284
+ when 5
285
+ r = v
286
+ g = pp
287
+ b = q
288
+ end
289
+
290
+ [r, g, b, a]
291
+ end
292
+
163
293
  private
164
294
 
165
295
  # @private
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: math2d
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ualace Henrique
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-16 00:00:00.000000000 Z
11
+ date: 2022-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -80,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
80
  - !ruby/object:Gem::Version
81
81
  version: '0'
82
82
  requirements: []
83
- rubygems_version: 3.3.9
83
+ rubygems_version: 3.3.23
84
84
  signing_key:
85
85
  specification_version: 4
86
86
  summary: Math2D