decolmor 1.1.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,38 +1,50 @@
1
1
  load_class(__FILE__)
2
2
 
3
3
  RSpec.describe Decolmor do
4
- describe 'you can set rounding globally for convert to HSL/HSV/HSB/CMYK' do
4
+
5
+ describe 'we can include the module into the class' do
6
+ let(:dummy_class) { Class.new { include Decolmor } }
7
+
8
+ it "class contains the module Decolmor" do
9
+ expect( dummy_class.include?(Decolmor) ).to eq true
10
+ end
11
+
12
+ it "methods will be available into our class as class methods" do
13
+ expect { dummy_class.hsx_round = 3 }.to change { dummy_class.hsx_round }.from(1).to(3)
14
+ end
15
+ end
16
+
17
+ describe 'we can set rounding globally for convert to HSL/HSV/HSB/CMYK' do
5
18
  after(:each) do
6
19
  Decolmor.hsx_round = 1
7
20
  end
8
21
 
9
- let(:colors) { FactoryBot.build(:colors_map, round: 2) }
22
+ let(:colors) { FactoryBot.build(:colors, round: 2) }
10
23
 
11
24
  it ".hsx_round by default 1" do
12
- expect( Decolmor::hsx_round ).to eq 1
25
+ expect( Decolmor.hsx_round ).to eq 1
13
26
  end
14
27
 
15
28
  it ".hsx_round= changes hsx_round" do
16
- expect { Decolmor::hsx_round = 2 }.to change { Decolmor.hsx_round }.from(1).to(2)
29
+ expect { Decolmor.hsx_round = 2 }.to change { Decolmor.hsx_round }.from(1).to(2)
17
30
  end
18
31
  end
19
32
 
20
-
21
33
  context 'HEX <==> RGB(A)' do
22
34
  describe ".hex_to_rgb" do
23
- let(:colors) { FactoryBot.build(:colors_map) }
35
+ let(:colors) { FactoryBot.build(:colors) }
24
36
  let(:alphas) { FactoryBot.build(:alpha) }
25
37
 
26
38
  it "HEX w prefix # to RGB" do
27
39
  colors.each_pair do |hex, values|
28
- expect( Decolmor::hex_to_rgb(hex) ).to eq values[:rgb]
40
+ expect( Decolmor.hex_to_rgb(hex) ).to eq values[:rgb]
29
41
  end
30
42
  end
31
43
 
32
44
  it "HEX w/o prefix # to RGB" do
33
45
  colors.each_pair do |hex, values|
34
46
  hex = hex.delete('#')
35
- expect( Decolmor::hex_to_rgb(hex) ).to eq values[:rgb]
47
+ expect( Decolmor.hex_to_rgb(hex) ).to eq values[:rgb]
36
48
  end
37
49
  end
38
50
 
@@ -42,7 +54,7 @@ RSpec.describe Decolmor do
42
54
  alphas.each_pair do |hex_alpha, alpha|
43
55
  hex = format('%s%s', color, hex_alpha)
44
56
  rgba = colors[color][:rgb] + [alpha[:rgb]]
45
- expect( Decolmor::hex_to_rgb(hex) ).to eq rgba
57
+ expect( Decolmor.hex_to_rgb(hex) ).to eq rgba
46
58
  end
47
59
  end
48
60
 
@@ -52,7 +64,15 @@ RSpec.describe Decolmor do
52
64
  rounding = 2
53
65
  hex = format('%s%s', color, hex_alpha)
54
66
  rgba = colors[color][:rgb] + [alpha[:rgb].round(rounding)]
55
- expect( Decolmor::hex_to_rgb(hex, rounding) ).to eq rgba
67
+ expect( Decolmor.hex_to_rgb(hex, rounding) ).to eq rgba
68
+ end
69
+ end
70
+
71
+ it "set range 0..255 for alpha channel" do
72
+ color = colors.keys.sample
73
+ alphas.each_pair do |hex_alpha, alpha|
74
+ hex = format('%s%s', color, hex_alpha)
75
+ expect( Decolmor.hex_to_rgb(hex, alpha_255: true).last ).to eq alpha[:rgb_255]
56
76
  end
57
77
  end
58
78
 
@@ -62,7 +82,7 @@ RSpec.describe Decolmor do
62
82
  alphas.each_pair do |hex_alpha, alpha|
63
83
  hex = format('%s%s', color, hex_alpha).delete('#')
64
84
  rgba = colors[color][:rgb] + [alpha[:rgb]]
65
- expect( Decolmor::hex_to_rgb(hex) ).to eq rgba
85
+ expect( Decolmor.hex_to_rgb(hex) ).to eq rgba
66
86
  end
67
87
  end
68
88
 
@@ -70,39 +90,49 @@ RSpec.describe Decolmor do
70
90
  colors = {'6FC' => [102, 255, 204], '#9C3' => [153, 204, 51], '36FF' => [51, 102, 255, 1]}
71
91
 
72
92
  colors.each_pair do |hex_short, rgb|
73
- expect( Decolmor::hex_to_rgb(hex_short) ).to eq rgb
93
+ expect( Decolmor.hex_to_rgb(hex_short) ).to eq rgb
74
94
  end
75
95
  end
76
96
  end
77
97
 
78
98
  describe ".rgb_to_hex" do
79
- let(:colors) { FactoryBot.build(:colors_map) }
99
+ let(:colors) { FactoryBot.build(:colors) }
80
100
  let(:alphas) { FactoryBot.build(:alpha) }
