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,28 @@
1
+ #--
2
+ # Colour management with Ruby.
3
+ #
4
+ # Copyright 2005 Austin Ziegler
5
+ # http://rubyforge.org/ruby-pdf/
6
+ #
7
+ # Licensed under a MIT-style licence.
8
+ #
9
+ # $Id: metallic.rb,v 1.1 2005/08/05 23:07:20 austin Exp $
10
+ #++
11
+
12
+ # This namespace contains some RGB metallic colours suggested by Jim Freeze.
13
+ module Color::RGB::Metallic
14
+ Aluminum = Color::RGB.new(0x99, 0x99, 0x99)
15
+ CoolCopper = Color::RGB.new(0xd9, 0x87, 0x19)
16
+ Copper = Color::RGB.new(0xb8, 0x73, 0x33)
17
+ Iron = Color::RGB.new(0x4c, 0x4c, 0x4c)
18
+ Lead = Color::RGB.new(0x19, 0x19, 0x19)
19
+ Magnesium = Color::RGB.new(0xb3, 0xb3, 0xb3)
20
+ Mercury = Color::RGB.new(0xe6, 0xe6, 0xe6)
21
+ Nickel = Color::RGB.new(0x80, 0x80, 0x80)
22
+ PolySilicon = Color::RGB.new(0x60, 0x00, 0x00)
23
+ Poly = PolySilicon
24
+ Silver = Color::RGB.new(0xcc, 0xcc, 0xcc)
25
+ Steel = Color::RGB.new(0x66, 0x66, 0x66)
26
+ Tin = Color::RGB.new(0x7f, 0x7f, 0x7f)
27
+ Tungsten = Color::RGB.new(0x33, 0x33, 0x33)
28
+ end
@@ -3,11 +3,17 @@
3
3
  #
4
4
  # Copyright 2005 Austin Ziegler
5
5
  # http://rubyforge.org/ruby-pdf/
6
+ #
7
+ # Licensed under a MIT-style licence.
8
+ #
9
+ # $Id: yiq.rb,v 1.3 2005/08/08 02:44:17 austin Exp $
6
10
  #++
7
11
 
8
12
  # A colour object representing YIQ (NTSC) colour encoding.
9
13
  class Color::YIQ
10
14
  # Creates a YIQ colour object from fractional values 0 .. 1.
15
+ #
16
+ # Color::YIQ.new(0.3, 0.2, 0.1)
11
17
  def self.from_fraction(y = 0, i = 0, q = 0)
12
18
  color = Color::YIQ.new
13
19
  color.y = y
@@ -17,6 +23,8 @@ class Color::YIQ
17
23
  end
18
24
 
19
25
  # Creates a YIQ colour object from percentages 0 .. 100.
26
+ #
27
+ # Color::YIQ.new(10, 20, 30)
20
28
  def initialize(y = 0, i = 0, q = 0)
21
29
  @y = y / 100.0
22
30
  @i = i / 100.0
@@ -27,13 +35,15 @@ class Color::YIQ
27
35
  # converted to YIQ before comparison, so the comparison between a YIQ
28
36
  # colour and a non-YIQ colour will be approximate and based on the other
29
37
  # colour's #to_yiq conversion. If there is no #to_yiq conversion, this
30
- # will raise an exception.
38
+ # will raise an exception. This will report that two YIQ values are
39
+ # equivalent if all component colours are within 1e-4 (0.0001) of each
40
+ # other.
31
41
  def ==(other)
32
42
  other = other.to_yiq
33
43
  other.kind_of?(Color::YIQ) and
34
- (@y == other.y) and
35
- (@i == other.i) and
36
- (@q == other.q)
44
+ ((@y - other.y).abs <= 1e-4) and
45
+ ((@i - other.i).abs <= 1e-4) and
46
+ ((@q - other.q).abs <= 1e-4)
37
47
  end
38
48
 
39
49
  def to_yiq
@@ -41,12 +51,28 @@ class Color::YIQ
41
51
  end
42
52
 
43
53
  def brightness
44
- @g
54
+ @y
55
+ end
56
+ def to_grayscale
57
+ Color::GrayScale.new(@y)
45
58
  end
46
- alias to_grayscale brightness
47
59
  alias to_greyscale to_grayscale
