module Emojimage

Constants

VERSION

Public Instance Methods

analyze(img) click to toggle source

Get the overall average color of an image.

# File lib/emojimage/emojimage.rb, line 54
def analyze img
        average img.pixels
end
average(colors, transparentBlock = false, blend = ChunkyPNG::Color::WHITE) click to toggle source

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.

# 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) click to toggle source

Get ‘ChunkyPNG::Image` from emoji

# File lib/emojimage/emojimage.rb, line 126
def chunkemoji e
        ChunkyPNG::Image.from_file(where e)
end
codify(blend = ChunkyPNG::Color::WHITE) click to toggle source

Create JSON file with info about emoji.

# 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) click to toggle source

Get distance between two colors.

# 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() click to toggle source

All the emoji

# File lib/emojimage/emojimage.rb, line 13
def emoji
        @@emoji
end
emojinfo() click to toggle source

Grab emoji info. If it doesn’t exist, return ‘false`.

# 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) click to toggle source

Get emoji that corresponds to ‘color`. `blend` represents a color to blend with for transparency (in other words, a background color).

# 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) click to toggle source

Sets up the enviroments by grabbing the emoji images and setting emoji info.

# 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) click to toggle source

Has this been set up with the current ‘blend` setting?

# 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) click to toggle source

Splits a hex value in case there are two hexes inside

# File lib/emojimage/emojimage.rb, line 90
def unpackhex c
        c.hex_inspect.split '-'
end
where(e) click to toggle source

Grab emoji image

# 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) click to toggle source

Find relative path.

# File lib/emojimage/emojimage.rb, line 30
def within path
        File.expand_path path, @@where
end