81
101
 
82
102
  it "RGB converts to HEX" do
83
103
  colors.each_pair do |hex, values|
84
- expect( Decolmor::rgb_to_hex(values[:rgb]) ).to eq hex
104
+ expect( Decolmor.rgb_to_hex(values[:rgb]) ).to eq hex
85
105
  end
86
106
  end
87
107
 
88
108
  it "RGBA converts to HEX w alpha" do
89
109
  color = colors.keys.sample
90
110
 
91
- alphas.each_pair do |hex, alpha|
92
- hex = format('%s%s', color, hex)
111
+ alphas.each_pair do |hex_alpha, alpha|
112
+ hex = format('%s%s', color, hex_alpha)
93
113
  rgba = colors[color][:rgb] + [alpha[:rgb]]
94
- expect( Decolmor::rgb_to_hex(rgba) ).to eq hex
114
+ expect( Decolmor.rgb_to_hex(rgba) ).to eq hex
115
+ end
116
+ end
117
+
118
+ it "set range 0..255 for alpha channel" do
119
+ color = colors.keys.sample
120
+
121
+ alphas.each_pair do |hex_alpha, alpha|
122
+ hex = format('%s%s', color, hex_alpha)
123
+ rgba = colors[color][:rgb] + [alpha[:rgb_255]]
124
+ expect( Decolmor.rgb_to_hex(rgba, alpha_255: true) ).to eq hex
95
125
  end
96
126
  end
97
127
  end
98
128
  end
99
129
 
100
-
130
+
101
131
  context 'simple generator RGB, you can set any channel(s)' do
102
132
  describe ".new_rgb" do
103
133
  it "generate RGB with values into range 0..255" do
104
134
  100.times do
105
- expect( Decolmor::new_rgb ).to all( be_between(0, 255) )
135
+ expect( Decolmor.new_rgb ).to all( be_between(0, 255) )
106
136
  end
107
137
  end
108
138
 
@@ -111,10 +141,10 @@ RSpec.describe Decolmor do
111
141
  color = {red: 72, green: 209, blue: 204, alpha: 244}
112
142
  # set and check each channel separately
113
143
  color.each_with_index do |(key, value), index|
114
- expect( Decolmor::new_rgb(**{key => value})[index] ).to eq value
144
+ expect( Decolmor.new_rgb(**{key => value})[index] ).to eq value
115
145
  end
116
146
  # set all channels
117
- expect( Decolmor::new_rgb(**color) ).to eq color.values
147
+ expect( Decolmor.new_rgb(**color) ).to eq color.values
118
148
  end
119
149
  end
120
150
  end
@@ -122,78 +152,78 @@ RSpec.describe Decolmor do
122
152
 
123
153
  context 'RGB(A) to HSL/HSV/HSB' do
124
154
  describe ".rgb_to_hsl" do
125
- let(:colors) { FactoryBot.build(:colors_map) }
155
+ let(:colors) { FactoryBot.build(:colors) }
126
156
  let(:alphas) { FactoryBot.build(:alpha) }
127
157
 
128
158
  it "RGB converts to HSL" do
129
159
  colors.each_pair do |hex, values|
130
- expect( Decolmor::rgb_to_hsl(values[:rgb]) ).to eq values[:hsl]
160
+ expect( Decolmor.rgb_to_hsl(values[:rgb]) ).to eq values[:hsl]
131
161
  end
132
162
  end
133
163
 
134
164
  it "alpha channel pass to HSL unchanged" do
135
165
  color = colors.keys.sample
136
- alphas.each_pair do |hex_, alpha|
166
+ alphas.each_pair do |_hex_alpha, alpha|
137
167
  rgba = colors[color][:rgb] + [alpha[:rgb]]
138
- expect( Decolmor::rgb_to_hsl(rgba).last ).to eq alpha[:rgb]
168
+ expect( Decolmor.rgb_to_hsl(rgba).last ).to eq alpha[:rgb]
139
169
  end
140
170
  end
141
171
 
142
172
  it "you can set rounding for resulting HSL values (default = 1)" do
143
173
  docs "round 1 enough for a lossless conversion RGB -> HSL/HSV/HSB -> RGB"
144
174
  colors.each_pair do |hex, values|
145
- expect( Decolmor::rgb_to_hsl(values[:rgb], 0) ).to eq values[:hsl].map(&:round)
175
+ expect( Decolmor.rgb_to_hsl(values[:rgb], 0) ).to eq values[:hsl].map(&:round)
146
176
  end
147
177
  end
148
178
 
149
179
  it "setting rounding doesn't affect alpha channel" do
150
180
  color = colors.keys.sample
151
- alphas.each_pair do |hex_, alpha|
181
+ alphas.each_pair do |_hex_alpha, alpha|
152
182
  rgba = colors[color][:rgb] + [alpha[:rgb]]
153
183
  # alpha shouldn't be rounded because its range is 0..1
154
184
  # if that did happen, we would get 0 or 1 instead of the normal value
155
- expect( Decolmor::rgb_to_hsl(rgba, 0).last ).to eq alpha[:rgb]
185
+ expect( Decolmor.rgb_to_hsl(rgba, 0).last ).to eq alpha[:rgb]
156
186
  end
157
187
  end
158
188
  end
159
189
 
160
190
  describe ".rgb_to_hsv" do
161
- let(:colors) { FactoryBot.build(:colors_map) }
191
+ let(:colors) { FactoryBot.build(:colors) }
162
192
  let(:alphas) { FactoryBot.build(:alpha) }