48
60
 
49
- attr_accessor :y
50
- attr_accessor :i
51
- attr_accessor :q
61
+ attr_accessor :y, :i, :q
62
+ remove_method :y=, :i=, :q=
63
+ def y=(yy) #:nodoc:
64
+ yy = 1.0 if yy > 1
65
+ yy = 0.0 if yy < 0
66
+ @y = yy
67
+ end
68
+ def i=(ii) #:nodoc:
69
+ ii = 1.0 if ii > 1
70
+ ii = 0.0 if ii < 0
71
+ @i = ii
72
+ end
73
+ def q=(qq) #:nodoc:
74
+ qq = 1.0 if qq > 1
75
+ qq = 0.0 if qq < 0
76
+ @q = qq
77
+ end
52
78
  end
data/metaconfig CHANGED
@@ -1,4 +1,13 @@
1
1
  #!/usr/bin/env ruby
2
- # $Id: metaconfig,v 1.1 2005/05/11 19:08:58 austin Exp $
3
- # vim: sts=2 sw=2 ts=4 et ai tw=77
2
+ #--
3
+ # Colour management with Ruby.
4
+ #
5
+ # Copyright 2005 Austin Ziegler
6
+ # http://rubyforge.org/ruby-pdf/
4
7
  #
8
+ # Licensed under a MIT-style licence.
9
+ #
10
+ # $Id: metaconfig,v 1.2 2005/08/05 23:07:20 austin Exp $
11
+ #++
12
+ # vim: sts=2 sw=2 ts=4 et ai tw=77
13
+
@@ -1,4 +1,13 @@
1
- # $Id: pre-setup.rb,v 1.2 2005/07/01 15:54:18 austin Exp $
1
+ #--
2
+ # Colour management with Ruby.
3
+ #
4
+ # Copyright 2005 Austin Ziegler
5
+ # http://rubyforge.org/ruby-pdf/
6
+ #
7
+ # Licensed under a MIT-style licence.
8
+ #
9
+ # $Id: pre-setup.rb,v 1.5 2005/08/08 02:53:16 austin Exp $
10
+ #++
2
11
  # vim: sts=2 sw=2 ts=4 et ai tw=77
3
12
 
4
13
  require 'rdoc/rdoc'
@@ -12,7 +21,7 @@ rescue Exception => e
12
21
  end
13
22
 
14
23
  def build_ri(files)
15
- RDoc::RDoc.new.document(["--ri-site", "--merge"] + files)
24
+ RDoc::RDoc.new.document(files)
16
25
  rescue RDoc::RDocError => e
17
26
  $stderr.puts e.message
18
27
  rescue Exception => e
@@ -43,4 +52,4 @@ ri = %w(--ri-site --merge)
43
52
  dox = %w(README Changelog lib)
44
53
  build_rdoc rdoc + dox
45
54
  build_ri ri + dox
