color-generator 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.yardopts +4 -0
- data/Gemfile +1 -1
- data/README.md +10 -4
- data/lib/color-generator.rb +15 -4
- data/lib/color-generator/version.rb +1 -1
- metadata +3 -2
data/.yardopts
ADDED
data/Gemfile
CHANGED
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.
|
17
|
+
color1 = generator.create_hex
|
18
18
|
# => "cfd2ac"
|
19
|
-
color2 = generator.
|
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.
|
25
|
+
color1 = generator.create_hex
|
26
26
|
# => "f7b3ff"
|
27
|
-
color2 = generator.
|
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.
|
data/lib/color-generator.rb
CHANGED
@@ -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
|
-
#
|
28
|
+
# Formats a random color as an RGB hex triplet.
|
27
29
|
#
|
28
30
|
# @return [String] an RGB hex triplet
|
29
|
-
def
|
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
|
-
|
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
|
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.
|
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:
|
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
|