163
- let(:colors_round_2) { FactoryBot.build(:colors_map, round: 2) }
193
+ let(:colors_round_2) { FactoryBot.build(:colors, round: 2) }
164
194
 
165
195
  it "RGB converts to HSV" do
166
196
  colors.each_pair do |hex, values|
167
- expect( Decolmor::rgb_to_hsv(values[:rgb]) ).to eq values[:hsv]
197
+ expect( Decolmor.rgb_to_hsv(values[:rgb]) ).to eq values[:hsv]
168
198
  end
169
199
  end
170
200
 
171
201
  it "alpha channel pass to HSV unchanged" do
172
202
  color = colors.keys.sample
173
- alphas.each_pair do |hex_, alpha|
203
+ alphas.each_pair do |_hex_alpha, alpha|
174
204
  rgba = colors[color][:rgb] + [alpha[:rgb]]
175
- expect( Decolmor::rgb_to_hsv(rgba).last ).to eq alpha[:rgb]
205
+ expect( Decolmor.rgb_to_hsv(rgba).last ).to eq alpha[:rgb]
176
206
  end
177
207
  end
178
208
 
179
209
  it "you can set rounding for resulting HSV values (default = 1)" do
180
210
  colors_round_2.each_pair do |hex, values|
181
- expect( Decolmor::rgb_to_hsv(values[:rgb], 0) ).to eq values[:hsv].map(&:round)
211
+ expect( Decolmor.rgb_to_hsv(values[:rgb], 0) ).to eq values[:hsv].map(&:round)
182
212
  end
183
213
  end
184
214
 
185
215
  it "setting rounding doesn't affect alpha channel" do
186
216
  color = colors.keys.sample
187
- alphas.each_pair do |hex_, alpha|
217
+ alphas.each_pair do |_hex_alpha, alpha|
188
218
  rgba = colors[color][:rgb] + [alpha[:rgb]]
189
- expect( Decolmor::rgb_to_hsv(rgba, 0).last ).to eq alpha[:rgb]
219
+ expect( Decolmor.rgb_to_hsv(rgba, 0).last ).to eq alpha[:rgb]
190
220
  end
191
221
  end
192
222
  end
193
223
 
194
224
  describe ".rgb_to_hsb" do
195
225
  it "alias .rgb_to_hsv" do
196
- expect( subject.method(:rgb_to_hsb ).original_name).to eq(:rgb_to_hsv)
226
+ expect( Decolmor.method(:rgb_to_hsb ).original_name).to eq(:rgb_to_hsv)
197
227
  end
198
228
  end
199
229
  end
@@ -201,46 +231,46 @@ RSpec.describe Decolmor do
201
231
 
202
232
  context 'HSL/HSV/HSB to RGB(A)' do
203
233
  describe ".hsl_to_rgb" do
204
- let(:colors) { FactoryBot.build(:colors_map) }
234
+ let(:colors) { FactoryBot.build(:colors) }
205
235
  let(:alphas) { FactoryBot.build(:alpha) }
206
236
 
207
237
  it "HSL converts to RGB" do
208
- colors.each_pair do |hex, values|
209
- expect( Decolmor::hsl_to_rgb(values[:hsl]) ).to eq values[:rgb]
238
+ colors.each_pair do |_hex, values|
239
+ expect( Decolmor.hsl_to_rgb(values[:hsl]) ).to eq values[:rgb]
210
240
  end
211
241
  end
212
242
 
213
243
  it "alpha channel pass to RGB unchanged" do
214
244
  color = colors.keys.sample
215
- alphas.each_pair do |hex_, values|
245
+ alphas.each_pair do |_hex_alpha, values|
216
246
  hsla = colors[color][:hsl] + [values[:rgb]]
217
- expect( Decolmor::hsl_to_rgb(hsla).last ).to eq values[:rgb]
247
+ expect( Decolmor.hsl_to_rgb(hsla).last ).to eq values[:rgb]
218
248
  end
219
249
  end
220
250
  end
221
251
 
222
252
  describe ".hsv_to_rgb" do
223
- let(:colors) { FactoryBot.build(:colors_map) }
253
+ let(:colors) { FactoryBot.build(:colors) }
224
254
  let(:alphas) { FactoryBot.build(:alpha) }
225
255
 
226
256
  it "HSV converts to RGB" do
227
- colors.each_pair do |hex, values|
228
- expect( Decolmor::hsv_to_rgb(values[:hsv]) ).to eq values[:rgb]
257
+ colors.each_pair do |_hex, values|
258
+ expect( Decolmor.hsv_to_rgb(values[:hsv]) ).to eq values[:rgb]
229
259
  end
230
260
  end
231
261
 
232
262
  it "alpha channel pass to RGB unchanged" do
233
263
  color = colors.keys.sample
234
- alphas.each_pair do |hex_, values|
264
+ alphas.each_pair do |_hex_alpha, values|
235
265
  hsva = colors[color][:hsv] + [values[:rgb]]
236
- expect( Decolmor::hsv_to_rgb(hsva).last ).to eq values[:rgb]
266
+ expect( Decolmor.hsv_to_rgb(hsva).last ).to eq values[:rgb]
237
267
  end
238
268
  end
239
269
  end
240
270
 
241
271
  describe ".hsb_to_rgb" do
242
272
  it "alias .hsv_to_rgb" do
