color 0.1.0 → 1.4.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.
@@ -1,131 +1,134 @@
1
- $:.unshift(File.dirname(__FILE__) + "/../lib/")
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Color
4
+ # Colour management with Ruby
5
+ # http://rubyforge.org/projects/color
6
+ # Version 1.4.0
7
+ #
8
+ # Licensed under a MIT-style licence. See Licence.txt in the main
9
+ # distribution for full licensing information.
10
+ #
11
+ # Copyright (c) 2005 - 2007 Austin Ziegler and Matt Lyon
12
+ #
13
+ # $Id: test_all.rb 55 2007-02-03 23:29:34Z austin $
14
+ #++
2
15
 
16
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
3
17
  require 'test/unit'
4
18
  require 'color'
19
+ require 'color/css'
5
20
 
6
- class TestColor < Test::Unit::TestCase
7
-
8
- COLORS = {
9
- :red => {:html => '#ff0000', :rgb => [255,0,0], :hsl => [0,100,50] },
10
- :yellow => {:html => '#ffff00', :rgb => [255,255,0], :hsl => [60,100,50] },
11
- :blue => {:html => '#0000ff', :rgb => [0,0,255], :hsl => [240,100,50] },
12
- :brown => {:html => '#a16328', :rgb => [161,99,40], :hsl => [29,60,39] },
13
- :carnation => {:html => '#ff5ed0', :rgb => [255,94,208], :hsl => [318,100,68] },
14
- :cayenne => {:html => '#8d0000', :rgb => [141,0,0], :hsl => [0,100,28] }
15
- }
16
-
17
- ## eigenclass conversion methods
18
-
19
- def test_rgb_to_hex
20
- COLORS.each_value do |color|
21
- assert_equal color[:html], Color.rgb_to_rgbhex(color[:rgb])
21
+ module TestColor
22
+ class TestColor < Test::Unit::TestCase
23
+ def setup
24
+ Kernel.module_eval do
25
+ alias old_warn warn
26
+
27
+ def warn(message)
28
+ $last_warn = message
29
+ end
30
+ end
22
31
  end
23
- end
24
-
25
- def test_hex_to_rgb
26
- COLORS.each_value do |color|
27
- assert_equal color[:rgb], Color.rgbhex_to_rgb(color[:html])
32
+
33
+ def teardown
34
+ Kernel.module_eval do
35
+ undef warn
36
+ alias warn old_warn
37
+ undef old_warn
38
+ end
28
39
  end
29
- end
30
-
31
- def test_rgb_to_hsl
32
- COLORS.each_value do |color|
33
- assert_equivalent color[:hsl], Color.rgb_to_hsl_human(color[:rgb])
40
+
41
+ def test_const
42
+ $last_warn = nil
43
+ assert_equal(Color::RGB::AliceBlue, Color::AliceBlue)
44
+ assert_equal("Color::AliceBlue has been deprecated. Use Color::RGB::AliceBlue instead.", $last_warn)
45
+
46
+ $last_warn = nil # Do this twice to make sure it always happens...
47
+ assert(Color::AliceBlue)
48
+ assert_equal("Color::AliceBlue has been deprecated. Use Color::RGB::AliceBlue instead.", $last_warn)
49
+
50
+ $last_warn = nil
51
+ assert_equal(Color::COLOR_VERSION, Color::VERSION)
52
+ assert_equal("Color::VERSION has been deprecated. Use Color::COLOR_VERSION instead.", $last_warn)
53
+
54
+ $last_warn = nil
55
+ assert_equal(Color::COLOR_VERSION, Color::COLOR_TOOLS_VERSION)
56
+ assert_equal("Color::COLOR_TOOLS_VERSION has been deprecated. Use Color::COLOR_VERSION instead.", $last_warn)
57
+
58
+ $last_warn = nil
59
+ assert(Color::COLOR_VERSION)
60
+ assert_nil($last_warn)
61
+ assert(Color::COLOR_EPSILON)
62
+ assert_nil($last_warn)
63
+
64
+ assert_raises(NameError) { assert(Color::MISSING_VALUE) }
34
65
  end
