rack-golden_frill 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in golden_frill.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ golden_frill (0.1.0)
5
+ chunky_png (>= 0.10.4)
6
+ rack (>= 1.0)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ chunky_png (0.10.4)
12
+ rack (1.2.1)
13
+ test-unit (2.1.1)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ bundler (>= 1.0.0)
20
+ chunky_png (>= 0.10.4)
21
+ golden_frill!
22
+ rack (>= 1.0)
23
+ test-unit
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.test_files = FileList['test/test*.rb']
8
+ t.verbose = true
9
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/golden_frill/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "rack-golden_frill"
6
+ s.version = GoldenFrill::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Alexander Bartlow"]
9
+ s.email = ["bartlowa@gmail.com"]
10
+ s.homepage = "http://rubygems.org/gems/golden_frill"
11
+ s.summary = "Simple Rack-based frils for your website"
12
+ s.description = "Generates on-demand 'ribbony' links that protrude to the left or right of active links"
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "rack-golden_frill"
16
+
17
+ s.add_dependency "chunky_png", ">= 0.10.4"
18
+ s.add_dependency "rack", ">= 1.0"
19
+ s.add_development_dependency "bundler", ">= 1.0.0"
20
+ s.add_development_dependency "test-unit"
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
24
+ s.require_path = 'lib'
25
+ end
@@ -0,0 +1,3 @@
1
+ module GoldenFrill
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,63 @@
1
+ require 'chunky_png'
2
+ module GoldenFrill
3
+ GoldenRatio = 1.618
4
+ # Requires:
5
+ # output_path : the path at which to output the requested image
6
+ # width: the width the rectangle above the frill
7
+ # height: the height of the rectangle above the frill
8
+ # base_color: the color of the rectangle above the frill
9
+ #
10
+ # Optional:
11
+ # frill_color: the color of the frill. If not provided, will be a darker color than the base
12
+ def self.run!(opts)
13
+ width, height, base_color, output_path = opts[:width], opts[:height], opts[:base_color], opts[:output_path]
14
+
15
+ bot_height = height.to_i + (width.to_f * GoldenRatio).to_i
16
+ png = ChunkyPNG::Image.new(width.to_i, bot_height)
17
+
18
+ rc = ChunkyPNG::Color.from_hex(base_color)
19
+ (1..(height - 1)).each do |h|
20
+ (1..(width - 1)).each do |w|
21
+ png[w,h] = rc
22
+ end
23
+ end
24
+
25
+ ribbon_color = if opts[:frill_color]
26
+ ChunkyPNG::Color.from_hex(opts[:frill_color])
27
+ else
28
+ self.darken_color(rc)
29
+ end
30
+
31
+ slope = (height - bot_height).to_f / (1 - width).to_f
32
+
33
+ (height..(bot_height - 1)).each do |h|
34
+ (1..(width - 1)).each do |w|
35
+ on_line_slope = (w * slope) + height
36
+ next if on_line_slope.to_i + 1 < h
37
+
38
+ alpha_scale = case (on_line_slope.to_i + 1) - h
39
+ when 1 then 0.8
40
+ when 0 then 0.5
41
+ else
42
+ 1.0
43
+ end
44
+ a = (0xff.to_f * alpha_scale).to_i
45
+
46
+ new_color = ((w + 3) - (width - 1)).times.inject(ribbon_color){|rc, fade|
47
+ darken_color(rc, 0.95)
48
+ }
49
+ new_color = ChunkyPNG::Color.fade(new_color, a)
50
+
51
+ # fade the color to a black gradient if in the last couple pixels of the ribbon
52
+ png[w,h] = new_color
53
+ end
54
+ end
55
+
56
+ png.save(output_path)
57
+ end
58
+
59
+ def self.darken_color(c, scale = 0.8)
60
+ r, g, b = %w{r g b}.map{|x| (ChunkyPNG::Color.send(x, c).to_f * scale).to_i}
61
+ ChunkyPNG::Color.rgb(r,g,b)
62
+ end
63
+ end
@@ -0,0 +1,30 @@
1
+ module Rack
2
+ class GoldenFrill
3
+ def initialize app, output_path
4
+ @app = app
5
+ @root = ::File.expand_path(output_path)
6
+ raise ArgumentError, "#{output_path} is not a directory" unless ::File.directory?(@root)
7
+ end
8
+
9
+ def call env
10
+ unless env["PATH_INFO"] =~ /#{@root}/
11
+ return @app.call(env)
12
+ end
13
+
14
+ request = ::Rack::Request.new(env)
15
+ command = env["PATH_INFO"].match(/#{@root}\/frill_(.*)\.png/i)[1]
16
+
17
+ image_path = ::File.join(@root,"frill_#{command}.png")
18
+ if ::File.exist?(image_path)
19
+ return ::Rack::File.new(@root).call(env)
20
+ end
21
+
22
+ opts = { :output_path => image_path }
23
+ opts[:base_color], opts[:width], opts[:height], opts[:frill_color] = command.split('.')
24
+ ::GoldenFrill.new(opts).run!
25
+
26
+ # Redirect to this URL since it will now be served.
27
+ return [301, {'Location' => request.url}, 'Redirecting to created image.']
28
+ end
29
+ end
30
+ end
@@ -0,0 +1 @@
1
+ *.png
@@ -0,0 +1,16 @@
1
+ require 'test/unit'
2
+ require 'golden_frill'
3
+ class TestFrills < Test::Unit::TestCase
4
+ OUTPUT_ROOT = ::File.expand_path('./images', File::dirname(__FILE__))
5
+ def test_output
6
+ output_path = File.join(OUTPUT_ROOT,"rectangle.png")
7
+ GoldenFrill.run!({
8
+ :output_path => output_path,
9
+ :base_color => "d4412d",
10
+ :width => 14,
11
+ :height => 36
12
+ })
13
+
14
+ assert(::File.exists?(output_path))
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-golden_frill
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Alexander Bartlow
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-20 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: chunky_png
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 10
30
+ - 4
31
+ version: 0.10.4
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: rack
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 1
43
+ - 0
44
+ version: "1.0"
45
+ type: :runtime
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ prerelease: false
50
+ requirement: &id003 !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 1
56
+ - 0
57
+ - 0
58
+ version: 1.0.0
59
+ type: :development
60
+ version_requirements: *id003
61
+ - !ruby/object:Gem::Dependency
62
+ name: test-unit
63
+ prerelease: false
64
+ requirement: &id004 !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ type: :development
72
+ version_requirements: *id004
73
+ description: Generates on-demand 'ribbony' links that protrude to the left or right of active links
74
+ email:
75
+ - bartlowa@gmail.com
76
+ executables: []
77
+
78
+ extensions: []
79
+
80
+ extra_rdoc_files: []
81
+
82
+ files:
83
+ - .gitignore
84
+ - Gemfile
85
+ - Gemfile.lock
86
+ - Rakefile
87
+ - golden_frill.gemspec
88
+ - lib/golden_frill.rb
89
+ - lib/golden_frill/version.rb
90
+ - lib/rack/golden_frill.rb
91
+ - test/images/.gitignore
92
+ - test/test_frills.rb
93
+ has_rdoc: true
94
+ homepage: http://rubygems.org/gems/golden_frill
95
+ licenses: []
96
+
97
+ post_install_message:
98
+ rdoc_options: []
99
+
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ segments:
107
+ - 0
108
+ version: "0"
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ segments:
114
+ - 1
115
+ - 3
116
+ - 6
117
+ version: 1.3.6
118
+ requirements: []
119
+
120
+ rubyforge_project: rack-golden_frill
121
+ rubygems_version: 1.3.6
122
+ signing_key:
123
+ specification_version: 3
124
+ summary: Simple Rack-based frils for your website
125
+ test_files: []
126
+