243
- expect( subject.method(:hsb_to_rgb ).original_name).to eq(:hsv_to_rgb)
273
+ expect( Decolmor.method(:hsb_to_rgb ).original_name).to eq(:hsv_to_rgb)
244
274
  end
245
275
  end
246
276
  end
@@ -248,46 +278,141 @@ RSpec.describe Decolmor do
248
278
 
249
279
  context 'Alternative implementation HSL/HSV/HSB to RGB(A)' do
250
280
  describe ".hsl_to_rgb_alt" do
251
- let(:colors) { FactoryBot.build(:colors_map) }
281
+ let(:colors) { FactoryBot.build(:colors) }
252
282
  let(:alphas) { FactoryBot.build(:alpha) }
253
283
 
254
284
  it "HSL converts to RGB" do
255
- colors.each_pair do |hex, values|
256
- expect( Decolmor::hsl_to_rgb_alt(values[:hsl]) ).to eq values[:rgb]
285
+ colors.each_pair do |_hex, values|
286
+ expect( Decolmor.hsl_to_rgb_alt(values[:hsl]) ).to eq values[:rgb]
257
287
  end
258
288
  end
259
289
 
260
290
  it "alpha channel pass to RGB unchanged" do
261
291
  color = colors.keys.sample
262
- alphas.each_pair do |hex_, values|
292
+ alphas.each_pair do |_hex_alpha, values|
263
293
  hsla = colors[color][:hsl] + [values[:rgb]]
264
- expect( Decolmor::hsl_to_rgb_alt(hsla).last ).to eq values[:rgb]
294
+ expect( Decolmor.hsl_to_rgb_alt(hsla).last ).to eq values[:rgb]
265
295
  end
266
296
  end
297
+
298
+ it "if hue == 360 we get valid values" do
299
+ rgb_int = [123, 1, 2]
300
+ # the resulting value from hsi with rounding 0 to an integer (hue will be 360)
301
+ # as 360/60 does not get_rgb_point in any of the ranges 0...1 or 5...6
302
+ # so in the method we use (hue % 360)
303
+ # at the same time solving if hue is not in the 0..360 range
304
+ # the fact that rgb_int and rgb_out differ is normal,
305
+ # because with integer HSL values its impossible to losslessly convert back to RGB
306
+ rgb_out = [121, 1, 1]
307
+ hsv = Decolmor.rgb_to_hsl(rgb_int, 0)
308
+ expect( Decolmor.hsl_to_rgb_alt(hsv) ).to eq rgb_out
309
+ end
267
310
  end
268
311
 
269
312
  describe ".hsv_to_rgb_alt" do
270
- let(:colors) { FactoryBot.build(:colors_map) }
313
+ let(:colors) { FactoryBot.build(:colors) }
271
314
  let(:alphas) { FactoryBot.build(:alpha) }
272
315
 
273
316
  it "HSV converts to RGB" do
274
- colors.each_pair do |hex, values|
275
- expect( Decolmor::hsv_to_rgb_alt(values[:hsv]) ).to eq values[:rgb]
317
+ colors.each_pair do |_hex, values|
318
+ expect( Decolmor.hsv_to_rgb_alt(values[:hsv]) ).to eq values[:rgb]
276
319
  end
277
320
  end
278
321
 
279
322
  it "alpha channel pass to RGB unchanged" do
280
323
  color = colors.keys.sample
281
- alphas.each_pair do |hex_, values|
324
+ alphas.each_pair do |_hex_alpha, values|
282
325
  hsva = colors[color][:hsv] + [values[:rgb]]
283
- expect( Decolmor::hsv_to_rgb_alt(hsva).last ).to eq values[:rgb]
326
+ expect( Decolmor.hsv_to_rgb_alt(hsva).last ).to eq values[:rgb]
284
327
  end
285
328
  end
329
+
330
+ it "if hue == 360 we get valid values" do
331
+ rgb_int = [128, 7, 8]
332
+ # the resulting value from hsi with rounding 0 to an integer (hue will be 360)
333
+ # as 360/60 does not get_rgb_point in any of the ranges 0...1 or 5...6
334
+ # so in the method we use (hue % 360)
335
+ # at the same time solving if hue is not in the 0..360 range
336
+ # the fact that rgb_int and rgb_out differ is normal,
337
+ # because with integer HSV values its impossible to losslessly convert back to RGB
338
+ rgb_out = [128, 6, 6]
339
+ hsv = Decolmor.rgb_to_hsv(rgb_int, 0)
340
+ expect( Decolmor.hsv_to_rgb_alt(hsv) ).to eq rgb_out
341
+ end
286
342
  end
287
343
 
288
344
  describe ".hsb_to_rgb_alt" do
289
345
  it "alias .hsv_to_rgb_alt" do
