gradients 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +15 -0
- data/Rakefile +26 -0
- data/ext/gem_rake.rb +126 -0
- data/gradients.gemspec +36 -0
- data/lib/gradients.rb +24 -0
- data/lib/gradients/color.rb +26 -0
- data/lib/gradients/version.rb +3 -0
- data/spec/gradients_spec.rb +69 -0
- data/spec/spec.opts +7 -0
- data/spec/spec_helper.rb +3 -0
- metadata +105 -0
data/README.rdoc
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
= Gradients
|
2
|
+
|
3
|
+
Make fun gradients, and then find colours within them:
|
4
|
+
|
5
|
+
Gradients.new('#000000', '#ffffff').at(0.5).to_s
|
6
|
+
=> '808080'
|
7
|
+
Gradients.new('#000000', '#ffffff').at(0.5).to_s(:whex)
|
8
|
+
=> '#808080'
|
9
|
+
|
10
|
+
Or, add lots of colours:
|
11
|
+
|
12
|
+
Gradients.new('#000000', '#ff0000', #ffffff').at(0.75).to_s(:whex)
|
13
|
+
=> '#ff8080'
|
14
|
+
|
15
|
+
And so forth...
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
require 'spec/rake/spectask'
|
4
|
+
Spec::Rake::SpecTask.new(:spec) do |t|
|
5
|
+
t.spec_opts ||= []
|
6
|
+
t.spec_opts << "--options" << "spec/spec.opts"
|
7
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
8
|
+
end
|
9
|
+
|
10
|
+
begin
|
11
|
+
require 'code_stats'
|
12
|
+
CodeStats::Tasks.new
|
13
|
+
rescue LoadError
|
14
|
+
end
|
15
|
+
|
16
|
+
require 'rake/rdoctask'
|
17
|
+
desc "Generate documentation"
|
18
|
+
Rake::RDocTask.new do |rd|
|
19
|
+
rd.main = "README.rdoc"
|
20
|
+
rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
|
21
|
+
rd.rdoc_dir = 'rdoc'
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'ext/gem_rake'
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
data/ext/gem_rake.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
module Bundler
|
2
|
+
class GemHelper
|
3
|
+
|
4
|
+
def self.install_tasks
|
5
|
+
dir = caller.find{|c| /Rakefile:/}[/^(.*?)\/Rakefile:/, 1]
|
6
|
+
GemHelper.new(dir).install
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_reader :spec_path, :base, :name
|
10
|
+
|
11
|
+
def initialize(base, name = nil)
|
12
|
+
@base = base
|
13
|
+
@name = name || interpolate_name
|
14
|
+
@spec_path = File.join(@base, "#{@name}.gemspec")
|
15
|
+
end
|
16
|
+
|
17
|
+
def install
|
18
|
+
desc 'Build your gem into the pkg directory'
|
19
|
+
task 'build' do
|
20
|
+
build_gem
|
21
|
+
end
|
22
|
+
|
23
|
+
desc 'Install your gem into the pkg directory'
|
24
|
+
task 'install' do
|
25
|
+
install_gem
|
26
|
+
end
|
27
|
+
|
28
|
+
desc 'Push your gem to rubygems'
|
29
|
+
task 'push' do
|
30
|
+
push_gem
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def build_gem
|
35
|
+
file_name = nil
|
36
|
+
sh("gem build #{spec_path}") {
|
37
|
+
file_name = File.basename(built_gem_path)
|
38
|
+
FileUtils.mkdir_p(File.join(base, 'pkg'))
|
39
|
+
FileUtils.mv(built_gem_path, 'pkg')
|
40
|
+
}
|
41
|
+
File.join(base, 'pkg', file_name)
|
42
|
+
end
|
43
|
+
|
44
|
+
def install_gem
|
45
|
+
built_gem_path = build_gem
|
46
|
+
sh("gem install #{built_gem_path}")
|
47
|
+
end
|
48
|
+
|
49
|
+
def push_gem
|
50
|
+
guard_clean
|
51
|
+
guard_already_tagged
|
52
|
+
tag_version {
|
53
|
+
git_push
|
54
|
+
rubygem_push(build_gem)
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
protected
|
59
|
+
def rubygem_push(path)
|
60
|
+
sh("gem push #{path}")
|
61
|
+
end
|
62
|
+
|
63
|
+
def built_gem_path
|
64
|
+
Dir[File.join(base, "#{name}-*.gem")].sort_by{|f| File.mtime(f)}.last
|
65
|
+
end
|
66
|
+
|
67
|
+
def interpolate_name
|
68
|
+
gemspecs = Dir[File.join(base, "*.gemspec")]
|
69
|
+
raise "Unable to determine name from existing gemspec." unless gemspecs.size == 1
|
70
|
+
|
71
|
+
File.basename(gemspecs.first)[/^(.*)\.gemspec$/, 1]
|
72
|
+
end
|
73
|
+
|
74
|
+
def git_push
|
75
|
+
sh "git push --all"
|
76
|
+
sh "git push --tags"
|
77
|
+
end
|
78
|
+
|
79
|
+
def guard_already_tagged
|
80
|
+
sh('git tag').split(/\n/).include?(current_version_tag) and raise("This tag has already been committed to the repo.")
|
81
|
+
end
|
82
|
+
|
83
|
+
def guard_clean
|
84
|
+
clean? or raise("There are files that need to be committed first.")
|
85
|
+
end
|
86
|
+
|
87
|
+
def clean?
|
88
|
+
sh("git ls-files -dm").split("\n").size.zero?
|
89
|
+
end
|
90
|
+
|
91
|
+
def tag_version
|
92
|
+
sh "git tag #{current_version_tag}"
|
93
|
+
yield if block_given?
|
94
|
+
rescue
|
95
|
+
sh "git tag -d #{current_version_tag}"
|
96
|
+
raise
|
97
|
+
end
|
98
|
+
|
99
|
+
def current_version
|
100
|
+
raise("Version file could not be found at #{version_file_path}") unless File.exist?(version_file_path)
|
101
|
+
File.read(version_file_path)[/V(ERSION|ersion)\s*=\s*(["'])(.*?)\2/, 3]
|
102
|
+
end
|
103
|
+
|
104
|
+
def version_file_path
|
105
|
+
File.join(base, 'lib', name, 'version.rb')
|
106
|
+
end
|
107
|
+
|
108
|
+
def current_version_tag
|
109
|
+
"v#{current_version}"
|
110
|
+
end
|
111
|
+
|
112
|
+
def sh(cmd, &block)
|
113
|
+
output, code = sh_with_code(cmd, &block)
|
114
|
+
code == 0 ? output : raise(output)
|
115
|
+
end
|
116
|
+
|
117
|
+
def sh_with_code(cmd, &block)
|
118
|
+
output = ''
|
119
|
+
Dir.chdir(base) {
|
120
|
+
output = `#{cmd}`
|
121
|
+
block.call if block and $? == 0
|
122
|
+
}
|
123
|
+
[output, $?]
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
data/gradients.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), 'lib', 'gradients', 'version')
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'gradients'
|
7
|
+
s.version = Gradients::VERSION
|
8
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
9
|
+
s.authors = ["Joshua Hull"]
|
10
|
+
s.date = '2010-07-31'
|
11
|
+
s.description = s.summary = "Colours! Colours! The creeping chaos fucks my mind. Generate gradients because colors are fun."
|
12
|
+
s.email = %q{joshbuddy@gmail.com}
|
13
|
+
s.extra_rdoc_files = ['README.rdoc']
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.homepage = %q{http://github.com/joshbuddy/gradients}
|
16
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubygems_version = %q{1.3.7}
|
19
|
+
s.test_files = `git ls-files`.split("\n").select{|f| f =~ /^spec/}
|
20
|
+
s.rubyforge_project = 'gradients'
|
21
|
+
|
22
|
+
# dependencies
|
23
|
+
s.add_development_dependency 'rspec'
|
24
|
+
s.add_development_dependency 'rake'
|
25
|
+
|
26
|
+
if s.respond_to? :specification_version then
|
27
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
28
|
+
s.specification_version = 3
|
29
|
+
|
30
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
31
|
+
else
|
32
|
+
end
|
33
|
+
else
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
data/lib/gradients.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'gradients/version'
|
2
|
+
require 'gradients/color'
|
3
|
+
|
4
|
+
class Gradients
|
5
|
+
def initialize(*colors)
|
6
|
+
if colors.size < 2
|
7
|
+
raise "you probably want at least 2 colors"
|
8
|
+
end
|
9
|
+
@colors = colors.map{|c| Color.parse(c)}
|
10
|
+
end
|
11
|
+
|
12
|
+
def at(position)
|
13
|
+
raise unless (0.0..1.0).include?(position)
|
14
|
+
start_color_index = (position * (@colors.size - 1)).to_i - (position == 1 ? 1 : 0)
|
15
|
+
start_color = @colors[start_color_index]
|
16
|
+
end_color = @colors[start_color_index + 1]
|
17
|
+
mapped_position = (position * (@colors.size - 1)) - start_color_index
|
18
|
+
Color.new(
|
19
|
+
(end_color.r - start_color.r) * mapped_position + start_color.r,
|
20
|
+
(end_color.g - start_color.g) * mapped_position + start_color.g,
|
21
|
+
(end_color.b - start_color.b) * mapped_position + start_color.b
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Gradients
|
2
|
+
class Color
|
3
|
+
attr_accessor :r, :g, :b
|
4
|
+
def self.parse(val)
|
5
|
+
case val
|
6
|
+
when /^#?[0-9a-f]{6}$/i
|
7
|
+
new(*val[-6, 6].map{|a|a.scan(/../).map{|b|(b.hex.to_f / 255)}}.flatten)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(r, g, b)
|
12
|
+
@r, @g, @b = r, g, b
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s(format = :hex)
|
16
|
+
case format
|
17
|
+
when :whex
|
18
|
+
"##{to_s(:hex)}"
|
19
|
+
when :hex
|
20
|
+
sprintf("%02x%02x%02x", (@r * 255).round, (@g * 255).round, (@b * 255).round)
|
21
|
+
else
|
22
|
+
raise 'format not supported'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
describe "Gradients" do
|
3
|
+
context "should calculate a gradient of two colors" do
|
4
|
+
before(:each) do
|
5
|
+
@gradient = Gradients.new('#000000', '#ffffff')
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should find the half way point" do
|
9
|
+
@gradient.at(0.5).to_s.should == '808080'
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should find the beginning" do
|
13
|
+
@gradient.at(0.0).to_s.should == '000000'
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should find the end" do
|
17
|
+
@gradient.at(1.0).to_s.should == 'ffffff'
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should find an arbitrary position" do
|
21
|
+
@gradient.at(0.76).to_s.should == 'c2c2c2'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "should calculate a gradient of two weird colors" do
|
26
|
+
before(:each) do
|
27
|
+
@gradient = Gradients.new('#f56510', '#a09981')
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should find the half way point" do
|
31
|
+
@gradient.at(0.5).to_s.should == 'cb7f49'
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should find the beginning" do
|
35
|
+
@gradient.at(0.0).to_s.should == 'f56510'
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should find the end" do
|
39
|
+
@gradient.at(1.0).to_s.should == 'a09981'
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should find an arbitrary position" do
|
43
|
+
@gradient.at(0.76).to_s.should == 'b48d66'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "should calculate a gradient of three weird colors" do
|
48
|
+
before(:each) do
|
49
|
+
@gradient = Gradients.new('#f56510', '#a09981', '#000000')
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should find the half way point" do
|
53
|
+
@gradient.at(0.5).to_s.should == 'a09981'
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should find the beginning" do
|
57
|
+
@gradient.at(0.0).to_s.should == 'f56510'
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should find the end" do
|
61
|
+
@gradient.at(1.0).to_s.should == '000000'
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should find an arbitrary position" do
|
65
|
+
@gradient.at(0.76).to_s.should == '4d493e'
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gradients
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Joshua Hull
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-31 00:00:00 -07:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: rspec
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rake
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
49
|
+
description: Colours! Colours! The creeping chaos fucks my mind. Generate gradients because colors are fun.
|
50
|
+
email: joshbuddy@gmail.com
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files:
|
56
|
+
- README.rdoc
|
57
|
+
files:
|
58
|
+
- README.rdoc
|
59
|
+
- Rakefile
|
60
|
+
- ext/gem_rake.rb
|
61
|
+
- gradients.gemspec
|
62
|
+
- lib/gradients.rb
|
63
|
+
- lib/gradients/color.rb
|
64
|
+
- lib/gradients/version.rb
|
65
|
+
- spec/gradients_spec.rb
|
66
|
+
- spec/spec.opts
|
67
|
+
- spec/spec_helper.rb
|
68
|
+
has_rdoc: true
|
69
|
+
homepage: http://github.com/joshbuddy/gradients
|
70
|
+
licenses: []
|
71
|
+
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options:
|
74
|
+
- --charset=UTF-8
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
hash: 3
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
hash: 3
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project: gradients
|
98
|
+
rubygems_version: 1.3.7
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: Colours! Colours! The creeping chaos fucks my mind. Generate gradients because colors are fun.
|
102
|
+
test_files:
|
103
|
+
- spec/gradients_spec.rb
|
104
|
+
- spec/spec.opts
|
105
|
+
- spec/spec_helper.rb
|