string_dot_gradient 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8dd54d9133693ce1125e621ea28e03b0fcc0e8229885be3a6853777470c89416
4
+ data.tar.gz: 3d4e18250a1b7bfd787f0e5778ade008ac5ff95d94ea38e48cb9632d38a90037
5
+ SHA512:
6
+ metadata.gz: 20c9b1a12caf24baa79eb704652c67836bca3497494696bb00d746baf1f3900c5cef33abc4d2e381315ac731541053dda1c871e96d049190fecddbd9a5896b99
7
+ data.tar.gz: 96b9d15b159911df728f641f35bf41b64744701fc8226ffa5e3f3ba7950a06998d6ce7ff875a247a4808418ec53d9c6104abf367a7167bdf4f64231acf9b3964
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "string_dot_gradient/version"
4
+ require 'string_dot_gradient/gradient.rb'
@@ -0,0 +1,77 @@
1
+ class String
2
+ def gradient(colour_start, colour_stop, bg: false, rotate: false)
3
+ colours, line_length = [], -1
4
+ temp = ''
5
+
6
+ r, g, b = hex_to_rgb(colour_start)
7
+ r2, g2, b2 = hex_to_rgb(colour_stop)
8
+ init = bg ? 48 : 38
9
+
10
+ each_line do |c|
11
+ _r, _g, _b = r, g, b
12
+ n = c.length
13
+
14
+ r_meth = r == r2 ? :itself : r2 > r ? [:+, r2.fdiv(n)] : [:-, r.fdiv(n)]
15
+ g_meth = g == g2 ? :itself : g2 > g ? [:+, g2.fdiv(n)] : [:-, g.fdiv(n)]
16
+ b_meth = b == b2 ? :itself : b2 > b ? [:+, b2.fdiv(n)] : [:-, b.fdiv(n)]
17
+
18
+ if line_length != n
19
+ line_length = n
20
+ colours.clear
21
+
22
+ i = -1
23
+ while (i += 1) < n
24
+ _r = _r.send(*r_meth)
25
+ _g = _g.send(*g_meth)
26
+ _b = _b.send(*b_meth)
27
+
28
+ colours << [
29
+ (_r < 0 ? 0 : _r > 255 ? 255 : _r.to_i),
30
+ (_g < 0 ? 0 : _g > 255 ? 255 : _g.to_i),
31
+ _b < 0 ? 0 : _b > 255 ? 255 : _b.to_i
32
+ ]
33
+ end
34
+ end
35
+
36
+ i = -1
37
+ while (i += 1) < n
38
+ temp.concat(
39
+ "\e[#{init};2;#{colours[i][0]};#{colours[i][1]};#{colours[i][2]}m#{c[i]}"
40
+ )
41
+ end
42
+ colours.rotate! if rotate
43
+
44
+ temp << "\e[0m".freeze
45
+ end
46
+
47
+ temp
48
+ end
49
+
50
+ private
51
+ def hex_to_rgb(hex)
52
+ colour = hex.dup.to_s
53
+ colour.strip!
54
+ colour.downcase!
55
+ colour[0] = ''.freeze if colour.start_with?(?#.freeze)
56
+
57
+ # out of range
58
+ oor = colour.scan(/[^a-f0-9]/)
59
+
60
+ unless oor.empty?
61
+ invalids = colour.chars.map { |x|
62
+ oor.include?(x) ? "\e[1;31m#{x}\e[0m" : x
63
+ }.join
64
+
65
+ puts "Hex Colour ##{invalids} is Out of Range"
66
+ raise ArgumentError
67
+ end
68
+
69
+ if colour.length == 3
70
+ colour.chars.map { |x| x.<<(x).to_i(16) }
71
+ elsif colour.length == 6
72
+ colour.chars.each_slice(2).map { |x| x.join.to_i(16) }
73
+ else
74
+ raise ArgumentError, "Invalid Hex Colour ##{colour}"
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module StringDotGradient
4
+ VERSION = "0.1.0"
5
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: string_dot_gradient
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sourav Goswami
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-07 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: An itty-bitty extension that adds gradient method to String class, for
14
+ Linux terminals
15
+ email:
16
+ - souravgoswami@protonmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/string_dot_gradient.rb
22
+ - lib/string_dot_gradient/gradient.rb
23
+ - lib/string_dot_gradient/version.rb
24
+ homepage: https://github.com/Souravgoswami/string_dot_gradient
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 2.2.0
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubygems_version: 3.1.4
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: An itty-bitty extension that adds gradient method to String class, for Linux
47
+ terminals
48
+ test_files: []