color-generator 0.0.1
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.
- data/.gitignore +6 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +27 -0
- data/Rakefile +16 -0
- data/USAGE +1 -0
- data/color-generator.gemspec +21 -0
- data/lib/color-generator.rb +75 -0
- data/lib/color-generator/version.rb +3 -0
- metadata +88 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Open North Inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Ruby Color Generator
|
2
|
+
|
3
|
+
[](https://gemnasium.com/opennorth/color-generator)
|
4
|
+
[](https://codeclimate.com/github/opennorth/color-generator)
|
5
|
+
|
6
|
+
This gem randomly generates very distinct colors with consistent lightness and saturation.
|
7
|
+
|
8
|
+
If you are using these colors as background colors, consistent lightness lets you use the same foreground color for each. Consistent saturation lets you avoid mixing pastels with vibrant colors, etc.
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
require 'color-generator'
|
13
|
+
generator = ColorGenerator.new 0.3, 1.0
|
14
|
+
color1 = generator.create
|
15
|
+
# => "f7b3ff"
|
16
|
+
color2 = generator.create
|
17
|
+
# => "b3ffe0"
|
18
|
+
|
19
|
+
## Acknowledgements
|
20
|
+
|
21
|
+
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.
|
22
|
+
|
23
|
+
## Bugs? Questions?
|
24
|
+
|
25
|
+
This gem's main repository is on GitHub: [http://github.com/opennorth/color-generator](http://github.com/opennorth/color-generator), where your contributions, forks, bug reports, feature requests, and feedback are greatly welcomed.
|
26
|
+
|
27
|
+
Copyright (c) 2012 Open North Inc., released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
|
7
|
+
task :default => :spec
|
8
|
+
|
9
|
+
begin
|
10
|
+
require 'yard'
|
11
|
+
YARD::Rake::YardocTask.new
|
12
|
+
rescue LoadError
|
13
|
+
task :yard do
|
14
|
+
abort 'YARD is not available. In order to run yard, you must: gem install yard'
|
15
|
+
end
|
16
|
+
end
|
data/USAGE
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
See README.md for full usage details.
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "color-generator/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "color-generator"
|
7
|
+
s.version = ColorGenerator::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Open North"]
|
10
|
+
s.email = ["info@opennorth.ca"]
|
11
|
+
s.homepage = "http://github.com/opennorth/color-generator"
|
12
|
+
s.summary = %q{Randomly generate distinct colors with consistent lightness and saturation}
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_development_dependency('rspec', '~> 2.10')
|
20
|
+
s.add_development_dependency('rake')
|
21
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
class ColorGenerator
|
2
|
+
GOLDEN_RATIO_CONJUGATE = 0.618033988749895
|
3
|
+
|
4
|
+
attr_reader :hue, :saturation, :value
|
5
|
+
|
6
|
+
# Initializes a color generator.
|
7
|
+
#
|
8
|
+
# @param [Float,Integer] saturation saturation in the interval [0, 1]
|
9
|
+
# @param [Float,Integer] value value in the interval [0, 1]
|
10
|
+
def initialize(saturation, value)
|
11
|
+
@hue = rand
|
12
|
+
@saturation = saturation.to_f
|
13
|
+
@value = value.to_f
|
14
|
+
end
|
15
|
+
|
16
|
+
# Generates a random color.
|
17
|
+
#
|
18
|
+
# @return [String] an RGB hex triplet
|
19
|
+
def create
|
20
|
+
@hue += GOLDEN_RATIO_CONJUGATE
|
21
|
+
@hue %= 1
|
22
|
+
'%02x%02x%02x' % self.class.rgb_from_hsv(hue, saturation, value)
|
23
|
+
end
|
24
|
+
|
25
|
+
# Converts a color from HSV to RGB.
|
26
|
+
#
|
27
|
+
# @param [Float] h hue in the interval [0, 1]
|
28
|
+
# @param [Float] s saturation in the interval [0, 1]
|
29
|
+
# @param [Float] v value in the interval [0, 1]
|
30
|
+
# @return [Array] an RGB decimal triplet
|
31
|
+
#
|
32
|
+
# @see http://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB
|
33
|
+
def self.rgb_from_hsv(h, s, v)
|
34
|
+
c = v * s
|
35
|
+
m = v - c
|
36
|
+
h_prime = h * 6
|
37
|
+
x_prime = v - c * (h_prime % 2 - 1).abs
|
38
|
+
|
39
|
+
# Unlike the Wikipedia article, we add m right away. This reduces the
|
40
|
+
# number of operations to perform. For example, for H' < 1:
|
41
|
+
#
|
42
|
+
# R = R1 + m
|
43
|
+
# R = C + m
|
44
|
+
# R = C + V - C
|
45
|
+
# R = V
|
46
|
+
#
|
47
|
+
# G = G1 + m
|
48
|
+
# G = X + m
|
49
|
+
# G = C (1 - |H' mod 2 - 1|) + m
|
50
|
+
# G = C (1 - |H' mod 2 - 1|) + V - C
|
51
|
+
# G = C - C |H' mod 2 - 1| + V - C
|
52
|
+
# G = V - C |H' mod 2 - 1|
|
53
|
+
#
|
54
|
+
# B = B1 + m
|
55
|
+
# B = 0 + m
|
56
|
+
# B = m
|
57
|
+
#
|
58
|
+
# The blog post above calculates two X values, for when H' mod 2 is
|
59
|
+
# greater or less than 1.
|
60
|
+
case h_prime.to_i
|
61
|
+
when 0 # 0 <= H' < 1
|
62
|
+
[v, x_prime, m]
|
63
|
+
when 1 # 1 <= H' < 2
|
64
|
+
[x_prime, v, m]
|
65
|
+
when 2 # 2 <= H' < 3
|
66
|
+
[m, v, x_prime]
|
67
|
+
when 3 # 3 <= H' < 4
|
68
|
+
[m, x_prime, v]
|
69
|
+
when 4 # 4 <= H' < 5
|
70
|
+
[x_prime, m, v]
|
71
|
+
else # 5 <= H' < 6
|
72
|
+
[v, m, x_prime]
|
73
|
+
end.map{|value| (value * 255).round}
|
74
|
+
end
|
75
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: color-generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Open North
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.10'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.10'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description:
|
47
|
+
email:
|
48
|
+
- info@opennorth.ca
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- .travis.yml
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- USAGE
|
60
|
+
- color-generator.gemspec
|
61
|
+
- lib/color-generator.rb
|
62
|
+
- lib/color-generator/version.rb
|
63
|
+
homepage: http://github.com/opennorth/color-generator
|
64
|
+
licenses: []
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.8.24
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Randomly generate distinct colors with consistent lightness and saturation
|
87
|
+
test_files: []
|
88
|
+
has_rdoc:
|