rmthemegen 0.0.23 → 0.0.25

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,511 @@
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 Color::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
+ # Color::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
+ # Color::RGB.from_fraction(.3, .2, .1)
31
+ def from_fraction(r = 0.0, g = 0.0, b = 0.0)
32
+ colour = Color::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
+ # Color::RGB.from_html("fed")
43
+ # Color::RGB.from_html("#fed")
44
+ # Color::RGB.from_html("#cabbed")
45
+ # Color::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
+ Color::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?(Color::RGB) and
71
+ ((@r - other.r).abs <= Color::COLOR_TOLERANCE) and
72
+ ((@g - other.g).abs <= Color::COLOR_TOLERANCE) and
73
+ ((@b - other.b).abs <= Color::COLOR_TOLERANCE)
74
+ end
75
+
76
+ # Creates an RGB colour object from the standard range 0..255.
77
+ #
78
+ # Color::RGB.new(32, 64, 128)
79
+ # Color::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
+ Color::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
+ Color::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 Color.near_zero?(delta) # close to 0.0, so it's a grey
205
+ hue = 0
206
+ sat = 0
207
+ else
208
+ if Color.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 # Color.near_zero_or_less?(@r - max)
219
+ hue = (sixth * ((@g - @b) / delta))
220
+ hue += 1.0 if @g < @b
221
+ elsif @g == max # Color.near_zero_or_less(@g - max)
222
+ hue = (sixth * ((@b - @r) / delta)) + (1.0 / 3.0)
223
+ elsif @b == max # Color.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
+ Color::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
+ Color::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
+ # Color::RGB::DarkBlue.adjust_brightness(10)
280
+ # Color::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
+ # Color::RGB::DarkBlue.adjust_saturation(10)
297
+ # Color::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
+ # Color::RGB::DarkBlue.adjust_hue(10)
314
+ # Color::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 = Color.normalize(rr / 255.0)
342
+ end
343
+ # Sets the red component of the colour as a percentage.
344
+ def red_p=(rr)
345
+ @r = Color.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 = Color.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 = Color.normalize(gg / 255.0)
369
+ end
370
+ # Sets the green component of the colour as a percentage.
371
+ def green_p=(gg)
372
+ @g = Color.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 = Color.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 = Color.normalize(bb / 255.0)
396
+ end
397
+ # Sets the blue component of the colour as a percentage.
398
+ def blue_p=(bb)
399
+ @b = Color.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 = Color.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
+ Color::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
+
452
+
453
+ # Outputs how much contrast this color has with another rgb color. Computes the same
454
+ # regardless of which one is considered foreground.
455
+ # If the other color does not have a to_rgb method, this will throw an exception
456
+ # anything over about 0.22 should have a high likelihood of begin legible.
457
+ # otherwise, to be safe go with something > 0.3
458
+ def contrast(other_rgb)
459
+ if other_rgb.respond_to? :to_rgb then
460
+ c2 = other_rgb.to_rgb
461
+ else
462
+ raise "rgb.rb unable to calculate contrast with object #{other_rgb.to_s}"
463
+ end
464
+ #the following numbers have been set with some care.
465
+ return (
466
+ self.diff_bri(other_rgb)*0.65 +
467
+ self.diff_hue(other_rgb)*0.20 +
468
+ self.diff_lum(other_rgb)*0.15 )
469
+ end
470
+
471
+
472
+ #provides the luminosity difference between two rbg vals
473
+ def diff_lum(rgb)
474
+ rgb=rgb.to_rgb
475
+ l1 = 0.2126 * (rgb.r) ** 2.2 +
476
+ 0.7152 * (rgb.b) ** 2.2 +
477
+ 0.0722 * (rgb.g) ** 2.2;
478
+
479
+ l2 = 0.2126 * (self.r) ** 2.2 +
480
+ 0.7152 * (self.b) ** 2.2 +
481
+ 0.0722 * (self.g) ** 2.2;
482
+
483
+ return ( ( ([l1,l2].max) + 0.05 )/ ( ([l1,l2].min) + 0.05 ) - 1 ) / 20
484
+ end
485
+
486
+ #provides the brightness difference.
487
+ def diff_bri(rgb)
488
+ rgb=rgb.to_rgb
489
+ br1 = (299 * rgb.r + 587 * rgb.g + 114 * rgb.b) ;
490
+ br2 = (299 * self.r + 587 * self.g + 114 * self.b) ;
491
+ return (br1-br2).abs/1000;
492
+ end
493
+
494
+ #provides the euclidean distance between the two color values
495
+ def diff_pyt(rgb)
496
+ rgb=rgb.to_rgb
497
+ (((rgb.r - self.r)**2 +
498
+ (rgb.g - self.g)**2 +
499
+ (rgb.b - self.b)**2)**0.5)/(1.7320508075688772)
500
+ end
501
+
502
+ #difference in the two colors' hue
503
+ def diff_hue(rgb)
504
+ rgb=rgb.to_rgb
505
+ return ((self.r-rgb.r).abs +
506
+ (self.g-rgb.g).abs +
507
+ (self.b-rgb.b).abs)/3
508
+ end
509
+ end
510
+
511
+ require File.dirname(__FILE__)+'/rgb-colors.rb'
@@ -0,0 +1,84 @@
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
+ # A colour object representing YIQ (NTSC) colour encoding.
14
+ class Color::YIQ
15
+ # Creates a YIQ colour object from fractional values 0 .. 1.
16
+ #
17
+ # Color::YIQ.new(0.3, 0.2, 0.1)
18
+ def self.from_fraction(y = 0, i = 0, q = 0)
19
+ color = Color::YIQ.new
20
+ color.y = y
21
+ color.i = i
22
+ color.q = q
23
+ color
24
+ end
25
+
26
+ # Creates a YIQ colour object from percentages 0 .. 100.
27
+ #
28
+ # Color::YIQ.new(10, 20, 30)
29
+ def initialize(y = 0, i = 0, q = 0)
30
+ @y = y / 100.0
31
+ @i = i / 100.0
32
+ @q = q / 100.0
33
+ end
34
+
35
+ # Compares the other colour to this one. The other colour will be
36
+ # converted to YIQ before comparison, so the comparison between a YIQ
37
+ # colour and a non-YIQ colour will be approximate and based on the other
38
+ # colour's #to_yiq conversion. If there is no #to_yiq conversion, this
39
+ # will raise an exception. This will report that two YIQ values are
40
+ # equivalent if all component colours are within COLOR_TOLERANCE of each
41
+ # other.
42
+ def ==(other)
43
+ other = other.to_yiq
44
+ other.kind_of?(Color::YIQ) and
45
+ ((@y - other.y).abs <= Color::COLOR_TOLERANCE) and
46
+ ((@i - other.i).abs <= Color::COLOR_TOLERANCE) and
47
+ ((@q - other.q).abs <= Color::COLOR_TOLERANCE)
48
+ end
49
+
50
+ def to_yiq
51
+ self
52
+ end
53
+
54
+ def brightness
55
+ @y
56
+ end
57
+ def to_grayscale
58
+ Color::GrayScale.new(@y)
59
+ end
60
+ alias to_greyscale to_grayscale
61
+
62
+ def y
63
+ @y
64
+ end
65
+ def y=(yy)
66
+ @y = Color.normalize(yy)
67
+ end
68
+ def i
69
+ @i
70
+ end
71
+ def i=(ii)
72
+ @i = Color.normalize(ii)
73
+ end
74
+ def q
75
+ @q
76
+ end
77
+ def q=(qq)
78
+ @q = Color.normalize(qq)
79
+ end
80
+
81
+ def inspect
82
+ "YIQ [%.2f%%, %.2f%%, %.2f%%]" % [ @y * 100, @i * 100, @q * 100 ]
83
+ end
84
+ end