string_dot_gradient 0.3.3 → 0.4.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: 06603caa967d156eea0acf41986eddbd09f651a4f21d64da62edbaa029b878b9
4
- data.tar.gz: f5d3c0fbc4b9bfc9caf61ea56b2f4c0518a457ce90a35c9c1425376c5a707963
3
+ metadata.gz: edc1903ceea6827357c02ef0efdea7dcf1de66b9e6487f61b43ab01932eae822
4
+ data.tar.gz: 561e461b724a013786533e03ce752756f224cc8d6aaee6f064ace18ccf7a1e11
5
5
  SHA512:
6
- metadata.gz: a03febddbf80cb165467b627e5f91709626d4f1dd0e56d84e1c023f41b450971d6043bddc408ce7a5bba774a3f0f3151d52112598dcefbcb58d1568d6f5836b4
7
- data.tar.gz: 9908f51246f7fbf885fa94e4499f0684241cb4159110fb472adfdf8e545251dae8bc8b9628e770a9478ad9217255c71b81f61163d98616ef49ad9494fd14ea5e
6
+ metadata.gz: c072842423ac1aadda79d3c445c6a0e7b347b5ddc67c62ee2a6a9195571e6c23b82485014e514af9d8fb363ad82ab411dd7d71568744cc90463845968f407874
7
+ data.tar.gz: c302d7acbfca9b35d182fe8b2a0406d6882487cc242b18c2d050b600ea9d087a49f2c9bb85db580c5d91b7207e6f193c4543e142a8700596c099888395353f22
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "string_dot_gradient/version"
3
+ require_relative 'string_dot_gradient/version'
4
+ require 'string_dot_gradient/hex_to_rgb.rb'
4
5
  require 'string_dot_gradient/gradient.rb'
@@ -58,6 +58,8 @@ class String
58
58
  overline: false
59
59
  )
60
60
 
61
+ space, tab = ?\s.freeze, ?\t.freeze
62
+
61
63
  block_given = block_given?
62
64
  temp = ''
63
65
  flatten_colours = arg_colours.flatten
@@ -87,7 +89,7 @@ class String
87
89
 
88
90
  all_rgbs = flatten_colours.map!(&method(:hex_to_rgb))
89
91
 
90
- yield style if block_given
92
+ yield style if block_given && style
91
93
 
92
94
  # r, g, b => starting r, g, b
93
95
  # r2, g2, b2 => stopping r, g, b
@@ -109,6 +111,11 @@ class String
109
111
  n_variable = 1 if n_variable < 1
110
112
 
111
113
  # colour operator, colour value
114
+ #
115
+ # r_op, g_op, b_op are also flags to determine
116
+ # if the r, g, and b values respectively should change or not
117
+ # For example, if the given blues are equal, the b_op is nil
118
+ # So it won't change the colour in the ouput
112
119
  r_op = r_val = nil
113
120
  g_op = g_val = nil
114
121
  b_op = b_val = nil
@@ -140,7 +147,7 @@ class String
140
147
  while (i += 1) < len
141
148
  _c = c[i]
142
149
 
143
- if !exclude_spaces || (exclude_spaces && !(_c == ?\s.freeze || _c == ?\t.freeze))
150
+ if !exclude_spaces || (_c != space && _c != tab)
144
151
  _r = _r.send(r_op, r_val) if r_op
145
152
  _g = _g.send(g_op, g_val) if g_op
146
153
  _b = _b.send(b_op, b_val) if b_op
@@ -150,11 +157,11 @@ class String
150
157
  g_to_i = _g.to_i
151
158
  b_to_i = _b.to_i
152
159
 
153
- f_r = r_to_i < 0 ? 0 : r_to_i > 255 ? 255 : r_to_i
154
- f_g = g_to_i < 0 ? 0 : g_to_i > 255 ? 255 : g_to_i
155
- f_b = b_to_i < 0 ? 0 : b_to_i > 255 ? 255 : b_to_i
160
+ clamped_r = r_to_i < 0 ? 0 : r_to_i > 255 ? 255 : r_to_i
161
+ clamped_g = g_to_i < 0 ? 0 : g_to_i > 255 ? 255 : g_to_i
162
+ clamped_b = b_to_i < 0 ? 0 : b_to_i > 255 ? 255 : b_to_i
156
163
 
157
- ret = "\e[#{init};2;#{f_r};#{f_g};#{f_b}m#{_c}"
164
+ ret = "\e[#{init};2;#{clamped_r};#{clamped_g};#{clamped_b}m#{_c}"
158
165
 
159
166
  if block_given
160
167
  yield ret
@@ -264,36 +271,4 @@ class String
264
271
 
265
272
  block_given ? nil : ret
266
273
  end