290
- expect( subject.method(:hsb_to_rgb_alt ).original_name).to eq(:hsv_to_rgb_alt)
346
+ expect( Decolmor.method(:hsb_to_rgb_alt ).original_name).to eq(:hsv_to_rgb_alt)
347
+ end
348
+ end
349
+ end
350
+
351
+
352
+ context 'RGB <==> HSI' do
353
+ describe ".rgb_to_hsi" do
354
+ let(:colors) { FactoryBot.build(:colors) }
355
+ let(:alphas) { FactoryBot.build(:alpha) }
356
+ let(:colors_r3) { FactoryBot.build(:colors, round: 3) }
357
+
358
+ it "RGB converts to hsi" do
359
+ colors.each_pair do |hex, values|
360
+ expect( Decolmor.rgb_to_hsi(values[:rgb]) ).to eq values[:hsi]
361
+ end
362
+ end
363
+
364
+ it "alpha channel pass to hsi unchanged" do
365
+ color = colors.keys.sample
366
+ alphas.each_pair do |_hex_alpha, alpha|
367
+ rgba = colors[color][:rgb] + [alpha[:rgb]]
368
+ expect( Decolmor.rgb_to_hsi(rgba).last ).to eq alpha[:rgb]
369
+ end
370
+ end
371
+
372
+ it "you can set rounding for resulting hsi values (default = 1)" do
373
+ colors_r3.each_pair do |hex, values|
374
+ expect( Decolmor.rgb_to_hsi(values[:rgb], 3) ).to eq values[:hsi]
375
+ end
376
+ end
377
+
378
+ it "setting rounding doesn't affect alpha channel" do
379
+ color = colors.keys.sample
380
+ alphas.each_pair do |_hex_alpha, alpha|
381
+ rgba = colors[color][:rgb] + [alpha[:rgb]]
382
+ expect( Decolmor.rgb_to_hsi(rgba, 0).last ).to eq alpha[:rgb]
383
+ end
384
+ end
385
+ end
386
+
387
+ describe ".hsi_to_rgb" do
388
+ let(:colors) { FactoryBot.build(:colors) }
389
+ let(:alphas) { FactoryBot.build(:alpha) }
390
+
391
+ it "hsi converts to RGB" do
392
+ colors.each_pair do |_hex, values|
393
+ expect( Decolmor.hsi_to_rgb(values[:hsi]) ).to eq values[:rgb]
394
+ end
395
+ end
396
+
397
+ it "alpha channel pass to RGB unchanged" do
398
+ color = colors.keys.sample
399
+ alphas.each_pair do |_hex_alpha, values|
400
+ hsia = colors[color][:hsi] + [values[:rgb]]
401
+ expect( Decolmor.hsi_to_rgb(hsia).last ).to eq values[:rgb]
402
+ end
403
+ end
404
+
405
+ it "if hue == 360 we get valid values" do
406
+ rgb_int = [255, 11, 13]
407
+ # the resulting value from hsi with rounding 0 to an integer (hue will be 360)
408
+ # as 360/60 does not get_rgb_point in any of the ranges 0...1 or 5...6
409
+ # so in the method we use (hue % 360)
410
+ # at the same time solving if hue is not in the 0..360 range
411
+ # the fact that rgb_int and rgb_out differ is normal,
412
+ # because with integer HSI values its impossible to losslessly convert back to RGB
413
+ rgb_out = [253, 11, 11]
414
+ hsi = Decolmor.rgb_to_hsi(rgb_int, 0)
415
+ expect( Decolmor.hsi_to_rgb(hsi) ).to eq rgb_out
291
416
  end
292
417
  end
293
418
  end
@@ -296,83 +421,83 @@ RSpec.describe Decolmor do
296
421
  context 'HSL <==> HSV (HSB)' do
297
422
  describe ".hsl_to_hsv" do
298
423
  # as for lossless conversion need to use float value with 2 decimal places
299
- let(:colors) { FactoryBot.build(:colors_map, round: 2) }
424
+ let(:colors) { FactoryBot.build(:colors, round: 2) }
300
425
  let(:alphas) { FactoryBot.build(:alpha) }
301
426
 
302
427
  it "HSL converts to HSV" do
303
428
  colors.each_pair do |hex_, values|
304
429
  hsv = values[:hsv].map { |value| value.round(1) }
305
- expect( Decolmor::hsl_to_hsv(values[:hsl]) ).to eq hsv
430
+ expect( Decolmor.hsl_to_hsv(values[:hsl]) ).to eq hsv
306
431
  end
307
432
  end
308
433
 
309
434
  it "alpha channel pass to HSV unchanged" do
310
435
  color = colors.keys.sample
311
- alphas.each_pair do |hex_, alpha|
436
+ alphas.each_pair do |_hex_alpha, alpha|
312
437
  hsla = colors[color][:hsl] + [alpha[:rgb]]
313
- expect( Decolmor::hsl_to_hsv(hsla).last ).to eq alpha[:rgb]
438
+ expect( Decolmor.hsl_to_hsv(hsla).last ).to eq alpha[:rgb]
314
439
  end
315
440
  end
316
441
 
317
442
  it "you can set rounding for resulting HSV values (default = 1)" do
318
- colors.each_pair do |hex, values|
319
- expect( Decolmor::hsl_to_hsv(values[:hsl], 0) ).to eq values[:hsv].map(&:round)
443
+ colors.each_pair do |_hex, values|
444
+ expect( Decolmor.hsl_to_hsv(values[:hsl], 0) ).to eq values[:hsv].map(&:round)
320
445
  end
321
446
  end
322
447
 
323
448
  it "setting rounding doesn't affect alpha channel" do
324
449
  color = colors.keys.sample
325
- alphas.each_pair do |hex_, alpha|
450
+ alphas.each_pair do |_hex_alpha, alpha|
326
451
  hsla = colors[color][:hsl] + [alpha[:rgb]]
327
- expect( Decolmor::hsl_to_hsv(hsla, 0).last ).to eq alpha[:rgb]
452
+ expect( Decolmor.hsl_to_hsv(hsla, 0).last ).to eq alpha[:rgb]
328
453
  end
329
454
  end
