Module: Emojimage
- Extended by:
- Emojimage
- Included in:
- Emojimage
- Defined in:
- lib/emojimage/cli.rb,
lib/emojimage/version.rb,
lib/emojimage/converted.rb,
lib/emojimage/emojimage.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.1.0"- @@emoji =
Emoji.all.select { |char| !char.custom? }
- @@where =
File. "../", File.dirname(__FILE__)
- @@info =
false
Instance Method Summary collapse
-
#analyze(img) ⇒ Object
Get the overall average color of an image.
-
#average(colors, transparentBlock = false, blend = ChunkyPNG::Color::WHITE) ⇒ Object
Average of an array of colors.
-
#chunkemoji(e) ⇒ Object
Get
ChunkyPNG::Imagefrom emoji. -
#codify(blend = ChunkyPNG::Color::WHITE) ⇒ Object
Create JSON file with info about emoji.
-
#compare(c1, c2) ⇒ Object
Get distance between two colors.
-
#emoji ⇒ Object
All the emoji.
-
#emojinfo ⇒ Object
Grab emoji info.
-
#find(color, blend) ⇒ Object
Get emoji that corresponds to
color. -
#setup(blend) ⇒ Object
Sets up the enviroments by grabbing the emoji images and setting emoji info.
-
#setup?(blend) ⇒ Boolean
Has this been set up with the current
blendsetting?. -
#unpackhex(c) ⇒ Object
Splits a hex value in case there are two hexes inside.
-
#where(e) ⇒ Object
Grab emoji image.
-
#within(path) ⇒ Object
Find relative path.
Instance Method Details
#analyze(img) ⇒ Object
Get the overall average color of an image.
54 55 56 |
# File 'lib/emojimage/emojimage.rb', line 54 def analyze img average img.pixels end |
#average(colors, transparentBlock = false, blend = ChunkyPNG::Color::WHITE) ⇒ Object
Average of an array of colors. Returns ChunkyPNG::Color::TRANSPARENT if transparentBlock is true and the colors are all transparent. Blends the transparent/semi-transparent with blend color.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/emojimage/emojimage.rb', line 35 def average colors, transparentBlock = false, blend = ChunkyPNG::Color::WHITE color = [0.0,0.0,0.0,0.0] count = 0.0 for pxl in colors weight = ((255 - (ChunkyPNG::Color.a pxl)) / 255.0).to_f color[0] += ((1.0 - weight) * (ChunkyPNG::Color.r pxl).to_f + weight * (ChunkyPNG::Color.r blend).to_f) color[1] += ((1.0 - weight) * (ChunkyPNG::Color.g pxl).to_f + weight * (ChunkyPNG::Color.g blend).to_f) color[2] += ((1.0 - weight) * (ChunkyPNG::Color.b pxl).to_f + weight * (ChunkyPNG::Color.b blend).to_f) color[3] += ChunkyPNG::Color.a pxl count += 1.0 end if transparentBlock and color[3] == 0.0 ChunkyPNG::Color::TRANSPARENT else ChunkyPNG::Color.rgb (color[0]/count).round, (color[1]/count).round, (color[2]/count).round end end |
#chunkemoji(e) ⇒ Object
Get ChunkyPNG::Image from emoji
126 127 128 |
# File 'lib/emojimage/emojimage.rb', line 126 def chunkemoji e ChunkyPNG::Image.from_file(where e) end |
#codify(blend = ChunkyPNG::Color::WHITE) ⇒ Object
Create JSON file with info about emoji.
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/emojimage/emojimage.rb', line 95 def codify blend = ChunkyPNG::Color::WHITE res = {"characters" => []} for e in @@emoji png = chunkemoji e res["characters"] << { "filename" => e.image_filename, "value" => average(png.pixels, false, blend), "unicode" => e.unicode_aliases.first, "key" => unpackhex(e) } end File.open(within("../public/data/emoji.json"), "w+") { |f| f.write res.to_json } end |
#compare(c1, c2) ⇒ Object
Get distance between two colors.
110 111 112 |
# File 'lib/emojimage/emojimage.rb', line 110 def compare c1, c2 Math.sqrt((ChunkyPNG::Color.r(c1)-ChunkyPNG::Color.r(c2))**2+(ChunkyPNG::Color.g(c1)-ChunkyPNG::Color.g(c2))**2+(ChunkyPNG::Color.b(c1)-ChunkyPNG::Color.b(c2))**2) end |
#emoji ⇒ Object
All the emoji
13 14 15 |
# File 'lib/emojimage/emojimage.rb', line 13 def emoji @@emoji end |
#emojinfo ⇒ Object
Grab emoji info. If it doesn’t exist, return false.
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/emojimage/emojimage.rb', line 78 def emojinfo if @@info == false if File.exist?(within("../public/data/emoji.json")) @@info = JSON.parse(File.open(within("../public/data/emoji.json"), 'rb') { |f| f.read }) else @@info = false end end @@info end |
#find(color, blend) ⇒ Object
Get emoji that corresponds to color. blend represents a color to blend with for transparency (in other words, a background color).
115 116 117 118 119 120 121 122 123 |
# File 'lib/emojimage/emojimage.rb', line 115 def find color, blend setup blend data = emojinfo if color == ChunkyPNG::Color::TRANSPARENT false else data["characters"].min_by { |char| compare color, char["value"] } end end |
#setup(blend) ⇒ Object
Sets up the enviroments by grabbing the emoji images and setting emoji info.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/emojimage/emojimage.rb', line 66 def setup blend gemojiV = "#{Gem.loaded_specs['gemoji'].version.to_s}\n#{blend}" unless setup? blend system("cd #{within "../"} && rake emoji") # puts "Initialized emoji images" codify blend File.open(within("../public/data/.last"), "w+") { |f| f.write gemojiV } # puts "Analyzed emoji images" end end |
#setup?(blend) ⇒ Boolean
Has this been set up with the current blend setting?
59 60 61 62 63 |
# File 'lib/emojimage/emojimage.rb', line 59 def setup? blend gemojiV = "#{Gem.loaded_specs['gemoji'].version.to_s}\n#{blend}" lastImport = File.open(within("../public/data/.last"), 'rb') { |f| f.read } lastImport == gemojiV end |
#unpackhex(c) ⇒ Object
Splits a hex value in case there are two hexes inside
90 91 92 |
# File 'lib/emojimage/emojimage.rb', line 90 def unpackhex c c.hex_inspect.split '-' end |
#where(e) ⇒ Object
Grab emoji image
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/emojimage/emojimage.rb', line 18 def where e if e.class == Hash address = e['filename'] elsif e.class == Emoji::Character address = e.image_filename else raise "Unknown emoji representation passed to Emojimage.where" end within "../public/images/emoji/#{address}" end |
#within(path) ⇒ Object
Find relative path.
30 31 32 |
# File 'lib/emojimage/emojimage.rb', line 30 def within path File. path, @@where end |