color-tools 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Colour management with Ruby.
4
+ #
5
+ # Copyright 2005 Austin Ziegler
6
+ # http://rubyforge.org/ruby-pdf/
7
+ #
8
+ # Licensed under a MIT-style licence.
9
+ #
10
+ # $Id: test_hsl.rb,v 1.2 2005/08/08 02:57:36 austin Exp $
11
+ #++
12
+
13
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
14
+ require 'test/unit' unless defined? $ZENTEST and $ZENTEST
15
+ require 'color'
16
+
17
+ module TestColor
18
+ class TestHSL < Test::Unit::TestCase
19
+ def setup
20
+ @hsl = Color::HSL.new(145, 20, 30)
21
+ end
22
+
23
+ def brightness
24
+ assert_in_delta(0.3, @hsl.brightness, 1e-4)
25
+ end
26
+
27
+ def test_h
28
+ assert_in_delta(0.4027, @hsl.h, 1e-4)
29
+ end
30
+
31
+ def test_h_equals
32
+ assert_in_delta(0.4027, @hsl.h, 1e-4)
33
+ assert_nothing_raised { @hsl.h = 0.33 }
34
+ assert_in_delta(0.33, @hsl.h, 1e-4)
35
+ assert_nothing_raised { @hsl.h = 3.3 }
36
+ assert_in_delta(1.0, @hsl.h, 1e-4)
37
+ assert_nothing_raised { @hsl.h = -3.3 }
38
+ assert_in_delta(0.0, @hsl.h, 1e-4)
39
+ end
40
+
41
+ def test_html
42
+ assert_equal("#3d5c4a", @hsl.html)
43
+ end
44
+
45
+ def test_l
46
+ assert_in_delta(0.3, @hsl.l, 1e-4)
47
+ end
48
+
49
+ def test_l_equals
50
+ assert_in_delta(0.3, @hsl.l, 1e-4)
51
+ assert_nothing_raised { @hsl.l = 0.33 }
52
+ assert_in_delta(0.33, @hsl.l, 1e-4)
53
+ assert_nothing_raised { @hsl.l = 3.3 }
54
+ assert_in_delta(1.0, @hsl.l, 1e-4)
55
+ assert_nothing_raised { @hsl.l = -3.3 }
56
+ assert_in_delta(0.0, @hsl.l, 1e-4)
57
+ end
58
+
59
+ def test_s
60
+ assert_in_delta(0.2, @hsl.s, 1e-4)
61
+ end
62
+
63
+ def test_s_equals
64
+ assert_in_delta(0.2, @hsl.s, 1e-4)
65
+ assert_nothing_raised { @hsl.s = 0.33 }
66
+ assert_in_delta(0.33, @hsl.s, 1e-4)
67
+ assert_nothing_raised { @hsl.s = 3.3 }
68
+ assert_in_delta(1.0, @hsl.s, 1e-4)
69
+ assert_nothing_raised { @hsl.s = -3.3 }
70
+ assert_in_delta(0.0, @hsl.s, 1e-4)
71
+ end
72
+
73
+ def test_to_cmyk
74
+ assert_kind_of(Color::CMYK, @hsl.to_cmyk)
75
+ end
76
+
77
+ def test_to_grayscale
78
+ assert_kind_of(Color::GrayScale, @hsl.to_grayscale)
79
+ end
80
+
81
+ def test_to_greyscale
82
+ assert_kind_of(Color::GreyScale, @hsl.to_greyscale)
83
+ end
84
+
85
+ def test_to_rgb
86
+ assert_kind_of(Color::RGB, @hsl.to_rgb)
87
+ end
88
+
89
+ def test_to_yiq
90
+ assert_kind_of(Color::YIQ, @hsl.to_yiq)
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,145 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Colour management with Ruby.
4
+ #
5
+ # Copyright 2005 Austin Ziegler
6
+ # http://rubyforge.org/ruby-pdf/
7
+ #
8
+ # Licensed under a MIT-style licence.
9
+ #
10
+ # $Id: test_monocontrast.rb,v 1.1 2005/08/08 02:44:17 austin Exp $
11
+ #++
12
+
13
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
14
+ require 'test/unit' unless defined? $ZENTEST and $ZENTEST
15
+ require 'color'
16
+ require 'color/palette/monocontrast'
17
+
18
+ module TestColor
19
+ module TestPalette
20
+ class TestMonoContrast < Test::Unit::TestCase
21
+ include Color::Palette
22
+ def setup
23
+ @high = Color::RGB.from_html("#c9e3a6")
24
+ @low = Color::RGB.from_html("#746b8e")
25
+ @mcp1 = MonoContrast.new(@high)
26
+ @mcp2 = MonoContrast.new(@low)
27
+ end
28
+
29
+ def test_background
30
+ assert_equal("#141711", @mcp1.background[-5].html)
31
+ assert_equal("#32392a", @mcp1.background[-4].html)
32
+ assert_equal("#657253", @mcp1.background[-3].html)
33
+ assert_equal("#97aa7d", @mcp1.background[-2].html)
34
+ assert_equal("#abc18d", @mcp1.background[-1].html)
35
+ assert_equal("#c9e3a6", @mcp1.background[ 0].html)
36
+ assert_equal("#d1e7b3", @mcp1.background[+1].html)
37
+ assert_equal("#d7eabc", @mcp1.background[+2].html) # d7eabd
38
+ assert_equal("#e4f1d3", @mcp1.background[+3].html) # e5f2d3
39
+ assert_equal("#f2f8e9", @mcp1.background[+4].html) # f1f8e9
40
+ assert_equal("#fafcf6", @mcp1.background[+5].html) # fafdf7
41
+
42
+ assert_equal("#0c0b0e", @mcp2.background[-5].html)
43
+ assert_equal("#1d1b24", @mcp2.background[-4].html)
44
+ assert_equal("#3a3647", @mcp2.background[-3].html)
45
+ assert_equal("#57506b", @mcp2.background[-2].html)
46
+ assert_equal("#635b79", @mcp2.background[-1].html)
47
+ assert_equal("#746b8e", @mcp2.background[ 0].html)
48
+ assert_equal("#89819f", @mcp2.background[+1].html)
49
+ assert_equal("#9790aa", @mcp2.background[+2].html) # 9790ab
50
+ assert_equal("#bab5c7", @mcp2.background[+3].html) # bab6c7
51
+ assert_equal("#dcdae3", @mcp2.background[+4].html)
52
+ assert_equal("#f1f0f4", @mcp2.background[+5].html) # f2f1f4
53
+ end
54
+
55
+ def test_brightness_diff
56
+ bd1 = @mcp1.brightness_diff(@high, @low)
57
+ bd2 = @mcp1.brightness_diff(@low, @high)
58
+ assert_in_delta(bd1, bd2, 1e-4)
59
+ end
60
+
61
+ def test_calculate_foreground
62
+ assert_equal("#ffffff", @mcp1.calculate_foreground(@low, @high).html)
63
+ assert_equal("#1d1b24", @mcp1.calculate_foreground(@high, @low).html)
64
+ end
65
+
66
+ def test_color_diff
67
+ assert_in_delta(@mcp1.color_diff(@low, @high),
68
+ @mcp1.color_diff(@high, @low), 1e-4)
69
+ end
70
+
71
+ def test_foreground
72
+ assert_equal("#c9e3a6", @mcp1.foreground[-5].html)
73
+ assert_equal("#e4f1d3", @mcp1.foreground[-4].html) # e5f2d3
74
+ assert_equal("#ffffff", @mcp1.foreground[-3].html)
75
+ assert_equal("#000000", @mcp1.foreground[-2].html)
76
+ assert_equal("#000000", @mcp1.foreground[-1].html)
77
+ assert_equal("#000000", @mcp1.foreground[ 0].html)
78
+ assert_equal("#000000", @mcp1.foreground[+1].html)
79
+ assert_equal("#000000", @mcp1.foreground[+2].html)
80
+ assert_equal("#32392a", @mcp1.foreground[+3].html)
81
+ assert_equal("#32392a", @mcp1.foreground[+4].html)
82
+ assert_equal("#32392a", @mcp1.foreground[+5].html)
83
+
84
+ assert_equal("#bab5c7", @mcp2.foreground[-5].html) # bab6c7
85
+ assert_equal("#dcdae3", @mcp2.foreground[-4].html)
86
+ assert_equal("#ffffff", @mcp2.foreground[-3].html)
87
+ assert_equal("#ffffff", @mcp2.foreground[-2].html)
88
+ assert_equal("#ffffff", @mcp2.foreground[-1].html)
89
+ assert_equal("#ffffff", @mcp2.foreground[ 0].html)
90
+ assert_equal("#000000", @mcp2.foreground[+1].html)
91
+ assert_equal("#000000", @mcp2.foreground[+2].html)
92
+ assert_equal("#000000", @mcp2.foreground[+3].html)
93
+ assert_equal("#1d1b24", @mcp2.foreground[+4].html)
94
+ assert_equal("#3a3647", @mcp2.foreground[+5].html)
95
+ end
96
+
97
+ def test_minimum_brightness_diff
98
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_BRIGHTNESS_DIFF,
99
+ @mcp1.minimum_brightness_diff, 1e-4)
100
+ end
101
+
102
+ def test_minimum_brightness_diff_equals
103
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_BRIGHTNESS_DIFF,
104
+ @mcp1.minimum_brightness_diff, 1e-4)
105
+ mcps = @mcp1.dup
106
+ assert_nothing_raised { @mcp1.minimum_brightness_diff = 0.75 }
107
+ assert_in_delta(0.75, @mcp1.minimum_brightness_diff, 1e-4)
108
+ assert_not_equal(@mcp1.foreground[-5], mcps.foreground[-5])
109
+ assert_nothing_raised { @mcp1.minimum_brightness_diff = 4.0 }
110
+ assert_in_delta(1, @mcp1.minimum_brightness_diff, 1e-4)
111
+ assert_nothing_raised { @mcp1.minimum_brightness_diff = -4.0 }
112
+ assert_in_delta(0, @mcp1.minimum_brightness_diff, 1e-4)
113
+ assert_nothing_raised { @mcp1.minimum_brightness_diff = nil }
114
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_BRIGHTNESS_DIFF,
115
+ @mcp1.minimum_brightness_diff, 1e-4)
116
+ end
117
+
118
+ def test_minimum_color_diff
119
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_COLOR_DIFF,
120
+ @mcp1.minimum_color_diff, 1e-4)
121
+ end
122
+
123
+ def test_minimum_color_diff_equals
124
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_COLOR_DIFF,
125
+ @mcp1.minimum_color_diff, 1e-4)
126
+ mcps = @mcp1.dup
127
+ assert_nothing_raised { @mcp1.minimum_color_diff = 0.75 }
128
+ assert_in_delta(0.75, @mcp1.minimum_color_diff, 1e-4)
129
+ assert_not_equal(@mcp1.foreground[-5], mcps.foreground[-5])
130
+ assert_nothing_raised { @mcp1.minimum_color_diff = 4.0 }
131
+ assert_in_delta(3, @mcp1.minimum_color_diff, 1e-4)
132
+ assert_nothing_raised { @mcp1.minimum_color_diff = -4.0 }
133
+ assert_in_delta(0, @mcp1.minimum_color_diff, 1e-4)
134
+ assert_nothing_raised { @mcp1.minimum_color_diff = nil }
135
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_COLOR_DIFF,
136
+ @mcp1.minimum_color_diff, 1e-4)
137
+ end
138
+
139
+ # This is empty because the #regenerate method is automatically run
140
+ # when changing the minimum_brightness_diff or minimum_color_diff.
141
+ def test_regenerate
142
+ end
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,160 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Colour management with Ruby.
4
+ #
5
+ # Copyright 2005 Austin Ziegler
6
+ # http://rubyforge.org/ruby-pdf/
7
+ #
8
+ # Licensed under a MIT-style licence.
9
+ #
10
+ # $Id: test_rgb.rb,v 1.1 2005/08/08 02:44:17 austin Exp $
11
+ #++
12
+
13
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
14
+ require 'test/unit' unless defined? $ZENTEST and $ZENTEST
15
+ require 'color'
16
+
17
+ module TestColor
18
+ class TestRGB < Test::Unit::TestCase
19
+ def test_adjust_brightness
20
+ assert_equal("#1a1aff", Color::RGB::Blue.adjust_brightness(10).html)
21
+ assert_equal("#0000e6", Color::RGB::Blue.adjust_brightness(-10).html)
22
+ end
23
+
24
+ def test_adjust_hue
25
+ assert_equal("#6600ff", Color::RGB::Blue.adjust_hue(10).html)
26
+ assert_equal("#0066ff", Color::RGB::Blue.adjust_hue(-10).html)
27
+ end
28
+
29
+ def test_adjust_saturation
30
+ assert_equal("#ef9374",
31
+ Color::RGB::DarkSalmon.adjust_saturation(10).html)
32
+ assert_equal("#e39980",
33
+ Color::RGB::DarkSalmon.adjust_saturation(-10).html)
34
+ end
35
+
36
+ def test_b
37
+ assert_in_delta(1.0, Color::RGB::Blue.b, 1e-4)
38
+ end
39
+
40
+ def test_b_equals
41
+ b = Color::RGB::Blue.dup
42
+ assert_in_delta(1.0, b.b, 1e-4)
43
+ assert_nothing_raised { b.b = 0.33 }
44
+ assert_in_delta(0.33, b.b, 1e-4)
45
+ assert_nothing_raised { b.b = 3.3 }
46
+ assert_in_delta(1.0, b.b, 1e-4)
47
+ assert_nothing_raised { b.b = -3.3 }
48
+ assert_in_delta(0.0, b.b, 1e-4)
49
+ end
50
+
51
+ def test_brightness
52
+ assert_in_delta(0.0, Color::RGB::Black.brightness, 1e-4)
53
+ assert_in_delta(0.5, Color::RGB::Grey50.brightness, 1e-4)
54
+ assert_in_delta(1.0, Color::RGB::White.brightness, 1e-4)
55
+ end
56
+
57
+ def test_darken_by
58
+ assert_in_delta(0.5, Color::RGB::Blue.darken_by(50).b, 1e-4)
59
+ end
60
+
61
+ def test_g
62
+ assert_in_delta(1.0, Color::RGB::Lime.g, 1e-4)
63
+ end
64
+
65
+ def test_g_equals
66
+ g = Color::RGB::Lime.dup
67
+ assert_in_delta(1.0, g.g, 1e-4)
68
+ assert_nothing_raised { g.g = 0.33 }
69
+ assert_in_delta(0.33, g.g, 1e-4)
70
+ assert_nothing_raised { g.g = 3.3 }
71
+ assert_in_delta(1.0, g.g, 1e-4)
72
+ assert_nothing_raised { g.g = -3.3 }
73
+ assert_in_delta(0.0, g.g, 1e-4)
74
+ end
75
+
76
+ def test_html
77
+ assert_equal("#000000", Color::RGB::Black.html)
78
+ assert_equal("#0000ff", Color::RGB::Blue.html)
79
+ assert_equal("#00ff00", Color::RGB::Lime.html)
80
+ assert_equal("#ff0000", Color::RGB::Red.html)
81
+ assert_equal("#ffffff", Color::RGB::White.html)
82
+ end
83
+
84
+ def test_lighten_by
85
+ assert_in_delta(1.0, Color::RGB::Blue.lighten_by(50).b, 1e-4)
86
+ assert_in_delta(0.5, Color::RGB::Blue.lighten_by(50).r, 1e-4)
87
+ assert_in_delta(0.5, Color::RGB::Blue.lighten_by(50).g, 1e-4)
88
+ end
89
+
90
+ def test_mix_with
91
+ assert_in_delta(0.5, Color::RGB::Red.mix_with(Color::RGB::Blue, 50).r,
92
+ 1e-4)
93
+ assert_in_delta(0.0, Color::RGB::Red.mix_with(Color::RGB::Blue, 50).g,
94
+ 1e-4)
95
+ assert_in_delta(0.5, Color::RGB::Red.mix_with(Color::RGB::Blue, 50).b,
96
+ 1e-4)
97
+ assert_in_delta(0.5, Color::RGB::Blue.mix_with(Color::RGB::Red, 50).r,
98
+ 1e-4)
99
+ assert_in_delta(0.0, Color::RGB::Blue.mix_with(Color::RGB::Red, 50).g,
100
+ 1e-4)
101
+ assert_in_delta(0.5, Color::RGB::Blue.mix_with(Color::RGB::Red, 50).b,
102
+ 1e-4)
103
+ end
104
+
105
+ def test_pdf_fill
106
+ assert_equal("0.000 0.000 0.000 rg", Color::RGB::Black.pdf_fill)
107
+ assert_equal("0.000 0.000 1.000 rg", Color::RGB::Blue.pdf_fill)
108
+ assert_equal("0.000 1.000 0.000 rg", Color::RGB::Lime.pdf_fill)
109
+ assert_equal("1.000 0.000 0.000 rg", Color::RGB::Red.pdf_fill)
110
+ assert_equal("1.000 1.000 1.000 rg", Color::RGB::White.pdf_fill)
111
+ end
112
+
113
+ def test_pdf_stroke
114
+ assert_equal("0.000 0.000 0.000 RG", Color::RGB::Black.pdf_stroke)
115
+ assert_equal("0.000 0.000 1.000 RG", Color::RGB::Blue.pdf_stroke)
116
+ assert_equal("0.000 1.000 0.000 RG", Color::RGB::Lime.pdf_stroke)
117
+ assert_equal("1.000 0.000 0.000 RG", Color::RGB::Red.pdf_stroke)
118
+ assert_equal("1.000 1.000 1.000 RG", Color::RGB::White.pdf_stroke)
119
+ end
120
+
121
+ def test_r
122
+ assert_in_delta(1.0, Color::RGB::Red.r, 1e-4)
123
+ end
124
+
125
+ def test_r_equals
126
+ r = Color::RGB::Red.dup
127
+ assert_in_delta(1.0, r.r, 1e-4)
128
+ assert_nothing_raised { r.r = 0.33 }
129
+ assert_in_delta(0.33, r.r, 1e-4)
130
+ assert_nothing_raised { r.r = 3.3 }
131
+ assert_in_delta(1.0, r.r, 1e-4)
132
+ assert_nothing_raised { r.r = -3.3 }
133
+ assert_in_delta(0.0, r.r, 1e-4)
134
+ end
135
+
136
+ def test_to_cmyk
137
+ assert_kind_of(Color::CMYK, Color::RGB::Black.to_cmyk)
138
+ end
139
+
140
+ def test_to_grayscale
141
+ assert_kind_of(Color::GrayScale, Color::RGB::Black.to_grayscale)
142
+ end
143
+
144
+ def test_to_greyscale
145
+ assert_kind_of(Color::GreyScale, Color::RGB::Black.to_greyscale)
146
+ end
147
+
148
+ def test_to_hsl
149
+ assert_kind_of(Color::HSL, Color::RGB::Black.to_hsl)
150
+ end
151
+
152
+ def test_to_rgb
153
+ assert_equal(Color::RGB::Black, Color::RGB::Black.to_rgb)
154
+ end
155
+
156
+ def test_to_yiq
157
+ assert_kind_of(Color::YIQ, Color::RGB::Black.to_yiq)
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Colour management with Ruby.
4
+ #
5
+ # Copyright 2005 Austin Ziegler
6
+ # http://rubyforge.org/ruby-pdf/
7
+ #
8
+ # Licensed under a MIT-style licence.
9
+ #
10
+ # $Id: test_yiq.rb,v 1.1 2005/08/08 02:44:17 austin Exp $
11
+ #++
12
+
13
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
14
+ require 'test/unit' unless defined? $ZENTEST and $ZENTEST
15
+ require 'color'
16
+
17
+ module TestColor
18
+ class TestYIQ < Test::Unit::TestCase
19
+ def setup
20
+ @yiq = Color::YIQ.from_fraction(0.1, 0.2, 0.3)
21
+ end
22
+
23
+ def test_brightness
24
+ assert_in_delta(0.1, @yiq.brightness, 1e-4)
25
+ end
26
+
27
+ def test_i
28
+ assert_in_delta(0.2, @yiq.i, 1e-4)
29
+ end
30
+
31
+ def test_i_equals
32
+ assert_in_delta(0.2, @yiq.i, 1e-4)
33
+ assert_nothing_raised { @yiq.i = 0.5 }
34
+ assert_in_delta(0.5, @yiq.i, 1e-4)
35
+ assert_nothing_raised { @yiq.i = 5 }
36
+ assert_in_delta(1.0, @yiq.i, 1e-4)
37
+ assert_nothing_raised { @yiq.i = -5 }
38
+ assert_in_delta(0.0, @yiq.i, 1e-4)
39
+ end
40
+
41
+ def test_q
42
+ assert_in_delta(0.3, @yiq.q, 1e-4)
43
+ end
44
+
45
+ def test_q_equals
46
+ assert_in_delta(0.3, @yiq.q, 1e-4)
47
+ assert_nothing_raised { @yiq.q = 0.5 }
48
+ assert_in_delta(0.5, @yiq.q, 1e-4)
49
+ assert_nothing_raised { @yiq.q = 5 }
50
+ assert_in_delta(1.0, @yiq.q, 1e-4)
51
+ assert_nothing_raised { @yiq.q = -5 }
52
+ assert_in_delta(0.0, @yiq.q, 1e-4)
53
+ end
54
+
55
+ def test_to_grayscale
56
+ assert_kind_of(Color::GrayScale, @yiq.to_grayscale)
57
+ end
58
+
59
+ def test_to_greyscale
60
+ assert_kind_of(Color::GreyScale, @yiq.to_greyscale)
61
+ end
62
+
63
+ def test_to_yiq
64
+ assert_equal(@yiq, @yiq.to_yiq)
65
+ end
66
+
67
+ def test_y
68
+ assert_in_delta(0.1, @yiq.y, 1e-4)
69
+ end
70
+
71
+ def test_y_equals
72
+ assert_in_delta(0.1, @yiq.y, 1e-4)
73
+ assert_nothing_raised { @yiq.y = 0.5 }
74
+ assert_in_delta(0.5, @yiq.y, 1e-4)
75
+ assert_nothing_raised { @yiq.y = 5 }
76
+ assert_in_delta(1.0, @yiq.y, 1e-4)
77
+ assert_nothing_raised { @yiq.y = -5 }
78
+ assert_in_delta(0.0, @yiq.y, 1e-4)
79
+ end
80
+ end
81
+ end