chess2png 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 546fa4ff5e9bb17384f6ea69659e25c68134aa81
4
+ data.tar.gz: 649369a75d2c0e4a9db25f9663590f68e1a56914
5
+ SHA512:
6
+ metadata.gz: ce49b144df33f71d26efdba166d3d6fe7b29806ec4a2dbcf420b85c452b972fd4fafe5565ecdd574065a879fe125504a1dfd0fa337f90e61b11e1379a57cb805
7
+ data.tar.gz: c676a7978c6dc250cba8e643bdf99b7100ecc6021d4cd466f29701a97ef0f385959f670b0a8c4024fd04106081e1b5d17236bd6500666921a282f3711db5b0c6
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ out.png
2
+ test.pgn
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ chess2png (0.0.1)
5
+ chess (~> 0.2.0)
6
+ chunky_png (~> 1.3.8)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ byebug (9.1.0)
12
+ chess (0.2.0)
13
+ chunky_png (1.3.8)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ byebug
20
+ chess2png!
21
+
22
+ BUNDLED WITH
23
+ 1.13.6
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
data/bin/chess2png ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'chess'
4
+ require 'chess2png'
5
+ require 'optparse'
6
+
7
+ options = {}
8
+ optparse = OptionParser.new do |opts|
9
+ opts.banner = "Usage: chess2png -i input -o output"
10
+
11
+ opts.on("-i", "--input PATH", "Path to a PGN file") do |input|
12
+ options[:input] = input
13
+ end
14
+
15
+ opts.on("-o", "--output PATH", "Path where the resulting PNG file will be saved") do |output|
16
+ options[:output] = output
17
+ end
18
+ end
19
+
20
+ begin
21
+ optparse.parse!
22
+
23
+ # from https://stackoverflow.com/a/2149183
24
+ mandatory = [:input, :output]
25
+ missing = mandatory.select{ |param| options[param].nil? }
26
+ unless missing.empty?
27
+ raise OptionParser::MissingArgument.new(missing.join(", "))
28
+ end
29
+
30
+ Chess2PNG.new.encode(Chess::Game.load_pgn(options[:input]).current).save(options[:output])
31
+ rescue OptionParser::InvalidOption, OptionParser::MissingArgument
32
+ puts $!.to_s
33
+ puts optparse
34
+ exit 1
35
+ end
data/chess2png.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ lib = File.expand_path("../lib", __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "chess2png/version"
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "chess2png"
9
+ gem.version = Chess2PNG::VERSION
10
+ gem.authors = ["Victor Goya"]
11
+ gem.email = ["phorque@phorque.it"]
12
+ gem.description = "Turn chess boards into PNG images"
13
+ gem.summary = "Turn chess boards into PNG images"
14
+ gem.homepage = "https://phorque.it"
15
+
16
+ gem.files = `git ls-files -z`.split("\x0")
17
+ gem.executables = %w(chess2png)
18
+ gem.require_paths = ["lib"]
19
+ gem.bindir = 'bin'
20
+
21
+ gem.licenses = ["MIT"]
22
+
23
+ gem.required_ruby_version = "~> 2.0"
24
+
25
+ gem.add_dependency 'chess', '~> 0.2.0'
26
+ gem.add_dependency 'chunky_png', '~> 1.3.8'
27
+
28
+ gem.add_development_dependency "byebug"
29
+ end
@@ -0,0 +1,3 @@
1
+ class Chess2PNG
2
+ VERSION = "0.0.1"
3
+ end
data/lib/chess2png.rb ADDED
@@ -0,0 +1,68 @@
1
+ require 'chess'
2
+ require 'chunky_png'
3
+
4
+ class Chess2PNG
5
+ BOARD_SIZE = 8
6
+
7
+ def initialize(options = {})
8
+ @pieces = options.delete(:pieces) || default_pieces
9
+ @background = options.delete(:background) || default_background
10
+ @square_size = options.delete(:square_size) || 61
11
+ end
12
+
13
+ def encode(board)
14
+ ChunkyPNG::Image.from_file(@background).tap do |image|
15
+ self.encode_to_image(board, image)
16
+ end
17
+ end
18
+
19
+ def encode_to_image(board, image)
20
+ BOARD_SIZE.times do |x|
21
+ BOARD_SIZE.times do |y|
22
+ piece_image = self.piece_to_image(board[x + y * BOARD_SIZE])
23
+
24
+ if piece_image
25
+ image.compose!(piece_image, x * @square_size, (BOARD_SIZE - 1) * @square_size - y * @square_size)
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def piece_to_image(piece)
32
+ @pieces.map do |piece, path|
33
+ [ piece, load_image(path) ]
34
+ end.to_h[piece]
35
+ end
36
+
37
+ def default_pieces
38
+ root = File.dirname(__FILE__)
39
+
40
+ {
41
+ 'P' => File.join(root, "../assets/Chess_plt60.png"),
42
+ 'R' => File.join(root, "../assets/Chess_rlt60.png"),
43
+ 'N' => File.join(root, "../assets/Chess_nlt60.png"),
44
+ 'B' => File.join(root, "../assets/Chess_blt60.png"),
45
+ 'Q' => File.join(root, "../assets/Chess_qlt60.png"),
46
+ 'K' => File.join(root, "../assets/Chess_klt60.png"),
47
+
48
+ 'p' => File.join(root, "../assets/Chess_pdt60.png"),
49
+ 'r' => File.join(root, "../assets/Chess_rdt60.png"),
50
+ 'n' => File.join(root, "../assets/Chess_ndt60.png"),
51
+ 'b' => File.join(root, "../assets/Chess_bdt60.png"),
52
+ 'q' => File.join(root, "../assets/Chess_qdt60.png"),
53
+ 'k' => File.join(root, "../assets/Chess_kdt60.png")
54
+ }
55
+ end
56
+
57
+ def default_background
58
+ root = File.dirname(__FILE__)
59
+
60
+ File.join(root, "../assets/background.png")
61
+ end
62
+
63
+ def load_image(path)
64
+ @image_cache ||= {}
65
+ @image_cache[path] ||= ChunkyPNG::Image.from_file(path)
66
+ @image_cache[path]
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chess2png
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Victor Goya
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chess
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: chunky_png
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.8
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.8
41
+ - !ruby/object:Gem::Dependency
42
+ name: byebug
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: Turn chess boards into PNG images
56
+ email:
57
+ - phorque@phorque.it
58
+ executables:
59
+ - chess2png
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - assets/Chess_bdt60.png
67
+ - assets/Chess_blt60.png
68
+ - assets/Chess_kdt60.png
69
+ - assets/Chess_klt60.png
70
+ - assets/Chess_ndt60.png
71
+ - assets/Chess_nlt60.png
72
+ - assets/Chess_pdt60.png
73
+ - assets/Chess_plt60.png
74
+ - assets/Chess_qdt60.png
75
+ - assets/Chess_qlt60.png
76
+ - assets/Chess_rdt60.png
77
+ - assets/Chess_rlt60.png
78
+ - assets/background.png
79
+ - bin/chess2png
80
+ - chess2png.gemspec
81
+ - lib/chess2png.rb
82
+ - lib/chess2png/version.rb
83
+ homepage: https://phorque.it
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '2.0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.5.2
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Turn chess boards into PNG images
107
+ test_files: []