rgba_png 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 72fcf43d2e852100e29d2ee5c6c7ee3fd6cb4ce6
4
+ data.tar.gz: 1105f3c222293381f9098700597f37b3ef53ca64
5
+ SHA512:
6
+ metadata.gz: 3ea1aa12f46a49321d42b030808ce72a1844a892ca0b5df69bfa9d8b92b991dd0239a148088483f22e13a57b548aee81fe7f0628f9d4ffd3c7498e0d0926ed7a
7
+ data.tar.gz: 95977f8b0503fc320fe9480124e0c397c4c106dffb573c37c7c3144f2f8c5e74e5bf9ce49f45b00781a518ddce2544515d4d8f2f5aff42657029d8eae8090d54
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ # IDE
20
+ .idea/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rgba_png.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Nelson Rodrigues
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ rgba-png
2
+ ===========
3
+
4
+ *Feed it rgba and it spews images!*
5
+
6
+ Parse a source file (.css, .scss, etc..) for rgba color definitions and generates png images accordingly.
7
+
8
+
9
+ ## Usage (command line)
10
+
11
+ There's two parameters: **path to the source file** and **path to images destination**.
12
+
13
+ Example:
14
+
15
+ rgba-png path/to/file.css path/to/images/folder
16
+
17
+ It assumes that CSS rules are defined in their own line.
18
+
19
+ ## Images
20
+
21
+ Images are named after the color properties, for example, the color below:
22
+
23
+ rgba(0, 252, 255, 0.8)
24
+
25
+ Will generate the following file:
26
+
27
+ 0-252-255-0.8.png
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ end
7
+
8
+ desc "Run test"
9
+ task :default => :test
data/bin/rgba-png ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rgba_png'
4
+
5
+ RgbaPng.init(ARGV[0], ARGV[1])
@@ -0,0 +1,3 @@
1
+ module RgbaPng
2
+ VERSION = "1.0"
3
+ end
data/lib/rgba_png.rb ADDED
@@ -0,0 +1,63 @@
1
+ require "oily_png"
2
+
3
+ class RgbaPng
4
+
5
+ def self.init(file, destination)
6
+ if !file || !destination
7
+ puts 'Usage: rgba-png sourcefile destination'
8
+
9
+ return
10
+ end
11
+
12
+ colors = parse(file)
13
+ create_files colors, destination
14
+
15
+ puts colors.uniq.length.to_s + ' image(s) created!'
16
+ end
17
+
18
+ # Creates colors array from source file
19
+ def self.parse(file)
20
+ colors = []
21
+ rgba = /rgba\((\d+),(\d+),(\d+),(\d?.\d+)\)/
22
+
23
+ IO.foreach(file) do |line|
24
+ is_rgba = line.match rgba
25
+
26
+ if is_rgba
27
+ color = {
28
+ r: is_rgba[1].to_i,
29
+ g: is_rgba[2].to_i,
30
+ b: is_rgba[3].to_i,
31
+ a: (is_rgba[4].to_f * 255).to_i # Convert alpha value to 0-255 range
32
+ }
33
+
34
+ colors.push(color)
35
+ end
36
+ end
37
+
38
+ colors
39
+ end
40
+
41
+ # Create file name from color, ex: 255-255-255-0.8
42
+ def self.file_name(color)
43
+ # Convert the alpha to 0-1 range
44
+ alpha = (color[:a] / 255.to_f).round(1)
45
+
46
+ color[:r].to_s + '-' + color[:g].to_s + '-' + color[:b].to_s + '-' + alpha.to_s
47
+ end
48
+
49
+ # Create png files from colors
50
+ def self.create_files(colors, destination)
51
+ colors.uniq.each do |color|
52
+ bg = ChunkyPNG::Color.rgba(color[:r], color[:g], color[:b], color[:a])
53
+ png = ChunkyPNG::Image.new(16, 16, bg)
54
+
55
+ unless Dir.exists? destination
56
+ Dir.mkdir destination
57
+ end
58
+
59
+ png.save(destination + '/' + (file_name(color) + '.png'), interlace: true)
60
+ end
61
+ end
62
+
63
+ end
data/rgba_png.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rgba-png/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rgba_png"
8
+ spec.version = RgbaPng::VERSION
9
+ spec.authors = ["Nelson Rodrigues"]
10
+ spec.email = ["nelson@fullfront.pt"]
11
+ spec.description = %q{Command line tool that creates PNG image files from rgba definitions in stylesheets.}
12
+ spec.summary = %q{Creates PNG image files from rgba definitions in stylesheets.}
13
+ spec.homepage = "https://github.com/nelsonr/rgba-to-png"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables << 'rgba-png'
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "oily_png"
24
+ end
@@ -0,0 +1,11 @@
1
+ body {
2
+ background: rgba(255,200,254,.3);
3
+ }
4
+
5
+ .container {
6
+ background-color: rgba(0,200,70,.5);
7
+ }
8
+
9
+ h1 {
10
+ color: rgba(255,120,10,.8);
11
+ }
@@ -0,0 +1,61 @@
1
+ require 'test/unit'
2
+ require 'rgba_png'
3
+ require 'fileutils'
4
+
5
+ if ENV['RUBYMINE']
6
+ require 'minitest/reporters'
7
+ MiniTest::Reporters.use!
8
+ end
9
+
10
+ class RgbaPngTest < Test::Unit::TestCase
11
+
12
+ def teardown
13
+ new_folder = File.dirname(__FILE__) + '/i-dont-exist-yet'
14
+ path = File.dirname(__FILE__) + '/images/'
15
+ file = '255-200-100-0.5.png'
16
+
17
+ if File.exists?(path + file)
18
+ File.delete(path + file)
19
+ end
20
+
21
+ if Dir.exists?(new_folder)
22
+ FileUtils.rm_r new_folder
23
+ end
24
+ end
25
+
26
+ def test_create_destination_folder_if_not_exists
27
+ colors = [{r: 255, g: 200, b: 100, a:128}]
28
+ destination = File.dirname(__FILE__) + '/i-dont-exist-yet'
29
+
30
+ RgbaPng.create_files(colors, destination)
31
+
32
+ assert_equal true, Dir.exists?(destination)
33
+ end
34
+
35
+ def test_create_file_from_color_hash_array
36
+ colors = [{r: 255, g: 200, b: 100, a:128}]
37
+ destination = File.dirname(__FILE__) + '/images'
38
+
39
+ RgbaPng.create_files(colors, destination)
40
+
41
+ assert_equal true, File.exists?(destination + '/255-200-100-0.5.png')
42
+ end
43
+
44
+ def test_create_file_name_from_color_hash
45
+ color = {r: 255, g: 200, b: 100, a:128}
46
+ assert_equal "255-200-100-0.5", RgbaPng.file_name(color)
47
+ end
48
+
49
+ def test_parse_source_file_should_create_hash_array
50
+ source_file = File.dirname(__FILE__) + '/test-style.css'
51
+
52
+ expected = [
53
+ {r: 255, g: 200, b: 254, a: 76},
54
+ {r: 0, g: 200, b: 70, a: 127},
55
+ {r: 255, g: 120, b: 10, a: 204}
56
+ ]
57
+
58
+ assert_equal expected, RgbaPng.parse(source_file)
59
+ end
60
+
61
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rgba_png
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Nelson Rodrigues
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: oily_png
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Command line tool that creates PNG image files from rgba definitions
56
+ in stylesheets.
57
+ email:
58
+ - nelson@fullfront.pt
59
+ executables:
60
+ - rgba-png
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - .gitignore
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/rgba-png
70
+ - lib/rgba-png/version.rb
71
+ - lib/rgba_png.rb
72
+ - rgba_png.gemspec
73
+ - test/test-style.css
74
+ - test/test_rgba_png.rb
75
+ homepage: https://github.com/nelsonr/rgba-to-png
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.0.3
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Creates PNG image files from rgba definitions in stylesheets.
99
+ test_files:
100
+ - test/test-style.css
101
+ - test/test_rgba_png.rb