330
455
  end
331
456
 
332
457
  describe ".hsl_to_hsb" do
333
458
  it "alias .hsl_to_hsv" do
334
- expect( subject.method(:hsl_to_hsb).original_name ).to eq(:hsl_to_hsv)
459
+ expect( Decolmor.method(:hsl_to_hsb).original_name ).to eq(:hsl_to_hsv)
335
460
  end
336
461
  end
337
462
 
338
463
  describe ".hsv_to_hsl" do
339
464
  # as for lossless conversion need to use float value with 2 decimal places
340
- let(:colors) { FactoryBot.build(:colors_map, round: 2) }
465
+ let(:colors) { FactoryBot.build(:colors, round: 2) }
341
466
  let(:alphas) { FactoryBot.build(:alpha) }
342
467
 
343
468
  it "HSV converts to HSL" do
344
- colors.each_pair do |hex_, values|
469
+ colors.each_pair do |_hex, values|
345
470
  hsl = values[:hsl].map { |value| value.round(1) }
346
- expect( Decolmor::hsv_to_hsl(values[:hsv]) ).to eq hsl
471
+ expect( Decolmor.hsv_to_hsl(values[:hsv]) ).to eq hsl
347
472
  end
348
473
  end
349
474
 
350
475
  it "alpha channel pass to HSL unchanged" do
351
476
  color = colors.keys.sample
352
- alphas.each_pair do |hex_, alpha|
477
+ alphas.each_pair do |_hex_alpha, alpha|
353
478
  hsva = colors[color][:hsv] + [alpha[:rgb]]
354
- expect( Decolmor::hsv_to_hsl(hsva).last ).to eq alpha[:rgb]
479
+ expect( Decolmor.hsv_to_hsl(hsva).last ).to eq alpha[:rgb]
355
480
  end
356
481
  end
357
482
 
358
483
  it "you can set rounding for resulting HSL values (default = 1)" do
359
- colors.each_pair do |hex, values|
360
- expect( Decolmor::hsv_to_hsl(values[:hsv], 0) ).to eq values[:hsl].map(&:round)
484
+ colors.each_pair do |_hex, values|
485
+ expect( Decolmor.hsv_to_hsl(values[:hsv], 0) ).to eq values[:hsl].map(&:round)
361
486
  end
362
487
  end
363
488
 
364
489
  it "setting rounding doesn't affect alpha channel" do
365
490
  color = colors.keys.sample
366
- alphas.each_pair do |hex_, alpha|
491
+ alphas.each_pair do |_hex, alpha|
367
492
  hsva = colors[color][:hsv] + [alpha[:rgb]]
368
- expect( Decolmor::hsv_to_hsl(hsva, 0).last ).to eq alpha[:rgb]
493
+ expect( Decolmor.hsv_to_hsl(hsva, 0).last ).to eq alpha[:rgb]
369
494
  end
370
495
  end
371
496
  end
372
497
 
373
498
  describe ".hsb_to_hsl" do
374
499
  it "alias .hsv_to_hsl" do
375
- expect( subject.method(:hsb_to_hsl).original_name ).to eq(:hsv_to_hsl)
500
+ expect( Decolmor.method(:hsb_to_hsl).original_name ).to eq(:hsv_to_hsl)
376
501
  end
377
502
  end
378
503
  end
@@ -380,59 +505,217 @@ RSpec.describe Decolmor do
380
505
 
381
506
  context 'RGB(A) <==> CMYK' do
382
507
  describe ".rgb_to_cmyk" do
383
- let(:colors) { FactoryBot.build(:colors_map) }
508
+ let(:colors) { FactoryBot.build(:colors) }
384
509
  let(:alphas) { FactoryBot.build(:alpha) }
385
510
 
386
511
  it "RGB converts to CMYK" do
387
512
  colors.each_pair do |hex_, values|
388
513
 
389
514
  cmyk = values[:cmyk].map {|arr| arr.round(1) }
390
- expect( Decolmor::rgb_to_cmyk(values[:rgb]) ).to eq cmyk
515
+ expect( Decolmor.rgb_to_cmyk(values[:rgb]) ).to eq cmyk
391
516
  end
392
517
  end
393
518
 
394
519
  it "alpha channel pass to HSL unchanged" do
395
520
  color = colors.keys.sample
396
- alphas.each_pair do |hex_, alpha|
521
+ alphas.each_pair do |_hex_alpha, alpha|
397
522
  rgba = colors[color][:rgb] + [alpha[:rgb]]
398
- expect( Decolmor::rgb_to_cmyk(rgba).last ).to eq alpha[:rgb]
523
+ expect( Decolmor.rgb_to_cmyk(rgba).last ).to eq alpha[:rgb]
399
524
  end
400
525
  end
401
526
 
402
527
  it "you can set rounding for resulting CMYK values (default = 1)" do
403
528
  colors.each_pair do |hex, values|
404
- expect( Decolmor::hsv_to_hsl(values[:hsv], 0) ).to eq values[:hsl].map(&:round)
529
+ expect( Decolmor.hsv_to_hsl(values[:hsv], 0) ).to eq values[:hsl].map(&:round)
405
530
  end
406
531
  end
407
532
 
408
533
  it "setting rounding doesn't affect alpha channel" do
409
534
  color = colors.keys.sample
410
- alphas.each_pair do |hex_, alpha|
535
+ alphas.each_pair do |_hex_alpha, alpha|
411
536
  rgba = colors[color][:rgb] + [alpha[:rgb]]
