string_dot_gradient 0.1.3 → 0.1.8
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 +4 -4
- data/lib/string_dot_gradient/gradient.rb +69 -57
- data/lib/string_dot_gradient/version.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f734ad48d80c57969e3fb4598f5a89fafdb35b30c2b5dddbc1bbbf1a835a829
|
4
|
+
data.tar.gz: 80c268d0c7754047bf1040d1ade34a3e9652180441ca3cbf23a0d67f8873d531
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0102ffe6acc5355cc7d29dea013439eb245fc5ec11415eea6cc91f1025a6128f639120b74d29d0dec44ebf085f73d41411d6027480b4270dc832d17e5e353cf7
|
7
|
+
data.tar.gz: d0d2c157c03af2a8688fcba1ade8d5177e0d8656512b9293a26672faf9dddb492a530e409df9e6ffa5f32f75f05d0c6c041ac13f4cc0a75c4067b4fc59bf9144
|
@@ -41,7 +41,6 @@ class String
|
|
41
41
|
# a string is uncommon, and developers are requested to delete
|
42
42
|
# \u0000 from string if such situations arise.
|
43
43
|
def gradient(*arg_colours, bg: false, exclude_spaces: true)
|
44
|
-
colours, line_length = [], -1
|
45
44
|
temp = ''
|
46
45
|
flatten_colours = arg_colours.flatten
|
47
46
|
|
@@ -49,79 +48,90 @@ class String
|
|
49
48
|
raise ArgumentError, "Given argument for colour is neither a String nor an Integer" if flatten_colours.any? { |x| !(x.is_a?(String) || x.is_a?(Integer)) }
|
50
49
|
|
51
50
|
all_rgbs = flatten_colours.map!(&method(:hex_to_rgb))
|
52
|
-
|
53
51
|
block_given = block_given?
|
54
52
|
|
55
|
-
r, g, b
|
56
|
-
r2, g2, b2
|
53
|
+
# r, g, b => starting r, g, b
|
54
|
+
# r2, g2, b2 => stopping r, g, b
|
55
|
+
r, g, b = *all_rgbs[0]
|
56
|
+
r2, g2, b2 = *all_rgbs[1]
|
57
57
|
rotate = all_rgbs.length > 2
|
58
58
|
|
59
59
|
init = bg ? 48 : 38
|
60
60
|
|
61
61
|
each_line do |c|
|
62
62
|
_r, _g, _b = r, g, b
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
_b = _b.send(*b_meth) unless _b > b_max || _b < b_min
|
86
|
-
end
|
87
|
-
else
|
88
|
-
# We also have duplication above,
|
89
|
-
# But we are not going to remove this code unless
|
90
|
-
# we find some efficient solution. Using a proc or method
|
91
|
-
# for setting these values and calling that is a way
|
92
|
-
# to make code slower.
|
93
|
-
_r = _r.send(*r_meth) unless _r > r_max || _r < r_min
|
94
|
-
_g = _g.send(*g_meth) unless _g > g_max || _g < g_min
|
95
|
-
_b = _b.send(*b_meth) unless _b > b_max || _b < b_min
|
96
|
-
end
|
97
|
-
|
98
|
-
r_to_i = _r.to_i
|
99
|
-
g_to_i = _g.to_i
|
100
|
-
b_to_i = _b.to_i
|
101
|
-
|
102
|
-
colours << [
|
103
|
-
r_to_i < 0 ? 0 : r_to_i > 255 ? 255 : r_to_i,
|
104
|
-
g_to_i < 0 ? 0 : g_to_i > 255 ? 255 : g_to_i,
|
105
|
-
b_to_i < 0 ? 0 : b_to_i > 255 ? 255 : b_to_i,
|
106
|
-
]
|
107
|
-
end
|
63
|
+
chomped = !!c.chomp!(''.freeze)
|
64
|
+
|
65
|
+
len = c.length
|
66
|
+
n_variable = exclude_spaces ? c.delete("\t\s".freeze).length : len
|
67
|
+
n_variable -= 1
|
68
|
+
n_variable = 1 if n_variable < 1
|
69
|
+
|
70
|
+
# colour operator, colour value
|
71
|
+
r_op = r_val = nil
|
72
|
+
g_op = g_val = nil
|
73
|
+
b_op = b_val = nil
|
74
|
+
|
75
|
+
if r2 > r
|
76
|
+
r_op, r_val = :+, r2.-(r).fdiv(n_variable)
|
77
|
+
elsif r2 < r
|
78
|
+
r_op, r_val = :-, r.-(r2).fdiv(n_variable)
|
79
|
+
end
|
80
|
+
|
81
|
+
if g2 > g
|
82
|
+
g_op, g_val = :+, g2.-(g).fdiv(n_variable)
|
83
|
+
elsif g2 < g
|
84
|
+
g_op, g_val = :-, g.-(g2).fdiv(n_variable)
|
108
85
|
end
|
109
86
|
|
87
|
+
if b2 > b
|
88
|
+
b_op, b_val = :+, b2.-(b).fdiv(n_variable)
|
89
|
+
elsif b2 < b
|
90
|
+
b_op, b_val = :-, b.-(b2).fdiv(n_variable)
|
91
|
+
end
|
92
|
+
|
93
|
+
# To avoid the value getting adding | subtracted from the initial character
|
94
|
+
_r = _r.send(r_op, r_val * -1) if r_op
|
95
|
+
_g = _g.send(g_op, g_val * -1) if g_op
|
96
|
+
_b = _b.send(b_op, b_val * -1) if b_op
|
97
|
+
|
110
98
|
i = -1
|
111
|
-
while (i += 1) <
|
99
|
+
while (i += 1) < len
|
100
|
+
_c = c[i]
|
101
|
+
|
102
|
+
if !exclude_spaces || (exclude_spaces && !(_c == ?\s.freeze || _c == ?\t.freeze))
|
103
|
+
_r = _r.send(r_op, r_val) if r_op
|
104
|
+
_g = _g.send(g_op, g_val) if g_op
|
105
|
+
_b = _b.send(b_op, b_val) if b_op
|
106
|
+
end
|
107
|
+
|
108
|
+
r_to_i = _r.to_i
|
109
|
+
g_to_i = _g.to_i
|
110
|
+
b_to_i = _b.to_i
|
111
|
+
|
112
|
+
f_r = r_to_i < 0 ? 0 : r_to_i > 255 ? 255 : r_to_i
|
113
|
+
f_g = g_to_i < 0 ? 0 : g_to_i > 255 ? 255 : g_to_i
|
114
|
+
f_b = b_to_i < 0 ? 0 : b_to_i > 255 ? 255 : b_to_i
|
115
|
+
|
116
|
+
ret = "\e[#{init};2;#{f_r};#{f_g};#{f_b}m#{_c}"
|
117
|
+
|
112
118
|
if block_given
|
113
|
-
yield
|
119
|
+
yield ret
|
114
120
|
else
|
115
|
-
temp
|
116
|
-
"\e[#{init};2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}"
|
117
|
-
)
|
121
|
+
temp << ret
|
118
122
|
end
|
119
123
|
end
|
120
124
|
|
125
|
+
ret = if !c.empty?
|
126
|
+
chomped ? "\e[0m\n".freeze : "\e[0m".freeze
|
127
|
+
elsif chomped
|
128
|
+
?\n.freeze
|
129
|
+
end
|
130
|
+
|
121
131
|
if block_given
|
122
|
-
yield
|
132
|
+
yield ret
|
123
133
|
else
|
124
|
-
temp <<
|
134
|
+
temp << ret
|
125
135
|
end
|
126
136
|
|
127
137
|
if rotate
|
@@ -137,6 +147,8 @@ class String
|
|
137
147
|
private
|
138
148
|
def hex_to_rgb(hex)
|
139
149
|
# Duplicate colour, even if colour is nil
|
150
|
+
# This workaround is for Ruby 2.0 to Ruby 2.2
|
151
|
+
# Which won't allow duplicate nil.
|
140
152
|
colour = hex && hex.dup.to_s || ''
|
141
153
|
colour.strip!
|
142
154
|
colour.downcase!
|
@@ -160,7 +172,7 @@ class String
|
|
160
172
|
elsif clen == 6
|
161
173
|
colour.chars.each_slice(2).map { |x| x.join.to_i(16) }
|
162
174
|
else
|
163
|
-
sli = clen > 6 ? 'too long' : clen < 3 ? 'too short' : 'invalid'
|
175
|
+
sli = clen > 6 ? 'too long'.freeze : clen < 3 ? 'too short'.freeze : 'invalid'.freeze
|
164
176
|
raise ArgumentError, "Invalid Hex Colour ##{colour} (length #{sli})"
|
165
177
|
end
|
166
178
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: string_dot_gradient
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
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-01-
|
11
|
+
date: 2021-01-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: An itty-bitty extension that adds gradient method to String class
|
14
|
-
Linux terminals
|
13
|
+
description: An itty-bitty extension that adds gradient method to String class that
|
14
|
+
supports any hex colour, for Linux | Unix terminals only
|
15
15
|
email:
|
16
16
|
- souravgoswami@protonmail.com
|
17
17
|
executables: []
|
@@ -33,7 +33,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
33
33
|
requirements:
|
34
34
|
- - ">="
|
35
35
|
- !ruby/object:Gem::Version
|
36
|
-
version: 2.
|
36
|
+
version: 2.0.0
|
37
37
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
39
|
- - ">="
|
@@ -43,6 +43,6 @@ requirements: []
|
|
43
43
|
rubygems_version: 3.1.4
|
44
44
|
signing_key:
|
45
45
|
specification_version: 4
|
46
|
-
summary: An itty-bitty extension that adds gradient method to String class
|
47
|
-
terminals
|
46
|
+
summary: An itty-bitty extension that adds gradient method to String class that supports
|
47
|
+
any hex colour, for Linux | Unix terminals only
|
48
48
|
test_files: []
|