slippyd-colorist 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slippyd-colorist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Michael Bleigh
8
+ - oleg dashevskii
9
+ - Slippy Douglas
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2009-06-29 00:00:00 -07:00
15
+ default_executable:
16
+ dependencies: []
17
+
18
+ description: Colorist is a library built to handle the easy conversion and manipulation of colors with a special emphasis on W3C standards and CSS-style hex color notation.
19
+ email: michael@intridea.com
20
+ executables: []
21
+
22
+ extensions: []
23
+
24
+ extra_rdoc_files:
25
+ - MIT_LICENSE.rdoc
26
+ - README.rdoc
27
+ files:
28
+ - .gitignore
29
+ - CHANGELOG.rdoc
30
+ - MIT_LICENSE.rdoc
31
+ - README.rdoc
32
+ - Rakefile
33
+ - VERSION
34
+ - colorist.gemspec
35
+ - lib/colorist.rb
36
+ - lib/colorist/color.rb
37
+ - lib/colorist/core_extensions.rb
38
+ has_rdoc: false
39
+ homepage: http://github.com/slippyd/colorist
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --main
43
+ - README.rdoc
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.1
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: A library built to handle the easy conversion and simple manipulation of colors.
65
+ test_files: []
66
+
data/Rakefile CHANGED
@@ -4,11 +4,11 @@ require 'rake'
4
4
  begin
5
5
  require 'jeweler'
6
6
  Jeweler::Tasks.new do |gem|
7
- gem.name = "colorist"
7
+ gem.name = "slippyd-colorist"
8
8
  gem.summary = %Q{A library built to handle the easy conversion and simple manipulation of colors.}
9
- gem.email = "michael@intridea.com"
10
- gem.homepage = "http://github.com/be9/colorist"
11
- gem.authors = ["Michael Bleigh", "oleg dashevskii"]
9
+ gem.email = "slippyd-colorist@6bitt.com"
10
+ gem.homepage = "http://github.com/slippyd/colorist"
11
+ gem.authors = ["Michael Bleigh", "oleg dashevskii", "Slippy Douglas"]
12
12
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
13
  gem.description = "Colorist is a library built to handle the easy conversion and manipulation of colors with a special emphasis on W3C standards and CSS-style hex color notation."
14
14
  gem.has_rdoc = true
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.5
@@ -3,8 +3,8 @@ module Colorist
3
3
  # Colorist gem. It provides methods to add, subtract, and calculate aspects
4
4
  # of the color based on W3C and other standards.
5
5
  class Color
6
- attr_accessor :r, :g, :b
7
-
6
+ attr_accessor :r, :g, :b, :a
7
+
8
8
  CSS_COLOR_NAMES = { "maroon" => 0x800000,
9
9
  "red" => 0xff0000,
10
10
  "orange" => 0xffa500,
@@ -22,19 +22,20 @@ module Colorist
22
22
  "black" => 0x000000,
23
23
  "silver" => 0xc0c0c0,
24
24
  "gray" => 0x808080 }
25
-
25
+
26
26
  # Creates a new color with the hex color provided as a number (i.e. 0x112233)
27
- def initialize(color=0x000000)
27
+ def initialize(color = 0x000000, alpha = 1.0)
28
28
  string = "%.6x" % color
29
29
  @r = string[0..1].hex
30
30
  @g = string[2..3].hex
31
31
  @b = string[4..5].hex
32
+ @a = alpha
32
33
  end
33
-
34
+
34
35
  # Initialize a color based on RGB values. By default, the values
35
36
  # should be between 0 and 255. If you use the option <tt>:percent => true</tt>,
36
37
  # the values should then be between 0.0 and 1.0.
37
- def self.from_rgb(r,g,b,options={})
38
+ def self.from_rgb(r, g, b, options = {})
38
39
  color = new
39
40
  # convert from 0.0 to 1.0 to 0 to 255 if the :percent option is used
40
41
  if options[:percent]
@@ -44,21 +45,27 @@ module Colorist
44
45
  end
45
46
  color
46
47
  end
47
-
48
+
49
+ def self.from_rgba(r, g, b, a, options = {})
50
+ color = from_rgb(r, g, b, options)
51
+ color.a = a
52
+ color
53
+ end
54
+
48
55
  # Initialize a colour based on HSV/HSB values. Hue should be between 0 and 360 (inclusive),
49
56
  # while saturation and value should be from 0.0 to 1.0.
50
57
  def self.from_hsv(hue, saturation, value)
51
58
  saturation = 1 if saturation > 1
