jsong 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jsong/version.rb +1 -1
  3. data/lib/jsong.rb +27 -3
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 99c467f0e6a1c61b775db7cc78d7f2032c54422b
4
- data.tar.gz: d92ccc25ac55046d376d34a8ac932f1071ad2fc6
3
+ metadata.gz: 3592f6188a4001c04af10edd196d7c5c8750147a
4
+ data.tar.gz: 5bab8f79b616229038f7e35ec4e0cc5390806827
5
5
  SHA512:
6
- metadata.gz: f74f1a134cb4f435b6269b6503ea9a20f5af656d18e7ef910fe76c9d7fa567e700e9bc6755fc893cc263b77f8dcc62d1b20e5bf24ae233a7ded37bc2ac0e1568
7
- data.tar.gz: 06f7684bab0f24b4a075da57d4c25419ff979adc214d3b44f02ecd7fd09e35b745f59faa1a35c5cf47cc9d5a03c4e2ff2bddbc31f24183a91cc834561d171321
6
+ metadata.gz: 9cf528efedd553acaeff61dd140dbaf6c9e45f5bd2ca12ae8a1d02e562aa497823c3aee0addecdc2e772d23839e62cd6e3c58d5c5b843d3befe592cd6bd815b3
7
+ data.tar.gz: c41f0904c64a9c9151114bc60977b679fb22306152d4548bbd55ed8f9d3b3e59a9ceef4f18d53be7c17f02bec7783b77e2011a0966ca37f06c5068342958b11b
data/lib/jsong/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Jsong
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/jsong.rb CHANGED
@@ -19,7 +19,14 @@ module Jsong
19
19
  }]
20
20
  }.freeze
21
21
 
22
- def Jsong.to_jsong(file_name, comment = nil, yaml = false)
22
+ # Converts a PNG image to JSON-G or YAML-G.
23
+ #
24
+ # @param comment [String] Optional global comment string.
25
+ # @param pixel_comment [String] Optional comment string for every pixel.
26
+ # @param yaml [true, false] Set to true if you need YAML-G.
27
+ #
28
+ # @return [String] JSON-G or YAML-G data.
29
+ def Jsong.to_jsong(file_name, comment: nil, pixel_comment: nil, yaml: false)
23
30
  image = ChunkyPNG::Image.from_file file_name
24
31
 
25
32
  if comment.nil? && image.metadata['Title']
@@ -42,7 +49,7 @@ module Jsong
42
49
  y: y
43
50
  },
44
51
  color: Jsong.rrggbbaa_to_jsong(pixel),
45
- comment: 'A pixel'
52
+ comment: pixel_comment || 'A pixel'
46
53
  }
47
54
  end
48
55
  end
@@ -54,7 +61,14 @@ module Jsong
54
61
  end
55
62
  end
56
63
 
57
- def Jsong.from_jsong(text, file_name, yaml = false)
64
+ # Converts JSON-G or YAML-G data to an image.
65
+ #
66
+ # @param text [String] Raw JSON-G or YAML-G data.
67
+ # @param file_name [String] Target file name.
68
+ # @param yaml [true, false] Set to true if the data is in YAML-G.
69
+ #
70
+ # @return [nil]
71
+ def Jsong.from_jsong(text, file_name, yaml: false)
58
72
  text = text.read if text.respond_to? :read
59
73
  data = if yaml; YAML.load text; else; JSON.parse text; end
60
74
  default_color = Jsong.encode_pixel data['layers'][-1]['default_color']
@@ -74,6 +88,11 @@ module Jsong
74
88
  png.save(file_name, :interlace => true)
75
89
  end
76
90
 
91
+ # Converts a single pixel hash to a chunky png pixel.
92
+ #
93
+ # @param px [Hash<String => Integer>] Hash containing red, green, blue and alpha values.
94
+ #
95
+ # @return [ChunkyPNG::Color]
77
96
  def Jsong.encode_pixel(px)
78
97
  r, g = px['red'], px['green']
79
98
  b, a = px['blue'], px['alpha']
@@ -81,6 +100,11 @@ module Jsong
81
100
  ChunkyPNG::Color.rgba(r, g, b, a)
82
101
  end
83
102
 
103
+ # Converts an integer to JSON-G hash.
104
+ #
105
+ # @param i [Integer] Base 10 colour value in hex format RRGGBBAA.
106
+ #
107
+ # @return [Hash<String => Integer>] Hash with red, green, blue and alpha values
84
108
  def Jsong.rrggbbaa_to_jsong(i)
85
109
  {
86
110
  red: (i / 16777216).floor,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsong
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Illia K.