412
- expect( Decolmor::rgb_to_cmyk(rgba, 0).last ).to eq alpha[:rgb]
537
+ expect( Decolmor.rgb_to_cmyk(rgba, 0).last ).to eq alpha[:rgb]
413
538
  end
414
539
  end
415
540
  end
416
541
 
417
542
  describe ".cmyk_to_rgb" do
418
- let(:colors) { FactoryBot.build(:colors_map) }
543
+ let(:colors) { FactoryBot.build(:colors) }
419
544
  let(:alphas) { FactoryBot.build(:alpha) }
420
545
 
421
546
  it "CMYK converts to RGB" do
422
547
  colors.each_pair do |hex_, values|
423
- expect( Decolmor::cmyk_to_rgb(values[:cmyk]) ).to eq values[:rgb]
548
+ expect( Decolmor.cmyk_to_rgb(values[:cmyk]) ).to eq values[:rgb]
424
549
  end
425
550
  end
426
551
 
427
552
  it "alpha channel pass to RGB unchanged" do
428
553
  color = colors.keys.sample
429
- alphas.each_pair do |hex_, values|
554
+ alphas.each_pair do |_hex_alpha, values|
430
555
  cmyka = colors[color][:cmyk] + [values[:rgb]]
431
- expect( Decolmor::cmyk_to_rgb(cmyka).last ).to eq values[:rgb]
556
+ expect( Decolmor.cmyk_to_rgb(cmyka).last ).to eq values[:rgb]
432
557
  end
433
558
  end
434
559
  end
435
560
  end
436
561
 
437
- end
438
562
 