52
59
  value = 1 if saturation > 1
53
-
60
+
54
61
  # Conversion formula taken from wikipedia
55
-
62
+
56
63
  f = (hue / 60.0) - (hue / 60).floor
57
-
64
+
58
65
  p = value * (1 - saturation)
59
66
  q = value * (1 - (saturation * f))
60
67
  t = value * (1 - (saturation * (1 - f)))
61
-
68
+
62
69
  r, g, b = case (hue / 60).floor % 6
63
70
  when 0 then [ value, t, p ]
64
71
  when 1 then [ q, value, p ]
@@ -67,10 +74,16 @@ module Colorist
67
74
  when 4 then [ t, p, value ]
68
75
  when 5 then [ value, p, q ]
69
76
  end
70
-
77
+
71
78
  from_rgb(r, g, b, :percent => true)
72
79
  end
73
-
80
+
81
+ def self.from_hsva(hue, saturation, value, alpha)
82
+ color = from_hsv(hue, saturation, value)
83
+ color.a = alpha
84
+ color
85
+ end
86
+
74
87
  # Converts a CSS hex string into a color. Works both with the
75
88
  # full form (i.e. <tt>#ffffff</tt>) and the abbreviated form (<tt>#fff</tt>). Can
76
89
  # also take any of the 16 named CSS colors.
@@ -89,7 +102,7 @@ module Colorist
89
102
  end
90
103
  color
91
104
  end
92
-
105
+
93
106
  # Create a new color from the provided object. Duplicates Color objects
94
107
  # and attempts to call <tt>to_color</tt> on other objects. Will raise
95
108
  # an ArgumentError if it is unable to coerce the color.
@@ -102,14 +115,15 @@ module Colorist
102
115
  some_entity.to_color
103
116
  end
104
117
  end
105
-
118
+
106
119
  # Create a duplicate of this color.
107
120
  def dup
108
- self.class.from_rgb(@r,@g,@b)
121
+ self.class.from_rgba(@r, @g, @b, @a)
109
122
  end
110
-
111
- # Add the individual RGB values of two colors together. You
112
- # may also use an equivalent numeric or string color representation.
123
+
124
+ # Add the individual RGB values of two colors together.
125
+ # You may also use an equivalent numeric or string color representation.
126
+ # The alpha value is kept from the original color (left-hand side).
113
127
  #
114
128
  # Examples:
115
129
  #
@@ -126,9 +140,10 @@ module Colorist
126
140
  color.b += other_color.b
127
141
  color
128
142
  end
129
-
143
+
130
144
  # Subtract the individual RGB values of the two colors together.
131
145
  # You may also use an equivalent numeric or string color representation.
146
+ # The alpha value is kept from the original color (left-hand side).
132
147
  def -(other_color)
133
148
  other_color = self.class.from(other_color)
134
149
  color = self.dup
@@ -137,11 +152,12 @@ module Colorist
137
152
  color.b -= other_color.b
138
153
  color
139
154
  end
140
-
155
+
141
156
  # Multiply the individual RGB values of two colors together or against a Float.
142
157
  # Colors divided in a way that is equivalent to each of the values being normalized to 0.0..1.0 prior to the operation
143
158
  # and normalized back to 0.0..255.0 after the operation.
144
159
  # You may also use an equivalent numeric or string color representation.
160
+ # The alpha value is kept from the original color (left-hand side).
145
161
  def *(other)
146
162
  color = self.dup
147
163
 
@@ -158,11 +174,12 @@ module Colorist
158
174
 
159
175
  color
160
176
  end
161
-
177
+
162
178
  # Divide the individual RGB values of the two colors together or against a Float.
163
179
  # Colors divided in a way that is equivalent to each of the values being normalized to 0.0..1.0 prior to the operation
164
180
  # and normalized back to 0.0..255.0 after the operation.
165
181
  # You may also use an equivalent numeric or string color representation.
182
+ # The alpha value is kept from the original color (left-hand side).
166
183
  def /(other)
167
184
  color = self.dup
168
185
 
@@ -179,69 +196,77 @@ module Colorist
179
196
 
180
197
  color
181
198
  end
182
-
199
+
183
200
  # Compares colors based on brightness.
184
201
  def <=>(other_color)
185
202
  other_color = self.class.from(other_color)
186
203
  brightness <=> other_color.brightness
187
204
  end
188
-
205
+
189
206
  # Compares colors based on brightness.
190
207
  def < (other_color)
191
208
  other_color = self.class.from(other_color)
192
209
  brightness < other_color.brightness
