rainbow 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/rainbow.rb +38 -18
  2. data/test/rainbow_test.rb +47 -3
  3. metadata +4 -4
data/lib/rainbow.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Sickill
2
2
  module Rainbow
3
-
4
- TERM_COLORS = {
3
+
4
+ TERM_COLORS = {
5
5
  :black => 0,
6
6
  :red => 1,
7
7
  :green => 2,
@@ -12,7 +12,7 @@ module Sickill
12
12
  :white => 7,
13
13
  :default => 9,
14
14
  }
15
-
15
+
16
16
  TERM_EFFECTS = {
17
17
  :reset => 0,
18
18
  :bright => 1,
@@ -22,31 +22,29 @@ module Sickill
22
22
  :inverse => 7,
23
23
  :hide => 8,
24
24
  }
25
-
26
- # Sets foreground color if this text.
27
- def foreground(color)
28
- color = color.to_sym
29
- validate_color(color)
30
- wrap_with_code(TERM_COLORS[color] + 30)
25
+
26
+ # Sets foreground color of this text.
27
+ def foreground(*color)
28
+ color = color.first if color.size == 1
29
+ wrap_with_code(get_color_code(color, :foreground))
31
30
  end
32
31
  alias_method :color, :foreground
33
32
  alias_method :colour, :foreground
34
33
 
35
-
34
+
36
35
  # Sets background color of this text.
37
- def background(color)
38
- color = color.to_sym
39
- validate_color(color)
40
- wrap_with_code(TERM_COLORS[color] + 40)
36
+ def background(*color)
37
+ color = color.first if color.size == 1
38
+ wrap_with_code(get_color_code(color, :background))
41
39
  end
42
-
40
+
43
41
  # Resets terminal to default colors/backgrounds.
44
42
  #
45
43
  # It shouldn't be needed to use this method because all methods append terminal reset code to end of string.
46
44
  def reset
47
45
  wrap_with_code(TERM_EFFECTS[:reset])
48
46
  end
49
-
47
+
50
48
  # Turns on bright/bold for this text.
51
49
  def bright
52
50
  wrap_with_code(TERM_EFFECTS[:bright])
@@ -56,7 +54,7 @@ module Sickill
56
54
  def italic
57
55
  wrap_with_code(TERM_EFFECTS[:italic])
58
56
  end
59
-
57
+
60
58
  # Turns on underline decoration for this text.
61
59
  def underline
62
60
  wrap_with_code(TERM_EFFECTS[:underline])
@@ -85,7 +83,29 @@ module Sickill
85
83
  out.concat("\e[0m") unless out =~ /\e\[0m$/
86
84
  out
87
85
  end
88
-
86
+
87
+ def get_color_code(color, type) #:nodoc:
88
+ case color
89
+ when Symbol
90
+ validate_color(color)
91
+ TERM_COLORS[color] + (type == :foreground ? 30 : 40)
92
+ when String
93
+ color = color.gsub("#", "")
94
+ r, g, b = color[0..1].to_i(16), color[2..3].to_i(16), color[4..5].to_i(16)
95
+ get_rgb_code(r, g, b, type)
96
+ when Array
97
+ raise ArgumentError.new("Bad number of arguments for RGB color definition, should be 3") unless color.size == 3
98
+ get_rgb_code(color[0], color[1], color[2], type)
99
+ end
100
+ end
101
+
102
+ def get_rgb_code(r, g, b, type) #:nodoc:
103
+ raise ArgumentError.new("RGB value outside 0-255 range") if [r, g, b].min < 0 || [r, g, b].max > 255
104
+ code = { :foreground => 38, :background => 48 }[type]
105
+ index = 16 + (6 * (r / 256.0)).to_i * 36 + (6 * (g / 256.0)).to_i * 6 + (6 * (b / 256.0)).to_i
106
+ "#{code};5;#{index}"
107
+ end
108
+
89
109
  def validate_color(color) #:nodoc:
90
110
  raise ArgumentError.new("Unknown color, valid colors: #{TERM_COLORS.keys.join(', ')}") unless TERM_COLORS.keys.include?(color)
91
111
  end
data/test/rainbow_test.rb CHANGED
@@ -2,11 +2,15 @@ require 'test/unit'
2
2
  require File.expand_path(File.dirname(__FILE__) + '/../lib/rainbow')
3
3
 
4
4
  class RainbowTest < Test::Unit::TestCase #:nodoc:
5
- def test_color
5
+ def test_color_by_name
6
6
  assert_equal "\e[31mhello\e[0m", "hello".color(:red)
7
7
  end
8
8
 
9
- def test_foreground
9
+ def test_color_by_rgb
10
+ assert_equal "\e[38;5;196mhello\e[0m", "hello".color(255, 0, 0)
11
+ end
12
+
13
+ def test_foreground_alias
10
14
  assert_equal "hello".color(:red), "hello".foreground(:red)
11
15
  end
12
16
 
@@ -14,13 +18,53 @@ class RainbowTest < Test::Unit::TestCase #:nodoc:
14
18
  assert_equal "hello".color(:red), "hello".colour(:red)
15
19
  end
16
20
 
17
- def test_background
21
+ def test_background_by_name
18
22
  assert_equal "\e[42mhello\e[0m", "hello".background(:green)
19
23
  end
20
24
 
25
+ def test_background_by_rgb
26
+ assert_equal "\e[48;5;46mhello\e[0m", "hello".background(0, 255, 0)
27
+ end
28
+
21
29
  def test_color_and_background
22
30
  assert_equal "\e[31m\e[42mhello\e[0m", "hello".color(:red).background(:green)
23
31
  end
32
+
33
+ def test_hex_color
34
+ assert_equal "\e[48;5;46mhello\e[0m", "hello".background("#00FF00")
35
+ assert_equal "\e[48;5;46mhello\e[0m", "hello".background("00FF00")
36
+ assert_equal "\e[48;5;46mhello\e[0m", "hello".background("00ff00")
37
+ end
38
+
39
+ def test_bad_color_name
40
+ assert_raises ArgumentError do
41
+ "hello".background(:baaaad)
42
+ end
43
+ end
44
+
45
+ def test_rgb_color_with_2_args
46
+ assert_raises ArgumentError do
47
+ "hello".background(1, 2)
48
+ end
49
+ end
50
+
51
+ def test_rgb_color_with_4_args
52
+ assert_raises ArgumentError do
53
+ "hello".background(1, 2, 3, 4)
54
+ end
55
+ end
56
+
57
+ def test_rgb_color_with_values_below_zero
58
+ assert_raises ArgumentError do
59
+ "hello".background(-3, 2, 3)
60
+ end
61
+ end
62
+
63
+ def test_rgb_color_with_values_above_255
64
+ assert_raises ArgumentError do
65
+ "hello".background(256, 2, 3)
66
+ end
67
+ end
24
68
 
25
69
  def test_bright
26
70
  assert_equal "\e[1mhello\e[0m", "hello".bright
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rainbow
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin Kulik
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-26 00:00:00 +02:00
12
+ date: 2009-11-10 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -50,8 +50,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  version:
51
51
  requirements: []
52
52
 
53
- rubyforge_project: rainbow
54
- rubygems_version: 1.3.3
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
55
  signing_key:
56
56
  specification_version: 3
57
57
  summary: Rainbow extends ruby String class enabling coloring text on ANSI terminals