pdf-labels 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. data/History.txt +8 -0
  2. data/LICENCE +38 -0
  3. data/Manifest.txt +141 -0
  4. data/README.txt +72 -0
  5. data/Rakefile +30 -0
  6. data/lib/alias.rb +8 -0
  7. data/lib/glabel_template.rb +36 -0
  8. data/lib/label.rb +52 -0
  9. data/lib/layout.rb +13 -0
  10. data/lib/length_node.rb +47 -0
  11. data/lib/markup.rb +25 -0
  12. data/lib/pdf_label_page.rb +171 -0
  13. data/lib/pdf_labels.rb +6 -0
  14. data/lib/template.rb +37 -0
  15. data/templates/avery-iso-templates.xml +222 -0
  16. data/templates/avery-other-templates.xml +21 -0
  17. data/templates/avery-us-templates.xml +599 -0
  18. data/templates/glabels-2.0.dtd +329 -0
  19. data/templates/misc-iso-templates.xml +434 -0
  20. data/templates/misc-other-templates.xml +21 -0
  21. data/templates/misc-us-templates.xml +183 -0
  22. data/templates/paper-sizes.xml +37 -0
  23. data/templates/zweckform-iso-templates.xml +197 -0
  24. data/test/test_pdf_label_page.rb +91 -0
  25. data/vendor/color.rb +87 -0
  26. data/vendor/color/cmyk.rb +182 -0
  27. data/vendor/color/css.rb +27 -0
  28. data/vendor/color/grayscale.rb +135 -0
  29. data/vendor/color/hsl.rb +130 -0
  30. data/vendor/color/palette.rb +15 -0
  31. data/vendor/color/palette/gimp.rb +107 -0
  32. data/vendor/color/palette/monocontrast.rb +180 -0
  33. data/vendor/color/rgb-colors.rb +189 -0
  34. data/vendor/color/rgb.rb +311 -0
  35. data/vendor/color/rgb/metallic.rb +28 -0
  36. data/vendor/color/yiq.rb +78 -0
  37. data/vendor/pdf/charts.rb +13 -0
  38. data/vendor/pdf/charts/stddev.rb +433 -0
  39. data/vendor/pdf/grid.rb +135 -0
  40. data/vendor/pdf/math.rb +108 -0
  41. data/vendor/pdf/pagenumbers.rb +288 -0
  42. data/vendor/pdf/quickref.rb +331 -0
  43. data/vendor/pdf/simpletable.rb +947 -0
  44. data/vendor/pdf/techbook.rb +901 -0
  45. data/vendor/pdf/writer.rb +2801 -0
  46. data/vendor/pdf/writer/arc4.rb +63 -0
  47. data/vendor/pdf/writer/fontmetrics.rb +202 -0
  48. data/vendor/pdf/writer/fonts/Courier-Bold.afm +342 -0
  49. data/vendor/pdf/writer/fonts/Courier-BoldOblique.afm +342 -0
  50. data/vendor/pdf/writer/fonts/Courier-Oblique.afm +342 -0
  51. data/vendor/pdf/writer/fonts/Courier.afm +342 -0
  52. data/vendor/pdf/writer/fonts/Helvetica-Bold.afm +2827 -0
  53. data/vendor/pdf/writer/fonts/Helvetica-BoldOblique.afm +2827 -0
  54. data/vendor/pdf/writer/fonts/Helvetica-Oblique.afm +3051 -0
  55. data/vendor/pdf/writer/fonts/Helvetica.afm +3051 -0
  56. data/vendor/pdf/writer/fonts/Symbol.afm +213 -0
  57. data/vendor/pdf/writer/fonts/Times-Bold.afm +2588 -0
  58. data/vendor/pdf/writer/fonts/Times-BoldItalic.afm +2384 -0
  59. data/vendor/pdf/writer/fonts/Times-Italic.afm +2667 -0
  60. data/vendor/pdf/writer/fonts/Times-Roman.afm +2419 -0
  61. data/vendor/pdf/writer/fonts/ZapfDingbats.afm +225 -0
  62. data/vendor/pdf/writer/graphics.rb +813 -0
  63. data/vendor/pdf/writer/graphics/imageinfo.rb +365 -0
  64. data/vendor/pdf/writer/lang.rb +44 -0
  65. data/vendor/pdf/writer/lang/en.rb +104 -0
  66. data/vendor/pdf/writer/object.rb +23 -0
  67. data/vendor/pdf/writer/object/action.rb +40 -0
  68. data/vendor/pdf/writer/object/annotation.rb +42 -0
  69. data/vendor/pdf/writer/object/catalog.rb +39 -0
  70. data/vendor/pdf/writer/object/contents.rb +69 -0
  71. data/vendor/pdf/writer/object/destination.rb +40 -0
  72. data/vendor/pdf/writer/object/encryption.rb +53 -0
  73. data/vendor/pdf/writer/object/font.rb +68 -0
  74. data/vendor/pdf/writer/object/fontdescriptor.rb +34 -0
  75. data/vendor/pdf/writer/object/fontencoding.rb +40 -0
  76. data/vendor/pdf/writer/object/image.rb +308 -0
  77. data/vendor/pdf/writer/object/info.rb +79 -0
  78. data/vendor/pdf/writer/object/outline.rb +30 -0
  79. data/vendor/pdf/writer/object/outlines.rb +30 -0
  80. data/vendor/pdf/writer/object/page.rb +195 -0
  81. data/vendor/pdf/writer/object/pages.rb +115 -0
  82. data/vendor/pdf/writer/object/procset.rb +46 -0
  83. data/vendor/pdf/writer/object/viewerpreferences.rb +74 -0
  84. data/vendor/pdf/writer/ohash.rb +58 -0
  85. data/vendor/pdf/writer/oreader.rb +25 -0
  86. data/vendor/pdf/writer/state.rb +48 -0
  87. data/vendor/pdf/writer/strokestyle.rb +140 -0
  88. data/vendor/transaction/simple.rb +693 -0
  89. data/vendor/transaction/simple/group.rb +133 -0
  90. data/vendor/transaction/simple/threadsafe.rb +52 -0
  91. data/vendor/transaction/simple/threadsafe/group.rb +23 -0
  92. data/vendor/xml-mapping/ChangeLog +128 -0
  93. data/vendor/xml-mapping/LICENSE +56 -0
  94. data/vendor/xml-mapping/README +386 -0
  95. data/vendor/xml-mapping/README_XPATH +175 -0
  96. data/vendor/xml-mapping/Rakefile +214 -0
  97. data/vendor/xml-mapping/TODO.txt +32 -0
  98. data/vendor/xml-mapping/doc/xpath_impl_notes.txt +119 -0
  99. data/vendor/xml-mapping/examples/company.rb +34 -0
  100. data/vendor/xml-mapping/examples/company.xml +26 -0
  101. data/vendor/xml-mapping/examples/company_usage.intin.rb +19 -0
  102. data/vendor/xml-mapping/examples/company_usage.intout +39 -0
  103. data/vendor/xml-mapping/examples/order.rb +61 -0
  104. data/vendor/xml-mapping/examples/order.xml +54 -0
  105. data/vendor/xml-mapping/examples/order_signature_enhanced.rb +7 -0
  106. data/vendor/xml-mapping/examples/order_signature_enhanced.xml +9 -0
  107. data/vendor/xml-mapping/examples/order_signature_enhanced_usage.intin.rb +12 -0
  108. data/vendor/xml-mapping/examples/order_signature_enhanced_usage.intout +16 -0
  109. data/vendor/xml-mapping/examples/order_usage.intin.rb +73 -0
  110. data/vendor/xml-mapping/examples/order_usage.intout +147 -0
  111. data/vendor/xml-mapping/examples/time_augm.intin.rb +19 -0
  112. data/vendor/xml-mapping/examples/time_augm.intout +23 -0
  113. data/vendor/xml-mapping/examples/time_node.rb +27 -0
  114. data/vendor/xml-mapping/examples/xpath_create_new.intin.rb +85 -0
  115. data/vendor/xml-mapping/examples/xpath_create_new.intout +181 -0
  116. data/vendor/xml-mapping/examples/xpath_docvsroot.intin.rb +30 -0
  117. data/vendor/xml-mapping/examples/xpath_docvsroot.intout +34 -0
  118. data/vendor/xml-mapping/examples/xpath_ensure_created.intin.rb +62 -0
  119. data/vendor/xml-mapping/examples/xpath_ensure_created.intout +114 -0
  120. data/vendor/xml-mapping/examples/xpath_pathological.intin.rb +42 -0
  121. data/vendor/xml-mapping/examples/xpath_pathological.intout +56 -0
  122. data/vendor/xml-mapping/examples/xpath_usage.intin.rb +51 -0
  123. data/vendor/xml-mapping/examples/xpath_usage.intout +57 -0
  124. data/vendor/xml-mapping/install.rb +40 -0
  125. data/vendor/xml-mapping/lib/xml/mapping.rb +14 -0
  126. data/vendor/xml-mapping/lib/xml/mapping/base.rb +571 -0
  127. data/vendor/xml-mapping/lib/xml/mapping/standard_nodes.rb +343 -0
  128. data/vendor/xml-mapping/lib/xml/mapping/version.rb +8 -0
  129. data/vendor/xml-mapping/lib/xml/xxpath.rb +354 -0
  130. data/vendor/xml-mapping/test/all_tests.rb +6 -0
  131. data/vendor/xml-mapping/test/company.rb +56 -0
  132. data/vendor/xml-mapping/test/documents_folders.rb +33 -0
  133. data/vendor/xml-mapping/test/fixtures/bookmarks1.xml +24 -0
  134. data/vendor/xml-mapping/test/fixtures/company1.xml +85 -0
  135. data/vendor/xml-mapping/test/fixtures/documents_folders.xml +71 -0
  136. data/vendor/xml-mapping/test/fixtures/documents_folders2.xml +30 -0
  137. data/vendor/xml-mapping/test/multiple_mappings.rb +80 -0
  138. data/vendor/xml-mapping/test/tests_init.rb +2 -0
  139. data/vendor/xml-mapping/test/xml_mapping_adv_test.rb +84 -0
  140. data/vendor/xml-mapping/test/xml_mapping_test.rb +201 -0
  141. data/vendor/xml-mapping/test/xpath_test.rb +273 -0
  142. metadata +191 -0