46
- #run_tests []
55
+ run_tests Dir["tests/test_*.rb"]
@@ -0,0 +1,116 @@
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_cmyk.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 TestCMYK < Test::Unit::TestCase
19
+ def setup
20
+ @cmyk = Color::CMYK.new(10, 20, 30, 40)
21
+ end
22
+
23
+ def test_c
24
+ assert_in_delta(0.1, @cmyk.c, 1e-4)
25
+ end
26
+
27
+ def test_c_equals
28
+ assert_in_delta(0.1, @cmyk.c, 1e-4)
29
+ assert_nothing_raised { @cmyk.c = 0.2 }
30
+ assert_in_delta(0.2, @cmyk.c, 1e-4)
31
+ assert_nothing_raised { @cmyk.c = 2.0 }
32
+ assert_in_delta(1.0, @cmyk.c, 1e-4)
33
+ assert_nothing_raised { @cmyk.c = -1.0 }
34
+ assert_in_delta(0.0, @cmyk.c, 1e-4)
35
+ end
36
+
37
+ def test_html
38
+ assert(@cmyk.html =~ /^#[0-9a-fA-F]{6}/)
39
+ end
40
+
41
+ def test_k
42
+ assert_in_delta(0.4, @cmyk.k, 1e-4)
43
+ end
44
+
45
+ def test_k_equals
46
+ assert_in_delta(0.4, @cmyk.k, 1e-4)
47
+ assert_nothing_raised { @cmyk.k = 0.2 }
48
+ assert_in_delta(0.2, @cmyk.k, 1e-4)
49
+ assert_nothing_raised { @cmyk.k = 2.0 }
50
+ assert_in_delta(1.0, @cmyk.k, 1e-4)
51
+ assert_nothing_raised { @cmyk.k = -1.0 }
52
+ assert_in_delta(0.0, @cmyk.k, 1e-4)
53
+ end
54
+
55
+ def test_m
56
+ assert_in_delta(0.2, @cmyk.m, 1e-4)
57
+ end
58
+
59
+ def test_m_equals
60
+ assert_in_delta(0.2, @cmyk.m, 1e-4)
61
+ assert_nothing_raised { @cmyk.m = 0.3 }
62
+ assert_in_delta(0.3, @cmyk.m, 1e-4)
63
+ assert_nothing_raised { @cmyk.m = 2.0 }
64
+ assert_in_delta(1.0, @cmyk.m, 1e-4)
65
+ assert_nothing_raised { @cmyk.m = -1.0 }
66
+ assert_in_delta(0.0, @cmyk.m, 1e-4)
67
+ end
68
+
69
+ def test_pdf_fill
70
+ assert_equal("0.100 0.200 0.300 0.400 k", @cmyk.pdf_fill)
71
+ end
72
+
73
+ def test_pdf_stroke
74
+ assert_equal("0.100 0.200 0.300 0.400 K", @cmyk.pdf_stroke)
75
+ end
76
+
77
+ def test_to_cmyk
78
+ assert(@cmyk.to_cmyk == @cmyk)
79
+ end
80
+
81
+ def test_to_grayscale
82
+ assert_kind_of(Color::GrayScale, @cmyk.to_grayscale)
83
+ end
84
+
85
+ def test_to_greyscale
86
+ assert_kind_of(Color::GreyScale, @cmyk.to_grayscale)
87
+ end
88
+
89
+ def test_to_hsl
90
+ assert_kind_of(Color::HSL, @cmyk.to_hsl)
91
+ end
92
+
93
+ def test_to_rgb
94
+ assert_kind_of(Color::RGB, @cmyk.to_rgb(true))
95
+ assert_kind_of(Color::RGB, @cmyk.to_rgb)
96
+ end
97
+
98
+ def test_to_yiq
99
+ assert_kind_of(Color::YIQ, @cmyk.to_yiq)
100
+ end
101
+
102
+ def test_y
103
+ assert_in_delta(0.3, @cmyk.y, 1e-4)
104
+ end
105
+
106
+ def test_y_equals
107
+ assert_in_delta(0.3, @cmyk.y, 1e-4)
108
+ assert_nothing_raised { @cmyk.y = 0.2 }
109
+ assert_in_delta(0.2, @cmyk.y, 1e-4)
110
+ assert_nothing_raised { @cmyk.y = 2.0 }
111
+ assert_in_delta(1.0, @cmyk.y, 1e-4)
112
+ assert_nothing_raised { @cmyk.y = -1.0 }
113
+ assert_in_delta(0.0, @cmyk.y, 1e-4)
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,28 @@
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_css.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/css'
17
+
18
+ module TestColor
19
+ class TestCSS < Test::Unit::TestCase
20
+ def test_index
21
+ assert_equal(Color::RGB::AliceBlue, Color::CSS[:aliceblue])
22
+ assert_equal(Color::RGB::AliceBlue, Color::CSS["AliceBlue"])
23
+ assert_equal(Color::RGB::AliceBlue, Color::CSS["aliceBlue"])
24
+ assert_equal(Color::RGB::AliceBlue, Color::CSS["aliceblue"])
25
+ assert_equal(Color::RGB::AliceBlue, Color::CSS[:AliceBlue])
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,79 @@
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_gimp.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/gimp'
17
+
18
+ module TestColor
19
+ module TestPalette
20
+ class TestGimp < Test::Unit::TestCase
21
+ def setup
22
+ wsc = <<-EOS
23
+ GIMP Palette
24
+ Name: W3C Named Colors
25
+ Columns: 2
26
+ #
27
+ # ColorZilla W3C Named Colors
28
+ #
29
+ 255 255 255 White
30
+ 255 255 0 Yellow
31
+ 255 0 255 Fuchsia
32
+ 255 0 0 Red
33
+ 192 192 192 Silver
34
+ 128 128 128 Gray
35
+ 128 128 0 Olive
36
+ 128 0 128 Purple
37
+ 128 0 0 Maroon
38
+ 0 255 255 Aqua
39
+ 0 255 0 Lime
40
+ 0 128 128 Teal
41
+ 0 128 0 Green
42
+ 0 0 255 Blue
43
+ 0 0 128 Navy
44
+ 0 0 0 Black
45
+ EOS
46
+ @gimp = Color::Palette::Gimp.new(wsc)
47
+ end
48
+
49
+ def test_each
50
+ assert_equal(16, @gimp.instance_variable_get(:@colors).size)
51
+ @gimp.each { |el| assert_kind_of(Color::RGB, el) }
52
+ end
53
+
54
+ def test_each_name
55
+ assert_equal(16, @gimp.instance_variable_get(:@names).size)
56
+
57
+ @gimp.each_name { |color_name, color_set|
58
+ assert_kind_of(Array, color_set)
59
+ color_set.each { |el|
60
+ assert_kind_of(Color::RGB, el)
61
+ }
62
+ }
63
+ end
64
+
65
+ def test_index
66
+ assert_equal(Color::RGB::White, @gimp[0])
67
+ assert_equal(Color::RGB::White, @gimp["White"][0])
68
+ end
69
+
70
+ def test_valid_eh
71
+ assert(@gimp.valid?)
72
+ end
73
+
74
+ def test_name
75
+ assert_equal("W3C Named Colors", @gimp.name)
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,87 @@
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_grayscale.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 TestGrayScale < Test::Unit::TestCase
19
+ def setup
20
+ @gs = Color::GrayScale.new(33)
21
+ end
22
+
23
+ def test_brightness
24
+ assert_in_delta(0.33, @gs.brightness, 1e-4)
25
+ end
26
+
27
+ def test_darken_by
28
+ assert_in_delta(0.297, @gs.darken_by(10).g, 1e-4)
29
+ end
30
+
31
+ def test_g
32
+ assert_in_delta(0.33, @gs.g, 1e-4)
33
+ end
34
+
35
+ def test_g_equals
36
+ assert_in_delta(0.33, @gs.g, 1e-4)
37
+ assert_nothing_raised { @gs.g = 0.4 }
38
+ assert_in_delta(0.4, @gs.g, 1e-4)
39
+ assert_nothing_raised { @gs.g = 2.0 }
40
+ assert_in_delta(1.0, @gs.g, 1e-4)
41
+ assert_nothing_raised { @gs.g = -2.0 }
42
+ assert_in_delta(0.0, @gs.g, 1e-4)
43
+ end
44
+
45
+ def test_html
46
+ assert_equal("#545454", @gs.html)
47
+ end
48
+
49
+ def test_lighten_by
50
+ assert_in_delta(0.363, @gs.lighten_by(10).g, 1e-4)
51
+ end
52
+
53
+ def test_pdf_fill
54
+ assert_equal("0.330 g", @gs.pdf_fill)
55
+ end
56
+
57
+ def test_pdf_stroke
58
+ assert_equal("0.330 G", @gs.pdf_stroke)
59
+ end
60
+
61
+ def test_to_cmyk
62
+ assert_kind_of(Color::CMYK, @gs.to_cmyk)
63
+ end
64
+
65
+ def test_to_grayscale
66
+ assert_equal(@gs, @gs.to_grayscale)
67
+ end
68
+
69
+ def test_to_greyscale
70
+ assert_equal(@gs, @gs.to_greyscale)
71
+ end
72
+
73
+ def test_to_hsl
74
+ assert_kind_of(Color::HSL, @gs.to_hsl)
75
+ end
76
+
77
+ def test_to_rgb
78
+ assert_kind_of(Color::RGB, @gs.to_rgb)
79
+ end
80
+
81
+ def test_to_yiq
82
+ assert_kind_of(Color::YIQ, @gs.to_yiq)
83
+ end
84
+ end
85
+ end
86
+
87
+ # Number of errors detected: 2