spectrum 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,355 @@
1
+ #--
2
+ # Color
3
+ # Colour management with Ruby
4
+ # http://rubyforge.org/projects/color
5
+ # Version 1.4.1
6
+ #
7
+ # Licensed under a MIT-style licence. See Licence.txt in the main
8
+ # distribution for full licensing information.
9
+ #
10
+ # Copyright (c) 2005 - 2010 Austin Ziegler and Matt Lyon
11
+ #++
12
+
13
+ class Spectrum::RGB
14
+ AliceBlue = Spectrum::RGB.new(0xf0, 0xf8, 0xff)
15
+ AntiqueWhite = Spectrum::RGB.new(0xfa, 0xeb, 0xd7)
16
+ Aqua = Spectrum::RGB.new(0x00, 0xff, 0xff)
17
+ Aquamarine = Spectrum::RGB.new(0x7f, 0xff, 0xd4)
18
+ Azure = Spectrum::RGB.new(0xf0, 0xff, 0xff)
19
+ Beige = Spectrum::RGB.new(0xf5, 0xf5, 0xdc)
20
+ Bisque = Spectrum::RGB.new(0xff, 0xe4, 0xc4)
21
+ Black = Spectrum::RGB.new(0, 0, 0)
22
+ BlanchedAlmond = Spectrum::RGB.new(0xff, 0xeb, 0xcd)
23
+ Blue = Spectrum::RGB.new(0x00, 0x00, 0xff)
24
+ BlueViolet = Spectrum::RGB.new(0x8a, 0x2b, 0xe2)
25
+ Brown = Spectrum::RGB.new(0xa5, 0x2a, 0x2a)
26
+ BurlyWood = Spectrum::RGB.new(0xde, 0xb8, 0x87)
27
+ Burlywood = BurlyWood
28
+ CadetBlue = Spectrum::RGB.new(0x5f, 0x9e, 0xa0)
29
+ Carnation = Spectrum::RGB.new(0xff, 0x5e, 0xd0)
30
+ Cayenne = Spectrum::RGB.new(0x8d, 0x00, 0x00)
31
+ Chartreuse = Spectrum::RGB.new(0x7f, 0xff, 0x00)
32
+ Chocolate = Spectrum::RGB.new(0xd2, 0x69, 0x1e)
33
+ Coral = Spectrum::RGB.new(0xff, 0x7f, 0x50)
34
+ CornflowerBlue = Spectrum::RGB.new(0x64, 0x95, 0xed)
35
+ Cornsilk = Spectrum::RGB.new(0xff, 0xf8, 0xdc)
36
+ Crimson = Spectrum::RGB.new(0xdc, 0x14, 0x3c)
37
+ Cyan = Spectrum::RGB.new(0x00, 0xff, 0xff)
38
+ DarkBlue = Spectrum::RGB.new(0x00, 0x00, 0x8b)
39
+ DarkCyan = Spectrum::RGB.new(0x00, 0x8b, 0x8b)
40
+ DarkGoldenRod = Spectrum::RGB.new(0xb8, 0x86, 0x0b)
41
+ DarkGoldenrod = DarkGoldenRod
42
+ DarkGray = Spectrum::RGB.new(0xa9, 0xa9, 0xa9)
43
+ DarkGreen = Spectrum::RGB.new(0x00, 0x64, 0x00)
44
+ DarkGrey = DarkGray
45
+ DarkKhaki = Spectrum::RGB.new(0xbd, 0xb7, 0x6b)
46
+ DarkMagenta = Spectrum::RGB.new(0x8b, 0x00, 0x8b)
47
+ DarkOliveGreen = Spectrum::RGB.new(0x55, 0x6b, 0x2f)
48
+ DarkOrange = Spectrum::RGB.new(0xff, 0x8c, 0x00)
49
+ DarkOrchid = Spectrum::RGB.new(0x99, 0x32, 0xcc)
50
+ DarkRed = Spectrum::RGB.new(0x8b, 0x00, 0x00)
51
+ DarkSalmon = Spectrum::RGB.new(0xe9, 0x96, 0x7a)
52
+ DarkSeaGreen = Spectrum::RGB.new(0x8f, 0xbc, 0x8f)
53
+ DarkSlateBlue = Spectrum::RGB.new(0x48, 0x3d, 0x8b)
54
+ DarkSlateGray = Spectrum::RGB.new(0x2f, 0x4f, 0x4f)
55
+ DarkSlateGrey = DarkSlateGray
56
+ DarkTurquoise = Spectrum::RGB.new(0x00, 0xce, 0xd1)
57
+ DarkViolet = Spectrum::RGB.new(0x94, 0x00, 0xd3)
58
+ DarkoliveGreen = DarkOliveGreen
59
+ Darkorange = Spectrum::RGB.new(0xff, 0x8c, 0x00)
60
+ Darksalmon = DarkSalmon
61
+ DeepPink = Spectrum::RGB.new(0xff, 0x14, 0x93)
62
+ DeepSkyBlue = Spectrum::RGB.new(0x00, 0xbf, 0xbf)
63
+ DimGray = Spectrum::RGB.new(0x69, 0x69, 0x69)
64
+ DimGrey = DimGray
65
+ DodgerBlue = Spectrum::RGB.new(0x1e, 0x90, 0xff)
66
+ Feldspar = Spectrum::RGB.new(0xd1, 0x92, 0x75)
67
+ FireBrick = Spectrum::RGB.new(0xb2, 0x22, 0x22)
68
+ Firebrick = FireBrick
69
+ FloralWhite = Spectrum::RGB.new(0xff, 0xfa, 0xf0)
70
+ ForestGreen = Spectrum::RGB.new(0x22, 0x8b, 0x22)
71
+ Fuchsia = Spectrum::RGB.new(0xff, 0x00, 0xff)
72
+ Gainsboro = Spectrum::RGB.new(0xdc, 0xdc, 0xdc)
73
+ GhostWhite = Spectrum::RGB.new(0xf8, 0xf8, 0xff)
74
+ Gold = Spectrum::RGB.new(0xff, 0xd7, 0x00)
75
+ GoldenRod = Spectrum::RGB.new(0xda, 0xa5, 0x20)
76
+ Goldenrod = GoldenRod
77
+ Gray = Spectrum::RGB.new(0x80, 0x80, 0x80)
78
+ Gray10 = Spectrum::RGB.from_percentage(10, 10, 10)
79
+ Gray20 = Spectrum::RGB.from_percentage(20, 20, 20)
80
+ Gray30 = Spectrum::RGB.from_percentage(30, 30, 30)
81
+ Gray40 = Spectrum::RGB.from_percentage(40, 40, 40)
82
+ Gray50 = Spectrum::RGB.from_percentage(50, 50, 50)
83
+ Gray60 = Spectrum::RGB.from_percentage(60, 60, 60)
84
+ Gray70 = Spectrum::RGB.from_percentage(70, 70, 70)
85
+ Gray80 = Spectrum::RGB.from_percentage(80, 80, 80)
86
+ Gray90 = Spectrum::RGB.from_percentage(90, 90, 90)
87
+ Green = Spectrum::RGB.new(0x00, 0x80, 0x00)
88
+ GreenYellow = Spectrum::RGB.new(0xad, 0xff, 0x2f)
89
+ Grey = Gray
90
+ Grey10 = Gray10
91
+ Grey20 = Gray20
92
+ Grey30 = Gray30
93
+ Grey40 = Gray40
94
+ Grey50 = Gray50
95
+ Grey60 = Gray60
96
+ Grey70 = Gray70
97
+ Grey80 = Gray80
98
+ Grey90 = Gray90
99
+ HoneyDew = Spectrum::RGB.new(0xf0, 0xff, 0xf0)
100
+ Honeydew = HoneyDew
101
+ HotPink = Spectrum::RGB.new(0xff, 0x69, 0xb4)
102
+ IndianRed = Spectrum::RGB.new(0xcd, 0x5c, 0x5c)
103
+ Indigo = Spectrum::RGB.new(0x4b, 0x00, 0x82)
104
+ Ivory = Spectrum::RGB.new(0xff, 0xff, 0xf0)
105
+ Khaki = Spectrum::RGB.new(0xf0, 0xe6, 0x8c)
106
+ Lavender = Spectrum::RGB.new(0xe6, 0xe6, 0xfa)
107
+ LavenderBlush = Spectrum::RGB.new(0xff, 0xf0, 0xf5)
108
+ LawnGreen = Spectrum::RGB.new(0x7c, 0xfc, 0x00)
109
+ LemonChiffon = Spectrum::RGB.new(0xff, 0xfa, 0xcd)
110
+ LightBlue = Spectrum::RGB.new(0xad, 0xd8, 0xe6)
111
+ LightCoral = Spectrum::RGB.new(0xf0, 0x80, 0x80)
112
+ LightCyan = Spectrum::RGB.new(0xe0, 0xff, 0xff)
113
+ LightGoldenRodYellow = Spectrum::RGB.new(0xfa, 0xfa, 0xd2)
114
+ LightGoldenrodYellow = LightGoldenRodYellow
115
+ LightGray = Spectrum::RGB.new(0xd3, 0xd3, 0xd3)
116
+ LightGreen = Spectrum::RGB.new(0x90, 0xee, 0x90)
117
+ LightGrey = LightGray
118
+ LightPink = Spectrum::RGB.new(0xff, 0xb6, 0xc1)
119
+ LightSalmon = Spectrum::RGB.new(0xff, 0xa0, 0x7a)
120
+ LightSeaGreen = Spectrum::RGB.new(0x20, 0xb2, 0xaa)
121
+ LightSkyBlue = Spectrum::RGB.new(0x87, 0xce, 0xfa)
122
+ LightSlateBlue = Spectrum::RGB.new(0x84, 0x70, 0xff)
123
+ LightSlateGray = Spectrum::RGB.new(0x77, 0x88, 0x99)
124
+ LightSlateGrey = LightSlateGray
125
+ LightSteelBlue = Spectrum::RGB.new(0xb0, 0xc4, 0xde)
126
+ LightYellow = Spectrum::RGB.new(0xff, 0xff, 0xe0)
127
+ Lightsalmon = LightSalmon
128
+ LightsteelBlue = LightSteelBlue
129
+ Lime = Spectrum::RGB.new(0x00, 0xff, 0x00)
130
+ LimeGreen = Spectrum::RGB.new(0x32, 0xcd, 0x32)
131
+ Linen = Spectrum::RGB.new(0xfa, 0xf0, 0xe6)
132
+ Magenta = Spectrum::RGB.new(0xff, 0x00, 0xff)
133
+ Maroon = Spectrum::RGB.new(0x80, 0x00, 0x00)
134
+ MediumAquaMarine = Spectrum::RGB.new(0x66, 0xcd, 0xaa)
135
+ MediumAquamarine = MediumAquaMarine
136
+ MediumBlue = Spectrum::RGB.new(0x00, 0x00, 0xcd)
137
+ MediumOrchid = Spectrum::RGB.new(0xba, 0x55, 0xd3)
138
+ MediumPurple = Spectrum::RGB.new(0x93, 0x70, 0xdb)
139
+ MediumSeaGreen = Spectrum::RGB.new(0x3c, 0xb3, 0x71)
140
+ MediumSlateBlue = Spectrum::RGB.new(0x7b, 0x68, 0xee)
141
+ MediumSpringGreen = Spectrum::RGB.new(0x00, 0xfa, 0x9a)
142
+ MediumTurquoise = Spectrum::RGB.new(0x48, 0xd1, 0xcc)
143
+ MediumVioletRed = Spectrum::RGB.new(0xc7, 0x15, 0x85)
144
+ MidnightBlue = Spectrum::RGB.new(0x19, 0x19, 0x70)
145
+ MintCream = Spectrum::RGB.new(0xf5, 0xff, 0xfa)
146
+ MistyRose = Spectrum::RGB.new(0xff, 0xe4, 0xe1)
147
+ Moccasin = Spectrum::RGB.new(0xff, 0xe4, 0xb5)
148
+ NavajoWhite = Spectrum::RGB.new(0xff, 0xde, 0xad)
149
+ Navy = Spectrum::RGB.new(0x00, 0x00, 0x80)
150
+ OldLace = Spectrum::RGB.new(0xfd, 0xf5, 0xe6)
151
+ Olive = Spectrum::RGB.new(0x80, 0x80, 0x00)
152
+ OliveDrab = Spectrum::RGB.new(0x6b, 0x8e, 0x23)
153
+ Olivedrab = OliveDrab
154
+ Orange = Spectrum::RGB.new(0xff, 0xa5, 0x00)
155
+ OrangeRed = Spectrum::RGB.new(0xff, 0x45, 0x00)
156
+ Orchid = Spectrum::RGB.new(0xda, 0x70, 0xd6)
157
+ PaleGoldenRod = Spectrum::RGB.new(0xee, 0xe8, 0xaa)
158
+ PaleGoldenrod = PaleGoldenRod
159
+ PaleGreen = Spectrum::RGB.new(0x98, 0xfb, 0x98)
160
+ PaleTurquoise = Spectrum::RGB.new(0xaf, 0xee, 0xee)
161
+ PaleVioletRed = Spectrum::RGB.new(0xdb, 0x70, 0x93)
162
+ PapayaWhip = Spectrum::RGB.new(0xff, 0xef, 0xd5)
163
+ PeachPuff = Spectrum::RGB.new(0xff, 0xda, 0xb9)
164
+ Peachpuff = PeachPuff
165
+ Peru = Spectrum::RGB.new(0xcd, 0x85, 0x3f)
166
+ Pink = Spectrum::RGB.new(0xff, 0xc0, 0xcb)
167
+ Plum = Spectrum::RGB.new(0xdd, 0xa0, 0xdd)
168
+ PowderBlue = Spectrum::RGB.new(0xb0, 0xe0, 0xe6)
169
+ Purple = Spectrum::RGB.new(0x80, 0x00, 0x80)
170
+ Red = Spectrum::RGB.new(0xff, 0x00, 0x00)
171
+ RosyBrown = Spectrum::RGB.new(0xbc, 0x8f, 0x8f)
172
+ RoyalBlue = Spectrum::RGB.new(0x41, 0x69, 0xe1)
173
+ SaddleBrown = Spectrum::RGB.new(0x8b, 0x45, 0x13)
174
+ Salmon = Spectrum::RGB.new(0xfa, 0x80, 0x72)
175
+ SandyBrown = Spectrum::RGB.new(0xf4, 0xa4, 0x60)
176
+ SeaGreen = Spectrum::RGB.new(0x2e, 0x8b, 0x57)
177
+ SeaShell = Spectrum::RGB.new(0xff, 0xf5, 0xee)
178
+ Seashell = SeaShell
179
+ Sienna = Spectrum::RGB.new(0xa0, 0x52, 0x2d)
180
+ Silver = Spectrum::RGB.new(0xc0, 0xc0, 0xc0)
181
+ SkyBlue = Spectrum::RGB.new(0x87, 0xce, 0xeb)
182
+ SlateBlue = Spectrum::RGB.new(0x6a, 0x5a, 0xcd)
183
+ SlateGray = Spectrum::RGB.new(0x70, 0x80, 0x90)
184
+ SlateGrey = SlateGray
185
+ Snow = Spectrum::RGB.new(0xff, 0xfa, 0xfa)
186
+ SpringGreen = Spectrum::RGB.new(0x00, 0xff, 0x7f)
187
+ SteelBlue = Spectrum::RGB.new(0x46, 0x82, 0xb4)
188
+ Tan = Spectrum::RGB.new(0xd2, 0xb4, 0x8c)
189
+ Teal = Spectrum::RGB.new(0x00, 0x80, 0x80)
190
+ Thistle = Spectrum::RGB.new(0xd8, 0xbf, 0xd8)
191
+ Tomato = Spectrum::RGB.new(0xff, 0x63, 0x47)
192
+ Turquoise = Spectrum::RGB.new(0x40, 0xe0, 0xd0)
193
+ Violet = Spectrum::RGB.new(0xee, 0x82, 0xee)
194
+ VioletRed = Spectrum::RGB.new(0xd0, 0x20, 0x90)
195
+ Wheat = Spectrum::RGB.new(0xf5, 0xde, 0xb3)
196
+ White = Spectrum::RGB.new(0xff, 0xff, 0xff)
197
+ WhiteSmoke = Spectrum::RGB.new(0xf5, 0xf5, 0xf5)
198
+ Yellow = Spectrum::RGB.new(0xff, 0xff, 0x00)
199
+ YellowGreen = Spectrum::RGB.new(0x9a, 0xcd, 0x32)
200
+
201
+ AliceBlue.freeze
202
+ AntiqueWhite.freeze
203
+ Aqua.freeze
204
+ Aquamarine.freeze
205
+ Azure.freeze
206
+ Beige.freeze
207
+ Bisque.freeze
208
+ Black.freeze
209
+ BlanchedAlmond.freeze
210
+ Blue.freeze
211
+ BlueViolet.freeze
212
+ Brown.freeze
213
+ Burlywood.freeze
214
+ CadetBlue.freeze
215
+ Cayenne.freeze
216
+ Carnation.freeze
217
+ Chartreuse.freeze
218
+ Chocolate.freeze
219
+ Coral.freeze
220
+ CornflowerBlue.freeze
221
+ Cornsilk.freeze
222
+ Crimson.freeze
223
+ Cyan.freeze
224
+ DarkBlue.freeze
225
+ DarkCyan.freeze
226
+ DarkGoldenrod.freeze
227
+ DarkGray.freeze
228
+ DarkGreen.freeze
229
+ DarkKhaki.freeze
230
+ DarkMagenta.freeze
231
+ DarkoliveGreen.freeze
232
+ Darkorange.freeze
233
+ DarkOrchid.freeze
234
+ DarkRed.freeze
235
+ Darksalmon.freeze
236
+ DarkSeaGreen.freeze
237
+ DarkSlateBlue.freeze
238
+ DarkSlateGray.freeze
239
+ DarkTurquoise.freeze
240
+ DarkViolet.freeze
241
+ DeepPink.freeze
242
+ DeepSkyBlue.freeze
243
+ DimGray.freeze
244
+ DodgerBlue.freeze
245
+ Feldspar.freeze
246
+ Firebrick.freeze
247
+ FloralWhite.freeze
248
+ ForestGreen.freeze
249
+ Fuchsia.freeze
250
+ Gainsboro.freeze
251
+ GhostWhite.freeze
252
+ Gold.freeze
253
+ Goldenrod.freeze
254
+ Gray.freeze
255
+ Green.freeze
256
+ GreenYellow.freeze
257
+ Honeydew.freeze
258
+ HotPink.freeze
259
+ IndianRed.freeze
260
+ Indigo.freeze
261
+ Ivory.freeze
262
+ Khaki.freeze
263
+ Lavender.freeze
264
+ LavenderBlush.freeze
265
+ LawnGreen.freeze
266
+ LemonChiffon.freeze
267
+ LightBlue.freeze
268
+ LightCoral.freeze
269
+ LightCyan.freeze
270
+ LightGoldenrodYellow.freeze
271
+ LightGray.freeze
272
+ LightGreen.freeze
273
+ LightPink.freeze
274
+ Lightsalmon.freeze
275
+ LightSeaGreen.freeze
276
+ LightSkyBlue.freeze
277
+ LightSlateBlue.freeze
278
+ LightSlateGray.freeze
279
+ LightsteelBlue.freeze
280
+ LightYellow.freeze
281
+ Lime.freeze
282
+ LimeGreen.freeze
283
+ Linen.freeze
284
+ Magenta.freeze
285
+ Maroon.freeze
286
+ MediumAquamarine.freeze
287
+ MediumBlue.freeze
288
+ MediumOrchid.freeze
289
+ MediumPurple.freeze
290
+ MediumSeaGreen.freeze
291
+ MediumSlateBlue.freeze
292
+ MediumSpringGreen.freeze
293
+ MediumTurquoise.freeze
294
+ MediumVioletRed.freeze
295
+ MidnightBlue.freeze
296
+ MintCream.freeze
297
+ MistyRose.freeze
298
+ Moccasin.freeze
299
+ NavajoWhite.freeze
300
+ Navy.freeze
301
+ OldLace.freeze
302
+ Olive.freeze
303
+ Olivedrab.freeze
304
+ Orange.freeze
305
+ OrangeRed.freeze
306
+ Orchid.freeze
307
+ PaleGoldenrod.freeze
308
+ PaleGreen.freeze
309
+ PaleTurquoise.freeze
310
+ PaleVioletRed.freeze
311
+ PapayaWhip.freeze
312
+ Peachpuff.freeze
313
+ Peru.freeze
314
+ Pink.freeze
315
+ Plum.freeze
316
+ PowderBlue.freeze
317
+ Purple.freeze
318
+ Red.freeze
319
+ RosyBrown.freeze
320
+ RoyalBlue.freeze
321
+ SaddleBrown.freeze
322
+ Salmon.freeze
323
+ SandyBrown.freeze
324
+ SeaGreen.freeze
325
+ Seashell.freeze
326
+ Sienna.freeze
327
+ Silver.freeze
328
+ SkyBlue.freeze
329
+ SlateBlue.freeze
330
+ SlateGray.freeze
331
+ Snow.freeze
332
+ SpringGreen.freeze
333
+ SteelBlue.freeze
334
+ Tan.freeze
335
+ Teal.freeze
336
+ Thistle.freeze
337
+ Tomato.freeze
338
+ Turquoise.freeze
339
+ Violet.freeze
340
+ VioletRed.freeze
341
+ Wheat.freeze
342
+ White.freeze
343
+ WhiteSmoke.freeze
344
+ Yellow.freeze
345
+ YellowGreen.freeze
346
+ Gray10.freeze
347
+ Gray20.freeze
348
+ Gray30.freeze
349
+ Gray40.freeze
350
+ Gray50.freeze
351
+ Gray60.freeze
352
+ Gray70.freeze
353
+ Gray80.freeze
354
+ Gray90.freeze
355
+ end
@@ -0,0 +1,453 @@
1
+ #--
2
+ # Color
3
+ # Colour management with Ruby
4
+ # http://rubyforge.org/projects/color
5
+ # Version 1.4.1
6
+ #
7
+ # Licensed under a MIT-style licence. See Licence.txt in the main
8
+ # distribution for full licensing information.
9
+ #
10
+ # Copyright (c) 2005 - 2010 Austin Ziegler and Matt Lyon
11
+ #++
12
+
13
+ # An RGB colour object.
14
+ class Spectrum::RGB
15
+ # The format of a DeviceRGB colour for PDF. In color-tools 2.0 this will
16
+ # be removed from this package and added back as a modification by the
17
+ # PDF::Writer package.
18
+ PDF_FORMAT_STR = "%.3f %.3f %.3f %s"
19
+
20
+ class << self
21
+ # Creates an RGB colour object from percentages 0..100.
22
+ #
23
+ # Spectrum::RGB.from_percentage(10, 20 30)
24
+ def from_percentage(r = 0, g = 0, b = 0)
25
+ from_fraction(r / 100.0, g / 100.0, b / 100.0)
26
+ end
27
+
28
+ # Creates an RGB colour object from fractional values 0..1.
29
+ #
30
+ # Spectrum::RGB.from_fraction(.3, .2, .1)
31
+ def from_fraction(r = 0.0, g = 0.0, b = 0.0)
32
+ colour = Spectrum::RGB.new
33
+ colour.r = r
34
+ colour.g = g
35
+ colour.b = b
36
+ colour
37
+ end
38
+
39
+ # Creates an RGB colour object from an HTML colour descriptor (e.g.,
40
+ # <tt>"fed"</tt> or <tt>"#cabbed;"</tt>.
41
+ #
42
+ # Spectrum::RGB.from_html("fed")
43
+ # Spectrum::RGB.from_html("#fed")
44
+ # Spectrum::RGB.from_html("#cabbed")
45
+ # Spectrum::RGB.from_html("cabbed")
46
+ def from_html(html_colour)
47
+ html_colour = html_colour.gsub(%r{[#;]}, '')
48
+ case html_colour.size
49
+ when 3
50
+ colours = html_colour.scan(%r{[0-9A-Fa-f]}).map { |el| (el * 2).to_i(16) }
51
+ when 6
52
+ colours = html_colour.scan(%r<[0-9A-Fa-f]{2}>).map { |el| el.to_i(16) }
53
+ else
54
+ raise ArgumentError
55
+ end
56
+
57
+ Spectrum::RGB.new(*colours)
58
+ end
59
+ end
60
+
61
+ # Compares the other colour to this one. The other colour will be
62
+ # converted to RGB before comparison, so the comparison between a RGB
63
+ # colour and a non-RGB colour will be approximate and based on the other
64
+ # colour's default #to_rgb conversion. If there is no #to_rgb conversion,
65
+ # this will raise an exception. This will report that two RGB colours are
66
+ # equivalent if all component values are within COLOR_TOLERANCE of each
67
+ # other.
68
+ def ==(other)
69
+ other = other.to_rgb
70
+ other.kind_of?(Spectrum::RGB) and
71
+ ((@r - other.r).abs <= Spectrum::COLOR_TOLERANCE) and
72
+ ((@g - other.g).abs <= Spectrum::COLOR_TOLERANCE) and
73
+ ((@b - other.b).abs <= Spectrum::COLOR_TOLERANCE)
74
+ end
75
+
76
+ # Creates an RGB colour object from the standard range 0..255.
77
+ #
78
+ # Spectrum::RGB.new(32, 64, 128)
79
+ # Spectrum::RGB.new(0x20, 0x40, 0x80)
80
+ def initialize(r = 0, g = 0, b = 0)
81
+ @r = r / 255.0
82
+ @g = g / 255.0
83
+ @b = b / 255.0
84
+ end
85
+
86
+ # Present the colour as a DeviceRGB fill colour string for PDF. This will
87
+ # be removed from the default package in color-tools 2.0.
88
+ def pdf_fill
89
+ PDF_FORMAT_STR % [ @r, @g, @b, "rg" ]
90
+ end
91
+
92
+ # Present the colour as a DeviceRGB stroke colour string for PDF. This
93
+ # will be removed from the default package in color-tools 2.0.
94
+ def pdf_stroke
95
+ PDF_FORMAT_STR % [ @r, @g, @b, "RG" ]
96
+ end
97
+
98
+ # Present the colour as an HTML/CSS colour string.
99
+ def html
100
+ r = (@r * 255).round
101
+ r = 255 if r > 255
102
+
103
+ g = (@g * 255).round
104
+ g = 255 if g > 255
105
+
106
+ b = (@b * 255).round
107
+ b = 255 if b > 255
108
+
109
+ "#%02x%02x%02x" % [ r, g, b ]
110
+ end
111
+
112
+ # Present the colour as an RGB HTML/CSS colour string (e.g., "rgb(0%, 50%,
113
+ # 100%)"). Note that this will perform a #to_rgb operation using the
114
+ # default conversion formula.
115
+ def css_rgb
116
+ "rgb(%3.2f%%, %3.2f%%, %3.2f%%)" % [ red_p, green_p, blue_p ]
117
+ end
118
+
119
+ # Present the colour as an RGBA (with alpha) HTML/CSS colour string (e.g.,
120
+ # "rgb(0%, 50%, 100%, 1)"). Note that this will perform a #to_rgb
121
+ # operation using the default conversion formula.
122
+ def css_rgba
123
+ "rgba(%3.2f%%, %3.2f%%, %3.2f%%, %3.2f)" % [ red_p, green_p, blue_p, 1 ]
124
+ end
125
+
126
+ # Present the colour as an HSL HTML/CSS colour string (e.g., "hsl(180,
127
+ # 25%, 35%)"). Note that this will perform a #to_hsl operation using the
128
+ # default conversion formula.
129
+ def css_hsl
130
+ to_hsl.css_hsl
131
+ end
132
+
133
+ # Present the colour as an HSLA (with alpha) HTML/CSS colour string (e.g.,
134
+ # "hsla(180, 25%, 35%, 1)"). Note that this will perform a #to_hsl
135
+ # operation using the default conversion formula.
136
+ def css_hsla
137
+ to_hsl.css_hsla
138
+ end
139
+
140
+ # Converts the RGB colour to CMYK. Most colour experts strongly suggest
141
+ # that this is not a good idea (some even suggesting that it's a very bad
142
+ # idea). CMYK represents additive percentages of inks on white paper,
143
+ # whereas RGB represents mixed colour intensities on a black screen.
144
+ #
145
+ # However, the colour conversion can be done. The basic method is
146
+ # multi-step:
147
+ #
148
+ # 1. Convert the R, G, and B components to C, M, and Y components.
149
+ # c = 1.0 - r
150
+ # m = 1.0 - g
151
+ # y = 1.0 - b
152
+ # 2. Compute the minimum amount of black (K) required to smooth the colour
153
+ # in inks.
154
+ # k = min(c, m, y)
155
+ # 3. Perform undercolour removal on the C, M, and Y components of the
156
+ # colours because less of each colour is needed for each bit of black.
157
+ # Also, regenerate the black (K) based on the undercolour removal so
158
+ # that the colour is more accurately represented in ink.
159
+ # c = min(1.0, max(0.0, c - UCR(k)))
160
+ # m = min(1.0, max(0.0, m - UCR(k)))
161
+ # y = min(1.0, max(0.0, y - UCR(k)))
162
+ # k = min(1.0, max(0.0, BG(k)))
163
+ #
164
+ # The undercolour removal function and the black generation functions
165
+ # return a value based on the brightness of the RGB colour.
166
+ def to_cmyk
167
+ c = 1.0 - @r.to_f
168
+ m = 1.0 - @g.to_f
169
+ y = 1.0 - @b.to_f
170
+
171
+ k = [c, m, y].min
172
+ k = k - (k * brightness)
173
+
174
+ c = [1.0, [0.0, c - k].max].min
175
+ m = [1.0, [0.0, m - k].max].min
176
+ y = [1.0, [0.0, y - k].max].min
177
+ k = [1.0, [0.0, k].max].min
178
+
179
+ Spectrum::CMYK.from_fraction(c, m, y, k)
180
+ end
181
+
182
+ def to_rgb(ignored = nil)
183
+ self
184
+ end
185
+
186
+ # Returns the YIQ (NTSC) colour encoding of the RGB value.
187
+ def to_yiq
188
+ y = (@r * 0.299) + (@g * 0.587) + (@b * 0.114)
189
+ i = (@r * 0.596) + (@g * -0.275) + (@b * -0.321)
190
+ q = (@r * 0.212) + (@g * -0.523) + (@b * 0.311)
191
+ Spectrum::YIQ.from_fraction(y, i, q)
192
+ end
193
+
194
+ # Returns the HSL colour encoding of the RGB value. The conversions here
195
+ # are based on forumlas from http://www.easyrgb.com/math.php and
196
+ # elsewhere.
197
+ def to_hsl
198
+ min = [ @r, @g, @b ].min
199
+ max = [ @r, @g, @b ].max
200
+ delta = (max - min).to_f
201
+
202
+ lum = (max + min) / 2.0
203
+
204
+ if Spectrum.near_zero?(delta) # close to 0.0, so it's a grey
205
+ hue = 0
206
+ sat = 0
207
+ else
208
+ if Spectrum.near_zero_or_less?(lum - 0.5)
209
+ sat = delta / (max + min).to_f
210
+ else
211
+ sat = delta / (2 - max - min).to_f
212
+ end
213
+
214
+ # This is based on the conversion algorithm from
215
+ # http://en.wikipedia.org/wiki/HSV_color_space#Conversion_from_RGB_to_HSL_or_HSV
216
+ # Contributed by Adam Johnson
217
+ sixth = 1 / 6.0
218
+ if @r == max # Spectrum.near_zero_or_less?(@r - max)
219
+ hue = (sixth * ((@g - @b) / delta))
220
+ hue += 1.0 if @g < @b
221
+ elsif @g == max # Spectrum.near_zero_or_less(@g - max)
222
+ hue = (sixth * ((@b - @r) / delta)) + (1.0 / 3.0)
223
+ elsif @b == max # Spectrum.near_zero_or_less?(@b - max)
224
+ hue = (sixth * ((@r - @g) / delta)) + (2.0 / 3.0)
225
+ end
226
+
227
+ hue += 1 if hue < 0
228
+ hue -= 1 if hue > 1
229
+ end
230
+ Spectrum::HSL.from_fraction(hue, sat, lum)
231
+ end
232
+
233
+ # Mix the RGB hue with White so that the RGB hue is the specified
234
+ # percentage of the resulting colour. Strictly speaking, this isn't a
235
+ # darken_by operation.
236
+ def lighten_by(percent)
237
+ mix_with(White, percent)
238
+ end
239
+
240
+ # Mix the RGB hue with Black so that the RGB hue is the specified
241
+ # percentage of the resulting colour. Strictly speaking, this isn't a
242
+ # darken_by operation.
243
+ def darken_by(percent)
244
+ mix_with(Black, percent)
245
+ end
246
+
247
+ # Mix the mask colour (which must be an RGB object) with the current
248
+ # colour at the stated opacity percentage (0..100).
249
+ def mix_with(mask, opacity)
250
+ opacity /= 100.0
251
+ rgb = self.dup
252
+
253
+ rgb.r = (@r * opacity) + (mask.r * (1 - opacity))
254
+ rgb.g = (@g * opacity) + (mask.g * (1 - opacity))
255
+ rgb.b = (@b * opacity) + (mask.b * (1 - opacity))
256
+
257
+ rgb
258
+ end
259
+
260
+ # Returns the brightness value for a colour, a number between 0..1. Based
261
+ # on the Y value of YIQ encoding, representing luminosity, or perceived
262
+ # brightness.
263
+ #
264
+ # This may be modified in a future version of color-tools to use the
265
+ # luminosity value of HSL.
266
+ def brightness
267
+ to_yiq.y
268
+ end
269
+ # Convert to grayscale.
270
+ def to_grayscale
271
+ Spectrum::GrayScale.from_fraction(to_hsl.l)
272
+ end
273
+ alias to_greyscale to_grayscale
274
+
275
+ # Returns a new colour with the brightness adjusted by the specified
276
+ # percentage. Negative percentages will darken the colour; positive
277
+ # percentages will brighten the colour.
278
+ #
279
+ # Spectrum::RGB::DarkBlue.adjust_brightness(10)
280
+ # Spectrum::RGB::DarkBlue.adjust_brightness(-10)
281
+ def adjust_brightness(percent)
282
+ percent /= 100.0
283
+ percent += 1.0
284
+ percent = [ percent, 2.0 ].min
285
+ percent = [ 0.0, percent ].max
286
+
287
+ hsl = to_hsl
288
+ hsl.l *= percent
289
+ hsl.to_rgb
290
+ end
291
+
292
+ # Returns a new colour with the saturation adjusted by the specified
293
+ # percentage. Negative percentages will reduce the saturation; positive
294
+ # percentages will increase the saturation.
295
+ #
296
+ # Spectrum::RGB::DarkBlue.adjust_saturation(10)
297
+ # Spectrum::RGB::DarkBlue.adjust_saturation(-10)
298
+ def adjust_saturation(percent)
299
+ percent /= 100.0
300
+ percent += 1.0
301
+ percent = [ percent, 2.0 ].min
302
+ percent = [ 0.0, percent ].max
303
+
304
+ hsl = to_hsl
305
+ hsl.s *= percent
306
+ hsl.to_rgb
307
+ end
308
+
309
+ # Returns a new colour with the hue adjusted by the specified percentage.
310
+ # Negative percentages will reduce the hue; positive percentages will
311
+ # increase the hue.
312
+ #
313
+ # Spectrum::RGB::DarkBlue.adjust_hue(10)
314
+ # Spectrum::RGB::DarkBlue.adjust_hue(-10)
315
+ def adjust_hue(percent)
316
+ percent /= 100.0
317
+ percent += 1.0
318
+ percent = [ percent, 2.0 ].min
319
+ percent = [ 0.0, percent ].max
320
+
321
+ hsl = to_hsl
322
+ hsl.h *= percent
323
+ hsl.to_rgb
324
+ end
325
+
326
+ # Returns the red component of the colour in the normal 0 .. 255 range.
327
+ def red
328
+ @r * 255.0
329
+ end
330
+ # Returns the red component of the colour as a percentage.
331
+ def red_p
332
+ @r * 100.0
333
+ end
334
+ # Returns the red component of the colour as a fraction in the range 0.0
335
+ # .. 1.0.
336
+ def r
337
+ @r
338
+ end
339
+ # Sets the red component of the colour in the normal 0 .. 255 range.
340
+ def red=(rr)
341
+ @r = Spectrum.normalize(rr / 255.0)
342
+ end
343
+ # Sets the red component of the colour as a percentage.
344
+ def red_p=(rr)
345
+ @r = Spectrum.normalize(rr / 100.0)
346
+ end
347
+ # Sets the red component of the colour as a fraction in the range 0.0 ..
348
+ # 1.0.
349
+ def r=(rr)
350
+ @r = Spectrum.normalize(rr)
351
+ end
352
+
353
+ # Returns the green component of the colour in the normal 0 .. 255 range.
354
+ def green
355
+ @g * 255.0
356
+ end
357
+ # Returns the green component of the colour as a percentage.
358
+ def green_p
359
+ @g * 100.0
360
+ end
361
+ # Returns the green component of the colour as a fraction in the range 0.0
362
+ # .. 1.0.
363
+ def g
364
+ @g
365
+ end
366
+ # Sets the green component of the colour in the normal 0 .. 255 range.
367
+ def green=(gg)
368
+ @g = Spectrum.normalize(gg / 255.0)
369
+ end
370
+ # Sets the green component of the colour as a percentage.
371
+ def green_p=(gg)
372
+ @g = Spectrum.normalize(gg / 100.0)
373
+ end
374
+ # Sets the green component of the colour as a fraction in the range 0.0 ..
375
+ # 1.0.
376
+ def g=(gg)
377
+ @g = Spectrum.normalize(gg)
378
+ end
379
+
380
+ # Returns the blue component of the colour in the normal 0 .. 255 range.
381
+ def blue
382
+ @b * 255.0
383
+ end
384
+ # Returns the blue component of the colour as a percentage.
385
+ def blue_p
386
+ @b * 100.0
387
+ end
388
+ # Returns the blue component of the colour as a fraction in the range 0.0
389
+ # .. 1.0.
390
+ def b
391
+ @b
392
+ end
393
+ # Sets the blue component of the colour in the normal 0 .. 255 range.
394
+ def blue=(bb)
395
+ @b = Spectrum.normalize(bb / 255.0)
396
+ end
397
+ # Sets the blue component of the colour as a percentage.
398
+ def blue_p=(bb)
399
+ @b = Spectrum.normalize(bb / 100.0)
400
+ end
401
+ # Sets the blue component of the colour as a fraction in the range 0.0 ..
402
+ # 1.0.
403
+ def b=(bb)
404
+ @b = Spectrum.normalize(bb)
405
+ end
406
+
407
+ # Adds another colour to the current colour. The other colour will be
408
+ # converted to RGB before addition. This conversion depends upon a #to_rgb
409
+ # method on the other colour.
410
+ #
411
+ # The addition is done using the RGB Accessor methods to ensure a valid
412
+ # colour in the result.
413
+ def +(other)
414
+ other = other.to_rgb
415
+ rgb = self.dup
416
+
417
+ rgb.r += other.r
418
+ rgb.g += other.g
419
+ rgb.b += other.b
420
+
421
+ rgb
422
+ end
423
+
424
+ # Subtracts another colour to the current colour. The other colour will be
425
+ # converted to RGB before subtraction. This conversion depends upon a
426
+ # #to_rgb method on the other colour.
427
+ #
428
+ # The subtraction is done using the RGB Accessor methods to ensure a valid
429
+ # colour in the result.
430
+ def -(other)
431
+ other = other.to_rgb
432
+ rgb = self.dup
433
+
434
+ rgb.r -= other.r
435
+ rgb.g -= other.g
436
+ rgb.b -= other.b
437
+
438
+ rgb
439
+ end
440
+
441
+ # Retrieve the maxmum RGB value from the current colour as a GrayScale
442
+ # colour
443
+ def max_rgb_as_grayscale
444
+ Spectrum::GrayScale.from_fraction([@r, @g, @b].max)
445
+ end
446
+ alias max_rgb_as_greyscale max_rgb_as_grayscale
447
+
448
+ def inspect
449
+ "RGB [#{html}]"
450
+ end
451
+ end
452
+
453
+ require 'spectrum/rgb-colors'