193
210
  end
194
-
211
+
195
212
  # Compares colors based on brightness.
196
213
  def > (other_color)
197
214
  other_color = self.class.from(other_color)
198
215
  brightness > other_color.brightness
199
216
  end
200
-
201
- # Equal if the red, green, and blue values are identical.
217
+
218
+ # Equal if the red, green, blue, and alpha values are identical.
202
219
  def ==(other_color)
203
220
  other_color = self.class.from(other_color)
204
- other_color.r == self.r && other_color.g == self.g && other_color.b == self.b
221
+ other_color.r == self.r && other_color.g == self.g && other_color.b == self.b && other_color.a == self.a
205
222
  end
206
-
223
+
207
224
  # Equal if the brightnesses of the two colors are identical.
208
225
  def ===(other_color)
209
226
  other_color = self.class.from(other_color)
210
227
  other_color.brightness == brightness
211
228
  end
212
-
229
+
213
230
  def r=(value) #:nodoc:
214
231
  @r = value; normalize; end
215
232
  def g=(value) #:nodoc:
216
233
  @g = value; normalize; end
217
234
  def b=(value) #:nodoc:
218
235
  @b = value; normalize; end
219
-
236
+ def a=(value) #:nodoc:
237
+ @a = value; normalize; end
238
+
220
239
  # Outputs a string representation of the color in the desired format.
221
240
  # The available formats are:
222
241
  #
223
242
  # * <tt>:css</tt> - As a CSS hex string (i.e. <tt>#ffffff</tt>) (default)
224
- # * <tt>:css_rgb</tt> - As a CSS RGB value string (i.e. <tt>rgb(255,255,255)</tt>)
243
+ # * <tt>:css_rgb</tt> - As a CSS RGB value string (i.e. <tt>rgb(255, 255, 255)</tt>)
244
+ # * <tt>:css_rgba</tt> - As a CSS RGBA value string (i.e. <tt>rgb(255, 255, 255, 1.0)</tt>)
225
245
  # * <tt>:rgb</tt> - As an RGB triplet (i.e. <tt>1.0, 1.0, 1.0</tt>)
246
+ # * <tt>:rgba</tt> - As an RGBA quadruplet (i.e. <tt>1.0, 1.0, 1.0, 1.0</tt>)
226
247
  def to_s(format=:css)
227
248
  case format
228
249
  when :css
229
250
  "#%.2x%.2x%.2x" % [r, g, b]
230
251
  when :css_rgb
231
- "rgb(%.2f,%.2f,%.2f)" % [r, g, b]
252
+ "rgb(%d, %d, %d)" % [r, g, b]
253
+ when :css_rgba
254
+ "rgba(%d, %d, %d, %.3f)" % [r, g, b, a]
232
255
  when :rgb
233
256
  "%.3f, %.3f, %.3f" % [r / 255, g / 255, b / 255]
257
+ when :rgba
258
+ "%.3f, %.3f, %.3f, %.3f" % [r / 255, g / 255, b / 255, a / 255]
234
259
  end
235
260
  end
236
-
261
+
237
262
  # Returns an array of the hue, saturation and value of the color.
238
263
  # Hue will range from 0-359, hue and saturation will be between 0 and 1.
239
-
264
+
240
265
  def to_hsv
241
266
  red, green, blue = *[r, g, b].collect {|x| x / 255.0}
242
267
  max = [red, green, blue].max
243
268
  min = [red, green, blue].min
244
-
269
+
245
270
  if min == max
246
271
  hue = 0
247
272
  elsif max == red
@@ -251,22 +276,22 @@ module Colorist
251
276
  elsif max == blue
252
277
  hue = 60 * ((red - green) / (max - min)) + 240
253
278
  end
254
-
279
+
255
280
  saturation = (max == 0) ? 0 : (max - min) / max
256
281
  [hue % 360, saturation, max]
257
282
  end
258
-
283
+
259
284
  def inspect
260
- "#<Color #{to_s(:css)}>"
285
+ "#<Color #{to_s(:css_rgba)}>"
261
286
  end
262
-
287
+
263
288
  # Returns the perceived brightness of the provided color on a
264
289
  # scale of 0.0 to 1.0 based on the formula provided. The formulas
265
290
  # available are:
266
291
  #
267
292
  # * <tt>:w3c</tt> - <tt>((r * 299 + g * 587 + b * 114) / 1000 / 255</tt>
268
293
  # * <tt>:standard</tt> - <tt>sqrt(0.241 * r^2 + 0.691 * g^2 + 0.068 * b^2) / 255</tt>
