string_dot_gradient 0.1.0 → 0.1.1
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 +71 -15
- data/lib/string_dot_gradient/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46742b73629d3f3bfec9b12628a2ccc174548d06dce6958faa4a7d849ad13d37
|
4
|
+
data.tar.gz: 613d584f92df46117cc9bf33115b80fb655363787ba236b3f28bf7f29823fb1b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc9e51d4ec0b19c6ae6243544d550958e2ed5f31f8f838d4143fceaf82ac3e1c420382d7acfb1a6da458396f6ee3cb03b3c38c0d7020da600d80d3260420586e
|
7
|
+
data.tar.gz: 25aff2e43cf96421a01b11da8a574c71f894327852befaac4f4a4193985af7bf064e69670d573b5802277680cee556dfedf620ebbb31533dd8e80849c735960b
|
@@ -1,10 +1,50 @@
|
|
1
1
|
class String
|
2
|
-
|
2
|
+
##
|
3
|
+
# = gradient(*arg_colours, bg: false) # => 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
|
+
def gradient(*arg_colours, bg: false)
|
3
33
|
colours, line_length = [], -1
|
4
34
|
temp = ''
|
35
|
+
flatten_colours = arg_colours.flatten
|
36
|
+
|
37
|
+
raise ArgumentError, "Wrong numeber of colours (given #{flatten_colours.length}, expected minimum 2)" if flatten_colours.length < 2
|
38
|
+
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)) }
|
39
|
+
|
40
|
+
all_rgbs = flatten_colours.map!(&method(:hex_to_rgb))
|
41
|
+
|
42
|
+
block_given = block_given?
|
43
|
+
|
44
|
+
r, g, b = all_rgbs[0]
|
45
|
+
r2, g2, b2 = all_rgbs[1]
|
46
|
+
rotate = all_rgbs.length > 2
|
5
47
|
|
6
|
-
r, g, b = hex_to_rgb(colour_start)
|
7
|
-
r2, g2, b2 = hex_to_rgb(colour_stop)
|
8
48
|
init = bg ? 48 : 38
|
9
49
|
|
10
50
|
each_line do |c|
|
@@ -15,7 +55,7 @@ class String
|
|
15
55
|
g_meth = g == g2 ? :itself : g2 > g ? [:+, g2.fdiv(n)] : [:-, g.fdiv(n)]
|
16
56
|
b_meth = b == b2 ? :itself : b2 > b ? [:+, b2.fdiv(n)] : [:-, b.fdiv(n)]
|
17
57
|
|
18
|
-
if line_length != n
|
58
|
+
if line_length != n || rotate
|
19
59
|
line_length = n
|
20
60
|
colours.clear
|
21
61
|
|
@@ -35,24 +75,38 @@ class String
|
|
35
75
|
|
36
76
|
i = -1
|
37
77
|
while (i += 1) < n
|
38
|
-
|
39
|
-
"\e[#{init};2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}"
|
40
|
-
|
78
|
+
if block_given
|
79
|
+
yield "\e[#{init};2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}"
|
80
|
+
else
|
81
|
+
temp.concat(
|
82
|
+
"\e[#{init};2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}"
|
83
|
+
)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
if block_given
|
88
|
+
yield "\e[0m".freeze
|
89
|
+
else
|
90
|
+
temp << "\e[0m".freeze
|
41
91
|
end
|
42
|
-
colours.rotate! if rotate
|
43
92
|
|
44
|
-
|
93
|
+
if rotate
|
94
|
+
all_rgbs.rotate!
|
95
|
+
r, g, b = all_rgbs[0]
|
96
|
+
r2, g2, b2 = all_rgbs[1]
|
97
|
+
end
|
45
98
|
end
|
46
99
|
|
47
|
-
temp
|
100
|
+
block_given ? nil : temp
|
48
101
|
end
|
49
102
|
|
50
103
|
private
|
51
104
|
def hex_to_rgb(hex)
|
52
|
-
colour
|
105
|
+
# Duplicate colour, even if colour is nil
|
106
|
+
colour = hex && hex.dup.to_s || ''
|
53
107
|
colour.strip!
|
54
108
|
colour.downcase!
|
55
|
-
colour[0] = ''.freeze if colour
|
109
|
+
colour[0] = ''.freeze if colour[0] == ?#.freeze
|
56
110
|
|
57
111
|
# out of range
|
58
112
|
oor = colour.scan(/[^a-f0-9]/)
|
@@ -66,12 +120,14 @@ class String
|
|
66
120
|
raise ArgumentError
|
67
121
|
end
|
68
122
|
|
69
|
-
|
123
|
+
clen = colour.length
|
124
|
+
if clen == 3
|
70
125
|
colour.chars.map { |x| x.<<(x).to_i(16) }
|
71
|
-
elsif
|
126
|
+
elsif clen == 6
|
72
127
|
colour.chars.each_slice(2).map { |x| x.join.to_i(16) }
|
73
128
|
else
|
74
|
-
|
129
|
+
sli = clen > 6 ? 'too long' : clen < 3 ? 'too short' : 'invalid'
|
130
|
+
raise ArgumentError, "Invalid Hex Colour ##{colour} (length #{sli})"
|
75
131
|
end
|
76
132
|
end
|
77
133
|
end
|