color_lib 1.4.4

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