string_dot_gradient 0.1.0 → 0.1.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.
- checksums.yaml +4 -4
- data/lib/string_dot_gradient/gradient.rb +138 -24
- 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: 107ebffe10579b889cfe5b6bd0ebbc774375674fd7e9db1998f3fc4561d86b12
|
4
|
+
data.tar.gz: 26588857b88ff2f8a732a840a3807d7d327448b55cc02eb385294e21c718d71b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c40057440f5a6d8c3d1855c3defa40e878a1aeadaefb3e7fda49fd95521135d0c3c418b10dfe3ffabb8b743e25bce3962cafebfb1b4725392619287c67f1938
|
7
|
+
data.tar.gz: 668f2212665a28aeabcc58cfd823d5a0995617f785a93d10ab6c301a714328dc057f9d3dade654a3021296a596de8fadec3dd30104681aaa3215426d89cf0493
|
@@ -1,58 +1,170 @@
|
|
1
1
|
class String
|
2
|
-
|
2
|
+
##
|
3
|
+
# = gradient(*arg_colours, bg: false, exclude_spaces: true) # => string or nil
|
4
|
+
#
|
5
|
+
# Prettifies your string by adding gradient colours.
|
6
|
+
#
|
7
|
+
# This method accept a lot of colours. For example:
|
8
|
+
#
|
9
|
+
# 1. puts "Hello".gradient('#f55', '#55f')
|
10
|
+
# This will add #f55 (red) to #55f (blue) gradient colours to your texts.
|
11
|
+
#
|
12
|
+
# 2. puts "Hello\nWorld".gradient('#f55', '#55f')
|
13
|
+
# This will add #f55 (red) to #55f (blue) gradient colours to your texts, spanning multiple lines.
|
14
|
+
#
|
15
|
+
# 3. puts "Hello\nWorld!\nColours\nare\nrotated here".gradient('f55','55f', '3eb' 'ff5')
|
16
|
+
# This will add #ff5555 (red) to #5555ff (blue) gradient colours to the first line,
|
17
|
+
# 5555ff to 33eebb colour to the 2nd line,
|
18
|
+
# 33eebb to ffff55 to the third line
|
19
|
+
# And then back to ffff55 to ff5555 to the fourth line,
|
20
|
+
# And it will continue to rotate between these colours.
|
21
|
+
#
|
22
|
+
# To stop rotating, just don't give more than two arguments to this method.
|
23
|
+
#
|
24
|
+
# Passing blocks is also optional, and is handy for animating text. For example:
|
25
|
+
# "Hello\nWorld!\nColours\nare\nrotated here".gradient('f55','55f', '3eb' 'ff5', bg:true) { |x| print x ; sleep 0.05 }
|
26
|
+
#
|
27
|
+
# This will pass the values to the block itself, and will draw the colourful text slowly.
|
28
|
+
# Passing block will return nil from the method because the values will be passed to the block variable instead.
|
29
|
+
#
|
30
|
+
# Adding the option bg will change the background colour, but will keep the foreground colour
|
31
|
+
# defined in the terminal settings.
|
32
|
+
#
|
33
|
+
# The option exclude_spaces, is expected to set either true or false.
|
34
|
+
# By default it's set to true.
|
35
|
+
# Enabling this option will not waste colours on white-spaces.
|
36
|
+
# White spaces only include: \s, \t
|
37
|
+
#
|
38
|
+
# Please do note that \u0000 and \r in the middle of the string will not be
|
39
|
+
# counted as a white space, but as a character instead.
|
40
|
+
# This is because \r wipes out the previous characters, and using \u0000 in
|
41
|
+
# a string is uncommon, and developers are requested to delete
|
42
|
+
# \u0000 from string if such situations arise.
|
43
|
+
def gradient(*arg_colours, bg: false, exclude_spaces: true)
|
3
44
|
colours, line_length = [], -1
|
4
45
|
temp = ''
|
46
|
+
flatten_colours = arg_colours.flatten
|
47
|
+
|
48
|
+
raise ArgumentError, "Wrong numeber of colours (given #{flatten_colours.length}, expected minimum 2)" if flatten_colours.length < 2
|
49
|
+
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
|
+
|
51
|
+
all_rgbs = flatten_colours.map!(&method(:hex_to_rgb))
|
52
|
+
block_given = block_given?
|
53
|
+
|
54
|
+
r, g, b = all_rgbs[0]
|
55
|
+
r2, g2, b2 = all_rgbs[1]
|
56
|
+
rotate = all_rgbs.length > 2
|
5
57
|
|
6
|
-
r, g, b = hex_to_rgb(colour_start)
|
7
|
-
r2, g2, b2 = hex_to_rgb(colour_stop)
|
8
58
|
init = bg ? 48 : 38
|
9
59
|
|
10
60
|
each_line do |c|
|
11
61
|
_r, _g, _b = r, g, b
|
12
62
|
n = c.length
|
63
|
+
n_variable = exclude_spaces ? c.dup.delete("\t\s".freeze).length : n
|
64
|
+
|
65
|
+
r_op = r_val = r_max = r_min = nil
|
66
|
+
g_op = g_val = g_max = g_min = nil
|
67
|
+
b_op = b_val = b_max = b_min = nil
|
68
|
+
|
69
|
+
r_comp_op = r_comp_val = nil
|
70
|
+
g_comp_op = g_comp_val = nil
|
71
|
+
b_comp_op = b_comp_val = nil
|
72
|
+
|
73
|
+
if r2 > r
|
74
|
+
r_op, r_val, r_max, r_min = :+, r2.fdiv(n_variable), r2, r
|
75
|
+
r_comp_op, r_comp_val = :<=, r_max
|
76
|
+
elsif r2 < r
|
77
|
+
r_op, r_val, r_max, r_min = :-, r.fdiv(n_variable), r, r2
|
78
|
+
r_comp_op, r_comp_val = :>=, r_min
|
79
|
+
end
|
80
|
+
|
81
|
+
if g2 > g
|
82
|
+
g_op, g_val, g_max, g_min = :+, g2.fdiv(n_variable), g2, g
|
83
|
+
g_comp_op, g_comp_val = :<=, g_max
|
84
|
+
elsif g2 < g
|
85
|
+
g_op, g_val, g_max, g_min = :-, g.fdiv(n_variable), g, g2
|
86
|
+
g_comp_op, g_comp_val = :>=, g_min
|
87
|
+
end
|
13
88
|
|
14
|
-
|
15
|
-
|
16
|
-
|
89
|
+
if b2 > b
|
90
|
+
b_op, b_val, b_max, b_min = :+, b2.fdiv(n_variable), b2, b
|
91
|
+
b_comp_op, b_comp_val = :<=, b_max
|
92
|
+
elsif b2 < b
|
93
|
+
b_op, b_val, b_max, b_min = :-, b.fdiv(n_variable), b, b2
|
94
|
+
b_comp_op, b_comp_val = :>=, b_min
|
95
|
+
end
|
17
96
|
|
18
|
-
if line_length != n
|
97
|
+
if line_length != n || rotate
|
19
98
|
line_length = n
|
20
99
|
colours.clear
|
21
100
|
|
22
101
|
i = -1
|
23
102
|
while (i += 1) < n
|
24
|
-
|
25
|
-
|
26
|
-
|
103
|
+
if exclude_spaces
|
104
|
+
_c = c[i]
|
105
|
+
|
106
|
+
if !(_c == ?\s.freeze || _c == ?\t.freeze)
|
107
|
+
_r = _r.send(r_op, r_val) if r_comp_op && _r.send(r_comp_op, r_comp_val)
|
108
|
+
_g = _g.send(g_op, g_val) if g_comp_op && _g.send(g_comp_op, g_comp_val)
|
109
|
+
_b = _b.send(b_op, b_val) if b_comp_op && _b.send(b_comp_op, b_comp_val)
|
110
|
+
end
|
111
|
+
else
|
112
|
+
# We also have duplication above,
|
113
|
+
# But we are not going to remove this code unless
|
114
|
+
# we find some efficient solution. Using a proc or method
|
115
|
+
# for setting these values and calling that is a way
|
116
|
+
# to make code slower.
|
117
|
+
_r = _r.send(r_op, r_val) if r_comp_op && _r.send(r_comp_op, r_comp_val)
|
118
|
+
_g = _g.send(g_op, g_val) if g_comp_op && _g.send(g_comp_op, g_comp_val)
|
119
|
+
_b = _b.send(b_op, b_val) if b_comp_op && _b.send(b_comp_op, b_comp_val)
|
120
|
+
end
|
121
|
+
|
122
|
+
r_to_i = _r.to_i
|
123
|
+
g_to_i = _g.to_i
|
124
|
+
b_to_i = _b.to_i
|
27
125
|
|
28
126
|
colours << [
|
29
|
-
|
30
|
-
|
31
|
-
|
127
|
+
r_to_i < 0 ? 0 : r_to_i > 255 ? 255 : r_to_i,
|
128
|
+
g_to_i < 0 ? 0 : g_to_i > 255 ? 255 : g_to_i,
|
129
|
+
b_to_i < 0 ? 0 : b_to_i > 255 ? 255 : b_to_i,
|
32
130
|
]
|
33
131
|
end
|
34
132
|
end
|
35
133
|
|
36
134
|
i = -1
|
37
135
|
while (i += 1) < n
|
38
|
-
|
39
|
-
"\e[#{init};2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}"
|
40
|
-
|
136
|
+
if block_given
|
137
|
+
yield "\e[#{init};2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}"
|
138
|
+
else
|
139
|
+
temp.concat(
|
140
|
+
"\e[#{init};2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}"
|
141
|
+
)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
if block_given
|
146
|
+
yield "\e[0m".freeze
|
147
|
+
else
|
148
|
+
temp << "\e[0m".freeze
|
41
149
|
end
|
42
|
-
colours.rotate! if rotate
|
43
150
|
|
44
|
-
|
151
|
+
if rotate
|
152
|
+
all_rgbs.rotate!
|
153
|
+
r, g, b = all_rgbs[0]
|
154
|
+
r2, g2, b2 = all_rgbs[1]
|
155
|
+
end
|
45
156
|
end
|
46
157
|
|
47
|
-
temp
|
158
|
+
block_given ? nil : temp
|
48
159
|
end
|
49
160
|
|
50
161
|
private
|
51
162
|
def hex_to_rgb(hex)
|
52
|
-
colour
|
163
|
+
# Duplicate colour, even if colour is nil
|
164
|
+
colour = hex && hex.dup.to_s || ''
|
53
165
|
colour.strip!
|
54
166
|
colour.downcase!
|
55
|
-
colour[0] = ''.freeze if colour
|
167
|
+
colour[0] = ''.freeze if colour[0] == ?#.freeze
|
56
168
|
|
57
169
|
# out of range
|
58
170
|
oor = colour.scan(/[^a-f0-9]/)
|
@@ -66,12 +178,14 @@ class String
|
|
66
178
|
raise ArgumentError
|
67
179
|
end
|
68
180
|
|
69
|
-
|
181
|
+
clen = colour.length
|
182
|
+
if clen == 3
|
70
183
|
colour.chars.map { |x| x.<<(x).to_i(16) }
|
71
|
-
elsif
|
184
|
+
elsif clen == 6
|
72
185
|
colour.chars.each_slice(2).map { |x| x.join.to_i(16) }
|
73
186
|
else
|
74
|
-
|
187
|
+
sli = clen > 6 ? 'too long'.freeze : clen < 3 ? 'too short'.freeze : 'invalid'.freeze
|
188
|
+
raise ArgumentError, "Invalid Hex Colour ##{colour} (length #{sli})"
|
75
189
|
end
|
76
190
|
end
|
77
191
|
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.5
|
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-09 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: []
|