File: from_hsla_spec.rb

package info (click to toggle)
ruby-rmagick 6.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,232 kB
  • sloc: cpp: 19,563; ruby: 17,147; sh: 88; javascript: 36; makefile: 13
file content (46 lines) | stat: -rw-r--r-- 2,400 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
RSpec.describe Magick::Pixel, '#from_hsla' do
  it 'works' do
    pixel = described_class.from_color('brown')

    expect { described_class.from_hsla(127, 50, 50) }.not_to raise_error
    expect { described_class.from_hsla(127, 50, 50, 0) }.not_to raise_error
    expect { described_class.from_hsla('99%', '100%', '100%', '100%') }.not_to raise_error
    expect { described_class.from_hsla(0, 0, 0, 0) }.not_to raise_error
    expect { described_class.from_hsla(359, 255, 255, 1.0) }.not_to raise_error
    expect { described_class.from_hsla([], 50, 50, 0) }.to raise_error(TypeError)
    expect { described_class.from_hsla(127, [], 50, 0) }.to raise_error(TypeError)
    expect { described_class.from_hsla(127, 50, [], 0) }.to raise_error(TypeError)
    expect { described_class.from_hsla }.to raise_error(ArgumentError)
    expect { described_class.from_hsla(127, 50, 50, 50, 50) }.to raise_error(ArgumentError)
    expect { described_class.from_hsla(-0.01, 0, 0) }.to raise_error(ArgumentError)
    expect { described_class.from_hsla(0, -0.01, 0) }.to raise_error(ArgumentError)
    expect { described_class.from_hsla(0, 0, -0.01) }.to raise_error(ArgumentError)
    expect { described_class.from_hsla(0, 0, 0, -0.01) }.to raise_error(ArgumentError)
    expect { described_class.from_hsla(0, 0, 0, 1.01) }.to raise_error(RangeError)
    expect { described_class.from_hsla(360, 0, 0) }.to raise_error(RangeError)
    expect { described_class.from_hsla(0, 256, 0) }.to raise_error(RangeError)
    expect { described_class.from_hsla(0, 0, 256) }.to raise_error(RangeError)
    expect { pixel.to_hsla }.not_to raise_error

    args = [200, 125.125, 250.5, 0.6]
    px = described_class.from_hsla(*args)
    hsla = px.to_hsla
    expect(hsla[0]).to be_within(0.25).of(args[0])
    expect(hsla[1]).to be_within(0.25).of(args[1])
    expect(hsla[2]).to be_within(0.25).of(args[2])
    expect(hsla[3]).to be_within(0.005).of(args[3])

    # test percentages
    args = ['20%', '20%', '20%', '20%']
    args2 = [360.0 / 5, 255.0 / 5, 255.0 / 5, 1.0 / 5]
    px = described_class.from_hsla(*args)
    hsla = px.to_hsla
    px2 = described_class.from_hsla(*args2)
    hsla2 = px2.to_hsla

    expect(hsla2[0]).to be_within(0.25).of(hsla[0])
    expect(hsla2[1]).to be_within(0.25).of(hsla[1])
    expect(hsla2[2]).to be_within(0.25).of(hsla[2])
    expect(hsla2[3]).to be_within(0.005).of(hsla[3])
  end
end