jsong 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jsong/version.rb +1 -1
- data/lib/jsong.rb +27 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3592f6188a4001c04af10edd196d7c5c8750147a
|
4
|
+
data.tar.gz: 5bab8f79b616229038f7e35ec4e0cc5390806827
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9cf528efedd553acaeff61dd140dbaf6c9e45f5bd2ca12ae8a1d02e562aa497823c3aee0addecdc2e772d23839e62cd6e3c58d5c5b843d3befe592cd6bd815b3
|
7
|
+
data.tar.gz: c41f0904c64a9c9151114bc60977b679fb22306152d4548bbd55ed8f9d3b3e59a9ceef4f18d53be7c17f02bec7783b77e2011a0966ca37f06c5068342958b11b
|
data/lib/jsong/version.rb
CHANGED
data/lib/jsong.rb
CHANGED
@@ -19,7 +19,14 @@ module Jsong
|
|
19
19
|
}]
|
20
20
|
}.freeze
|
21
21
|
|
22
|
-
|
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
|
-
|
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,
|