color-generator 0.0.2 → 0.0.3

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,4 @@
1
+ --no-private
2
+ --hide-void-return
3
+ --embed-mixin ClassMethods
4
+ --markup=markdown
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in scraperwiki-api.gemspec
3
+ # Specify your gem's dependencies in the gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -14,19 +14,25 @@ If you are using these colors as background colors, consistent lightness lets yo
14
14
  Generate colors using the HSL color representation:
15
15
 
16
16
  generator = ColorGenerator.new saturation: 0.3, lightness: 0.75
17
- color1 = generator.create
17
+ color1 = generator.create_hex
18
18
  # => "cfd2ac"
19
- color2 = generator.create
19
+ color2 = generator.create_hex
20
20
  # => "cbacd2"
21
21
 
22
22
  Generate colors using the HSV color representation:
23
23
 
24
24
  generator = ColorGenerator.new saturation: 0.3, value: 1.0
25
- color1 = generator.create
25
+ color1 = generator.create_hex
26
26
  # => "f7b3ff"
27
- color2 = generator.create
27
+ color2 = generator.create_hex
28
28
  # => "b3ffe0"
29
29
 
30
+ If you want to make color generation repeatable, set a seed for the pseudorandom number generator:
31
+
32
+ generator = ColorGenerator.new saturation: 0.3, value: 1.0, seed: 12345
33
+
34
+ If you prefer a decimal RGB value, call `create_rgb` instead of `create_hex`.
35
+
30
36
  ## Acknowledgements
31
37
 
32
38
  Thanks to Martin Ankerl for his [blog post](http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/) which inspired this gem.
@@ -11,7 +11,9 @@ class ColorGenerator
11
11
  # sets the color representation to HSL
12
12
  # @option opts [Float,Integer] :value value in the interval [0, 1], sets the
13
13
  # color representation to HSV
14
+ # @option opts [Integer] :seed seed for the pseudorandom number generator
14
15
  def initialize(opts = {})
16
+ srand(opts[:seed]) if opts.key?(:seed)
15
17
  @hue = rand
16
18
  @saturation = opts[:saturation].to_f
17
19
  if opts.has_key? :lightness
@@ -23,17 +25,26 @@ class ColorGenerator
23
25
  end
24
26
  end
25
27
 
26
- # Generates a random color.
28
+ # Formats a random color as an RGB hex triplet.
27
29
  #
28
30
  # @return [String] an RGB hex triplet
29
- def create
31
+ def create_hex
32
+ '%02x%02x%02x' % create_rgb
33
+ end
34
+
35
+ # For backwards compatibility.
36
+ alias_method :create, :create_hex
37
+
38
+ # Generates a random color as an RGB decimal triplet.
39
+ #
40
+ # @return [Array] an RGB decimal triplet
41
+ def create_rgb
30
42
  @hue = (hue + GOLDEN_RATIO_CONJUGATE) % 1
31
- color = if hsl?
43
+ if hsl?
32
44
  self.class.rgb_from_hsl hue, saturation, lightness
33
45
  else
34
46
  self.class.rgb_from_hsv hue, saturation, value
35
47
  end
36
- '%02x%02x%02x' % color
37
48
  end
38
49
 
39
50
  # @return [Boolean] whether the color representation is HSL
@@ -1,3 +1,3 @@
1
1
  module ColorGenerator
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: color-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-12 00:00:00.000000000 Z
12
+ date: 2013-02-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -52,6 +52,7 @@ extra_rdoc_files: []
52
52
  files:
53
53
  - .gitignore
54
54
  - .travis.yml
55
+ - .yardopts
55
56
  - Gemfile
56
57
  - LICENSE
57
58
  - README.md