@@ -0,0 +1,87 @@
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: color.rb,v 1.8 2005/08/08 02:44:17 austin Exp $
10
+ #++
11
+
12
+ # = Colour Management with Ruby
13
+ #
14
+ # == Copyright
15
+ # Copyright 2005 by Austin Ziegler
16
+ #
17
+ # Color::RGB and Color::CMYK were originally developed for the Ruby PDF
18
+ # project and PDF::Writer and represent wholly unique code.
19
+ #
20
+ # Color::Palette was developed based on techniques described by Andy
21
+ # "Malarkey"[http://www.stuffandnonsense.co.uk/archives/creating_colour_palettes.html]
22
+ # Clarke, implemented in JavaScript by Steve G. Chipman at
23
+ # SlayerOffice[http://slayeroffice.com/tools/color_palette/] and by Patrick
24
+ # Fitzgerald of BarelyFitz[http://www.barelyfitz.com/projects/csscolor/] in
25
+ # PHP.
26
+ #
27
+ # == LICENCE
28
+ # Permission is hereby granted, free of charge, to any person obtaining a
29
+ # copy of this software and associated documentation files (the "Software"),
30
+ # to deal in the Software without restriction, including without limitation
31
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
32
+ # and/or sell copies of the Software, and to permit persons to whom the
33
+ # Software is furnished to do so, subject to the following conditions:
34
+ #
35
+ # * The names of its contributors may not be used to endorse or promote
36
+ # products derived from this software without specific prior written
37
+ # permission.
38
+ #
39
+ # The above copyright notice and this permission notice shall be included in
40
+ # all copies or substantial portions of the Software.
41
+ #
42
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
45
+ # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
47
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
48
+ # DEALINGS IN THE SOFTWARE.
49
+ module Color
50
+ COLOR_TOOLS_VERSION = '1.3.0'
51
+
52
+ class RGB; end
53
+ class CMYK; end
54
+ class GrayScale; end
55
+ class YIQ; end
56
+ end
57
+
58
+ require 'color/rgb'
59
+ require 'color/cmyk'
60
+ require 'color/grayscale'
61
+ require 'color/hsl'
62
+ require 'color/yiq'
63
+ require 'color/rgb/metallic'
64
+
65
+ # We load EVERYTHING if we're being run under ZenTest.
66
+ if defined? $ZENTEST and $ZENTEST
67
+ require 'color/css'
68
+ require 'color/palette/gimp'
69
+ require 'color/palette/monocontrast'
70
+ end
71
+
72
+ module Color
73
+ def self.const_missing(name) #:nodoc:
74
+ if Color::RGB.const_defined?(name)
75
+ warn "These colour constants have been deprecated. Use Color::RGB::#{name} instead."
76
+ Color::RGB::constants.each do |const|
77
+ next if const == "PDF_FORMAT_STR"
78
+ next if const == "Metallic"
79
+ const_set(const, Color::RGB.const_get(const))
80
+ end
81
+ class << Color; remove_method :const_missing; end
82
+ Color.const_get(name)
83
+ else
84
+ super
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,182 @@
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: cmyk.rb,v 1.5 2005/08/08 02:44:17 austin Exp $
10
+ #++
11
+
12
+ # An CMYK colour object. CMYK (cyan, magenta, yellow, and black) colours
13
+ # are based on additive percentages of ink. A CMYK colour of (0.3, 0, 0.8,
14
+ # 0.3) would be mixed from 30% cyan, 0% magenta, 80% yellow, and 30%
15
+ # black.
16
+ class Color::CMYK
17
+ # The format of a DeviceCMYK colour for PDF. In color-tools 2.0 this
18
+ # will be removed from this package and added back as a modification by
19
+ # the PDF::Writer package.
20
+ PDF_FORMAT_STR = "%.3f %.3f %.3f %.3f %s"
21
+
22
+ # Compares the other colour to this one. The other colour will be
23
+ # converted to CMYK before comparison, so the comparison between a CMYK
24
+ # colour and a non-CMYK colour will be approximate and based on the
25
+ # other colour's #to_cmyk conversion. If there is no #to_cmyk
26
+ # conversion, this will raise an exception. This will report that two
27
+ # CMYK colours are equivalent if all component values are within 1e-4
28
+ # (0.0001) of each other.
29
+ def ==(other)
30
+ other = other.to_cmyk
31
+ other.kind_of?(Color::CMYK) and
32
+ ((@c - other.c).abs <= 1e-4) and
33
+ ((@m - other.m).abs <= 1e-4) and
34
+ ((@y - other.y).abs <= 1e-4) and
35
+ ((@k - other.k).abs <= 1e-4)
36
+ end
37
+
38
+ # Creates a CMYK colour object from fractional values 0..1.
39
+ #
40
+ # Color::CMYK.from_fraction(0.3, 0, 0.8, 0.3)
41
+ def self.from_fraction(c = 0, m = 0, y = 0, k = 0)
42
+ colour = Color::CMYK.new
43
+ colour.c = c
44
+ colour.m = m
45
+ colour.y = y
46
+ colour.k = k
47
+ colour
48
+ end
49
+
50
+ # Creates a CMYK colour object from percentages. Internally, the colour
51
+ # is managed as fractional values 0..1.
52
+ #
53
+ # Color::CMYK.from_fraction(30, 0, 80, 30)
54
+ def initialize(c = 0, m = 0, y = 0, k = 0)
55
+ @c = c / 100.0
56
+ @m = m / 100.0
57
+ @y = y / 100.0
58
+ @k = k / 100.0
59
+ end
60
+
61
+ # Present the colour as a DeviceCMYK fill colour string for PDF. This
62
+ # will be removed from the default package in color-tools 2.0.
63
+ def pdf_fill
64
+ PDF_FORMAT_STR % [ @c, @m, @y, @k, "k" ]
65
+ end
66
+
67
+ # Present the colour as a DeviceCMYK stroke colour string for PDF. This
68
+ # will be removed from the default package in color-tools 2.0.
69
+ def pdf_stroke
70
+ PDF_FORMAT_STR % [ @c, @m, @y, @k, "K" ]
71
+ end
72
+
73
+ # Present the colour as an RGB HTML/CSS colour string. Note that this
74
+ # will perform a #to_rgb operation using the default conversion formula.
75
+ def html
76
+ to_rgb.html
77
+ end
78
+
79
+ # Converts the CMYK colour to RGB. Most colour experts strongly suggest
80
+ # that this is not a good idea (some even suggesting that it's a very
81
+ # bad idea). CMYK represents additive percentages of inks on white
82
+ # paper, whereas RGB represents mixed colour intensities on a black
83
+ # screen.
84
+ #
85
+ # However, the colour conversion can be done, and there are two
86
+ # different methods for the conversion that provide slightly different
87
+ # results. Adobe PDF conversions are done with the first form.
88
+ #
89
+ # # Adobe PDF Display Formula
90
+ # r = 1.0 - min(1.0, c + k)
91
+ # g = 1.0 - min(1.0, m + k)
92
+ # b = 1.0 - min(1.0, y + k)
93
+ #
94
+ # # Other
95
+ # r = 1.0 - (c * (1.0 - k) + k)
96
+ # g = 1.0 - (m * (1.0 - k) + k)
97
+ # b = 1.0 - (y * (1.0 - k) + k)
98
+ #
99
+ # If we have a CMYK colour of [33% 66% 83% 25%], the first method will
100
+ # give an approximate RGB colour of (107, 23, 0) or #6b1700. The second
101
+ # method will give an approximate RGB colour of (128, 65, 33) or
102
+ # #804121. Which is correct? Although the colours may seem to be
103
+ # drastically different in the RGB colour space, they are very similar
104
+ # colours, differing mostly in intensity. The first is a darker,
105
+ # slightly redder brown; the second is a lighter brown.
106
+ #
107
+ # Because of this subtlety, both methods are now offered for conversion
108
+ # in color-tools 1.2 or later. The Adobe method is not used by default;
109
+ # to enable it, pass +true+ to #to_rgb.
110
+ #
111
+ # Future versions of color-tools may offer other conversion mechanisms
112
+ # that offer greater colour fidelity.
113
+ def to_rgb(use_adobe_method = false)
114
+ if use_adobe_method
115
+ r = 1.0 - [1.0, @c + @k].min
116
+ g = 1.0 - [1.0, @m + @k].min
117
+ b = 1.0 - [1.0, @y + @k].min
118
+ else
119
+ r = 1.0 - (@c.to_f * (1.0 - @k.to_f) + @k.to_f)
120
+ g = 1.0 - (@m.to_f * (1.0 - @k.to_f) + @k.to_f)
121
+ b = 1.0 - (@y.to_f * (1.0 - @k.to_f) + @k.to_f)
122
+ end
123
+ Color::RGB.from_fraction(r, g, b)
124
+ end
125
+
126
+ # Converts the CMYK colour to a single greyscale value. There are
127
+ # undoubtedly multiple methods for this conversion, but only a minor
128
+ # variant of the Adobe conversion method will be used:
129
+ #
130
+ # g = 1.0 - min(1.0, 0.299 * c + 0.587 * m + 0.114 * y + k)
131
+ #
132
+ # This treats the CMY values similarly to YIQ (NTSC) values and then
133
+ # adds the level of black. This is a variant of the Adobe version
134
+ # because it uses the more precise YIQ (NTSC) conversion values for Y
135
+ # (intensity) rather than the approximates provided by Adobe (0.3, 0.59,
136
+ # and 0.11).
137
+ def to_grayscale
138
+ c = 0.299 * @c.to_f
139
+ m = 0.587 * @m.to_f
140
+ y = 0.114 * @y.to_f
141
+ g = 1.0 - [1.0, c + m + y + @k].min
142
+ Color::GrayScale.from_fraction(g)
143
+ end
144
+ alias to_greyscale to_grayscale
145
+
146
+ def to_cmyk
147
+ self
148
+ end
149
+
150
+ # Converts to RGB then YIQ.
151
+ def to_yiq
152
+ to_rgb.to_yiq
153
+ end
154
+
155
+ # Converts to RGB then HSL.
156
+ def to_hsl
157
+ to_rgb.to_hsl
158
+ end
159
+
160
+ attr_accessor :c, :m, :y, :k
161
+ remove_method :c=, :m=, :y=, :k= ;
162
+ def c=(cc) #:nodoc:
163
+ cc = 1.0 if cc > 1
164
+ cc = 0.0 if cc < 0
165
+ @c = cc
166
+ end
167
+ def m=(mm) #:nodoc:
168
+ mm = 1.0 if mm > 1
169
+ mm = 0.0 if mm < 0
170
+ @m = mm
171
+ end
172
+ def y=(yy) #:nodoc:
173
+ yy = 1.0 if yy > 1
174
+ yy = 0.0 if yy < 0
175
+ @y = yy
176
+ end
177
+ def k=(kk) #:nodoc:
178
+ kk = 1.0 if kk > 1
179
+ kk = 0.0 if kk < 0
180
+ @k = kk
181
+ end
182
+ end
@@ -0,0 +1,27 @@
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: css.rb,v 1.1 2005/08/05 23:07:20 austin Exp $
10
+ #++
11
+
12
+ require 'color'
13
+
14
+ # This namespace contains some CSS colour names.
15
+ module Color::CSS
16
+ # Returns the RGB colour for name or +nil+ if the name is not valid.
17
+ def self.[](name)
18
+ @colors[name.to_s.downcase.to_sym]
19
+ end
20
+
21
+ @colors = {}
22
+ Color::RGB.constants.each do |const|
23
+ next if const == "PDF_FORMAT_STR"
24
+ next if const == "Metallic"
25
+ @colors[const.downcase.to_sym] ||= Color::RGB.const_get(const)
26
+ end
27
+ end
@@ -0,0 +1,135 @@
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: grayscale.rb,v 1.3 2005/08/08 02:44:17 austin Exp $
10
+ #++
11
+
12
+ # A colour object representing shades of grey. Used primarily in PDF
13
+ # document creation.
14
+ class Color::GrayScale
15
+ # The format of a DeviceGrey colour for PDF. In color-tools 2.0 this
16
+ # will be removed from this package and added back as a modification by
17
+ # the PDF::Writer package.
18
+ PDF_FORMAT_STR = "%.3f %s"
19
+
20
+ # Creates a greyscale colour object from fractional values 0..1.
21
+ #
22
+ # Color::GreyScale.from_fraction(0.5)
23
+ def self.from_fraction(g = 0)
24
+ color = Color::GrayScale.new
25
+ color.g = g
26
+ color
27
+ end
28
+
29
+ # Creates a greyscale colour object from percentages 0..100.
30
+ #
31
+ # Color::GrayScale.new(50)
32
+ def initialize(g = 0)
33
+ @g = g / 100.0
34
+ end
35
+
36
+ # Compares the other colour to this one. The other colour will be
37
+ # converted to GreyScale before comparison, so the comparison between a
38
+ # GreyScale colour and a non-GreyScale colour will be approximate and
39
+ # based on the other colour's #to_greyscale conversion. If there is no
40
+ # #to_greyscale conversion, this will raise an exception. This will
41
+ # report that two GreyScale values are equivalent if they are within
42
+ # 1e-4 (0.0001) of each other.
43
+ def ==(other)
44
+ other = other.to_grayscale
45
+ other.kind_of?(Color::GrayScale) and
46
+ ((@g - other.g).abs <= 1e-4)
47
+ end
48
+
49
+ # Present the colour as a DeviceGrey fill colour string for PDF. This
50
+ # will be removed from the default package in color-tools 2.0.
51
+ def pdf_fill
52
+ PDF_FORMAT_STR % [ @g, "g" ]
53
+ end
54
+
55
+ # Present the colour as a DeviceGrey stroke colour string for PDF. This
56
+ # will be removed from the default package in color-tools 2.0.
57
+ def pdf_stroke
58
+ PDF_FORMAT_STR % [ @g, "G" ]
59
+ end
60
+
61
+ def to_255
62
+ [(@g * 255).round, 255].min
63
+ end
64
+ private :to_255
65
+
66
+ # Present the colour as an HTML/CSS colour string.
67
+ def html
68
+ gs = "%02x" % to_255
69
+ "##{gs * 3}"
70
+ end
71
+
72
+ # Convert the greyscale colour to CMYK.
73
+ def to_cmyk
74
+ k = 1.0 - @g.to_f
75
+ Color::CMYK.from_fraction(0, 0, 0, k)
76
+ end
77
+
78
+ # Convert the greyscale colour to RGB.
79
+ def to_rgb(ignored = true)
80
+ g = to_255
81
+ Color::RGB.new(g, g, g)
82
+ end
83
+
84
+ def to_grayscale
85
+ self
86
+ end
87
+ alias to_greyscale to_grayscale
88
+
89
+ # Lightens the greyscale colour by the stated percent.
90
+ def lighten_by(percent)
91
+ g = [@g + (@g * (percent / 100.0)), 1.0].min
92
+ Color::GrayScale.from_fraction(g)
93
+ end
94
+
95
+ # Darken the RGB hue by the stated percent.
96
+ def darken_by(percent)
97
+ g = [@g - (@g * (percent / 100.0)), 0.0].max
98
+ Color::GrayScale.from_fraction(g)
99
+ end
100
+
101
+ # Returns the YIQ (NTSC) colour encoding of the greyscale value. This
102
+ # is an approximation, as the values for I and Q are calculated by
103
+ # treating the greyscale value as an RGB value. The Y (intensity or
104
+ # brightness) value is the same as the greyscale value.
105
+ def to_yiq
106
+ y = @g
107
+ i = (@g * 0.596) + (@g * -0.275) + (@g * -0.321)
108
+ q = (@g * 0.212) + (@g * -0.523) + (@g * 0.311)
109
+ Color::YIQ.from_fraction(y, i, q)
110
+ end
111
+
112
+ # Returns the HSL colour encoding of the greyscale value.
113
+ def to_hsl
114
+ Color::HSL.from_fraction(0, 0, @g)
115
+ end
116
+
117
+ # Returns the brightness value for this greyscale value; this is the
118
+ # greyscale value.
119
+ def brightness
120
+ @g
121
+ end
122
+
123
+ attr_accessor :g
124
+ remove_method :g= ;
125
+ def g=(gg) #:nodoc:
126
+ gg = 1.0 if gg > 1
127
+ gg = 0.0 if gg < 0
128
+ @g = gg
129
+ end
130
+ end
131
+
132
+ module Color
133
+ # A synonym for Color::GrayScale.
134
+ GreyScale = GrayScale
135
+ end
@@ -0,0 +1,130 @@
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: hsl.rb,v 1.2 2005/08/08 02:44:17 austin Exp $
10
+ #++
11
+
12
+ # An HSL colour object. Internally, the hue (#h), saturation (#s), and
13
+ # luminosity (#l) values are dealt with as fractional values in the range
14
+ # 0..1.
15
+ class Color::HSL
16
+ class << self
17
+ # Creates an HSL colour object from fractional values 0..1.
18
+ def from_fraction(h = 0.0, s = 0.0, l = 0.0)
19
+ colour = Color::HSL.new
20
+ colour.h = h
21
+ colour.s = s
22
+ colour.l = l
23
+ colour
24
+ end
25
+ end
26
+
27
+ # Compares the other colour to this one. The other colour will be
28
+ # converted to HSL before comparison, so the comparison between a HSL
29
+ # colour and a non-HSL colour will be approximate and based on the other
30
+ # colour's #to_hsl conversion. If there is no #to_hsl conversion, this
31
+ # will raise an exception. This will report that two HSL values are
32
+ # equivalent if all component values are within 1e-4 (0.0001) of each
33
+ # other.
34
+ def ==(other)
35
+ other = other.to_hsl
36
+ other.kind_of?(Color::HSL) and
37
+ ((@h - other.h).abs <= 1e-4) and
38
+ ((@s - other.s).abs <= 1e-4) and
39
+ ((@l - other.l).abs <= 1e-4)
40
+ end
41
+
42
+ # Creates an HSL colour object from the standard values of degrees and
43
+ # percentages (e.g., 145�, 30%, 50%).
44
+ def initialize(h = 0, s = 0, l = 0)
45
+ @h = h / 360.0
46
+ @s = s / 100.0
47
+ @l = l / 100.0
48
+ end
49
+
50
+ # Present the colour as an HTML/CSS colour string.
51
+ def html
52
+ to_rgb.html
53
+ end
54
+
55
+ # Converting to HSL as adapted from Foley and Van-Dam from
56
+ # http://www.bobpowell.net/RGBHSB.htm.
57
+ def to_rgb(ignored = nil)
58
+ # If luminosity is zero, the colour is always black.
59
+ return Color::RGB.new if @l == 0
60
+ # If luminosity is one, the colour is always white.
61
+ return Color::RGB.new(0xff, 0xff, 0xff) if @l == 1
62
+ # If saturation is zero, the colour is always a greyscale colour.
63
+ return Color::RGB.new(@l, @l, @l) if @s <= 1e-5
64
+
65
+ if (@l - 0.5) < 1e-5
66
+ tmp2 = @l * (1.0 + @s.to_f)
67
+ else
68
+ tmp2 = @l + @s - (@l * @s.to_f)
69
+ end
70
+ tmp1 = 2.0 * @l - tmp2
71
+
72
+ t3 = [ @h + 1.0 / 3.0, @h, @h - 1.0 / 3.0 ]
73
+ t3 = t3.map { |tmp3|
74
+ tmp3 += 1.0 if tmp3 < 1e-5
75
+ tmp3 -= 1.0 if (tmp3 - 1.0) > 1e-5
76
+ tmp3
77
+ }
78
+
79
+ rgb = t3.map do |tmp3|
80
+ if ((6.0 * tmp3) - 1.0) < 1e-5
81
+ tmp1 + ((tmp2 - tmp1) * tmp3 * 6.0)
82
+ elsif ((2.0 * tmp3) - 1.0) < 1e-5
83
+ tmp2
84
+ elsif ((3.0 * tmp3) - 2.0) < 1e-5
85
+ tmp1 + (tmp2 - tmp1) * ((2 / 3.0) - tmp3) * 6.0
86
+ else
87
+ tmp1
88
+ end
89
+ end
90
+
91
+ Color::RGB.from_fraction(*rgb)
92
+ end
93
+
94
+ # Converts to RGB then YIQ.
95
+ def to_yiq
96
+ to_rgb.to_yiq
97
+ end
98
+
99
+ # Converts to RGB then CMYK.
100
+ def to_cmyk
101
+ to_rgb.to_cmyk
102
+ end
103
+
104
+ # Returns the luminosity (#l) of the colour.
105
+ def brightness
106
+ @l
107
+ end
108
+ def to_greyscale
109
+ Color::GrayScale.from_fraction(@l)
110
+ end
111
+ alias to_grayscale to_greyscale
112
+
113
+ attr_accessor :h, :s, :l
114
+ remove_method :h=, :s=, :l= ;
115
+ def h=(hh) #:nodoc:
116
+ hh = 1.0 if hh > 1
117
+ hh = 0.0 if hh < 0
118
+ @h = hh
119
+ end
120
+ def s=(ss) #:nodoc:
121
+ ss = 1.0 if ss > 1
122
+ ss = 0.0 if ss < 0
123
+ @s = ss
124
+ end
125
+ def l=(ll) #:nodoc:
126
+ ll = 1.0 if ll > 1
127
+ ll = 0.0 if ll < 0
128
+ @l = ll
129
+ end
130
+ end