267
-
268
- private
269
- def hex_to_rgb(hex)
270
- # Duplicate colour, even if colour is nil
271
- # This workaround is for Ruby 2.0 to Ruby 2.2
272
- # Which won't allow duplicate nil.
273
- colour = hex && hex.dup.to_s || ''
274
- colour.strip!
275
- colour.downcase!
276
- colour[0] = ''.freeze if colour[0] == ?#.freeze
277
-
278
- # out of range
279
- oor = colour.scan(/[^a-f0-9]/)
280
-
281
- unless oor.empty?
282
- invalids = colour.chars.map { |x|
283
- oor.include?(x) ? "\e[1;31m#{x}\e[0m" : x
284
- }.join
285
-
286
- raise ArgumentError, "\e[0mHex Colour \e[1m##{invalids} is Out of Range\e[0m"
287
- end
288
-
289
- clen = colour.length
290
- if clen == 3
291
- colour.chars.map { |x| x.<<(x).to_i(16) }
292
- elsif clen == 6
293
- colour.chars.each_slice(2).map { |x| x.join.to_i(16) }
294
- else
295
- sli = clen > 6 ? 'too long'.freeze : clen < 3 ? 'too short'.freeze : 'invalid'.freeze
296
- raise ArgumentError, "Invalid Hex Colour ##{colour} (length #{sli})"
297
- end
298
- end
299
274
  end
@@ -0,0 +1,53 @@
1
+ class String
2
+ # Fast conversion to RGB when Integer is passed.
3
+ # For example: 0xffffff for white,
4
+ # 0x000000 or 0x0 for black, 0x00aa00 for deep green
5
+ # 0xff50a6 for pink, 0xff5555 for light red, etc.
6
+ #
7
+ # Similarly:
8
+ # (255 * 256 * 256) + (255 * 256) + (255) => 0xffffff
9
+ # (0 * 256 * 256) + (0 * 256) + 0 => 0x0
10
+ # (255 * 256 * 256) + (85 * 256) + 85 => #ff5555
11
+ # (85 * 256 * 256) + (85 * 256) + 255 => #5555ff
12
+ # (255 * 256 * 256) + (170 * 256) + 0 => 0xffaa00
13
+ # (0 * 256 * 256) + (170 * 256) + 0 => 0x00aa00
14
+ def hex_to_rgb(hex)
15
+ return [
16
+ 255 & hex >> 16,
17
+ 255 & hex >> 8,
18
+ 255 & hex
19
+ ] if hex.is_a?(Integer)
20
+
21
+ # Duplicate colour, even if colour is nil
22
+ # This workaround is for Ruby 2.0 to Ruby 2.2
23
+ # Which won't allow duplicate nil.
24
+ colour = hex && hex.dup.to_s || ''
25
+ colour.strip!
26
+ colour.downcase!
27
+ colour[0] = ''.freeze if colour[0] == ?#.freeze
28
+
29
+ # out of range
30
+ oor = colour.scan(/[^a-f0-9]/)
31
+
32
+ unless oor.empty?
33
+ invalids = colour.chars.map { |x|
34
+ oor.include?(x) ? "\e[1;31m#{x}\e[0m" : x
35
+ }.join
36
+
37
+ raise ArgumentError, "\e[0mHex Colour \e[1m##{invalids} is Out of Range\e[0m"
38
+ end
39
+
40
+ clen = colour.length
41
+
42
+ if clen == 3
43
+ colour.chars.map { |x| x.<<(x).to_i(16) }
44
+ elsif clen == 6
45
+ colour.chars.each_slice(2).map { |x| x.join.to_i(16) }
46
+ else
47
+ sli = clen > 6 ? 'too long'.freeze : clen < 3 ? 'too short'.freeze : 'invalid'.freeze
48
+ raise ArgumentError, "Invalid Hex Colour ##{colour} (length #{sli})"
49
+ end
50
+ end
51
+
52
+ private(:hex_to_rgb)
53
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StringDotGradient
4
- VERSION = "0.3.3"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string_dot_gradient
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sourav Goswami
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-01 00:00:00.000000000 Z
11
+ date: 2021-06-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: An itty-bitty extension that adds "gradient" method to String class that
14
14
  supports any hex colour, for Linux terminals
@@ -20,6 +20,7 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - lib/string_dot_gradient.rb
22
22
  - lib/string_dot_gradient/gradient.rb
23
+ - lib/string_dot_gradient/hex_to_rgb.rb
23
24
  - lib/string_dot_gradient/version.rb
24
25
  homepage: https://github.com/Souravgoswami/string_dot_gradient
25
26
  licenses:
@@ -40,7 +41,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
41
  - !ruby/object:Gem::Version
41
42
  version: '0'
42
43
  requirements: []
43
- rubygems_version: 3.2.13
44
+ rubygems_version: 3.2.19
44
45
  signing_key:
45
46
  specification_version: 4
46
47
  summary: An itty-bitty extension that adds "gradient" method to String class that