269
- def brightness(formula=:w3c)
294
+ def brightness(formula = :w3c)
270
295
  case formula
271
296
  when :standard
272
297
  Math.sqrt(0.241 * r**2 + 0.691 * g**2 + 0.068 * b**2) / 255
@@ -274,7 +299,7 @@ module Colorist
274
299
  ((r * 299 + g * 587 + b * 114) / 255000.0)
275
300
  end
276
301
  end
277
-
302
+
278
303
  # Contrast this color with another color using the provided formula. The
279
304
  # available formulas are:
280
305
  #
@@ -288,14 +313,14 @@ module Colorist
288
313
  ([self.b, other_color.b].max - [self.b, other_color.b].min)) / 765.0
289
314
  end
290
315
  end
291
-
316
+
292
317
  # Returns the opposite of the current color.
293
318
  def invert
294
- self.class.from_rgb(255 - r, 255 - g, 255 - b)
319
+ self.class.from_rgba(255 - r, 255 - g, 255 - b, a)
295
320
  end
296
-
321
+
297
322
  alias opposite invert
298
-
323
+
299
324
  # Uses a naive formula to generate a gradient between this color and the given color.
300
325
  # Returns the array of colors that make the gradient, including this color and the
301
326
  # target color. By default will return 10 colors, but this can be changed by supplying
@@ -305,69 +330,86 @@ module Colorist
305
330
  red = color_to.r - r
306
331
  green = color_to.g - g
307
332
  blue = color_to.b - b
308
-
333
+
309
334
  result = (1..(steps - 3)).to_a.collect do |step|
310
335
  percentage = step.to_f / (steps - 1)
311
336
  self.class.from_rgb(r + (red * percentage), g + (green * percentage), b + (blue * percentage))
312
337
  end
313
-
338
+
314
339
  # Add first and last colors to result, avoiding uneccessary calculation and rounding errors
315
-
340
+
316
341
  result.unshift(self.dup)
317
342
  result.push(color.dup)
318
343
  result
319
344
  end
320
-
345
+
321
346
  # Converts the current color to grayscale using the brightness
322
347
  # formula provided. See #brightness for a description of the
323
348
  # available formulas.
324
- def to_grayscale(formula=:w3c)
349
+ def to_grayscale(formula = :w3c)
325
350
  b = brightness(formula)
326
351
  self.class.from_rgb(255 * b, 255 * b, 255 * b)
327
352
  end
328
-
353
+
329
354
  # Returns an appropriate text color (either black or white) based on
330
355
  # the brightness of this color. The +threshold+ specifies the brightness
331
356
  # cutoff point.
332
- def text_color(threshold=0.6, formula=:standard)
357
+ def text_color(threshold = 0.6, formula = :standard)
333
358
  brightness(formula) > threshold ? self.class.new(0x000000) : self.class.new(0xffffff)
334
359
  end
335
-
360
+
336
361
  # Adjusts any of H, S, V values with relative values: +opts[:h]+, +opts[:s]+, +opts[:v]+
337
362
  # and returns adjusted value.
338
363
  def adjust(opts = {})
339
364
  unless [:h, :s, :v].any? { |part| opts.include? part }
340
365
  raise ArgumentError, "please specify at least one of :h, :s, or :v options"
341
366
  end
342
-
367
+
343
368
  h, s, v = *self.to_hsv
344
-
345
- h = _within(0, h + opts[:h], 359) if opts[:h]
346
- s = _within(0, s + opts[:s], 1) if opts[:s]
347
- v = _within(0, v + opts[:v], 1) if opts[:v]
348
-
369
+
370
+ h = (h + opts[:h]) % 360 if opts[:h]
371
+ s = _clamp(s + opts[:s], 0..1) if opts[:s]
372
+ v = _clamp(v + opts[:v], 0..1) if opts[:v]
373
+
349
374
  self.class.from_hsv(h, s, v)
350
375
  end
351
-
376
+
377
+ # Adjusts any of R, G, B, or A values with aboslute values: opts[:a], opts[:r], opts[:b], opts[:a]
378
+ # and returns adjusted value.
379
+ def with(opts = {})
380
+ unless [:r, :g, :b, :a].any? { |part| opts.include? part }
381
+ raise ArgumentError, "please specify at least one of :r, :g, :b, or :a options"
382
+ end
383
+
384
+ color = self.dup
385
+
386
+ color.r = opts[:r] if opts[:r]
387
+ color.g = opts[:g] if opts[:g]
388
+ color.b = opts[:b] if opts[:b]
389
+ color.a = opts[:a] if opts[:a]
390
+
391
+ color
392
+ end
393
+
352
394
  protected
