ombre 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +54 -0
  3. data/lib/ombre.rb +56 -16
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd24e51a4a0474d903fd3b012848fdcaa3cec7e5e07d5b35cddce11a7b190d1d
4
- data.tar.gz: 6abacfc82cfd70b3c7117e0b6ff521aa2d167ebb184826e12fe6fac1c7df18d7
3
+ metadata.gz: 0d8a4ddbfcbaa0f408f2bfc24ef443a72c8c66dd4167e2be8d68981ac8b29838
4
+ data.tar.gz: b69094d4367a287bf35a058b8c9b32f8b4af026a92a49045fb5cdc3a87e820b7
5
5
  SHA512:
6
- metadata.gz: d363322c1244e03111d7e941a7b51281e41b0e9e5e0ed1a54156ea9435dcdbac19213878da9b13da117b92378d6752e7bcd7218e580ff28d70a3804fc0c9202f
7
- data.tar.gz: 8896b50a283742f8fd34ebfb522dce8ee2eabecb4bdbf09a6690bb4eef69f8df388d6a4d8a70f55183889458ca0b28c6bf4ba2a5c736979105ad6567f5c83623
6
+ metadata.gz: 30326d68bfea689327dbb6852fb4cd46fd1cbe84a1fb5e422b58771eb402fccb5c7206116a05c5e6866883be4560bb3249c9e2b167c6613f0273d67cdad86b0d
7
+ data.tar.gz: 1084d2fecaca0e70de079434a92a54e68f52aa3068e2ab8999c9909f0784bd10113ecc0a0ed431643e8c60a8d5ff652597ce6ad8b96dec258fbbe8d508697f53
data/README.md CHANGED
@@ -0,0 +1,54 @@
1
+ # Ruby Ombre
2
+
3
+ Ombre provides gradients for your terminal between any number of RGB values.
4
+
5
+ ## Features
6
+
7
+ * Horizontal, vertical, and diagonal gradients for any number of colors.
8
+ ## True Color Support
9
+
10
+ This will only look good on terminals that support 24bit colors, for now.
11
+
12
+ ## Setup
13
+
14
+ Add to `Gemfile`:
15
+
16
+ ```ruby
17
+ gem 'ombre'
18
+ ```
19
+
20
+ and run `bundle install`.
21
+
22
+ In Ruby do:
23
+
24
+ ```ruby
25
+ require 'ombre'
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ There are four main functions available for `Ombre`:
31
+
32
+ ```ruby
33
+ # colors in the below functions should be an array of 24 bit hexidecimal RGB values as strings.
34
+
35
+ # Returns the block of text marked up with colors to create a horizontal gradient from left to right
36
+ Ombre.horizontal(text, colors)
37
+ Ombre.horizontal("############\n# #\n# Horiz. ###\n# Ombre ####\n# Test #####\n# From #####\n# Red ######\n# To #######\n# Blue #####\n# #\n############", ["FF0000", "0000FF"])
38
+
39
+ # Returns the block of text marked up with colors to create a vertical gradient from top to bottom
40
+ Ombre.vertical(text, colors)
41
+ Ombre.vertical("############\n# #\n# Vertical #\n# Ombre ####\n# Test #####\n# From #####\n# Red ######\n# To #######\n# Blue #####\n# #\n############", ["FF0000", "0000FF"])
42
+
43
+ # Returns the block of text marked up with colors to create a diagonal gradient from top-left to bottom-right
44
+ Ombre.diagonal(text, colors)
45
+ Ombre.diagonal("############\n# #\n# Diagonal #\n# Ombre ####\n# Test #####\n# From #####\n# Red ######\n# To #######\n# Blue #####\n# #\n############", ["FF0000", "0000FF"])
46
+
47
+ # Returns the block of text marked up with colors to create a diagonal gradient from bottom-left to top-right
48
+ Ombre.diagonal_up(text, colors)
49
+ Ombre.diagonal_up("############\n# #\n# Diagonal #\n# Ombre ####\n# Test #####\n# From #####\n# Red ######\n# To #######\n# Blue #####\n# #\n############", ["FF0000", "0000FF"])
50
+ ```
51
+
52
+
53
+ Copyright (c) 2022 Justin Paulson, released under the
54
+ MIT license.
data/lib/ombre.rb CHANGED
@@ -1,41 +1,81 @@
1
1
  class Ombre
2
- def self.vertical color1, color2, text
3
- red1, green1, blue1 = get_colors(color1)
4
- red2, green2, blue2 = get_colors(color2)
5
- r_step, g_step, b_step = get_steps(color1, color2, text.lines.count)
2
+ def self.vertical text, colors
6
3
  text.lines.each_with_index.map do |line, i|
7
- color_text (red1 + i * r_step), (green1 + i * g_step), (blue1 + i * b_step), line
4
+ red, green, blue = get_offset_color colors, i/text.lines.count.to_f
5
+ color_text red, green, blue, line
8
6
  end.join
9
7
  end
10
8
 
11
- def self.horizontal color1, color2, text
12
- red1, green1, blue1 = get_colors(color1)
13
- red2, green2, blue2 = get_colors(color2)
14
- r_step, g_step, b_step = get_steps(color1, color2, text.lines.max.length)
9
+ def self.horizontal text, colors
15
10
  text.lines.map do |line|
16
- line.chars.each_with_index.map do |line, i|
17
- color_text (red1 + i * r_step), (green1 + i * g_step), (blue1 + i * b_step), line
11
+ line.chars.each_with_index.map do |char, i|
12
+ red, green, blue = get_offset_color colors, i/text.lines.max.length.to_f
13
+ color_text red, green, blue, char
18
14
  end.join
19
15
  end.join
20
16
  end
21
17
 
18
+ def self.diagonal text, colors
19
+ max_y = text.lines.count
20
+ max_x = text.lines.max.length
21
+ text.lines.each_with_index.map do |line, y|
22
+ line.chars.each_with_index.map do |char, x|
23
+ ratio = (y + x)/(max_y + max_x).to_f
24
+ red, green, blue = get_offset_color colors, ratio
25
+ color_text red, green, blue, char
26
+ end.join
27
+ end.join
28
+ end
29
+
30
+ def self.diagonal_up text, colors
31
+ max_y = text.lines.count
32
+ max_x = text.lines.max.length
33
+ text.lines.each_with_index.map do |line, y|
34
+ line.chars.each_with_index.map do |char, x|
35
+ ratio = (max_y - y + x)/(max_y + max_x).to_f
36
+ red, green, blue = get_offset_color colors, ratio
37
+ color_text red, green, blue, char
38
+ end.join
39
+ end.join
40
+ end
41
+
22
42
  private
23
43
 
24
44
  def self.get_colors color
25
45
  color = color.gsub('#','')
46
+
26
47
  red = color[0..1].to_i(16).to_f
27
48
  green = color[2..3].to_i(16).to_f
28
49
  blue = color[4..5].to_i(16).to_f
50
+
29
51
  [red, green, blue]
30
52
  end
31
53
 
32
- def self.get_steps c1, c2, i
33
- r1, g1, b1 = get_colors c1
34
- r2, g2, b2 = get_colors c2
35
- [(r2-r1)/i.to_f, (g2-g1)/i.to_f, (b2-b1)/i.to_f]
54
+ def self.get_offset_color colors, ratio
55
+ segment = -1
56
+ segment += 1 while ratio > (segment + 1)/(colors.count - 1).to_f && segment < colors.count - 2
57
+
58
+ color1 = colors[segment]
59
+ color2 = colors[segment+1]
60
+
61
+ percent = (ratio - segment/(colors.count-1).to_f)/(1/(colors.count-1).to_f)
62
+
63
+ r1, g1, b1 = get_colors color1
64
+ r2, g2, b2 = get_colors color2
65
+
66
+ r_dist = (r1-r2).abs * percent
67
+ red = r1 > r2 ? r1 - r_dist : r1 + r_dist
68
+
69
+ g_dist = (g1-g2).abs * percent
70
+ green = g1 > g2 ? g1 - g_dist : g1 + g_dist
71
+
72
+ b_dist = (b1-b2).abs * percent
73
+ blue = b1 > b2 ? b1 - b_dist : b1 + b_dist
74
+
75
+ [red, green, blue]
36
76
  end
37
77
 
38
78
  def self.color_text r, g, b, text
39
- "\x1b[38;2;#{r.to_i};#{g.to_i};#{b.to_i}m#{text}"
79
+ "\x1b[38;2;#{r.to_i};#{g.to_i};#{b.to_i}m#{text}\x1b[0m"
40
80
  end
41
81
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ombre
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Paulson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-20 00:00:00.000000000 Z
11
+ date: 2022-04-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Provides gradient colors for command line output using RGB hex values.
14
14
  email: jpaulson@hey.com