chunky_png 1.3.2 → 1.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/BENCHMARKING.md +38 -0
- data/CONTRIBUTING.md +50 -0
- data/LICENSE +1 -1
- data/chunky_png.gemspec +12 -12
- data/lib/chunky_png/canvas/operations.rb +1 -1
- data/lib/chunky_png/color.rb +7 -7
- data/lib/chunky_png/dimension.rb +16 -15
- data/lib/chunky_png/version.rb +1 -1
- data/spec/chunky_png/canvas/adam7_interlacing_spec.rb +24 -24
- data/spec/chunky_png/canvas/data_url_exporting_spec.rb +2 -2
- data/spec/chunky_png/canvas/data_url_importing_spec.rb +4 -4
- data/spec/chunky_png/canvas/drawing_spec.rb +60 -60
- data/spec/chunky_png/canvas/masking_spec.rb +15 -15
- data/spec/chunky_png/canvas/operations_spec.rb +69 -66
- data/spec/chunky_png/canvas/png_decoding_spec.rb +26 -26
- data/spec/chunky_png/canvas/png_encoding_spec.rb +74 -74
- data/spec/chunky_png/canvas/resampling_spec.rb +47 -47
- data/spec/chunky_png/canvas/stream_exporting_spec.rb +20 -20
- data/spec/chunky_png/canvas/stream_importing_spec.rb +3 -3
- data/spec/chunky_png/canvas_spec.rb +95 -92
- data/spec/chunky_png/color_spec.rb +151 -151
- data/spec/chunky_png/datastream_spec.rb +6 -6
- data/spec/chunky_png/dimension_spec.rb +15 -15
- data/spec/chunky_png/image_spec.rb +4 -4
- data/spec/chunky_png/point_spec.rb +25 -25
- data/spec/chunky_png/rmagick_spec.rb +4 -4
- data/spec/chunky_png/vector_spec.rb +39 -34
- data/spec/chunky_png_spec.rb +1 -2
- data/spec/png_suite_spec.rb +38 -38
- data/spec/spec_helper.rb +14 -10
- metadata +37 -33
- data/BENCHMARKS.rdoc +0 -31
@@ -1,18 +1,18 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ChunkyPNG::Canvas do
|
4
|
-
|
4
|
+
|
5
5
|
describe '#to_rgba_stream' do
|
6
6
|
it "should export a sample canvas to an RGBA stream correctly" do
|
7
7
|
canvas = ChunkyPNG::Canvas.new(2, 2, [ChunkyPNG::Color.rgba(1,2,3,4), ChunkyPNG::Color.rgba(5,6,7,8),
|
8
8
|
ChunkyPNG::Color.rgba(4,3,2,1), ChunkyPNG::Color.rgba(8,7,6,5)])
|
9
9
|
|
10
|
-
canvas.to_rgba_stream.
|
10
|
+
expect(canvas.to_rgba_stream).to eql [1,2,3,4,5,6,7,8,4,3,2,1,8,7,6,5].pack('C16')
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
it "should export an image to an RGBA datastream correctly" do
|
14
|
-
reference_canvas('pixelstream_reference').to_rgba_stream.
|
15
|
-
end
|
14
|
+
expect(reference_canvas('pixelstream_reference').to_rgba_stream).to eql resource_data('pixelstream.rgba')
|
15
|
+
end
|
16
16
|
end
|
17
17
|
|
18
18
|
describe '#to_rgb_stream' do
|
@@ -20,40 +20,40 @@ describe ChunkyPNG::Canvas do
|
|
20
20
|
canvas = ChunkyPNG::Canvas.new(2, 2, [ChunkyPNG::Color.rgba(1,2,3,4), ChunkyPNG::Color.rgba(5,6,7,8),
|
21
21
|
ChunkyPNG::Color.rgba(4,3,2,1), ChunkyPNG::Color.rgba(8,7,6,5)])
|
22
22
|
|
23
|
-
canvas.to_rgb_stream.
|
24
|
-
end
|
25
|
-
|
23
|
+
expect(canvas.to_rgb_stream).to eql [1,2,3,5,6,7,4,3,2,8,7,6].pack('C12')
|
24
|
+
end
|
25
|
+
|
26
26
|
it "should export an image to an RGB datastream correctly" do
|
27
|
-
reference_canvas('pixelstream_reference').to_rgb_stream.
|
27
|
+
expect(reference_canvas('pixelstream_reference').to_rgb_stream).to eql resource_data('pixelstream.rgb')
|
28
28
|
end
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
describe '#to_grayscale_stream' do
|
32
|
-
|
32
|
+
|
33
33
|
it "should export a grayscale image to a grayscale datastream correctly" do
|
34
34
|
canvas = ChunkyPNG::Canvas.new(2, 2, [ChunkyPNG::Color.grayscale(1), ChunkyPNG::Color.grayscale(2),
|
35
35
|
ChunkyPNG::Color.grayscale(3), ChunkyPNG::Color.grayscale(4)])
|
36
|
-
canvas.to_grayscale_stream.
|
37
|
-
end
|
36
|
+
expect(canvas.to_grayscale_stream).to eql [1,2,3,4].pack('C4')
|
37
|
+
end
|
38
|
+
|
38
39
|
|
39
|
-
|
40
40
|
it "should export a color image to a grayscale datastream, using B values" do
|
41
41
|
canvas = ChunkyPNG::Canvas.new(2, 2, [ChunkyPNG::Color.rgba(1,2,3,4), ChunkyPNG::Color.rgba(5,6,7,8),
|
42
42
|
ChunkyPNG::Color.rgba(4,3,2,1), ChunkyPNG::Color.rgba(8,7,6,5)])
|
43
|
-
canvas.to_grayscale_stream.
|
44
|
-
end
|
43
|
+
expect(canvas.to_grayscale_stream).to eql [3,7,2,6].pack('C4')
|
44
|
+
end
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
describe '#to_alpha_channel_stream' do
|
48
48
|
it "should export an opaque image to an alpha channel datastream correctly" do
|
49
49
|
grayscale_array = Array.new(reference_canvas('pixelstream_reference').pixels.length, 255)
|
50
|
-
reference_canvas('pixelstream_reference').to_alpha_channel_stream.
|
50
|
+
expect(reference_canvas('pixelstream_reference').to_alpha_channel_stream).to eql grayscale_array.pack('C*')
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
it "should export a transparent image to an alpha channel datastream correctly" do
|
54
54
|
canvas = ChunkyPNG::Canvas.new(2, 2, [ChunkyPNG::Color.rgba(1,2,3,4), ChunkyPNG::Color.rgba(5,6,7,8),
|
55
55
|
ChunkyPNG::Color.rgba(4,3,2,1), ChunkyPNG::Color.rgba(8,7,6,5)])
|
56
|
-
canvas.to_alpha_channel_stream.
|
56
|
+
expect(canvas.to_alpha_channel_stream).to eql [4,8,1,5].pack('C4')
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
@@ -6,7 +6,7 @@ describe ChunkyPNG::Canvas do
|
|
6
6
|
it "should load an image correctly from a datastream" do
|
7
7
|
File.open(resource_file('pixelstream.rgb')) do |stream|
|
8
8
|
matrix = ChunkyPNG::Canvas.from_rgb_stream(240, 180, stream)
|
9
|
-
matrix.
|
9
|
+
expect(matrix).to eql reference_canvas('pixelstream_reference')
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
@@ -15,7 +15,7 @@ describe ChunkyPNG::Canvas do
|
|
15
15
|
it "should load an image correctly from a datastream" do
|
16
16
|
File.open(resource_file('pixelstream.bgr')) do |stream|
|
17
17
|
matrix = ChunkyPNG::Canvas.from_bgr_stream(240, 180, stream)
|
18
|
-
matrix.
|
18
|
+
expect(matrix).to eql reference_canvas('pixelstream_reference')
|
19
19
|
end
|
20
20
|
end
|
21
21
|
end
|
@@ -24,7 +24,7 @@ describe ChunkyPNG::Canvas do
|
|
24
24
|
it "should load an image correctly from a datastream" do
|
25
25
|
File.open(resource_file('pixelstream.rgba')) do |stream|
|
26
26
|
matrix = ChunkyPNG::Canvas.from_rgba_stream(240, 180, stream)
|
27
|
-
matrix.
|
27
|
+
expect(matrix).to eql reference_canvas('pixelstream_reference')
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
@@ -11,216 +11,219 @@ describe ChunkyPNG::Canvas do
|
|
11
11
|
describe '#initialize' do
|
12
12
|
it "should accept a single color value as background color" do
|
13
13
|
canvas = ChunkyPNG::Canvas.new(2, 2, 'red @ 0.8')
|
14
|
-
canvas[1, 0].
|
14
|
+
expect(canvas[1, 0]).to eql ChunkyPNG::Color.parse('red @ 0.8')
|
15
15
|
end
|
16
|
-
|
16
|
+
|
17
17
|
it "should raise an error if the color value is not understood" do
|
18
|
-
|
18
|
+
expect { ChunkyPNG::Canvas.new(2, 2, :nonsense) }.to raise_error(ArgumentError)
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
it "should accept an array as initial pixel values" do
|
22
22
|
canvas = ChunkyPNG::Canvas.new(2, 2, [1,2,3,4])
|
23
|
-
canvas[0, 0].
|
24
|
-
canvas[1, 0].
|
25
|
-
canvas[0, 1].
|
26
|
-
canvas[1, 1].
|
23
|
+
expect(canvas[0, 0]).to eql 1
|
24
|
+
expect(canvas[1, 0]).to eql 2
|
25
|
+
expect(canvas[0, 1]).to eql 3
|
26
|
+
expect(canvas[1, 1]).to eql 4
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
it "should raise an ArgumentError if the initial array does not have the correct number of elements" do
|
30
|
-
|
31
|
-
|
30
|
+
expect { ChunkyPNG::Canvas.new(2, 2, [1,2,3]) }.to raise_error(ArgumentError)
|
31
|
+
expect { ChunkyPNG::Canvas.new(2, 2, [1,2,3,4,5]) }.to raise_error(ArgumentError)
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
it "should use a transparent background by default" do
|
35
35
|
canvas = ChunkyPNG::Canvas.new(1, 1)
|
36
|
-
canvas[0,0].
|
36
|
+
expect(canvas[0,0]).to eql ChunkyPNG::Color::TRANSPARENT
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
40
|
describe '#dimension' do
|
41
41
|
it "should return the dimensions as a Dimension instance" do
|
42
|
-
subject.dimension.
|
42
|
+
expect(subject.dimension).to eql ChunkyPNG::Dimension('1x1')
|
43
43
|
end
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
describe '#area' do
|
47
47
|
it "should return the dimensions as two-item array" do
|
48
|
-
subject.area.
|
48
|
+
expect(subject.area).to eql ChunkyPNG::Dimension('1x1').area
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
52
|
describe '#include?' do
|
53
53
|
it "should return true if the coordinates are within bounds, false otherwise" do
|
54
|
-
subject.include_xy?( 0, 0).
|
55
|
-
|
56
|
-
subject.include_xy?(-1, 0).
|
57
|
-
subject.include_xy?( 1, 0).
|
58
|
-
subject.include_xy?( 0, -1).
|
59
|
-
subject.include_xy?( 0, 1).
|
60
|
-
subject.include_xy?(-1, -1).
|
61
|
-
subject.include_xy?(-1, 1).
|
62
|
-
subject.include_xy?( 1, -1).
|
63
|
-
subject.include_xy?( 1, 1).
|
64
|
-
end
|
65
|
-
|
54
|
+
expect(subject.include_xy?( 0, 0)).to eql true
|
55
|
+
|
56
|
+
expect(subject.include_xy?(-1, 0)).to eql false
|
57
|
+
expect(subject.include_xy?( 1, 0)).to eql false
|
58
|
+
expect(subject.include_xy?( 0, -1)).to eql false
|
59
|
+
expect(subject.include_xy?( 0, 1)).to eql false
|
60
|
+
expect(subject.include_xy?(-1, -1)).to eql false
|
61
|
+
expect(subject.include_xy?(-1, 1)).to eql false
|
62
|
+
expect(subject.include_xy?( 1, -1)).to eql false
|
63
|
+
expect(subject.include_xy?( 1, 1)).to eql false
|
64
|
+
end
|
65
|
+
|
66
66
|
it "should accept strings, arrays, hashes and points as well" do
|
67
|
-
subject.
|
67
|
+
expect(subject).to include('0, 0')
|
68
68
|
subject.should_not include('0, 1')
|
69
|
-
subject.
|
69
|
+
expect(subject).to include([0, 0])
|
70
70
|
subject.should_not include([0, 1])
|
71
|
-
subject.
|
71
|
+
expect(subject).to include(:y => 0, :x => 0)
|
72
72
|
subject.should_not include(:y => 1, :x => 0)
|
73
|
-
subject.
|
73
|
+
expect(subject).to include(ChunkyPNG::Point.new(0, 0))
|
74
74
|
subject.should_not include(ChunkyPNG::Point.new(0, 1))
|
75
75
|
end
|
76
76
|
end
|
77
|
-
|
77
|
+
|
78
78
|
describe '#include_x?' do
|
79
79
|
it "should return true if the x-coordinate is within bounds, false otherwise" do
|
80
|
-
subject.include_x?( 0).
|
81
|
-
subject.include_x?(-1).
|
82
|
-
subject.include_x?( 1).
|
80
|
+
expect(subject.include_x?( 0)).to eql true
|
81
|
+
expect(subject.include_x?(-1)).to eql false
|
82
|
+
expect(subject.include_x?( 1)).to eql false
|
83
83
|
end
|
84
84
|
end
|
85
|
-
|
85
|
+
|
86
86
|
describe '#include_y?' do
|
87
87
|
it "should return true if the y-coordinate is within bounds, false otherwise" do
|
88
|
-
subject.include_y?( 0).
|
89
|
-
subject.include_y?(-1).
|
90
|
-
subject.include_y?( 1).
|
88
|
+
expect(subject.include_y?( 0)).to eql true
|
89
|
+
expect(subject.include_y?(-1)).to eql false
|
90
|
+
expect(subject.include_y?( 1)).to eql false
|
91
91
|
end
|
92
92
|
end
|
93
|
-
|
93
|
+
|
94
94
|
describe '#assert_xy!' do
|
95
95
|
it "should not raise an exception if the coordinates are within bounds" do
|
96
|
-
subject.
|
97
|
-
|
96
|
+
expect(subject).to receive(:include_xy?).with(0, 0).and_return(true)
|
97
|
+
expect { subject.send(:assert_xy!, 0, 0) }.to_not raise_error
|
98
98
|
end
|
99
|
-
|
99
|
+
|
100
100
|
it "should raise an exception if the coordinates are out of bounds bounds" do
|
101
|
-
subject.
|
102
|
-
|
101
|
+
expect(subject).to receive(:include_xy?).with(0, -1).and_return(false)
|
102
|
+
expect { subject.send(:assert_xy!, 0, -1) }.to raise_error(ChunkyPNG::OutOfBounds)
|
103
103
|
end
|
104
104
|
end
|
105
|
-
|
105
|
+
|
106
106
|
describe '#assert_x!' do
|
107
107
|
it "should not raise an exception if the x-coordinate is within bounds" do
|
108
|
-
subject.
|
109
|
-
|
108
|
+
expect(subject).to receive(:include_x?).with(0).and_return(true)
|
109
|
+
expect { subject.send(:assert_x!, 0) }.to_not raise_error
|
110
110
|
end
|
111
|
-
|
111
|
+
|
112
112
|
it "should raise an exception if the x-coordinate is out of bounds bounds" do
|
113
|
-
subject.
|
114
|
-
|
113
|
+
expect(subject).to receive(:include_y?).with(-1).and_return(false)
|
114
|
+
expect { subject.send(:assert_y!, -1) }.to raise_error(ChunkyPNG::OutOfBounds)
|
115
115
|
end
|
116
116
|
end
|
117
|
-
|
117
|
+
|
118
118
|
describe '#[]' do
|
119
119
|
it "should return the pixel value if the coordinates are within bounds" do
|
120
|
-
subject[0, 0].
|
120
|
+
expect(subject[0, 0]).to eql ChunkyPNG::Color::WHITE
|
121
121
|
end
|
122
|
-
|
122
|
+
|
123
123
|
it "should assert the coordinates to be within bounds" do
|
124
|
-
subject.
|
124
|
+
expect(subject).to receive(:assert_xy!).with(0, 0)
|
125
125
|
subject[0, 0]
|
126
126
|
end
|
127
127
|
end
|
128
|
-
|
128
|
+
|
129
129
|
describe '#get_pixel' do
|
130
130
|
it "should return the pixel value if the coordinates are within bounds" do
|
131
|
-
subject.get_pixel(0, 0).
|
131
|
+
expect(subject.get_pixel(0, 0)).to eql ChunkyPNG::Color::WHITE
|
132
132
|
end
|
133
|
-
|
133
|
+
|
134
134
|
it "should not assert nor check the coordinates" do
|
135
|
-
subject.
|
136
|
-
subject.
|
135
|
+
expect(subject).to_not receive(:assert_xy!)
|
136
|
+
expect(subject).to_not receive(:include_xy?)
|
137
137
|
subject.get_pixel(0, 0)
|
138
138
|
end
|
139
139
|
end
|
140
|
-
|
140
|
+
|
141
141
|
describe '#[]=' do
|
142
142
|
it "should change the pixel's color value" do
|
143
|
-
|
143
|
+
expect { subject[0, 0] = ChunkyPNG::Color::BLACK }.to change { subject[0, 0] }.
|
144
|
+
from(ChunkyPNG::Color::WHITE).to(ChunkyPNG::Color::BLACK)
|
144
145
|
end
|
145
|
-
|
146
|
+
|
146
147
|
it "should assert the bounds of the image" do
|
147
|
-
subject.
|
148
|
+
expect(subject).to receive(:assert_xy!).with(0, 0)
|
148
149
|
subject[0, 0] = ChunkyPNG::Color::BLACK
|
149
150
|
end
|
150
151
|
end
|
151
|
-
|
152
|
+
|
152
153
|
describe 'set_pixel' do
|
153
154
|
it "should change the pixel's color value" do
|
154
|
-
|
155
|
+
expect { subject.set_pixel(0, 0, ChunkyPNG::Color::BLACK) }.to change { subject[0, 0] }.
|
156
|
+
from(ChunkyPNG::Color::WHITE).to(ChunkyPNG::Color::BLACK)
|
155
157
|
end
|
156
|
-
|
158
|
+
|
157
159
|
it "should not assert or check the bounds of the image" do
|
158
|
-
subject.
|
159
|
-
subject.
|
160
|
+
expect(subject).to_not receive(:assert_xy!)
|
161
|
+
expect(subject).to_not receive(:include_xy?)
|
160
162
|
subject.set_pixel(0, 0, ChunkyPNG::Color::BLACK)
|
161
163
|
end
|
162
164
|
end
|
163
|
-
|
165
|
+
|
164
166
|
describe '#set_pixel_if_within_bounds' do
|
165
167
|
it "should change the pixel's color value" do
|
166
|
-
|
168
|
+
expect { subject.set_pixel_if_within_bounds(0, 0, ChunkyPNG::Color::BLACK) }.to change { subject[0, 0] }.
|
169
|
+
from(ChunkyPNG::Color::WHITE).to(ChunkyPNG::Color::BLACK)
|
167
170
|
end
|
168
171
|
|
169
172
|
it "should not assert, but only check the coordinates" do
|
170
|
-
subject.
|
171
|
-
subject.
|
173
|
+
expect(subject).to_not receive(:assert_xy!)
|
174
|
+
expect(subject).to receive(:include_xy?).with(0, 0)
|
172
175
|
subject.set_pixel_if_within_bounds(0, 0, ChunkyPNG::Color::BLACK)
|
173
176
|
end
|
174
177
|
|
175
178
|
it "should do nothing if the coordinates are out of bounds" do
|
176
|
-
subject.set_pixel_if_within_bounds(-1, 1, ChunkyPNG::Color::BLACK).
|
177
|
-
subject[0, 0].
|
179
|
+
expect(subject.set_pixel_if_within_bounds(-1, 1, ChunkyPNG::Color::BLACK)).to be_nil
|
180
|
+
expect(subject[0, 0]).to eql ChunkyPNG::Color::WHITE
|
178
181
|
end
|
179
182
|
end
|
180
|
-
|
183
|
+
|
181
184
|
describe '#row' do
|
182
185
|
before { @canvas = reference_canvas('operations') }
|
183
186
|
|
184
187
|
it "should give an out of bounds exception when y-coordinate is out of bounds" do
|
185
|
-
|
186
|
-
|
188
|
+
expect { @canvas.row(-1) }.to raise_error(ChunkyPNG::OutOfBounds)
|
189
|
+
expect { @canvas.row(16) }.to raise_error(ChunkyPNG::OutOfBounds)
|
187
190
|
end
|
188
191
|
|
189
192
|
it "should return the correct pixels" do
|
190
193
|
data = @canvas.row(0)
|
191
|
-
data.
|
192
|
-
data.
|
194
|
+
expect(data.length).to eql @canvas.width
|
195
|
+
expect(data).to eql [65535, 268500991, 536936447, 805371903, 1073807359, 1342242815, 1610678271, 1879113727, 2147549183, 2415984639, 2684420095, 2952855551, 3221291007, 3489726463, 3758161919, 4026597375]
|
193
196
|
end
|
194
197
|
end
|
195
|
-
|
198
|
+
|
196
199
|
describe '#column' do
|
197
200
|
before { @canvas = reference_canvas('operations') }
|
198
201
|
|
199
202
|
it "should give an out of bounds exception when x-coordinate is out of bounds" do
|
200
|
-
|
201
|
-
|
203
|
+
expect { @canvas.column(-1) }.to raise_error(ChunkyPNG::OutOfBounds)
|
204
|
+
expect { @canvas.column(16) }.to raise_error(ChunkyPNG::OutOfBounds)
|
202
205
|
end
|
203
206
|
|
204
207
|
it "should return the correct pixels" do
|
205
208
|
data = @canvas.column(0)
|
206
|
-
data.
|
207
|
-
data.
|
209
|
+
expect(data.length).to eql @canvas.height
|
210
|
+
expect(data).to eql [65535, 1114111, 2162687, 3211263, 4259839, 5308415, 6356991, 7405567, 8454143, 9502719, 10551295, 11599871, 12648447, 13697023, 14745599, 15794175]
|
208
211
|
end
|
209
212
|
end
|
210
|
-
|
213
|
+
|
211
214
|
describe '#replace_canvas' do
|
212
215
|
it "should change the dimension of the canvas" do
|
213
|
-
|
216
|
+
expect { subject.send(:replace_canvas!, 2, 2, [1,2,3,4]) }.to change { subject.dimension }.
|
214
217
|
from(ChunkyPNG::Dimension('1x1')).to(ChunkyPNG::Dimension('2x2'))
|
215
218
|
end
|
216
|
-
|
219
|
+
|
217
220
|
it "should change the pixel array" do
|
218
|
-
|
221
|
+
expect { subject.send(:replace_canvas!, 2, 2, [1,2,3,4]) }.to change { subject.pixels }.
|
219
222
|
from([ChunkyPNG::Color('white')]).to([1,2,3,4])
|
220
223
|
end
|
221
|
-
|
224
|
+
|
222
225
|
it "should return itself" do
|
223
|
-
subject.send(:replace_canvas!, 2, 2, [1,2,3,4]).
|
226
|
+
expect(subject.send(:replace_canvas!, 2, 2, [1,2,3,4])).to equal(subject)
|
224
227
|
end
|
225
228
|
end
|
226
229
|
end
|
@@ -2,20 +2,20 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe 'ChunyPNG.Color' do
|
4
4
|
it 'should interpret 4 arguments as RGBA values' do
|
5
|
-
ChunkyPNG::Color(1, 2, 3, 4).
|
5
|
+
expect(ChunkyPNG::Color(1, 2, 3, 4)).to eql ChunkyPNG::Color.rgba(1, 2, 3, 4)
|
6
6
|
end
|
7
7
|
|
8
8
|
it 'should interpret 3 arguments as RGBA values' do
|
9
|
-
ChunkyPNG::Color(1, 2, 3).
|
9
|
+
expect(ChunkyPNG::Color(1, 2, 3)).to eql ChunkyPNG::Color.rgb(1, 2, 3)
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'should interpret 2 arguments as a color to parse and an opacity value' do
|
13
|
-
ChunkyPNG::Color('0x0a649664', 0xaa).
|
14
|
-
ChunkyPNG::Color('spring green @ 0.6666', 0xff).
|
13
|
+
expect(ChunkyPNG::Color('0x0a649664', 0xaa)).to eql 0x0a6496aa
|
14
|
+
expect(ChunkyPNG::Color('spring green @ 0.6666', 0xff)).to eql 0x00ff7fff
|
15
15
|
end
|
16
16
|
|
17
17
|
it 'should interpret 1 argument as a color to parse' do
|
18
|
-
ChunkyPNG::Color.
|
18
|
+
expect(ChunkyPNG::Color).to receive(:parse).with('0x0a649664')
|
19
19
|
ChunkyPNG::Color('0x0a649664')
|
20
20
|
end
|
21
21
|
end
|
@@ -36,128 +36,128 @@ describe ChunkyPNG::Color do
|
|
36
36
|
|
37
37
|
describe '#parse' do
|
38
38
|
it 'should interpret a hex string correctly' do
|
39
|
-
parse('0x0a649664').
|
39
|
+
expect(parse('0x0a649664')).to eql ChunkyPNG::Color.from_hex('#0a649664')
|
40
40
|
end
|
41
41
|
|
42
42
|
it 'should interpret a color name correctly' do
|
43
|
-
parse(:spring_green).
|
44
|
-
parse('spring green').
|
45
|
-
parse('spring green @ 0.6666').
|
43
|
+
expect(parse(:spring_green)).to eql 0x00ff7fff
|
44
|
+
expect(parse('spring green')).to eql 0x00ff7fff
|
45
|
+
expect(parse('spring green @ 0.6666')).to eql 0x00ff7faa
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'should return numbers as is' do
|
49
|
-
parse('12345').
|
50
|
-
parse(12345).
|
49
|
+
expect(parse('12345')).to eql 12345
|
50
|
+
expect(parse(12345)).to eql 12345
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
54
|
describe '#pixel_bytesize' do
|
55
55
|
it 'should return the normal amount of bytes with a bit depth of 8' do
|
56
|
-
pixel_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 8).
|
56
|
+
expect(pixel_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 8)).to eql 3
|
57
57
|
end
|
58
58
|
|
59
59
|
it 'should return a multiple of the normal amount of bytes with a bit depth greater than 8' do
|
60
|
-
pixel_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 16).
|
61
|
-
pixel_bytesize(ChunkyPNG::COLOR_TRUECOLOR_ALPHA, 16).
|
62
|
-
pixel_bytesize(ChunkyPNG::COLOR_GRAYSCALE_ALPHA, 16).
|
60
|
+
expect(pixel_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 16)).to eql 6
|
61
|
+
expect(pixel_bytesize(ChunkyPNG::COLOR_TRUECOLOR_ALPHA, 16)).to eql 8
|
62
|
+
expect(pixel_bytesize(ChunkyPNG::COLOR_GRAYSCALE_ALPHA, 16)).to eql 4
|
63
63
|
end
|
64
64
|
|
65
65
|
it 'should return 1 with a bit depth lower than 0' do
|
66
|
-
pixel_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 4).
|
67
|
-
pixel_bytesize(ChunkyPNG::COLOR_INDEXED, 2).
|
68
|
-
pixel_bytesize(ChunkyPNG::COLOR_GRAYSCALE_ALPHA, 1).
|
66
|
+
expect(pixel_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 4)).to eql 1
|
67
|
+
expect(pixel_bytesize(ChunkyPNG::COLOR_INDEXED, 2)).to eql 1
|
68
|
+
expect(pixel_bytesize(ChunkyPNG::COLOR_GRAYSCALE_ALPHA, 1)).to eql 1
|
69
69
|
end
|
70
70
|
end
|
71
71
|
|
72
72
|
describe '#pass_bytesize' do
|
73
73
|
it 'should calculate a pass size correctly' do
|
74
|
-
pass_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 8, 10, 10).
|
74
|
+
expect(pass_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 8, 10, 10)).to eql 310
|
75
75
|
end
|
76
76
|
|
77
77
|
it 'should return 0 if one of the dimensions is zero' do
|
78
|
-
pass_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 8, 0, 10).
|
79
|
-
pass_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 8, 10, 0).
|
78
|
+
expect(pass_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 8, 0, 10)).to eql 0
|
79
|
+
expect(pass_bytesize(ChunkyPNG::COLOR_TRUECOLOR, 8, 10, 0)).to eql 0
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
83
|
describe '#rgba' do
|
84
84
|
it 'should represent pixels as the correct number' do
|
85
|
-
rgba(255, 255, 255, 255).
|
86
|
-
rgba( 0, 0, 0, 255).
|
87
|
-
rgba( 10, 100, 150, 255).
|
88
|
-
rgba( 10, 100, 150, 100).
|
89
|
-
rgba( 10, 100, 150, 0).
|
85
|
+
expect(rgba(255, 255, 255, 255)).to eql @white
|
86
|
+
expect(rgba( 0, 0, 0, 255)).to eql @black
|
87
|
+
expect(rgba( 10, 100, 150, 255)).to eql @opaque
|
88
|
+
expect(rgba( 10, 100, 150, 100)).to eql @non_opaque
|
89
|
+
expect(rgba( 10, 100, 150, 0)).to eql @fully_transparent
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
93
93
|
describe '#from_hex' do
|
94
94
|
it 'should load colors correctly from hex notation' do
|
95
|
-
from_hex('0a649664').
|
96
|
-
from_hex('#0a649664').
|
97
|
-
from_hex('0x0a649664').
|
98
|
-
from_hex('0a6496').
|
99
|
-
from_hex('#0a6496').
|
100
|
-
from_hex('0x0a6496').
|
101
|
-
from_hex('abc').
|
102
|
-
from_hex('#abc').
|
103
|
-
from_hex('0xabc').
|
95
|
+
expect(from_hex('0a649664')).to eql @non_opaque
|
96
|
+
expect(from_hex('#0a649664')).to eql @non_opaque
|
97
|
+
expect(from_hex('0x0a649664')).to eql @non_opaque
|
98
|
+
expect(from_hex('0a6496')).to eql @opaque
|
99
|
+
expect(from_hex('#0a6496')).to eql @opaque
|
100
|
+
expect(from_hex('0x0a6496')).to eql @opaque
|
101
|
+
expect(from_hex('abc')).to eql 0xaabbccff
|
102
|
+
expect(from_hex('#abc')).to eql 0xaabbccff
|
103
|
+
expect(from_hex('0xabc')).to eql 0xaabbccff
|
104
104
|
end
|
105
105
|
|
106
106
|
it 'should allow setting opacity explicitly' do
|
107
|
-
from_hex('0x0a6496', 0x64).
|
108
|
-
from_hex('#0a6496', 0x64).
|
109
|
-
from_hex('0xabc', 0xdd).
|
110
|
-
from_hex('#abc', 0xdd).
|
107
|
+
expect(from_hex('0x0a6496', 0x64)).to eql @non_opaque
|
108
|
+
expect(from_hex('#0a6496', 0x64)).to eql @non_opaque
|
109
|
+
expect(from_hex('0xabc', 0xdd)).to eql 0xaabbccdd
|
110
|
+
expect(from_hex('#abc', 0xdd)).to eql 0xaabbccdd
|
111
111
|
end
|
112
112
|
end
|
113
113
|
|
114
114
|
describe '#from_hsv' do
|
115
115
|
it 'should load colors correctly from an HSV triple' do
|
116
116
|
# At 0 brightness, should be @black independent of hue or sat
|
117
|
-
from_hsv(0, 0, 0).
|
118
|
-
from_hsv(100, 1, 0).
|
119
|
-
from_hsv(100, 0.5, 0).
|
120
|
-
|
117
|
+
expect(from_hsv(0, 0, 0)).to eql @black
|
118
|
+
expect(from_hsv(100, 1, 0)).to eql @black
|
119
|
+
expect(from_hsv(100, 0.5, 0)).to eql @black
|
120
|
+
|
121
121
|
# At brightness 1 and sat 0, should be @white regardless of hue
|
122
|
-
from_hsv(0, 0, 1).
|
123
|
-
from_hsv(100, 0, 1).
|
122
|
+
expect(from_hsv(0, 0, 1)).to eql @white
|
123
|
+
expect(from_hsv(100, 0, 1)).to eql @white
|
124
124
|
|
125
125
|
# Converting the "pure" colors should work
|
126
|
-
from_hsv(0, 1, 1).
|
127
|
-
from_hsv(120, 1, 1).
|
128
|
-
from_hsv(240, 1, 1).
|
126
|
+
expect(from_hsv(0, 1, 1)).to eql @red
|
127
|
+
expect(from_hsv(120, 1, 1)).to eql @green
|
128
|
+
expect(from_hsv(240, 1, 1)).to eql @blue
|
129
129
|
|
130
130
|
# And, finally, one random color
|
131
|
-
from_hsv(120, 0.5, 0.80).
|
131
|
+
expect(from_hsv(120, 0.5, 0.80)).to eql 0x66cc66ff
|
132
132
|
end
|
133
133
|
|
134
134
|
it 'should optionally accept a fourth param for alpha' do
|
135
|
-
from_hsv(0, 1, 1, 255).
|
136
|
-
from_hsv(120, 1, 1, 255).
|
137
|
-
from_hsv(240, 1, 1, 255).
|
138
|
-
from_hsv(0, 1, 1, 0).
|
139
|
-
from_hsv(120, 1, 1, 0).
|
140
|
-
from_hsv(240, 1, 1, 0).
|
135
|
+
expect(from_hsv(0, 1, 1, 255)).to eql @red
|
136
|
+
expect(from_hsv(120, 1, 1, 255)).to eql @green
|
137
|
+
expect(from_hsv(240, 1, 1, 255)).to eql @blue
|
138
|
+
expect(from_hsv(0, 1, 1, 0)).to eql 0xff000000 # transparent red
|
139
|
+
expect(from_hsv(120, 1, 1, 0)).to eql 0x00ff0000 # transparent green
|
140
|
+
expect(from_hsv(240, 1, 1, 0)).to eql 0x0000ff00 # transparent blue
|
141
141
|
end
|
142
142
|
end
|
143
143
|
|
144
144
|
describe '#from_hsl' do
|
145
145
|
it 'should load colors correctly from an HSL triple' do
|
146
146
|
# At 0 lightness, should always be black
|
147
|
-
from_hsl(0, 0, 0).
|
148
|
-
from_hsl(100, 0, 0).
|
149
|
-
from_hsl(54, 0.5, 0).
|
150
|
-
|
147
|
+
expect(from_hsl(0, 0, 0)).to eql @black
|
148
|
+
expect(from_hsl(100, 0, 0)).to eql @black
|
149
|
+
expect(from_hsl(54, 0.5, 0)).to eql @black
|
150
|
+
|
151
151
|
# At 1 lightness, should always be white
|
152
|
-
from_hsl(0, 0, 1).
|
153
|
-
from_hsl(0, 0.5, 1).
|
154
|
-
from_hsl(110, 0, 1).
|
155
|
-
|
152
|
+
expect(from_hsl(0, 0, 1)).to eql @white
|
153
|
+
expect(from_hsl(0, 0.5, 1)).to eql @white
|
154
|
+
expect(from_hsl(110, 0, 1)).to eql @white
|
155
|
+
|
156
156
|
# 'Pure' colors should work
|
157
|
-
from_hsl(0, 1, 0.5).
|
158
|
-
from_hsl(120, 1, 0.5).
|
159
|
-
from_hsl(240, 1, 0.5).
|
160
|
-
|
157
|
+
expect(from_hsl(0, 1, 0.5)).to eql @red
|
158
|
+
expect(from_hsl(120, 1, 0.5)).to eql @green
|
159
|
+
expect(from_hsl(240, 1, 0.5)).to eql @blue
|
160
|
+
|
161
161
|
# Random colors
|
162
162
|
from_hsl(87.27, 0.5, 0.5686) == 0x96c85aff
|
163
163
|
from_hsl(271.83, 0.5399, 0.4176) == 0x6e31a4ff
|
@@ -165,194 +165,194 @@ describe ChunkyPNG::Color do
|
|
165
165
|
end
|
166
166
|
|
167
167
|
it 'should optionally accept a fourth param for alpha' do
|
168
|
-
from_hsl(0, 1, 0.5, 255).
|
169
|
-
from_hsl(120, 1, 0.5, 255).
|
170
|
-
from_hsl(240, 1, 0.5, 255).
|
171
|
-
from_hsl(0, 1, 0.5, 0).
|
172
|
-
from_hsl(120, 1, 0.5, 0).
|
173
|
-
from_hsl(240, 1, 0.5, 0).
|
168
|
+
expect(from_hsl(0, 1, 0.5, 255)).to eql @red
|
169
|
+
expect(from_hsl(120, 1, 0.5, 255)).to eql @green
|
170
|
+
expect(from_hsl(240, 1, 0.5, 255)).to eql @blue
|
171
|
+
expect(from_hsl(0, 1, 0.5, 0)).to eql 0xff000000 # transparent red
|
172
|
+
expect(from_hsl(120, 1, 0.5, 0)).to eql 0x00ff0000 # transparent green
|
173
|
+
expect(from_hsl(240, 1, 0.5, 0)).to eql 0x0000ff00 # transparent blue
|
174
174
|
end
|
175
175
|
end
|
176
176
|
|
177
177
|
describe '#html_color' do
|
178
178
|
it 'should find the correct color value' do
|
179
|
-
html_color(:springgreen).
|
180
|
-
html_color(:spring_green).
|
181
|
-
html_color('springgreen').
|
182
|
-
html_color('spring green').
|
183
|
-
html_color('SpringGreen').
|
184
|
-
html_color('SPRING_GREEN').
|
179
|
+
expect(html_color(:springgreen)).to eql 0x00ff7fff
|
180
|
+
expect(html_color(:spring_green)).to eql 0x00ff7fff
|
181
|
+
expect(html_color('springgreen')).to eql 0x00ff7fff
|
182
|
+
expect(html_color('spring green')).to eql 0x00ff7fff
|
183
|
+
expect(html_color('SpringGreen')).to eql 0x00ff7fff
|
184
|
+
expect(html_color('SPRING_GREEN')).to eql 0x00ff7fff
|
185
185
|
end
|
186
186
|
|
187
187
|
it 'should set the opacity level explicitly' do
|
188
|
-
html_color(:springgreen, 0xff).
|
189
|
-
html_color(:springgreen, 0xaa).
|
190
|
-
html_color(:springgreen, 0x00).
|
188
|
+
expect(html_color(:springgreen, 0xff)).to eql 0x00ff7fff
|
189
|
+
expect(html_color(:springgreen, 0xaa)).to eql 0x00ff7faa
|
190
|
+
expect(html_color(:springgreen, 0x00)).to eql 0x00ff7f00
|
191
191
|
end
|
192
192
|
|
193
193
|
it 'should set opacity levels from the color name' do
|
194
|
-
html_color('Spring green @ 1.0').
|
195
|
-
html_color('Spring green @ 0.666').
|
196
|
-
html_color('Spring green @ 0.0').
|
194
|
+
expect(html_color('Spring green @ 1.0')).to eql 0x00ff7fff
|
195
|
+
expect(html_color('Spring green @ 0.666')).to eql 0x00ff7faa
|
196
|
+
expect(html_color('Spring green @ 0.0')).to eql 0x00ff7f00
|
197
197
|
end
|
198
198
|
|
199
199
|
it 'should raise for an unkown color name' do
|
200
|
-
|
200
|
+
expect { html_color(:nonsense) }.to raise_error(ArgumentError)
|
201
201
|
end
|
202
202
|
end
|
203
203
|
|
204
204
|
describe '#opaque?' do
|
205
205
|
it 'should correctly check for opaqueness' do
|
206
|
-
opaque?(@white).
|
207
|
-
opaque?(@black).
|
208
|
-
opaque?(@opaque).
|
209
|
-
opaque?(@non_opaque).
|
210
|
-
opaque?(@fully_transparent).
|
206
|
+
expect(opaque?(@white)).to eql true
|
207
|
+
expect(opaque?(@black)).to eql true
|
208
|
+
expect(opaque?(@opaque)).to eql true
|
209
|
+
expect(opaque?(@non_opaque)).to eql false
|
210
|
+
expect(opaque?(@fully_transparent)).to eql false
|
211
211
|
end
|
212
212
|
end
|
213
213
|
|
214
214
|
describe 'extraction of separate color channels' do
|
215
215
|
it 'should extract components from a color correctly' do
|
216
|
-
r(@opaque).
|
217
|
-
g(@opaque).
|
218
|
-
b(@opaque).
|
219
|
-
a(@opaque).
|
216
|
+
expect(r(@opaque)).to eql 10
|
217
|
+
expect(g(@opaque)).to eql 100
|
218
|
+
expect(b(@opaque)).to eql 150
|
219
|
+
expect(a(@opaque)).to eql 255
|
220
220
|
end
|
221
221
|
end
|
222
222
|
|
223
223
|
describe '#grayscale_teint' do
|
224
224
|
it 'should calculate the correct grayscale teint' do
|
225
|
-
grayscale_teint(@opaque).
|
226
|
-
grayscale_teint(@non_opaque).
|
225
|
+
expect(grayscale_teint(@opaque)).to eql 79
|
226
|
+
expect(grayscale_teint(@non_opaque)).to eql 79
|
227
227
|
end
|
228
228
|
end
|
229
229
|
|
230
230
|
describe '#to_grayscale' do
|
231
231
|
it 'should use the grayscale teint for r, g and b' do
|
232
232
|
gs = to_grayscale(@non_opaque)
|
233
|
-
r(gs).
|
234
|
-
g(gs).
|
235
|
-
b(gs).
|
233
|
+
expect(r(gs)).to eql grayscale_teint(@non_opaque)
|
234
|
+
expect(g(gs)).to eql grayscale_teint(@non_opaque)
|
235
|
+
expect(b(gs)).to eql grayscale_teint(@non_opaque)
|
236
236
|
end
|
237
237
|
|
238
238
|
it 'should preserve the alpha channel' do
|
239
|
-
a(to_grayscale(@non_opaque)).
|
240
|
-
a(to_grayscale(@opaque)).
|
239
|
+
expect(a(to_grayscale(@non_opaque))).to eql a(@non_opaque)
|
240
|
+
expect(a(to_grayscale(@opaque))).to eql ChunkyPNG::Color::MAX
|
241
241
|
end
|
242
242
|
end
|
243
243
|
|
244
244
|
describe '#to_hex' do
|
245
245
|
it 'should represent colors correcly using hex notation' do
|
246
|
-
to_hex(@white).
|
247
|
-
to_hex(@black).
|
248
|
-
to_hex(@opaque).
|
249
|
-
to_hex(@non_opaque).
|
250
|
-
to_hex(@fully_transparent).
|
246
|
+
expect(to_hex(@white)).to eql '#ffffffff'
|
247
|
+
expect(to_hex(@black)).to eql '#000000ff'
|
248
|
+
expect(to_hex(@opaque)).to eql '#0a6496ff'
|
249
|
+
expect(to_hex(@non_opaque)).to eql '#0a649664'
|
250
|
+
expect(to_hex(@fully_transparent)).to eql '#0a649600'
|
251
251
|
end
|
252
252
|
|
253
253
|
it 'should represent colors correcly using hex notation without alpha channel' do
|
254
|
-
to_hex(@white, false).
|
255
|
-
to_hex(@black, false).
|
256
|
-
to_hex(@opaque, false).
|
257
|
-
to_hex(@non_opaque, false).
|
258
|
-
to_hex(@fully_transparent, false).
|
254
|
+
expect(to_hex(@white, false)).to eql '#ffffff'
|
255
|
+
expect(to_hex(@black, false)).to eql '#000000'
|
256
|
+
expect(to_hex(@opaque, false)).to eql '#0a6496'
|
257
|
+
expect(to_hex(@non_opaque, false)).to eql '#0a6496'
|
258
|
+
expect(to_hex(@fully_transparent, false)).to eql '#0a6496'
|
259
259
|
end
|
260
260
|
end
|
261
261
|
|
262
262
|
describe '#to_hsv' do
|
263
263
|
it 'should return a [hue, saturation, value] array' do
|
264
|
-
to_hsv(@white).
|
265
|
-
to_hsv(@black).
|
266
|
-
to_hsv(@red).
|
267
|
-
to_hsv(@blue).
|
268
|
-
to_hsv(@green).
|
269
|
-
to_hsv(0x805440ff)[0].
|
270
|
-
to_hsv(0x805440ff)[1].
|
271
|
-
to_hsv(0x805440ff)[2].
|
264
|
+
expect(to_hsv(@white)).to eql [0, 0.0, 1.0]
|
265
|
+
expect(to_hsv(@black)).to eql [0, 0.0, 0.0]
|
266
|
+
expect(to_hsv(@red)).to eql [0, 1.0, 1.0]
|
267
|
+
expect(to_hsv(@blue)).to eql [240, 1.0, 1.0]
|
268
|
+
expect(to_hsv(@green)).to eql [120, 1.0, 1.0]
|
269
|
+
expect(to_hsv(0x805440ff)[0]).to be_within(1).of(19)
|
270
|
+
expect(to_hsv(0x805440ff)[1]).to be_within(0.01).of(0.5)
|
271
|
+
expect(to_hsv(0x805440ff)[2]).to be_within(0.01).of(0.5)
|
272
272
|
end
|
273
273
|
|
274
274
|
it 'should optionally include the alpha channel' do
|
275
|
-
to_hsv(@white, true).
|
276
|
-
to_hsv(@red, true).
|
277
|
-
to_hsv(@blue, true).
|
278
|
-
to_hsv(@green, true).
|
279
|
-
to_hsv(@opaque, true)[3].
|
280
|
-
to_hsv(@fully_transparent, true)[3].
|
275
|
+
expect(to_hsv(@white, true)).to eql [0, 0.0, 1.0, 255]
|
276
|
+
expect(to_hsv(@red, true)).to eql [0, 1.0, 1.0, 255]
|
277
|
+
expect(to_hsv(@blue, true)).to eql [240, 1.0, 1.0, 255]
|
278
|
+
expect(to_hsv(@green, true)).to eql [120, 1.0, 1.0, 255]
|
279
|
+
expect(to_hsv(@opaque, true)[3]).to eql 255
|
280
|
+
expect(to_hsv(@fully_transparent, true)[3]).to eql 0
|
281
281
|
end
|
282
282
|
end
|
283
283
|
|
284
284
|
describe '#to_hsl' do
|
285
285
|
it 'should return a [hue, saturation, lightness] array' do
|
286
|
-
to_hsl(@white).
|
287
|
-
to_hsl(@black).
|
288
|
-
to_hsl(@red).
|
289
|
-
to_hsl(@blue).
|
290
|
-
to_hsl(@green).
|
286
|
+
expect(to_hsl(@white)).to eql [0, 0.0, 1.0]
|
287
|
+
expect(to_hsl(@black)).to eql [0, 0.0, 0.0]
|
288
|
+
expect(to_hsl(@red)).to eql [0, 1.0, 0.5]
|
289
|
+
expect(to_hsl(@blue)).to eql [240, 1.0, 0.5]
|
290
|
+
expect(to_hsl(@green)).to eql [120, 1.0, 0.5]
|
291
291
|
end
|
292
292
|
|
293
293
|
it 'should optionally include the alpha channel in the returned array' do
|
294
|
-
to_hsl(@white, true).
|
295
|
-
to_hsl(@black, true).
|
296
|
-
to_hsl(@red, true).
|
297
|
-
to_hsl(@blue, true).
|
298
|
-
to_hsl(@green, true).
|
299
|
-
to_hsl(@opaque, true)[3].
|
300
|
-
to_hsl(@fully_transparent, true)[3].
|
294
|
+
expect(to_hsl(@white, true)).to eql [0, 0.0, 1.0, 255]
|
295
|
+
expect(to_hsl(@black, true)).to eql [0, 0.0, 0.0, 255]
|
296
|
+
expect(to_hsl(@red, true)).to eql [0, 1.0, 0.5, 255]
|
297
|
+
expect(to_hsl(@blue, true)).to eql [240, 1.0, 0.5, 255]
|
298
|
+
expect(to_hsl(@green, true)).to eql [120, 1.0, 0.5, 255]
|
299
|
+
expect(to_hsl(@opaque, true)[3]).to eql 255
|
300
|
+
expect(to_hsl(@fully_transparent, true)[3]).to eql 0
|
301
301
|
end
|
302
302
|
end
|
303
303
|
|
304
304
|
describe 'conversion to other formats' do
|
305
305
|
it 'should convert the individual color values back correctly' do
|
306
|
-
to_truecolor_bytes(@opaque).
|
307
|
-
to_truecolor_alpha_bytes(@non_opaque).
|
306
|
+
expect(to_truecolor_bytes(@opaque)).to eql [10, 100, 150]
|
307
|
+
expect(to_truecolor_alpha_bytes(@non_opaque)).to eql [10, 100, 150, 100]
|
308
308
|
end
|
309
309
|
end
|
310
310
|
|
311
311
|
describe '#compose' do
|
312
312
|
|
313
313
|
it 'should use the foregorund color as is when the background color is fully transparent' do
|
314
|
-
compose(@non_opaque, @fully_transparent).
|
314
|
+
expect(compose(@non_opaque, @fully_transparent)).to eql @non_opaque
|
315
315
|
end
|
316
316
|
|
317
317
|
it 'should use the foregorund color as is when an opaque color is given as foreground color' do
|
318
|
-
compose(@opaque, @white).
|
318
|
+
expect(compose(@opaque, @white)).to eql @opaque
|
319
319
|
end
|
320
320
|
|
321
321
|
it 'should use the background color as is when a fully transparent pixel is given as foreground color' do
|
322
|
-
compose(@fully_transparent, @white).
|
322
|
+
expect(compose(@fully_transparent, @white)).to eql @white
|
323
323
|
end
|
324
324
|
|
325
325
|
it 'should compose pixels correctly with both algorithms' do
|
326
|
-
compose_quick(@non_opaque, @white).
|
327
|
-
compose_precise(@non_opaque, @white).
|
326
|
+
expect(compose_quick(@non_opaque, @white)).to eql 0x9fc2d6ff
|
327
|
+
expect(compose_precise(@non_opaque, @white)).to eql 0x9fc2d6ff
|
328
328
|
end
|
329
329
|
end
|
330
330
|
|
331
331
|
describe '#decompose_alpha' do
|
332
332
|
it 'should decompose the alpha channel correctly' do
|
333
|
-
decompose_alpha(0x9fc2d6ff, @opaque, @white).
|
333
|
+
expect(decompose_alpha(0x9fc2d6ff, @opaque, @white)).to eql 0x00000064
|
334
334
|
end
|
335
335
|
|
336
336
|
it 'should return fully transparent if the background channel matches the resulting color' do
|
337
|
-
decompose_alpha(0xabcdefff, 0xff000000, 0xabcdefff).
|
337
|
+
expect(decompose_alpha(0xabcdefff, 0xff000000, 0xabcdefff)).to eql 0x00
|
338
338
|
end
|
339
339
|
|
340
340
|
it 'should return fully opaque if the background channel matches the mask color' do
|
341
|
-
decompose_alpha(0xff000000, 0xabcdefff, 0xabcdefff).
|
341
|
+
expect(decompose_alpha(0xff000000, 0xabcdefff, 0xabcdefff)).to eql 0xff
|
342
342
|
end
|
343
343
|
|
344
344
|
it 'should return fully opaque if the resulting color matches the mask color' do
|
345
|
-
decompose_alpha(0xabcdefff, 0xabcdefff, 0xffffffff).
|
345
|
+
expect(decompose_alpha(0xabcdefff, 0xabcdefff, 0xffffffff)).to eql 255
|
346
346
|
end
|
347
347
|
end
|
348
348
|
|
349
349
|
describe '#blend' do
|
350
350
|
it 'should blend colors correctly' do
|
351
|
-
blend(@opaque, @black).
|
351
|
+
expect(blend(@opaque, @black)).to eql 0x05324bff
|
352
352
|
end
|
353
353
|
|
354
354
|
it 'should not matter what color is used as foreground, and what as background' do
|
355
|
-
blend(@opaque, @black).
|
355
|
+
expect(blend(@opaque, @black)).to eql blend(@black, @opaque)
|
356
356
|
end
|
357
357
|
end
|
358
358
|
|