353
-
395
+
354
396
  def normalize #:nodoc:
355
- @r = 255 if @r > 255
356
- @g = 255 if @g > 255
357
- @b = 255 if @b > 255
358
- @r = 0 if @r < 0
359
- @g = 0 if @g < 0
360
- @b = 0 if @b < 0
397
+ @r = _clamp(@r, (0..255)) unless (0..255).include? @r
398
+ @g = _clamp(@g, (0..255)) unless (0..255).include? @g
399
+ @b = _clamp(@b, (0..255)) unless (0..255).include? @b
400
+ @a = _clamp(@a, (0.0..1.0)) unless (0.0..1.0).include? @a
361
401
  end
362
-
363
- def _within(min, value, max)
364
- if value < min
365
- min
366
- elsif value > max
367
- max
402
+
403
+ def _clamp(value, range)
404
+ if value < range.first
405
+ range.first
406
+ elsif value > range.last
407
+ range.last
368
408
  else
369
409
  value
370
410
  end
371
411
  end
372
- end
373
- end
412
+
413
+ end # Color
414
+
415
+ end # Colorist
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slippyd-colorist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Bleigh
@@ -11,12 +11,12 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2009-06-29 00:00:00 -07:00
14
+ date: 2009-10-12 00:00:00 -07:00
15
15
  default_executable:
16
16
  dependencies: []
17
17
 
18
18
  description: Colorist is a library built to handle the easy conversion and manipulation of colors with a special emphasis on W3C standards and CSS-style hex color notation.
19
- email: michael@intridea.com
19
+ email: slippyd-colorist@6bitt.com
20
20
  executables: []
21
21
 
22
22
  extensions: []
@@ -26,17 +26,19 @@ extra_rdoc_files:
26
26
  - README.rdoc
27
27
  files:
28
28
  - .gitignore
29
+ - .specification
29
30
  - CHANGELOG.rdoc
30
31
  - MIT_LICENSE.rdoc
31
32
  - README.rdoc
32
33
  - Rakefile
33
34
  - VERSION
34
- - colorist.gemspec
35
35
  - lib/colorist.rb
36
36
  - lib/colorist/color.rb
37
37
  - lib/colorist/core_extensions.rb
38
- has_rdoc: false
38
+ has_rdoc: true
39
39
  homepage: http://github.com/slippyd/colorist
40
+ licenses: []
41
+
40
42
  post_install_message:
41
43
  rdoc_options:
42
44
  - --main
@@ -58,9 +60,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
60
  requirements: []
59
61
 
60
62
  rubyforge_project:
61
- rubygems_version: 1.2.0
63
+ rubygems_version: 1.3.5
62
64
  signing_key:
63
- specification_version: 4
65
+ specification_version: 3
64
66
  summary: A library built to handle the easy conversion and simple manipulation of colors.
65
67
  test_files: []
66
68
 
@@ -1,43 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- Gem::Specification.new do |s|
4
- s.name = 'colorist'
5
- s.version = '0.0.4'
6
-
7
- s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Michael Bleigh", "oleg dashevskii", "Slippy Douglas"]
9
- s.date = '2009-06-29'
10
- s.description = %q{Colorist is a library built to handle the easy conversion and manipulation of colors with a special emphasis on W3C standards and CSS-style hex color notation.}
11
- s.email = %q<michael@intridea.com>
12
- s.extra_rdoc_files = [
13
- "MIT_LICENSE.rdoc",
14
- "README.rdoc"
15
- ]
16
- s.files = [
17
- ".gitignore",
18
- "CHANGELOG.rdoc",
19
- "MIT_LICENSE.rdoc",
20
- "README.rdoc",
21
- "Rakefile",
22
- "VERSION",
23
- "colorist.gemspec",
24
- "lib/colorist.rb",
25
- "lib/colorist/color.rb",
26
- "lib/colorist/core_extensions.rb"
27
- ]
28
- s.homepage = %q<http://github.com/slippyd/colorist>
29
- s.rdoc_options = ["--main", "README.rdoc"]
30
- s.require_paths = ["lib"]
31
- s.rubygems_version = '1.3.3'
32
- s.summary = %q{A library built to handle the easy conversion and simple manipulation of colors.}
33
-
34
- if s.respond_to? :specification_version then
35
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
36
- s.specification_version = 4
37
-
38
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
39
- else
40
- end
41
- else
42
- end
43
- end