563
+ context 'HEX <==> HSL/HSV/HSB/HSI/CMYK' do
564
+ describe ".hex_to_hsl" do
565
+ let(:color) { FactoryBot.build(:colors, :one) }
566
+
567
+ it "call hex_to_rgb & rgb_to_hsl" do
568
+ hex, values = *color.first
569
+ expect(Decolmor).to receive(:hex_to_rgb).with(hex, {alpha_255: false}).and_return(values[:rgb])
570
+ expect(Decolmor).to receive(:rgb_to_hsl).with(values[:rgb], Decolmor.hsx_round)
571
+ Decolmor.hex_to_hsl(hex)
572
+ end
573
+
574
+ it "w additional args (rounding, alpha_255) call hex_to_rgb & rgb_to_hsl" do
575
+ hex, values = *color.first
576
+ expect(Decolmor).to receive(:hex_to_rgb).with(hex, {alpha_255: true}).and_return(values[:rgb])
577
+ expect(Decolmor).to receive(:rgb_to_hsl).with(values[:rgb], 2)
578
+ Decolmor.hex_to_hsl(hex, 2, alpha_255: true)
579
+ end
580
+ end
581
+
582
+ describe ".hsl_to_hex" do
583
+ let(:color) { FactoryBot.build(:colors, :one) }
584
+
585
+ it "call hsl_to_rgb & rgb_to_hex" do
586
+ values = color.first.last
587
+ expect(Decolmor).to receive(:hsl_to_rgb).with(values[:hsl]).and_return(values[:rgb])
588
+ expect(Decolmor).to receive(:rgb_to_hex).with(values[:rgb], {alpha_255: false})
589
+ Decolmor.hsl_to_hex(values[:hsl])
590
+ end
591
+
592
+ it "w additional args (rounding, alpha_255) call hsl_to_rgb & rgb_to_hex" do
593
+ _hex, values = color.first
594
+ expect(Decolmor).to receive(:hsl_to_rgb).with(values[:hsl]).and_return(values[:rgb])
595
+ expect(Decolmor).to receive(:rgb_to_hex).with(values[:rgb], {alpha_255: true})
596
+ Decolmor.hsl_to_hex(values[:hsl], alpha_255: true)
597
+ end
598
+ end
599
+
600
+ describe ".hex_to_hsv" do
601
+ let(:color) { FactoryBot.build(:colors, :one) }
602
+
603
+ it "call hex_to_rgb & rgb_to_hsv" do
604
+ hex, values = *color.first
605
+ expect(Decolmor).to receive(:hex_to_rgb).with(hex, {alpha_255: false}).and_return(values[:rgb])
606
+ expect(Decolmor).to receive(:rgb_to_hsv).with(values[:rgb], Decolmor.hsx_round)
607
+ Decolmor.hex_to_hsv(hex)
608
+ end
609
+
610
+ it "w additional args (rounding, alpha_255) call hex_to_rgb & rgb_to_hsv" do
611
+ hex, values = *color.first
612
+ expect(Decolmor).to receive(:hex_to_rgb).with(hex, {alpha_255: true}).and_return(values[:rgb])
613
+ expect(Decolmor).to receive(:rgb_to_hsv).with(values[:rgb], 2)
614
+ Decolmor.hex_to_hsv(hex, 2, alpha_255: true)
615
+ end
616
+ end
617
+
618
+ describe ".hsv_to_hex" do
619
+ let(:color) { FactoryBot.build(:colors, :one) }
620
+
621
+ it "call hsv_to_rgb & rgb_to_hex" do
622
+ values = color.first.last
623
+ expect(Decolmor).to receive(:hsv_to_rgb).with(values[:hsv]).and_return(values[:rgb])
624
+ expect(Decolmor).to receive(:rgb_to_hex).with(values[:rgb], {alpha_255: false})
625
+ Decolmor.hsv_to_hex(values[:hsv])
626
+ end
627
+
628
+ it "w additional args (rounding, alpha_255) call hsv_to_rgb & rgb_to_hex" do
629
+ values = color.first.last
630
+ expect(Decolmor).to receive(:hsv_to_rgb).with(values[:hsv]).and_return(values[:rgb])
631
+ expect(Decolmor).to receive(:rgb_to_hex).with(values[:rgb], {alpha_255: true})
632
+ Decolmor.hsv_to_hex(values[:hsv], alpha_255: true)
633
+ end
634
+ end
635
+
636
+ describe ".hex_to_hsb" do
637
+ it "alias .rgb_to_hsv" do
638
+ expect( Decolmor.method(:hex_to_hsb ).original_name).to eq(:hex_to_hsv)
639
+ end
640
+ end
641
+
642
+ describe ".hsb_to_hex" do
643
+ it "alias .rgb_to_hsv" do
644
+ expect( Decolmor.method(:hsb_to_hex ).original_name).to eq(:hsv_to_hex)
645
+ end
646
+ end
647
+
648
+ describe ".hex_to_hsi" do
649
+ let(:color) { FactoryBot.build(:colors, :one) }
650
+
651
+ it "call hex_to_rgb & rgb_to_hsi" do
652
+ hex, values = *color.first
653
+ expect(Decolmor).to receive(:hex_to_rgb).with(hex, {alpha_255: false}).and_return(values[:rgb])
654
+ expect(Decolmor).to receive(:rgb_to_hsi).with(values[:rgb], Decolmor.hsx_round)
655
+ Decolmor.hex_to_hsi(hex)
656
+ end
657
+
658
+ it "w additional args (rounding, alpha_255) call hex_to_rgb & rgb_to_hsi" do
659
+ hex, values = *color.first
660
+ expect(Decolmor).to receive(:hex_to_rgb).with(hex, {alpha_255: true}).and_return(values[:rgb])
661
+ expect(Decolmor).to receive(:rgb_to_hsi).with(values[:rgb], 2)
662
+ Decolmor.hex_to_hsi(hex, 2, alpha_255: true)
663
+ end
664
+ end
665
+
666
+ describe ".hsi_to_hex" do
667
+ let(:color) { FactoryBot.build(:colors, :one) }
668
+
669
+ it "call hsi_to_rgb & rgb_to_hex" do
670
+ values = color.first.last
671
+ expect(Decolmor).to receive(:hsi_to_rgb).with(values[:hsi]).and_return(values[:rgb])
672
+ expect(Decolmor).to receive(:rgb_to_hex).with(values[:rgb], {alpha_255: false})
673
+ Decolmor.hsi_to_hex(values[:hsi])
674
+ end
675
+
676
+ it "w additional args (rounding, alpha_255) call hsi_to_rgb & rgb_to_hex" do
677
+ values = color.first.last
678
+ expect(Decolmor).to receive(:hsi_to_rgb).with(values[:hsi]).and_return(values[:rgb])
679
+ expect(Decolmor).to receive(:rgb_to_hex).with(values[:rgb], {alpha_255: true})
680
+ Decolmor.hsi_to_hex(values[:hsi], alpha_255: true)
681
+ end
682
+ end
683
+
684
+ describe ".hex_to_cmyk" do
685
+ let(:color) { FactoryBot.build(:colors, :one) }
686
+
687
+ it "call hex_to_rgb & rgb_to_cmyk" do
688
+ hex, values = *color.first
689
+ expect(Decolmor).to receive(:hex_to_rgb).with(hex, {alpha_255: false}).and_return(values[:rgb])
690
+ expect(Decolmor).to receive(:rgb_to_cmyk).with(values[:rgb], Decolmor.hsx_round)
691
+ Decolmor.hex_to_cmyk(hex)
692
+ end
693
+
694
+ it "w additional args (rounding, alpha_255) call hex_to_rgb & rgb_to_cmyk" do
695
+ hex, values = *color.first
696
+ expect(Decolmor).to receive(:hex_to_rgb).with(hex, {alpha_255: true}).and_return(values[:rgb])
697
+ expect(Decolmor).to receive(:rgb_to_cmyk).with(values[:rgb], 2)
698
+ Decolmor.hex_to_cmyk(hex, 2, alpha_255: true)
699
+ end
700
+ end
701
+
702
+ describe ".cmyk_to_hex" do
703
+ let(:color) { FactoryBot.build(:colors, :one) }
704
+
705
+ it "call cmyk_to_rgb & rgb_to_hex" do
706
+ values = color.first.last
707
+ expect(Decolmor).to receive(:cmyk_to_rgb).with(values[:cmyk]).and_return(values[:rgb])
708
+ expect(Decolmor).to receive(:rgb_to_hex).with(values[:rgb], {alpha_255: false})
709
+ Decolmor.cmyk_to_hex(values[:cmyk])
710
+ end
711
+
712
+ it "w additional args (rounding, alpha_255) call cmyk_to_rgb & rgb_to_hex" do
713
+ values = color.first.last
714
+ expect(Decolmor).to receive(:cmyk_to_rgb).with(values[:cmyk]).and_return(values[:rgb])
715
+ expect(Decolmor).to receive(:rgb_to_hex).with(values[:rgb], {alpha_255: true})
716
+ Decolmor.cmyk_to_hex(values[:cmyk], alpha_255: true)
717
+ end
718
+ end
719
+ end
720
+
721
+ end