spectrum 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,153 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Color
4
+ # Colour management with Ruby
5
+ # http://rubyforge.org/projects/color
6
+ # Version 1.5.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 - 2010 Austin Ziegler and Matt Lyon
12
+ #
13
+ # HSL Tests provided by Adam Johnson
14
+ #++
15
+
16
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
17
+ require 'test/unit'
18
+ require 'spectrum'
19
+
20
+ module TestColor
21
+ class TestHSL < Test::Unit::TestCase
22
+ def setup
23
+ # @hsl = Spectrum::HSL.new(262, 67, 42)
24
+ @hsl = Spectrum::HSL.new(145, 20, 30)
25
+ # @rgb = Spectrum::RGB.new(88, 35, 179)
26
+ end
27
+
28
+ def test_rgb_roundtrip_conversion
29
+ hsl = Spectrum::HSL.new(262, 67, 42)
30
+ c = hsl.to_rgb.to_hsl
31
+ assert_in_delta hsl.h, c.h, Spectrum::COLOR_TOLERANCE, "Hue"
32
+ assert_in_delta hsl.s, c.s, Spectrum::COLOR_TOLERANCE, "Saturation"
33
+ assert_in_delta hsl.l, c.l, Spectrum::COLOR_TOLERANCE, "Luminance"
34
+ end
35
+
36
+ def test_brightness
37
+ assert_in_delta 0.3, @hsl.brightness, Spectrum::COLOR_TOLERANCE
38
+ end
39
+
40
+ def test_hue
41
+ assert_in_delta 0.4027, @hsl.h, Spectrum::COLOR_TOLERANCE
42
+ assert_in_delta 145, @hsl.hue, Spectrum::COLOR_TOLERANCE
43
+ assert_nothing_raised { @hsl.hue = 33 }
44
+ assert_in_delta 0.09167, @hsl.h, Spectrum::COLOR_TOLERANCE
45
+ assert_nothing_raised { @hsl.hue = -33 }
46
+ assert_in_delta 0.90833, @hsl.h, Spectrum::COLOR_TOLERANCE
47
+ assert_nothing_raised { @hsl.h = 3.3 }
48
+ assert_in_delta 360, @hsl.hue, Spectrum::COLOR_TOLERANCE
49
+ assert_nothing_raised { @hsl.h = -3.3 }
50
+ assert_in_delta 0.0, @hsl.h, Spectrum::COLOR_TOLERANCE
51
+ assert_nothing_raised { @hsl.hue = 0 }
52
+ assert_nothing_raised { @hsl.hue -= 20 }
53
+ assert_in_delta 340, @hsl.hue, Spectrum::COLOR_TOLERANCE
54
+ assert_nothing_raised { @hsl.hue += 45 }
55
+ assert_in_delta 25, @hsl.hue, Spectrum::COLOR_TOLERANCE
56
+ end
57
+
58
+ def test_saturation
59
+ assert_in_delta 0.2, @hsl.s, Spectrum::COLOR_TOLERANCE
60
+ assert_in_delta 20, @hsl.saturation, Spectrum::COLOR_TOLERANCE
61
+ assert_nothing_raised { @hsl.saturation = 33 }
62
+ assert_in_delta 0.33, @hsl.s, Spectrum::COLOR_TOLERANCE
63
+ assert_nothing_raised { @hsl.s = 3.3 }
64
+ assert_in_delta 100, @hsl.saturation, Spectrum::COLOR_TOLERANCE
65
+ assert_nothing_raised { @hsl.s = -3.3 }
66
+ assert_in_delta 0.0, @hsl.s, Spectrum::COLOR_TOLERANCE
67
+ end
68
+
69
+ def test_luminance
70
+ assert_in_delta 0.3, @hsl.l, Spectrum::COLOR_TOLERANCE
71
+ assert_in_delta 30, @hsl.luminosity, Spectrum::COLOR_TOLERANCE
72
+ assert_nothing_raised { @hsl.luminosity = 33 }
73
+ assert_in_delta 0.33, @hsl.l, Spectrum::COLOR_TOLERANCE
74
+ assert_nothing_raised { @hsl.l = 3.3 }
75
+ assert_in_delta 100, @hsl.lightness, Spectrum::COLOR_TOLERANCE
76
+ assert_nothing_raised { @hsl.l = -3.3 }
77
+ assert_in_delta 0.0, @hsl.l, Spectrum::COLOR_TOLERANCE
78
+ end
79
+
80
+ def test_html_css
81
+ assert_equal "hsl(145.00, 20.00%, 30.00%)", @hsl.css_hsl
82
+ assert_equal "hsla(145.00, 20.00%, 30.00%, 1.00)", @hsl.css_hsla
83
+ end
84
+
85
+ def test_to_cmyk
86
+ cmyk = nil
87
+ assert_nothing_raised { cmyk = @hsl.to_cmyk }
88
+ assert_kind_of Spectrum::CMYK, cmyk
89
+ assert_in_delta 0.3223, cmyk.c, Spectrum::COLOR_TOLERANCE
90
+ assert_in_delta 0.2023, cmyk.m, Spectrum::COLOR_TOLERANCE
91
+ assert_in_delta 0.2723, cmyk.y, Spectrum::COLOR_TOLERANCE
92
+ assert_in_delta 0.4377, cmyk.k, Spectrum::COLOR_TOLERANCE
93
+ end
94
+
95
+ def test_to_grayscale
96
+ gs = nil
97
+ assert_nothing_raised { gs = @hsl.to_grayscale }
98
+ assert_kind_of Spectrum::GreyScale, gs
99
+ assert_in_delta 30, gs.gray, Spectrum::COLOR_TOLERANCE
100
+ end
101
+
102
+ def test_to_rgb
103
+ rgb = nil
104
+ assert_nothing_raised { rgb = @hsl.to_rgb }
105
+ assert_kind_of Spectrum::RGB, rgb
106
+ assert_in_delta 0.24, rgb.r, Spectrum::COLOR_TOLERANCE
107
+ assert_in_delta 0.36, rgb.g, Spectrum::COLOR_TOLERANCE
108
+ assert_in_delta 0.29, rgb.b, Spectrum::COLOR_TOLERANCE
109
+ assert_equal "#3d5c4a", @hsl.html
110
+ assert_equal "rgb(24.00%, 36.00%, 29.00%)", @hsl.css_rgb
111
+ assert_equal "rgba(24.00%, 36.00%, 29.00%, 1.00)", @hsl.css_rgba
112
+ # The following tests address a bug reported by Jean Krohn on June 6,
113
+ # 2006 and excercise some previously unexercised code in to_rgb.
114
+ assert_equal Spectrum::RGB::Black, Spectrum::HSL.new(75, 75, 0)
115
+ assert_equal Spectrum::RGB::White, Spectrum::HSL.new(75, 75, 100)
116
+ assert_equal Spectrum::RGB::Gray80, Spectrum::HSL.new(75, 0, 80)
117
+
118
+ # The following tests a bug reported by Adam Johnson on 29 October
119
+ # 2010.
120
+ rgb = Spectrum::RGB.from_fraction(0.34496, 0.1386, 0.701399)
121
+ c = Spectrum::HSL.new(262, 67, 42).to_rgb
122
+ assert_in_delta rgb.r, c.r, Spectrum::COLOR_TOLERANCE, "Red"
123
+ assert_in_delta rgb.g, c.g, Spectrum::COLOR_TOLERANCE, "Green"
124
+ assert_in_delta rgb.b, c.b, Spectrum::COLOR_TOLERANCE, "Blue"
125
+ end
126
+
127
+ def test_to_yiq
128
+ yiq = nil
129
+ assert_nothing_raised { yiq = @hsl.to_yiq }
130
+ assert_kind_of Spectrum::YIQ, yiq
131
+ assert_in_delta 0.3161, yiq.y, Spectrum::COLOR_TOLERANCE
132
+ assert_in_delta 0.0, yiq.i, Spectrum::COLOR_TOLERANCE
133
+ assert_in_delta 0.0, yiq.q, Spectrum::COLOR_TOLERANCE
134
+ end
135
+
136
+ def test_mix_with
137
+ red = Spectrum::RGB::Red.to_hsl
138
+ yellow = Spectrum::RGB::Yellow.to_hsl
139
+ assert_in_delta 0, red.hue, Spectrum::COLOR_TOLERANCE
140
+ assert_in_delta 60, yellow.hue, Spectrum::COLOR_TOLERANCE
141
+ ry25 = red.mix_with yellow, 0.25
142
+ assert_in_delta 15, ry25.hue, Spectrum::COLOR_TOLERANCE
143
+ ry50 = red.mix_with yellow, 0.50
144
+ assert_in_delta 30, ry50.hue, Spectrum::COLOR_TOLERANCE
145
+ ry75 = red.mix_with yellow, 0.75
146
+ assert_in_delta 45, ry75.hue, Spectrum::COLOR_TOLERANCE
147
+ end
148
+
149
+ def test_inspect
150
+ assert_equal "HSL [145.00 deg, 20.00%, 30.00%]", @hsl.inspect
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,142 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Color
4
+ # Colour management with Ruby
5
+ # http://rubyforge.org/projects/color
6
+ # Version 1.5.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 - 2010 Austin Ziegler and Matt Lyon
12
+ #++
13
+
14
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
15
+ require 'test/unit'
16
+ require 'spectrum'
17
+ require 'spectrum/palette/monocontrast'
18
+
19
+ module TestColor
20
+ module TestPalette
21
+ class TestMonoContrast < Test::Unit::TestCase
22
+ include Spectrum::Palette
23
+ def setup
24
+ @high = Spectrum::RGB.from_html("#c9e3a6")
25
+ @low = Spectrum::RGB.from_html("#746b8e")
26
+ @mcp1 = MonoContrast.new(@high)
27
+ @mcp2 = MonoContrast.new(@low)
28
+ end
29
+
30
+ def test_background
31
+ assert_equal("#141711", @mcp1.background[-5].html)
32
+ assert_equal("#32392a", @mcp1.background[-4].html)
33
+ assert_equal("#657253", @mcp1.background[-3].html)
34
+ assert_equal("#97aa7d", @mcp1.background[-2].html)
35
+ assert_equal("#abc18d", @mcp1.background[-1].html)
36
+ assert_equal("#c9e3a6", @mcp1.background[ 0].html)
37
+ assert_equal("#d1e7b3", @mcp1.background[+1].html)
38
+ assert_equal("#d7eabc", @mcp1.background[+2].html) # d7eabd
39
+ assert_equal("#e4f1d3", @mcp1.background[+3].html) # e5f2d3
40
+ assert_equal("#f2f8e9", @mcp1.background[+4].html) # f1f8e9
41
+ assert_equal("#fafcf6", @mcp1.background[+5].html) # fafdf7
42
+
43
+ assert_equal("#0c0b0e", @mcp2.background[-5].html)
44
+ assert_equal("#1d1b24", @mcp2.background[-4].html)
45
+ assert_equal("#3a3647", @mcp2.background[-3].html)
46
+ assert_equal("#57506b", @mcp2.background[-2].html)
47
+ assert_equal("#635b79", @mcp2.background[-1].html)
48
+ assert_equal("#746b8e", @mcp2.background[ 0].html)
49
+ assert_equal("#89819f", @mcp2.background[+1].html)
50
+ assert_equal("#9790aa", @mcp2.background[+2].html) # 9790ab
51
+ assert_equal("#bab5c7", @mcp2.background[+3].html) # bab6c7
52
+ assert_equal("#dcdae3", @mcp2.background[+4].html)
53
+ assert_equal("#f1f0f4", @mcp2.background[+5].html) # f2f1f4
54
+ end
55
+
56
+ def test_brightness_diff
57
+ bd1 = @mcp1.brightness_diff(@high, @low)
58
+ bd2 = @mcp1.brightness_diff(@low, @high)
59
+ assert_in_delta(bd1, bd2, Spectrum::COLOR_TOLERANCE)
60
+ end
61
+
62
+ def test_calculate_foreground
63
+ assert_equal("#ffffff", @mcp1.calculate_foreground(@low, @high).html)
64
+ assert_equal("#1d1b24", @mcp1.calculate_foreground(@high, @low).html)
65
+ end
66
+
67
+ def test_color_diff
68
+ assert_in_delta(@mcp1.color_diff(@low, @high),
69
+ @mcp1.color_diff(@high, @low),
70
+ Spectrum::COLOR_TOLERANCE)
71
+ end
72
+
73
+ def test_foreground
74
+ assert_equal("#c9e3a6", @mcp1.foreground[-5].html)
75
+ assert_equal("#e4f1d3", @mcp1.foreground[-4].html) # e5f2d3
76
+ assert_equal("#ffffff", @mcp1.foreground[-3].html)
77
+ assert_equal("#000000", @mcp1.foreground[-2].html)
78
+ assert_equal("#000000", @mcp1.foreground[-1].html)
79
+ assert_equal("#000000", @mcp1.foreground[ 0].html)
80
+ assert_equal("#000000", @mcp1.foreground[+1].html)
81
+ assert_equal("#000000", @mcp1.foreground[+2].html)
82
+ assert_equal("#32392a", @mcp1.foreground[+3].html)
83
+ assert_equal("#32392a", @mcp1.foreground[+4].html)
84
+ assert_equal("#32392a", @mcp1.foreground[+5].html)
85
+
86
+ assert_equal("#bab5c7", @mcp2.foreground[-5].html) # bab6c7
87
+ assert_equal("#dcdae3", @mcp2.foreground[-4].html)
88
+ assert_equal("#ffffff", @mcp2.foreground[-3].html)
89
+ assert_equal("#ffffff", @mcp2.foreground[-2].html)
90
+ assert_equal("#ffffff", @mcp2.foreground[-1].html)
91
+ assert_equal("#ffffff", @mcp2.foreground[ 0].html)
92
+ assert_equal("#000000", @mcp2.foreground[+1].html)
93
+ assert_equal("#000000", @mcp2.foreground[+2].html)
94
+ assert_equal("#000000", @mcp2.foreground[+3].html)
95
+ assert_equal("#1d1b24", @mcp2.foreground[+4].html)
96
+ assert_equal("#3a3647", @mcp2.foreground[+5].html)
97
+ end
98
+
99
+ def test_minimum_brightness_diff
100
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_BRIGHTNESS_DIFF,
101
+ @mcp1.minimum_brightness_diff, Spectrum::COLOR_TOLERANCE)
102
+ end
103
+
104
+ def test_minimum_brightness_diff_equals
105
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_BRIGHTNESS_DIFF,
106
+ @mcp1.minimum_brightness_diff, Spectrum::COLOR_TOLERANCE)
107
+ mcps = @mcp1.dup
108
+ assert_nothing_raised { @mcp1.minimum_brightness_diff = 0.75 }
109
+ assert_in_delta(0.75, @mcp1.minimum_brightness_diff, Spectrum::COLOR_TOLERANCE)
110
+ assert_not_equal(@mcp1.foreground[-5], mcps.foreground[-5])
111
+ assert_nothing_raised { @mcp1.minimum_brightness_diff = 4.0 }
112
+ assert_in_delta(1, @mcp1.minimum_brightness_diff, Spectrum::COLOR_TOLERANCE)
113
+ assert_nothing_raised { @mcp1.minimum_brightness_diff = -4.0 }
114
+ assert_in_delta(0, @mcp1.minimum_brightness_diff, Spectrum::COLOR_TOLERANCE)
115
+ assert_nothing_raised { @mcp1.minimum_brightness_diff = nil }
116
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_BRIGHTNESS_DIFF,
117
+ @mcp1.minimum_brightness_diff, Spectrum::COLOR_TOLERANCE)
118
+ end
119
+
120
+ def test_minimum_color_diff
121
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_COLOR_DIFF,
122
+ @mcp1.minimum_color_diff, Spectrum::COLOR_TOLERANCE)
123
+ end
124
+
125
+ def test_minimum_color_diff_equals
126
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_COLOR_DIFF,
127
+ @mcp1.minimum_color_diff, Spectrum::COLOR_TOLERANCE)
128
+ mcps = @mcp1.dup
129
+ assert_nothing_raised { @mcp1.minimum_color_diff = 0.75 }
130
+ assert_in_delta(0.75, @mcp1.minimum_color_diff, Spectrum::COLOR_TOLERANCE)
131
+ assert_not_equal(@mcp1.foreground[-5], mcps.foreground[-5])
132
+ assert_nothing_raised { @mcp1.minimum_color_diff = 4.0 }
133
+ assert_in_delta(3, @mcp1.minimum_color_diff, Spectrum::COLOR_TOLERANCE)
134
+ assert_nothing_raised { @mcp1.minimum_color_diff = -4.0 }
135
+ assert_in_delta(0, @mcp1.minimum_color_diff, Spectrum::COLOR_TOLERANCE)
136
+ assert_nothing_raised { @mcp1.minimum_color_diff = nil }
137
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_COLOR_DIFF,
138
+ @mcp1.minimum_color_diff, Spectrum::COLOR_TOLERANCE)
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,344 @@
1
+ #!/usr/bin/env ruby
2
+ #--
3
+ # Color
4
+ # Colour management with Ruby
5
+ # http://rubyforge.org/projects/color
6
+ # Version 1.5.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 - 2010 Austin Ziegler and Matt Lyon
12
+ #++
13
+
14
+ $LOAD_PATH.unshift("#{File.dirname(__FILE__)}/../lib") if __FILE__ == $0
15
+ require 'test/unit'
16
+ require 'spectrum'
17
+
18
+ module TestColor
19
+ class TestRGB < Test::Unit::TestCase
20
+ def test_adjust_brightness
21
+ assert_equal("#1a1aff", Spectrum::RGB::Blue.adjust_brightness(10).html)
22
+ assert_equal("#0000e6", Spectrum::RGB::Blue.adjust_brightness(-10).html)
23
+ end
24
+
25
+ def test_adjust_hue
26
+ assert_equal("#6600ff", Spectrum::RGB::Blue.adjust_hue(10).html)
27
+ assert_equal("#0066ff", Spectrum::RGB::Blue.adjust_hue(-10).html)
28
+ end
29
+
30
+ def test_adjust_saturation
31
+ assert_equal("#ef9374",
32
+ Spectrum::RGB::DarkSalmon.adjust_saturation(10).html)
33
+ assert_equal("#e39980",
34
+ Spectrum::RGB::DarkSalmon.adjust_saturation(-10).html)
35
+ end
36
+
37
+ def test_red
38
+ red = Spectrum::RGB::Red.dup
39
+ assert_in_delta(1.0, red.r, Spectrum::COLOR_TOLERANCE)
40
+ assert_in_delta(100, red.red_p, Spectrum::COLOR_TOLERANCE)
41
+ assert_in_delta(255, red.red, Spectrum::COLOR_TOLERANCE)
42
+ assert_in_delta(1.0, red.r, Spectrum::COLOR_TOLERANCE)
43
+ assert_nothing_raised { red.red_p = 33 }
44
+ assert_in_delta(0.33, red.r, Spectrum::COLOR_TOLERANCE)
45
+ assert_nothing_raised { red.red = 330 }
46
+ assert_in_delta(1.0, red.r, Spectrum::COLOR_TOLERANCE)
47
+ assert_nothing_raised { red.r = -3.3 }
48
+ assert_in_delta(0.0, red.r, Spectrum::COLOR_TOLERANCE)
49
+ end
50
+
51
+ def test_green
52
+ lime = Spectrum::RGB::Lime.dup
53
+ assert_in_delta(1.0, lime.g, Spectrum::COLOR_TOLERANCE)
54
+ assert_in_delta(100, lime.green_p, Spectrum::COLOR_TOLERANCE)
55
+ assert_in_delta(255, lime.green, Spectrum::COLOR_TOLERANCE)
56
+ assert_nothing_raised { lime.green_p = 33 }
57
+ assert_in_delta(0.33, lime.g, Spectrum::COLOR_TOLERANCE)
58
+ assert_nothing_raised { lime.green = 330 }
59
+ assert_in_delta(1.0, lime.g, Spectrum::COLOR_TOLERANCE)
60
+ assert_nothing_raised { lime.g = -3.3 }
61
+ assert_in_delta(0.0, lime.g, Spectrum::COLOR_TOLERANCE)
62
+ end
63
+
64
+ def test_blue
65
+ blue = Spectrum::RGB::Blue.dup
66
+ assert_in_delta(1.0, blue.b, Spectrum::COLOR_TOLERANCE)
67
+ assert_in_delta(255, blue.blue, Spectrum::COLOR_TOLERANCE)
68
+ assert_in_delta(100, blue.blue_p, Spectrum::COLOR_TOLERANCE)
69
+ assert_nothing_raised { blue.blue_p = 33 }
70
+ assert_in_delta(0.33, blue.b, Spectrum::COLOR_TOLERANCE)
71
+ assert_nothing_raised { blue.blue = 330 }
72
+ assert_in_delta(1.0, blue.b, Spectrum::COLOR_TOLERANCE)
73
+ assert_nothing_raised { blue.b = -3.3 }
74
+ assert_in_delta(0.0, blue.b, Spectrum::COLOR_TOLERANCE)
75
+ end
76
+
77
+ def test_brightness
78
+ assert_in_delta(0.0, Spectrum::RGB::Black.brightness, Spectrum::COLOR_TOLERANCE)
79
+ assert_in_delta(0.5, Spectrum::RGB::Grey50.brightness, Spectrum::COLOR_TOLERANCE)
80
+ assert_in_delta(1.0, Spectrum::RGB::White.brightness, Spectrum::COLOR_TOLERANCE)
81
+ end
82
+
83
+ def test_darken_by
84
+ assert_in_delta(0.5, Spectrum::RGB::Blue.darken_by(50).b,
85
+ Spectrum::COLOR_TOLERANCE)
86
+ end
87
+
88
+ def test_html
89
+ assert_equal("#000000", Spectrum::RGB::Black.html)
90
+ assert_equal(Spectrum::RGB::Black, Spectrum::RGB.from_html("#000000"))
91
+ assert_equal("#0000ff", Spectrum::RGB::Blue.html)
92
+ assert_equal("#00ff00", Spectrum::RGB::Lime.html)
93
+ assert_equal("#ff0000", Spectrum::RGB::Red.html)
94
+ assert_equal("#ffffff", Spectrum::RGB::White.html)
95
+
96
+ assert_equal("rgb(0.00%, 0.00%, 0.00%)", Spectrum::RGB::Black.css_rgb)
97
+ assert_equal("rgb(0.00%, 0.00%, 100.00%)", Spectrum::RGB::Blue.css_rgb)
98
+ assert_equal("rgb(0.00%, 100.00%, 0.00%)", Spectrum::RGB::Lime.css_rgb)
99
+ assert_equal("rgb(100.00%, 0.00%, 0.00%)", Spectrum::RGB::Red.css_rgb)
100
+ assert_equal("rgb(100.00%, 100.00%, 100.00%)", Spectrum::RGB::White.css_rgb)
101
+
102
+ assert_equal("rgba(0.00%, 0.00%, 0.00%, 1.00)", Spectrum::RGB::Black.css_rgba)
103
+ assert_equal("rgba(0.00%, 0.00%, 100.00%, 1.00)", Spectrum::RGB::Blue.css_rgba)
104
+ assert_equal("rgba(0.00%, 100.00%, 0.00%, 1.00)", Spectrum::RGB::Lime.css_rgba)
105
+ assert_equal("rgba(100.00%, 0.00%, 0.00%, 1.00)", Spectrum::RGB::Red.css_rgba)
106
+ assert_equal("rgba(100.00%, 100.00%, 100.00%, 1.00)",
107
+ Spectrum::RGB::White.css_rgba)
108
+ end
109
+
110
+ def test_lighten_by
111
+ assert_in_delta(1.0, Spectrum::RGB::Blue.lighten_by(50).b,
112
+ Spectrum::COLOR_TOLERANCE)
113
+ assert_in_delta(0.5, Spectrum::RGB::Blue.lighten_by(50).r,
114
+ Spectrum::COLOR_TOLERANCE)
115
+ assert_in_delta(0.5, Spectrum::RGB::Blue.lighten_by(50).g,
116
+ Spectrum::COLOR_TOLERANCE)
117
+ end
118
+
119
+ def test_mix_with
120
+ assert_in_delta(0.5, Spectrum::RGB::Red.mix_with(Spectrum::RGB::Blue, 50).r,
121
+ Spectrum::COLOR_TOLERANCE)
122
+ assert_in_delta(0.0, Spectrum::RGB::Red.mix_with(Spectrum::RGB::Blue, 50).g,
123
+ Spectrum::COLOR_TOLERANCE)
124
+ assert_in_delta(0.5, Spectrum::RGB::Red.mix_with(Spectrum::RGB::Blue, 50).b,
125
+ Spectrum::COLOR_TOLERANCE)
126
+ assert_in_delta(0.5, Spectrum::RGB::Blue.mix_with(Spectrum::RGB::Red, 50).r,
127
+ Spectrum::COLOR_TOLERANCE)
128
+ assert_in_delta(0.0, Spectrum::RGB::Blue.mix_with(Spectrum::RGB::Red, 50).g,
129
+ Spectrum::COLOR_TOLERANCE)
130
+ assert_in_delta(0.5, Spectrum::RGB::Blue.mix_with(Spectrum::RGB::Red, 50).b,
131
+ Spectrum::COLOR_TOLERANCE)
132
+ end
133
+
134
+ def test_pdf_fill
135
+ assert_equal("0.000 0.000 0.000 rg", Spectrum::RGB::Black.pdf_fill)
136
+ assert_equal("0.000 0.000 1.000 rg", Spectrum::RGB::Blue.pdf_fill)
137
+ assert_equal("0.000 1.000 0.000 rg", Spectrum::RGB::Lime.pdf_fill)
138
+ assert_equal("1.000 0.000 0.000 rg", Spectrum::RGB::Red.pdf_fill)
139
+ assert_equal("1.000 1.000 1.000 rg", Spectrum::RGB::White.pdf_fill)
140
+ assert_equal("0.000 0.000 0.000 RG", Spectrum::RGB::Black.pdf_stroke)
141
+ assert_equal("0.000 0.000 1.000 RG", Spectrum::RGB::Blue.pdf_stroke)
142
+ assert_equal("0.000 1.000 0.000 RG", Spectrum::RGB::Lime.pdf_stroke)
143
+ assert_equal("1.000 0.000 0.000 RG", Spectrum::RGB::Red.pdf_stroke)
144
+ assert_equal("1.000 1.000 1.000 RG", Spectrum::RGB::White.pdf_stroke)
145
+ end
146
+
147
+ def test_to_cmyk
148
+ assert_kind_of(Spectrum::CMYK, Spectrum::RGB::Black.to_cmyk)
149
+ assert_equal(Spectrum::CMYK.new(0, 0, 0, 100), Spectrum::RGB::Black.to_cmyk)
150
+ assert_equal(Spectrum::CMYK.new(0, 0, 100, 0),
151
+ Spectrum::RGB::Yellow.to_cmyk)
152
+ assert_equal(Spectrum::CMYK.new(100, 0, 0, 0), Spectrum::RGB::Cyan.to_cmyk)
153
+ assert_equal(Spectrum::CMYK.new(0, 100, 0, 0),
154
+ Spectrum::RGB::Magenta.to_cmyk)
155
+ assert_equal(Spectrum::CMYK.new(0, 100, 100, 0), Spectrum::RGB::Red.to_cmyk)
156
+ assert_equal(Spectrum::CMYK.new(100, 0, 100, 0),
157
+ Spectrum::RGB::Lime.to_cmyk)
158
+ assert_equal(Spectrum::CMYK.new(100, 100, 0, 0),
159
+ Spectrum::RGB::Blue.to_cmyk)
160
+ assert_equal(Spectrum::CMYK.new(10.32, 60.52, 10.32, 39.47),
161
+ Spectrum::RGB::Purple.to_cmyk)
162
+ assert_equal(Spectrum::CMYK.new(10.90, 59.13, 59.13, 24.39),
163
+ Spectrum::RGB::Brown.to_cmyk)
164
+ assert_equal(Spectrum::CMYK.new(0, 63.14, 18.43, 0),
165
+ Spectrum::RGB::Carnation.to_cmyk)
166
+ assert_equal(Spectrum::CMYK.new(7.39, 62.69, 62.69, 37.32),
167
+ Spectrum::RGB::Cayenne.to_cmyk)
168
+ end
169
+
170
+ def test_to_grayscale
171
+ assert_kind_of(Spectrum::GrayScale, Spectrum::RGB::Black.to_grayscale)
172
+ assert_equal(Spectrum::GrayScale.from_fraction(0),
173
+ Spectrum::RGB::Black.to_grayscale)
174
+ assert_equal(Spectrum::GrayScale.from_fraction(0.5),
175
+ Spectrum::RGB::Yellow.to_grayscale)
176
+ assert_equal(Spectrum::GrayScale.from_fraction(0.5),
177
+ Spectrum::RGB::Cyan.to_grayscale)
178
+ assert_equal(Spectrum::GrayScale.from_fraction(0.5),
179
+ Spectrum::RGB::Magenta.to_grayscale)
180
+ assert_equal(Spectrum::GrayScale.from_fraction(0.5),
181
+ Spectrum::RGB::Red.to_grayscale)
182
+ assert_equal(Spectrum::GrayScale.from_fraction(0.5),
183
+ Spectrum::RGB::Lime.to_grayscale)
184
+ assert_equal(Spectrum::GrayScale.from_fraction(0.5),
185
+ Spectrum::RGB::Blue.to_grayscale)
186
+ assert_equal(Spectrum::GrayScale.from_fraction(0.2510),
187
+ Spectrum::RGB::Purple.to_grayscale)
188
+ assert_equal(Spectrum::GrayScale.new(40.58),
189
+ Spectrum::RGB::Brown.to_grayscale)
190
+ assert_equal(Spectrum::GrayScale.new(68.43),
191
+ Spectrum::RGB::Carnation.to_grayscale)
192
+ assert_equal(Spectrum::GrayScale.new(27.65),
193
+ Spectrum::RGB::Cayenne.to_grayscale)
194
+ end
195
+
196
+ def test_to_hsl
197
+ assert_kind_of(Spectrum::HSL, Spectrum::RGB::Black.to_hsl)
198
+ assert_equal(Spectrum::HSL.new, Spectrum::RGB::Black.to_hsl)
199
+ assert_equal(Spectrum::HSL.new(60, 100, 50), Spectrum::RGB::Yellow.to_hsl)
200
+ assert_equal(Spectrum::HSL.new(180, 100, 50), Spectrum::RGB::Cyan.to_hsl)
201
+ assert_equal(Spectrum::HSL.new(300, 100, 50), Spectrum::RGB::Magenta.to_hsl)
202
+ assert_equal(Spectrum::HSL.new(0, 100, 50), Spectrum::RGB::Red.to_hsl)
203
+ assert_equal(Spectrum::HSL.new(120, 100, 50), Spectrum::RGB::Lime.to_hsl)
204
+ assert_equal(Spectrum::HSL.new(240, 100, 50), Spectrum::RGB::Blue.to_hsl)
205
+ assert_equal(Spectrum::HSL.new(300, 100, 25.10),
206
+ Spectrum::RGB::Purple.to_hsl)
207
+ assert_equal(Spectrum::HSL.new(0, 59.42, 40.59),
208
+ Spectrum::RGB::Brown.to_hsl)
209
+ assert_equal(Spectrum::HSL.new(317.5, 100, 68.43),
210
+ Spectrum::RGB::Carnation.to_hsl)
211
+ assert_equal(Spectrum::HSL.new(0, 100, 27.64),
212
+ Spectrum::RGB::Cayenne.to_hsl)
213
+
214
+ assert_equal("hsl(0.00, 0.00%, 0.00%)", Spectrum::RGB::Black.css_hsl)
215
+ assert_equal("hsl(60.00, 100.00%, 50.00%)",
216
+ Spectrum::RGB::Yellow.css_hsl)
217
+ assert_equal("hsl(180.00, 100.00%, 50.00%)", Spectrum::RGB::Cyan.css_hsl)
218
+ assert_equal("hsl(300.00, 100.00%, 50.00%)",
219
+ Spectrum::RGB::Magenta.css_hsl)
220
+ assert_equal("hsl(0.00, 100.00%, 50.00%)", Spectrum::RGB::Red.css_hsl)
221
+ assert_equal("hsl(120.00, 100.00%, 50.00%)", Spectrum::RGB::Lime.css_hsl)
222
+ assert_equal("hsl(240.00, 100.00%, 50.00%)", Spectrum::RGB::Blue.css_hsl)
223
+ assert_equal("hsl(300.00, 100.00%, 25.10%)",
224
+ Spectrum::RGB::Purple.css_hsl)
225
+ assert_equal("hsl(0.00, 59.42%, 40.59%)", Spectrum::RGB::Brown.css_hsl)
226
+ assert_equal("hsl(317.52, 100.00%, 68.43%)",
227
+ Spectrum::RGB::Carnation.css_hsl)
228
+ assert_equal("hsl(0.00, 100.00%, 27.65%)", Spectrum::RGB::Cayenne.css_hsl)
229
+
230
+ assert_equal("hsla(0.00, 0.00%, 0.00%, 1.00)",
231
+ Spectrum::RGB::Black.css_hsla)
232
+ assert_equal("hsla(60.00, 100.00%, 50.00%, 1.00)",
233
+ Spectrum::RGB::Yellow.css_hsla)
234
+ assert_equal("hsla(180.00, 100.00%, 50.00%, 1.00)",
235
+ Spectrum::RGB::Cyan.css_hsla)
236
+ assert_equal("hsla(300.00, 100.00%, 50.00%, 1.00)",
237
+ Spectrum::RGB::Magenta.css_hsla)
238
+ assert_equal("hsla(0.00, 100.00%, 50.00%, 1.00)",
239
+ Spectrum::RGB::Red.css_hsla)
240
+ assert_equal("hsla(120.00, 100.00%, 50.00%, 1.00)",
241
+ Spectrum::RGB::Lime.css_hsla)
242
+ assert_equal("hsla(240.00, 100.00%, 50.00%, 1.00)",
243
+ Spectrum::RGB::Blue.css_hsla)
244
+ assert_equal("hsla(300.00, 100.00%, 25.10%, 1.00)",
245
+ Spectrum::RGB::Purple.css_hsla)
246
+ assert_equal("hsla(0.00, 59.42%, 40.59%, 1.00)",
247
+ Spectrum::RGB::Brown.css_hsla)
248
+ assert_equal("hsla(317.52, 100.00%, 68.43%, 1.00)",
249
+ Spectrum::RGB::Carnation.css_hsla)
250
+ assert_equal("hsla(0.00, 100.00%, 27.65%, 1.00)",
251
+ Spectrum::RGB::Cayenne.css_hsla)
252
+
253
+ # The following tests a bug reported by Jean Krohn on 10 June 2006
254
+ # where HSL conversion was not quite correct, resulting in a bad
255
+ # round-trip.
256
+ assert_equal("#008800", Spectrum::RGB.from_html("#008800").to_hsl.html)
257
+ assert_not_equal("#002288", Spectrum::RGB.from_html("#008800").to_hsl.html)
258
+
259
+ # The following tests a bug reported by Adam Johnson on 29 October
260
+ # 2010.
261
+ hsl = Spectrum::HSL.new(262, 67, 42)
262
+ c = Spectrum::RGB.from_fraction(0.34496, 0.1386, 0.701399).to_hsl
263
+ assert_in_delta hsl.h, c.h, Spectrum::COLOR_TOLERANCE, "Hue"
264
+ assert_in_delta hsl.s, c.s, Spectrum::COLOR_TOLERANCE, "Saturation"
265
+ assert_in_delta hsl.l, c.l, Spectrum::COLOR_TOLERANCE, "Luminance"
266
+ end
267
+
268
+ def test_to_rgb
269
+ assert_equal(Spectrum::RGB::Black, Spectrum::RGB::Black.to_rgb)
270
+ end
271
+
272
+ def test_to_yiq
273
+ assert_kind_of(Spectrum::YIQ, Spectrum::RGB::Black.to_yiq)
274
+ assert_equal(Spectrum::YIQ.new, Spectrum::RGB::Black.to_yiq)
275
+ assert_equal(Spectrum::YIQ.new(88.6, 32.1, 0), Spectrum::RGB::Yellow.to_yiq)
276
+ assert_equal(Spectrum::YIQ.new(70.1, 0, 0), Spectrum::RGB::Cyan.to_yiq)
277
+ assert_equal(Spectrum::YIQ.new(41.3, 27.5, 52.3),
278
+ Spectrum::RGB::Magenta.to_yiq)
279
+ assert_equal(Spectrum::YIQ.new(29.9, 59.6, 21.2), Spectrum::RGB::Red.to_yiq)
280
+ assert_equal(Spectrum::YIQ.new(58.7, 0, 0), Spectrum::RGB::Lime.to_yiq)
281
+ assert_equal(Spectrum::YIQ.new(11.4, 0, 31.1), Spectrum::RGB::Blue.to_yiq)
282
+ assert_equal(Spectrum::YIQ.new(20.73, 13.80, 26.25),
283
+ Spectrum::RGB::Purple.to_yiq)
284
+ assert_equal(Spectrum::YIQ.new(30.89, 28.75, 10.23),
285
+ Spectrum::RGB::Brown.to_yiq)
286
+ assert_equal(Spectrum::YIQ.new(60.84, 23.28, 27.29),
287
+ Spectrum::RGB::Carnation.to_yiq)
288
+ assert_equal(Spectrum::YIQ.new(16.53, 32.96, 11.72),
289
+ Spectrum::RGB::Cayenne.to_yiq)
290
+ end
291
+
292
+ def test_add
293
+ assert_nothing_raised { Spectrum::RGB::Cyan + Spectrum::RGB::Yellow }
294
+ white = Spectrum::RGB::Cyan + Spectrum::RGB::Yellow
295
+ assert_not_nil(white)
296
+ assert_equal(Spectrum::RGB::White, white)
297
+
298
+ c1 = Spectrum::RGB.new(0x80, 0x80, 0x00)
299
+ c2 = Spectrum::RGB.new(0x45, 0x20, 0xf0)
300
+ cr = Spectrum::RGB.new(0xc5, 0xa0, 0xf0)
301
+
302
+ assert_equal(cr, c1 + c2)
303
+ end
304
+
305
+ def test_subtract
306
+ black = Spectrum::RGB::LightCoral - Spectrum::RGB::Honeydew
307
+ assert_equal(Spectrum::RGB::Black, black)
308
+
309
+ c1 = Spectrum::RGB.new(0x85, 0x80, 0x00)
310
+ c2 = Spectrum::RGB.new(0x40, 0x20, 0xf0)
311
+ cr = Spectrum::RGB.new(0x45, 0x60, 0x00)
312
+
313
+ assert_equal(cr, c1 - c2)
314
+ end
315
+
316
+ def test_mean_grayscale
317
+ c1 = Spectrum::RGB.new(0x85, 0x80, 0x00)
318
+ c1_max = assert_nothing_raised { c1.max_rgb_as_greyscale }
319
+ c1_max = c1.max_rgb_as_greyscale
320
+ c1_result = Spectrum::GrayScale.from_fraction(0x85 / 255.0)
321
+
322
+ assert_equal(c1_result, c1_max)
323
+ end
324
+
325
+ def test_from_html
326
+ assert_equal("RGB [#333333]", Spectrum::RGB.from_html("#333").inspect)
327
+ assert_equal("RGB [#333333]", Spectrum::RGB.from_html("333").inspect)
328
+ assert_equal("RGB [#555555]", Spectrum::RGB.from_html("#555555").inspect)
329
+ assert_equal("RGB [#555555]", Spectrum::RGB.from_html("555555").inspect)
330
+ assert_raises(ArgumentError) { Spectrum::RGB.from_html("#5555555") }
331
+ assert_raises(ArgumentError) { Spectrum::RGB.from_html("5555555") }
332
+ assert_raises(ArgumentError) { Spectrum::RGB.from_html("#55555") }
333
+ assert_raises(ArgumentError) { Spectrum::RGB.from_html("55555") }
334
+ end
335
+
336
+ def test_inspect
337
+ assert_equal("RGB [#000000]", Spectrum::RGB::Black.inspect)
338
+ assert_equal("RGB [#0000ff]", Spectrum::RGB::Blue.inspect)
339
+ assert_equal("RGB [#00ff00]", Spectrum::RGB::Lime.inspect)
340
+ assert_equal("RGB [#ff0000]", Spectrum::RGB::Red.inspect)
341
+ assert_equal("RGB [#ffffff]", Spectrum::RGB::White.inspect)
342
+ end
343
+ end
344
+ end