color_lib 1.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,129 @@
1
+ require 'minitest/autorun'
2
+ require 'test/unit/assertions'
3
+ require 'color_lib'
4
+ require 'color_lib/css'
5
+
6
+ module TestColorLib
7
+ class TestColorLib < Minitest::Test
8
+ include Test::Unit::Assertions
9
+
10
+ def setup
11
+ Kernel.module_eval do
12
+ alias old_warn warn
13
+
14
+ def warn(message)
15
+ $last_warn = message
16
+ end
17
+ end
18
+ end
19
+
20
+ def teardown
21
+ Kernel.module_eval do
22
+ undef warn
23
+ alias warn old_warn
24
+ undef old_warn
25
+ end
26
+ end
27
+
28
+ def test_const
29
+ $last_warn = nil
30
+ assert_equal(ColorLib::RGB::AliceBlue, ColorLib::AliceBlue)
31
+ assert_equal("ColorLib::AliceBlue has been deprecated. Use ColorLib::RGB::AliceBlue instead.", $last_warn)
32
+
33
+ $last_warn = nil # Do this twice to make sure it always happens...
34
+ assert(ColorLib::AliceBlue)
35
+ assert_equal("ColorLib::AliceBlue has been deprecated. Use ColorLib::RGB::AliceBlue instead.", $last_warn)
36
+
37
+ $last_warn = nil
38
+ assert_equal(ColorLib::COLOR_VERSION, ColorLib::VERSION)
39
+
40
+ $last_warn = nil
41
+ assert_equal(ColorLib::COLOR_VERSION, ColorLib::COLOR_TOOLS_VERSION)
42
+ assert_equal("ColorLib::COLOR_TOOLS_VERSION has been deprecated. Use ColorLib::COLOR_VERSION instead.", $last_warn)
43
+
44
+ $last_warn = nil
45
+ assert(ColorLib::COLOR_VERSION)
46
+ assert_nil($last_warn)
47
+ assert(ColorLib::COLOR_EPSILON)
48
+ assert_nil($last_warn)
49
+
50
+ assert_raises(NameError) { assert(ColorLib::MISSING_VALUE) }
51
+ end
52
+
53
+ def test_normalize
54
+ (1..10).each do |i|
55
+ assert_equal(0.0, ColorLib.normalize(-7 * i))
56
+ assert_equal(0.0, ColorLib.normalize(-7 / i))
57
+ assert_equal(0.0, ColorLib.normalize(0 - i))
58
+ assert_equal(1.0, ColorLib.normalize(255 + i))
59
+ assert_equal(1.0, ColorLib.normalize(256 * i))
60
+ assert_equal(1.0, ColorLib.normalize(65536 / i))
61
+ end
62
+ (0..255).each do |i|
63
+ assert_in_delta(i / 255.0, ColorLib.normalize(i / 255.0),
64
+ 1e-2)
65
+ end
66
+ end
67
+
68
+ def test_normalize_range
69
+ assert_equal(0, ColorLib.normalize_8bit(-1))
70
+ assert_equal(0, ColorLib.normalize_8bit(0))
71
+ assert_equal(127, ColorLib.normalize_8bit(127))
72
+ assert_equal(172, ColorLib.normalize_8bit(172))
73
+ assert_equal(255, ColorLib.normalize_8bit(255))
74
+ assert_equal(255, ColorLib.normalize_8bit(256))
75
+
76
+ assert_equal(0, ColorLib.normalize_16bit(-1))
77
+ assert_equal(0, ColorLib.normalize_16bit(0))
78
+ assert_equal(127, ColorLib.normalize_16bit(127))
79
+ assert_equal(172, ColorLib.normalize_16bit(172))
80
+ assert_equal(255, ColorLib.normalize_16bit(255))
81
+ assert_equal(256, ColorLib.normalize_16bit(256))
82
+ assert_equal(65535, ColorLib.normalize_16bit(65535))
83
+ assert_equal(65535, ColorLib.normalize_16bit(66536))
84
+
85
+ assert_equal(-100, ColorLib.normalize_to_range(-101, -100..100))
86
+ assert_equal(-100, ColorLib.normalize_to_range(-100.5, -100..100))
87
+ assert_equal(-100, ColorLib.normalize_to_range(-100, -100..100))
88
+ assert_equal(-100, ColorLib.normalize_to_range(-100.0, -100..100))
89
+ assert_equal(-99.5, ColorLib.normalize_to_range(-99.5, -100..100))
90
+ assert_equal(-50, ColorLib.normalize_to_range(-50, -100..100))
91
+ assert_equal(-50.5, ColorLib.normalize_to_range(-50.5, -100..100))
92
+ assert_equal(0, ColorLib.normalize_to_range(0, -100..100))
93
+ assert_equal(50, ColorLib.normalize_to_range(50, -100..100))
94
+ assert_equal(50.5, ColorLib.normalize_to_range(50.5, -100..100))
95
+ assert_equal(99, ColorLib.normalize_to_range(99, -100..100))
96
+ assert_equal(99.5, ColorLib.normalize_to_range(99.5, -100..100))
97
+ assert_equal(100, ColorLib.normalize_to_range(100, -100..100))
98
+ assert_equal(100, ColorLib.normalize_to_range(100.0, -100..100))
99
+ assert_equal(100, ColorLib.normalize_to_range(100.5, -100..100))
100
+ assert_equal(100, ColorLib.normalize_to_range(101, -100..100))
101
+ end
102
+
103
+ def test_new
104
+ $last_warn = nil
105
+ c = ColorLib.new("#fff")
106
+ assert_kind_of(ColorLib::HSL, c)
107
+ assert_equal(ColorLib::RGB::White.to_hsl, c)
108
+ assert_equal("ColorLib.new has been deprecated. Use ColorLib::RGB.new instead.", $last_warn)
109
+
110
+ $last_warn = nil
111
+ c = ColorLib.new([0, 0, 0])
112
+ assert_kind_of(ColorLib::HSL, c)
113
+ assert_equal(ColorLib::RGB::Black.to_hsl, c)
114
+ assert_equal("ColorLib.new has been deprecated. Use ColorLib::RGB.new instead.", $last_warn)
115
+
116
+ $last_warn = nil
117
+ c = ColorLib.new([10, 20, 30], :hsl)
118
+ assert_kind_of(ColorLib::HSL, c)
119
+ assert_equal(ColorLib::HSL.new(10, 20, 30), c)
120
+ assert_equal("ColorLib.new has been deprecated. Use ColorLib::HSL.new instead.", $last_warn)
121
+
122
+ $last_warn = nil
123
+ c = ColorLib.new([10, 20, 30, 40], :cmyk)
124
+ assert_kind_of(ColorLib::HSL, c)
125
+ assert_equal(ColorLib::CMYK.new(10, 20, 30, 40).to_hsl, c)
126
+ assert_equal("ColorLib.new has been deprecated. Use ColorLib::CMYK.new instead.", $last_warn)
127
+ end
128
+ end
129
+ end
data/test/test_css.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'minitest/autorun'
2
+ require 'test/unit/assertions'
3
+ require 'color_lib'
4
+ require 'color_lib/css'
5
+
6
+ module TestColorLib
7
+ class TestCSS < Minitest::Test
8
+ include Test::Unit::Assertions
9
+
10
+ def test_index
11
+ assert_equal(ColorLib::RGB::AliceBlue, ColorLib::CSS[:aliceblue])
12
+ assert_equal(ColorLib::RGB::AliceBlue, ColorLib::CSS["AliceBlue"])
13
+ assert_equal(ColorLib::RGB::AliceBlue, ColorLib::CSS["aliceBlue"])
14
+ assert_equal(ColorLib::RGB::AliceBlue, ColorLib::CSS["aliceblue"])
15
+ assert_equal(ColorLib::RGB::AliceBlue, ColorLib::CSS[:AliceBlue])
16
+ end
17
+ end
18
+ end
data/test/test_gimp.rb ADDED
@@ -0,0 +1,87 @@
1
+ require 'minitest/autorun'
2
+ require 'test/unit/assertions'
3
+ require 'color_lib'
4
+ require 'color_lib/palette/gimp'
5
+
6
+ module TestColorLib
7
+ module TestPalette
8
+ class TestGimp < Minitest::Test
9
+ include Test::Unit::Assertions
10
+ include ColorLib::Palette
11
+
12
+ GIMP_W3C = <<-EOS
13
+ GIMP Palette
14
+ Name: W3C Named ColorLibs
15
+ Columns: 2
16
+ #
17
+ # ColorLibZilla W3C Named ColorLibs
18
+ #
19
+ 255 255 255 White
20
+ 255 255 0 Yclow
21
+ 255 0 255 Fuchsia
22
+ 255 0 0 Red
23
+ 192 192 192 Silver
24
+ 128 128 128 Gray
25
+ 128 128 0 Olive
26
+ 128 0 128 Purple
27
+ 128 0 0 Maroon
28
+ 0 255 255 Aqua
29
+ 0 255 0 Lime
30
+ 0 128 128 Teal
31
+ 0 128 0 Green
32
+ 0 0 255 Blue
33
+ 0 0 128 Navy
34
+ 0 0 0 Black
35
+ EOS
36
+
37
+ def setup
38
+ @filename = "test#{Process.pid}.gimp"
39
+ end
40
+
41
+ def teardown
42
+ require 'fileutils'
43
+ FileUtils.rm_f @filename if File.exist? @filename
44
+ end
45
+
46
+ def test_each
47
+ @gimp = Gimp.new(GIMP_W3C)
48
+ assert_equal(16, @gimp.instance_variable_get(:@colors).size)
49
+ @gimp.each { |c| assert_kind_of(ColorLib::RGB, c) }
50
+ end
51
+
52
+ def test_each_name
53
+ @gimp = Gimp.new(GIMP_W3C)
54
+ assert_equal(16, @gimp.instance_variable_get(:@names).size)
55
+
56
+ @gimp.each_name { |color_name, color_set|
57
+ assert_kind_of(Array, color_set)
58
+ color_set.each { |c|
59
+ assert_kind_of(ColorLib::RGB, c)
60
+ }
61
+ }
62
+ end
63
+
64
+ def test_index
65
+ File.open(@filename, "wb") do |f|
66
+ f.write GIMP_W3C
67
+ end
68
+ @gimp = Gimp.from_file(@filename)
69
+ assert_equal(ColorLib::RGB::White, @gimp[0])
70
+ assert_equal(ColorLib::RGB::White, @gimp["White"][0])
71
+ assert_equal([ColorLib::RGB::White, ColorLib::RGB::Black],
72
+ @gimp.values_at(0, -1))
73
+ assert_equal(16, @gimp.size)
74
+ end
75
+
76
+ def test_valid_eh
77
+ @gimp = Gimp.new(GIMP_W3C)
78
+ assert(@gimp.valid?)
79
+ end
80
+
81
+ def test_name
82
+ @gimp = Gimp.new(GIMP_W3C)
83
+ assert_equal("W3C Named ColorLibs", @gimp.name)
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,110 @@
1
+ require 'minitest/autorun'
2
+ require 'test/unit/assertions'
3
+ require 'color_lib'
4
+
5
+ module TestColorLib
6
+ class TestGrayScale < Minitest::Test
7
+ include Test::Unit::Assertions
8
+
9
+ def setup
10
+ @gs = ColorLib::GrayScale.from_percent(33)
11
+ end
12
+
13
+ def test_brightness
14
+ assert_in_delta(0.33, @gs.brightness, ColorLib::COLOR_TOLERANCE)
15
+ end
16
+
17
+ def test_darken_by
18
+ assert_in_delta(29.7, @gs.darken_by(10).gray, ColorLib::COLOR_TOLERANCE)
19
+ end
20
+
21
+ def test_g
22
+ assert_in_delta(0.33, @gs.g, ColorLib::COLOR_TOLERANCE)
23
+ assert_in_delta(33, @gs.grey, ColorLib::COLOR_TOLERANCE)
24
+ @gs.gray = 40
25
+ assert_in_delta(0.4, @gs.g, ColorLib::COLOR_TOLERANCE)
26
+ @gs.g = 2.0
27
+ assert_in_delta(100, @gs.gray, ColorLib::COLOR_TOLERANCE)
28
+ @gs.grey = -2.0
29
+ assert_in_delta(0.0, @gs.g, ColorLib::COLOR_TOLERANCE)
30
+ end
31
+
32
+ def test_html_css
33
+ assert_equal("#545454", @gs.html)
34
+ assert_equal("rgb(33.00%, 33.00%, 33.00%)", @gs.css_rgb)
35
+ assert_equal("rgba(33.00%, 33.00%, 33.00%, 1.00)", @gs.css_rgba)
36
+ end
37
+
38
+ def test_lighten_by
39
+ assert_in_delta(0.363, @gs.lighten_by(10).g, ColorLib::COLOR_TOLERANCE)
40
+ end
41
+
42
+ def test_pdf_fill
43
+ assert_equal("0.330 g", @gs.pdf_fill)
44
+ assert_equal("0.330 G", @gs.pdf_stroke)
45
+ end
46
+
47
+ def test_to_cmyk
48
+ cmyk = nil
49
+ cmyk = @gs.to_cmyk
50
+ assert_kind_of(ColorLib::CMYK, cmyk)
51
+ assert_in_delta(0.0, cmyk.c, ColorLib::COLOR_TOLERANCE)
52
+ assert_in_delta(0.0, cmyk.m, ColorLib::COLOR_TOLERANCE)
53
+ assert_in_delta(0.0, cmyk.y, ColorLib::COLOR_TOLERANCE)
54
+ assert_in_delta(0.67, cmyk.k, ColorLib::COLOR_TOLERANCE)
55
+ end
56
+
57
+ def test_to_grayscale
58
+ assert_equal(@gs, @gs.to_grayscale)
59
+ assert_equal(@gs, @gs.to_greyscale)
60
+ end
61
+
62
+ def test_to_hsl
63
+ hsl = nil
64
+ hsl = @gs.to_hsl
65
+ assert_kind_of(ColorLib::HSL, hsl)
66
+ assert_in_delta(0.0, hsl.h, ColorLib::COLOR_TOLERANCE)
67
+ assert_in_delta(0.0, hsl.s, ColorLib::COLOR_TOLERANCE)
68
+ assert_in_delta(0.33, hsl.l, ColorLib::COLOR_TOLERANCE)
69
+ assert_equal("hsl(0.00, 0.00%, 33.00%)", @gs.css_hsl)
70
+ assert_equal("hsla(0.00, 0.00%, 33.00%, 1.00)", @gs.css_hsla)
71
+ end
72
+
73
+ def test_to_rgb
74
+ rgb = nil
75
+ rgb = @gs.to_rgb
76
+ assert_kind_of(ColorLib::RGB, rgb)
77
+ assert_in_delta(0.33, rgb.r, ColorLib::COLOR_TOLERANCE)
78
+ assert_in_delta(0.33, rgb.g, ColorLib::COLOR_TOLERANCE)
79
+ assert_in_delta(0.33, rgb.b, ColorLib::COLOR_TOLERANCE)
80
+ end
81
+
82
+ def test_to_yiq
83
+ yiq = nil
84
+ yiq = @gs.to_yiq
85
+ assert_kind_of(ColorLib::YIQ, yiq)
86
+ assert_in_delta(0.33, yiq.y, ColorLib::COLOR_TOLERANCE)
87
+ assert_in_delta(0.0, yiq.i, ColorLib::COLOR_TOLERANCE)
88
+ assert_in_delta(0.0, yiq.q, ColorLib::COLOR_TOLERANCE)
89
+ end
90
+
91
+ def test_add
92
+ delta = @gs + ColorLib::GrayScale.new(20)
93
+ max = @gs + ColorLib::GrayScale.new(80)
94
+
95
+ assert_in_delta(1.0, max.g, ColorLib::COLOR_TOLERANCE)
96
+ assert_in_delta(0.53, delta.g, ColorLib::COLOR_TOLERANCE)
97
+ end
98
+
99
+ def test_subtract
100
+ delta = @gs - ColorLib::GrayScale.new(20)
101
+ max = @gs - ColorLib::GrayScale.new(80)
102
+ assert_in_delta(0.0, max.g, ColorLib::COLOR_TOLERANCE)
103
+ assert_in_delta(0.13, delta.g, ColorLib::COLOR_TOLERANCE)
104
+ end
105
+
106
+ def test_inspect
107
+ assert_equal("Gray [33.00%]", @gs.inspect)
108
+ end
109
+ end
110
+ end
data/test/test_hsl.rb ADDED
@@ -0,0 +1,140 @@
1
+ require 'minitest/autorun'
2
+ require 'test/unit/assertions'
3
+ require 'color_lib'
4
+
5
+ module TestColorLib
6
+ class TestHSL < Minitest::Test
7
+ include Test::Unit::Assertions
8
+
9
+ def setup
10
+ # @hsl = ColorLib::HSL.new(262, 67, 42)
11
+ @hsl = ColorLib::HSL.new(145, 20, 30)
12
+ # @rgb = ColorLib::RGB.new(88, 35, 179)
13
+ end
14
+
15
+ def test_rgb_roundtrip_conversion
16
+ hsl = ColorLib::HSL.new(262, 67, 42)
17
+ c = hsl.to_rgb.to_hsl
18
+ assert_in_delta hsl.h, c.h, ColorLib::COLOR_TOLERANCE, "Hue"
19
+ assert_in_delta hsl.s, c.s, ColorLib::COLOR_TOLERANCE, "Saturation"
20
+ assert_in_delta hsl.l, c.l, ColorLib::COLOR_TOLERANCE, "Luminance"
21
+ end
22
+
23
+ def test_brightness
24
+ assert_in_delta 0.3, @hsl.brightness, ColorLib::COLOR_TOLERANCE
25
+ end
26
+
27
+ def test_hue
28
+ assert_in_delta 0.4027, @hsl.h, ColorLib::COLOR_TOLERANCE
29
+ assert_in_delta 145, @hsl.hue, ColorLib::COLOR_TOLERANCE
30
+ @hsl.hue = 33
31
+ assert_in_delta 0.09167, @hsl.h, ColorLib::COLOR_TOLERANCE
32
+ @hsl.hue = -33
33
+ assert_in_delta 0.90833, @hsl.h, ColorLib::COLOR_TOLERANCE
34
+ @hsl.h = 3.3
35
+ assert_in_delta 360, @hsl.hue, ColorLib::COLOR_TOLERANCE
36
+ @hsl.h = -3.3
37
+ assert_in_delta 0.0, @hsl.h, ColorLib::COLOR_TOLERANCE
38
+ @hsl.hue = 0
39
+ @hsl.hue -= 20
40
+ assert_in_delta 340, @hsl.hue, ColorLib::COLOR_TOLERANCE
41
+ @hsl.hue += 45
42
+ assert_in_delta 25, @hsl.hue, ColorLib::COLOR_TOLERANCE
43
+ end
44
+
45
+ def test_saturation
46
+ assert_in_delta 0.2, @hsl.s, ColorLib::COLOR_TOLERANCE
47
+ assert_in_delta 20, @hsl.saturation, ColorLib::COLOR_TOLERANCE
48
+ @hsl.saturation = 33
49
+ assert_in_delta 0.33, @hsl.s, ColorLib::COLOR_TOLERANCE
50
+ @hsl.s = 3.3
51
+ assert_in_delta 100, @hsl.saturation, ColorLib::COLOR_TOLERANCE
52
+ @hsl.s = -3.3
53
+ assert_in_delta 0.0, @hsl.s, ColorLib::COLOR_TOLERANCE
54
+ end
55
+
56
+ def test_luminance
57
+ assert_in_delta 0.3, @hsl.l, ColorLib::COLOR_TOLERANCE
58
+ assert_in_delta 30, @hsl.luminosity, ColorLib::COLOR_TOLERANCE
59
+ @hsl.luminosity = 33
60
+ assert_in_delta 0.33, @hsl.l, ColorLib::COLOR_TOLERANCE
61
+ @hsl.l = 3.3
62
+ assert_in_delta 100, @hsl.lightness, ColorLib::COLOR_TOLERANCE
63
+ @hsl.l = -3.3
64
+ assert_in_delta 0.0, @hsl.l, ColorLib::COLOR_TOLERANCE
65
+ end
66
+
67
+ def test_html_css
68
+ assert_equal "hsl(145.00, 20.00%, 30.00%)", @hsl.css_hsl
69
+ assert_equal "hsla(145.00, 20.00%, 30.00%, 1.00)", @hsl.css_hsla
70
+ end
71
+
72
+ def test_to_cmyk
73
+ cmyk = nil
74
+ cmyk = @hsl.to_cmyk
75
+ assert_kind_of ColorLib::CMYK, cmyk
76
+ assert_in_delta 0.3223, cmyk.c, ColorLib::COLOR_TOLERANCE
77
+ assert_in_delta 0.2023, cmyk.m, ColorLib::COLOR_TOLERANCE
78
+ assert_in_delta 0.2723, cmyk.y, ColorLib::COLOR_TOLERANCE
79
+ assert_in_delta 0.4377, cmyk.k, ColorLib::COLOR_TOLERANCE
80
+ end
81
+
82
+ def test_to_grayscale
83
+ gs = nil
84
+ gs = @hsl.to_grayscale
85
+ assert_kind_of ColorLib::GreyScale, gs
86
+ assert_in_delta 30, gs.gray, ColorLib::COLOR_TOLERANCE
87
+ end
88
+
89
+ def test_to_rgb
90
+ rgb = nil
91
+ rgb = @hsl.to_rgb
92
+ assert_kind_of ColorLib::RGB, rgb
93
+ assert_in_delta 0.24, rgb.r, ColorLib::COLOR_TOLERANCE
94
+ assert_in_delta 0.36, rgb.g, ColorLib::COLOR_TOLERANCE
95
+ assert_in_delta 0.29, rgb.b, ColorLib::COLOR_TOLERANCE
96
+ assert_equal "#3d5c4a", @hsl.html
97
+ assert_equal "rgb(24.00%, 36.00%, 29.00%)", @hsl.css_rgb
98
+ assert_equal "rgba(24.00%, 36.00%, 29.00%, 1.00)", @hsl.css_rgba
99
+ # The following tests address a bug reported by Jean Krohn on June 6,
100
+ # 2006 and excercise some previously unexercised code in to_rgb.
101
+ assert_equal ColorLib::RGB::Black, ColorLib::HSL.new(75, 75, 0)
102
+ assert_equal ColorLib::RGB::White, ColorLib::HSL.new(75, 75, 100)
103
+ assert_equal ColorLib::RGB::Gray80, ColorLib::HSL.new(75, 0, 80)
104
+
105
+ # The following tests a bug reported by Adam Johnson on 29 October
106
+ # 2010.
107
+ rgb = ColorLib::RGB.from_fraction(0.34496, 0.1386, 0.701399)
108
+ c = ColorLib::HSL.new(262, 67, 42).to_rgb
109
+ assert_in_delta rgb.r, c.r, ColorLib::COLOR_TOLERANCE, "Red"
110
+ assert_in_delta rgb.g, c.g, ColorLib::COLOR_TOLERANCE, "Green"
111
+ assert_in_delta rgb.b, c.b, ColorLib::COLOR_TOLERANCE, "Blue"
112
+ end
113
+
114
+ def test_to_yiq
115
+ yiq = nil
116
+ yiq = @hsl.to_yiq
117
+ assert_kind_of ColorLib::YIQ, yiq
118
+ assert_in_delta 0.3161, yiq.y, ColorLib::COLOR_TOLERANCE
119
+ assert_in_delta 0.0, yiq.i, ColorLib::COLOR_TOLERANCE
120
+ assert_in_delta 0.0, yiq.q, ColorLib::COLOR_TOLERANCE
121
+ end
122
+
123
+ def test_mix_with
124
+ red = ColorLib::RGB::Red.to_hsl
125
+ yellow = ColorLib::RGB::Yellow.to_hsl
126
+ assert_in_delta 0, red.hue, ColorLib::COLOR_TOLERANCE
127
+ assert_in_delta 60, yellow.hue, ColorLib::COLOR_TOLERANCE
128
+ ry25 = red.mix_with yellow, 0.25
129
+ assert_in_delta 15, ry25.hue, ColorLib::COLOR_TOLERANCE
130
+ ry50 = red.mix_with yellow, 0.50
131
+ assert_in_delta 30, ry50.hue, ColorLib::COLOR_TOLERANCE
132
+ ry75 = red.mix_with yellow, 0.75
133
+ assert_in_delta 45, ry75.hue, ColorLib::COLOR_TOLERANCE
134
+ end
135
+
136
+ def test_inspect
137
+ assert_equal "HSL [145.00 deg, 20.00%, 30.00%]", @hsl.inspect
138
+ end
139
+ end
140
+ end