35
- end
36
-
37
- def test_hsl_to_rgb
38
- COLORS.each_value do |color|
39
- assert_equivalent color[:rgb], Color.hsl_to_rgb(color[:hsl])
66
+
67
+ def test_normalize
68
+ (1..10).each do |i|
69
+ assert_equal(0.0, Color.normalize(-7 * i))
70
+ assert_equal(0.0, Color.normalize(-7 / i))
71
+ assert_equal(0.0, Color.normalize(0 - i))
72
+ assert_equal(1.0, Color.normalize(255 + i))
73
+ assert_equal(1.0, Color.normalize(256 * i))
74
+ assert_equal(1.0, Color.normalize(65536 / i))
75
+ end
76
+ (0..255).each do |i|
77
+ assert_in_delta(i / 255.0, Color.normalize(i / 255.0),
78
+ 1e-2)
79
+ end
40
80
  end
41
- end
42
-
43
- def test_hsl_to_rgb_with_floats
44
- COLORS.each_value do |color|
45
- assert_equivalent color[:rgb], Color.hsl_to_rgb([(color[:hsl][0]/360.0), (color[:hsl][1]/100.0), (color[:hsl][2]/100.0)])
81
+
82
+ def test_normalize_range
83
+ assert_equal(0, Color.normalize_8bit(-1))
84
+ assert_equal(0, Color.normalize_8bit(0))
85
+ assert_equal(127, Color.normalize_8bit(127))
86
+ assert_equal(172, Color.normalize_8bit(172))
87
+ assert_equal(255, Color.normalize_8bit(255))
88
+ assert_equal(255, Color.normalize_8bit(256))
89
+
90
+ assert_equal(-100, Color.normalize_to_range(-101, -100..100))
91
+ assert_equal(-100, Color.normalize_to_range(-100.5, -100..100))
92
+ assert_equal(-100, Color.normalize_to_range(-100, -100..100))
93
+ assert_equal(-100, Color.normalize_to_range(-100.0, -100..100))
94
+ assert_equal(-99.5, Color.normalize_to_range(-99.5, -100..100))
95
+ assert_equal(-50, Color.normalize_to_range(-50, -100..100))
96
+ assert_equal(-50.5, Color.normalize_to_range(-50.5, -100..100))
97
+ assert_equal(0, Color.normalize_to_range(0, -100..100))
98
+ assert_equal(50, Color.normalize_to_range(50, -100..100))
99
+ assert_equal(50.5, Color.normalize_to_range(50.5, -100..100))
100
+ assert_equal(99, Color.normalize_to_range(99, -100..100))
101
+ assert_equal(99.5, Color.normalize_to_range(99.5, -100..100))
102
+ assert_equal(100, Color.normalize_to_range(100, -100..100))
103
+ assert_equal(100, Color.normalize_to_range(100.0, -100..100))
104
+ assert_equal(100, Color.normalize_to_range(100.5, -100..100))
105
+ assert_equal(100, Color.normalize_to_range(101, -100..100))
46
106
  end
47
- end
48
-
49
- # instance methods
50
-
51
- def test_intialize_from_hex
52
- COLORS.each_value do |color|
53
- instance = Color.new(color[:html])
54
- assert_equal color[:hsl], instance.to_hsl
107
+
108
+ def test_new
109
+ $last_warn = nil
110
+ c = Color.new("#fff")
111
+ assert_kind_of(Color::HSL, c)
112
+ assert_equal(Color::RGB::White.to_hsl, c)
113
+ assert_equal("Color.new has been deprecated. Use Color::RGB.new instead.", $last_warn)
114
+
115
+ $last_warn = nil
116
+ c = Color.new([0, 0, 0])
117
+ assert_kind_of(Color::HSL, c)
118
+ assert_equal(Color::RGB::Black.to_hsl, c)
119
+ assert_equal("Color.new has been deprecated. Use Color::RGB.new instead.", $last_warn)
120
+
121
+ $last_warn = nil
122
+ c = Color.new([10, 20, 30], :hsl)
123
+ assert_kind_of(Color::HSL, c)
124
+ assert_equal(Color::HSL.new(10, 20, 30), c)
125
+ assert_equal("Color.new has been deprecated. Use Color::HSL.new instead.", $last_warn)
126
+
127
+ $last_warn = nil
128
+ c = Color.new([10, 20, 30, 40], :cmyk)
129
+ assert_kind_of(Color::HSL, c)
130
+ assert_equal(Color::CMYK.new(10, 20, 30, 40).to_hsl, c)
131
+ assert_equal("Color.new has been deprecated. Use Color::CMYK.new instead.", $last_warn)
55
132
  end
56
133
  end
57
-
58
- def test_hue
59
- assert_equal 60, Color.new(COLORS[:yellow][:html]).hue
60
- end
61
-
62
- def test_hue_is
63
- assert_equal 0.1, Color.new('000000').hue = 0.1
64
- assert_equal 100, Color.new('000000').hue = 100
65
- assert_equal 80, Color.new(COLORS[:yellow][:html]).hue += 20
66
- # TODO: is there a way to have -= return 340 instead of -20 ?
67
- red = Color.new(COLORS[:red][:html])
68
- red.hue -= 20
69
- assert_equal 340, red.hue
70
- end
71
-
72
- def test_saturation
73
- assert_equal 60, Color.new(COLORS[:brown][:html]).saturation
74
- end
75
-
76
- def test_saturation_is
77
- assert_equal 0.1, Color.new('000000').saturation = 0.1
78
- assert_equal 10, Color.new('000000').saturation = 10
79
- assert_equal 80, Color.new(COLORS[:red][:html]).saturation -= 20
80
- assert_equal 70, Color.new(COLORS[:brown][:html]).saturation += 10
81
- end
82
-
83
- def test_saturation_is_lower_limit
84
- brown = Color.new(COLORS[:brown][:html])
85
- brown.saturation -= 80
86
- assert_equal 0, brown.saturation
87
- end
88
-
89
- def test_saturation_is_upper_limit
90
- brown = Color.new(COLORS[:brown][:html])
91
- brown.saturation += 80
92
- assert_equal 100, brown.saturation
93
- end
94
-
95
- def test_lightness
96
- assert_equal 50, Color.new(COLORS[:red][:html]).lightness
97
- end
98
-
99
- def test_lightness_is
100
- assert_equal 0.1, Color.new('000000').lightness = 0.1
101
- assert_equal 10, Color.new('000000').lightness = 10
102
- assert_equal 30, Color.new(COLORS[:red][:html]).lightness -= 20
103
- assert_equal 70, Color.new(COLORS[:red][:html]).lightness += 20
104
- end
105
-
106
- def test_lightness_is_lower_limit
107
- red = Color.new(COLORS[:red][:html])
108
- red.lightness -= 80
109
- assert_equal 0, red.lightness
110
- end
111
-
112
- def test_saturation_is_upper_limit
113
- red = Color.new(COLORS[:red][:html])
114
- red.lightness += 80
115
- assert_equal 100, red.lightness
116
- end
117
-
118
- def test_mix_with
119
- red, yellow = Color.new(COLORS[:red][:html]), Color.new(COLORS[:yellow][:html])
120
- assert_equal 30, red.mix_with(yellow, 0.5).hue
121
- end
122
-
123
- #helpers
124
-
125
- def assert_equivalent(target=[], input=[])
126
- difference = 0
127
- target.each_with_index {|value, index| difference += (value - input[index]).abs }
128
- assert difference <= 5, "[#{input.join(',')}] is not close enough to [#{target.join(',')}]"
129
- end
130
-
131
- end
134
+ end
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Color
4
+ # Colour management with Ruby
5
+ # http://rubyforge.org/projects/color
6
+ # Version 1.4.0
7
+ #
8
+ # Licensed under a MIT-style licence. See Licence.txt in the main
9
+ # distribution for full licensing information.
10
+ #
11
+ # Copyright (c) 2005 - 2007 Austin Ziegler and Matt Lyon
12
+ #
13
+ # $Id: test_all.rb 55 2007-02-03 23:29:34Z austin $
14
+ #++
15
+
16
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
17
+ require 'test/unit'
18
+ require 'color'
19
+ require 'color/css'
20
+
21
+ module TestColor
22
+ class TestCSS < Test::Unit::TestCase
23
+ def test_index
24
+ assert_equal(Color::RGB::AliceBlue, Color::CSS[:aliceblue])
25
+ assert_equal(Color::RGB::AliceBlue, Color::CSS["AliceBlue"])
26
+ assert_equal(Color::RGB::AliceBlue, Color::CSS["aliceBlue"])
27
+ assert_equal(Color::RGB::AliceBlue, Color::CSS["aliceblue"])
28
+ assert_equal(Color::RGB::AliceBlue, Color::CSS[:AliceBlue])
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,103 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Color
4
+ # Colour management with Ruby
5
+ # http://rubyforge.org/projects/color
6
+ # Version 1.4.0
7
+ #
8
+ # Licensed under a MIT-style licence. See Licence.txt in the main
9
+ # distribution for full licensing information.
10
+ #
11
+ # Copyright (c) 2005 - 2007 Austin Ziegler and Matt Lyon
12
+ #
13
+ # $Id: test_all.rb 55 2007-02-03 23:29:34Z austin $
14
+ #++
15
+
16
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
17
+ require 'test/unit'
18
+ require 'color'
19
+ require 'color/palette/gimp'
20
+
21
+ module TestColor
22
+ module TestPalette
23
+ class TestGimp < Test::Unit::TestCase
24
+ include Color::Palette
25
+
26
+ GIMP_W3C = <<-EOS
27
+ GIMP Palette
28
+ Name: W3C Named Colors
29
+ Columns: 2
30
+ #
31
+ # ColorZilla W3C Named Colors
32
+ #
33
+ 255 255 255 White
34
+ 255 255 0 Yclow
35
+ 255 0 255 Fuchsia
36
+ 255 0 0 Red
37
+ 192 192 192 Silver
38
+ 128 128 128 Gray
39
+ 128 128 0 Olive
40
+ 128 0 128 Purple
41
+ 128 0 0 Maroon
42
+ 0 255 255 Aqua
43
+ 0 255 0 Lime
44
+ 0 128 128 Teal
45
+ 0 128 0 Green
46
+ 0 0 255 Blue
47
+ 0 0 128 Navy
48
+ 0 0 0 Black
49
+ EOS
50
+
51
+ def setup
52
+ @filename = "test#{Process.pid}.gimp"
53
+ end
54
+
55
+ def teardown
56
+ require 'fileutils'
57
+ FileUtils.rm_f @filename if File.exist? @filename
58
+ end
59
+
60
+ def test_each
61
+ @gimp = Gimp.new(GIMP_W3C)
62
+ assert_equal(16, @gimp.instance_variable_get(:@colors).size)
63
+ @gimp.each { |c| assert_kind_of(Color::RGB, c) }
64
+ end
65
+
66
+ def test_each_name
67
+ @gimp = Gimp.new(GIMP_W3C)
68
+ assert_equal(16, @gimp.instance_variable_get(:@names).size)
69
+
70
+ @gimp.each_name { |color_name, color_set|
71
+ assert_kind_of(Array, color_set)
72
+ color_set.each { |c|
73
+ assert_kind_of(Color::RGB, c)
74
+ }
75
+ }
76
+ end
77
+
78
+ def test_index
79
+ assert_nothing_raised do
80
+ File.open(@filename, "wb") do |f|
81
+ f.write GIMP_W3C
82
+ end
83
+ end
84
+ assert_nothing_raised { @gimp = Gimp.from_file(@filename) }
85
+ assert_equal(Color::RGB::White, @gimp[0])
86
+ assert_equal(Color::RGB::White, @gimp["White"][0])
87
+ assert_equal([Color::RGB::White, Color::RGB::Black],
88
+ @gimp.values_at(0, -1))
89
+ assert_equal(16, @gimp.size)
90
+ end
91
+
92
+ def test_valid_eh
93
+ @gimp = Gimp.new(GIMP_W3C)
94
+ assert(@gimp.valid?)
95
+ end
96
+
97
+ def test_name
98
+ @gimp = Gimp.new(GIMP_W3C)
99
+ assert_equal("W3C Named Colors", @gimp.name)
100
+ end
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,123 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Color
4
+ # Colour management with Ruby
5
+ # http://rubyforge.org/projects/color
6
+ # Version 1.4.0
7
+ #
8
+ # Licensed under a MIT-style licence. See Licence.txt in the main
9
+ # distribution for full licensing information.
10
+ #
11
+ # Copyright (c) 2005 - 2007 Austin Ziegler and Matt Lyon
12
+ #
13
+ # $Id: test_all.rb 55 2007-02-03 23:29:34Z austin $
14
+ #++
15
+
16
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
17
+ require 'test/unit'
18
+ require 'color'
19
+
20
+ module TestColor
21
+ class TestGrayScale < Test::Unit::TestCase
22
+ def setup
23
+ @gs = Color::GrayScale.from_percent(33)
24
+ end
25
+
26
+ def test_brightness
27
+ assert_in_delta(0.33, @gs.brightness, Color::COLOR_TOLERANCE)
28
+ end
29
+
30
+ def test_darken_by
31
+ assert_in_delta(29.7, @gs.darken_by(10).gray, Color::COLOR_TOLERANCE)
32
+ end
33
+
34
+ def test_g
35
+ assert_in_delta(0.33, @gs.g, Color::COLOR_TOLERANCE)
36
+ assert_in_delta(33, @gs.grey, Color::COLOR_TOLERANCE)
37
+ assert_nothing_raised { @gs.gray = 40 }
38
+ assert_in_delta(0.4, @gs.g, Color::COLOR_TOLERANCE)
39
+ assert_nothing_raised { @gs.g = 2.0 }
40
+ assert_in_delta(100, @gs.gray, Color::COLOR_TOLERANCE)
41
+ assert_nothing_raised { @gs.grey = -2.0 }
42
+ assert_in_delta(0.0, @gs.g, Color::COLOR_TOLERANCE)
43
+ end
44
+
45
+ def test_html_css
46
+ assert_equal("#545454", @gs.html)
47
+ assert_equal("rgb(33.00%, 33.00%, 33.00%)", @gs.css_rgb)
48
+ assert_equal("rgba(33.00%, 33.00%, 33.00%, 1.00)", @gs.css_rgba)
49
+ end
50
+
51
+ def test_lighten_by
52
+ assert_in_delta(0.363, @gs.lighten_by(10).g, Color::COLOR_TOLERANCE)
53
+ end
54
+
55
+ def test_pdf_fill
56
+ assert_equal("0.330 g", @gs.pdf_fill)
57
+ assert_equal("0.330 G", @gs.pdf_stroke)
58
+ end
59
+
60
+ def test_to_cmyk
61
+ cmyk = nil
62
+ assert_nothing_raised { cmyk = @gs.to_cmyk }
63
+ assert_kind_of(Color::CMYK, cmyk)
64
+ assert_in_delta(0.0, cmyk.c, Color::COLOR_TOLERANCE)
65
+ assert_in_delta(0.0, cmyk.m, Color::COLOR_TOLERANCE)
66
+ assert_in_delta(0.0, cmyk.y, Color::COLOR_TOLERANCE)
67
+ assert_in_delta(0.67, cmyk.k, Color::COLOR_TOLERANCE)
68
+ end
69
+
70
+ def test_to_grayscale
71
+ assert_equal(@gs, @gs.to_grayscale)
72
+ assert_equal(@gs, @gs.to_greyscale)
73
+ end
74
+
75
+ def test_to_hsl
76
+ hsl = nil
77
+ assert_nothing_raised { hsl = @gs.to_hsl }
78
+ assert_kind_of(Color::HSL, hsl)
79
+ assert_in_delta(0.0, hsl.h, Color::COLOR_TOLERANCE)
80
+ assert_in_delta(0.0, hsl.s, Color::COLOR_TOLERANCE)
81
+ assert_in_delta(0.33, hsl.l, Color::COLOR_TOLERANCE)
82
+ assert_equal("hsl(0.00, 0.00%, 33.00%)", @gs.css_hsl)
83
+ assert_equal("hsla(0.00, 0.00%, 33.00%, 1.00)", @gs.css_hsla)
84
+ end
85
+
86
+ def test_to_rgb
87
+ rgb = nil
88
+ assert_nothing_raised { rgb = @gs.to_rgb }
89
+ assert_kind_of(Color::RGB, rgb)
90
+ assert_in_delta(0.33, rgb.r, Color::COLOR_TOLERANCE)
91
+ assert_in_delta(0.33, rgb.g, Color::COLOR_TOLERANCE)
92
+ assert_in_delta(0.33, rgb.b, Color::COLOR_TOLERANCE)
93
+ end
94
+
95
+ def test_to_yiq
96
+ yiq = nil
97
+ assert_nothing_raised { yiq = @gs.to_yiq }
98
+ assert_kind_of(Color::YIQ, yiq)
99
+ assert_in_delta(0.33, yiq.y, Color::COLOR_TOLERANCE)
100
+ assert_in_delta(0.0, yiq.i, Color::COLOR_TOLERANCE)
101
+ assert_in_delta(0.0, yiq.q, Color::COLOR_TOLERANCE)
102
+ end
103
+
104
+ def test_add
105
+ delta = @gs + Color::GrayScale.new(20)
106
+ max = @gs + Color::GrayScale.new(80)
107
+
108
+ assert_in_delta(1.0, max.g, Color::COLOR_TOLERANCE)
109
+ assert_in_delta(0.53, delta.g, Color::COLOR_TOLERANCE)
110
+ end
111
+
112
+ def test_subtract
113
+ delta = @gs - Color::GrayScale.new(20)
114
+ max = @gs - Color::GrayScale.new(80)
115
+ assert_in_delta(0.0, max.g, Color::COLOR_TOLERANCE)
116
+ assert_in_delta(0.13, delta.g, Color::COLOR_TOLERANCE)
117
+ end
118
+
119
+ def test_inspect
120
+ assert_equal("Gray [33.00%]", @gs.inspect)
121
+ end
122
+ end
123
+ end