Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Created April 9, 2013 14:52
Show Gist options
  • Select an option

  • Save dermotbalson/5346329 to your computer and use it in GitHub Desktop.

Select an option

Save dermotbalson/5346329 to your computer and use it in GitHub Desktop.
25. Emoji
function setup()
--use the emoji viewer software to get the number of the icon you want
--(when viewing a page of icons, press on the one you want, and note the number
emoji=unicode2UTF8(128120)
end
function draw()
background(0)
font("AppleColorEmoji")
fontSize(128)
text(emoji, WIDTH/2, HEIGHT/2)
end
function unicode2UTF8(u)
u = math.max(0, math.floor(u)) -- A positive integer
local UTF8
if u < 0x80 then -- less than 8 bits
UTF8 = string.char(u)
elseif u < 0x800 then -- less than 12 bits
local b2 = u % 0x40 + 0x80
local b1 = math.floor(u/0x40) + 0xC0
UTF8 = string.char(b1, b2)
elseif u < 0x10000 then -- less than 16 bits
local b3 = u % 0x40 + 0x80
local b2 = math.floor(u/0x40) % 0x40 + 0x80
local b1 = math.floor(u/0x1000) + 0xE0
UTF8 = string.char(b1, b2, b3)
elseif u < 0x200000 then -- less than 22 bits
local b4 = u % 0x40 + 0x80
local b3 = math.floor(u/0x40) % 0x40 + 0x80
local b2 = math.floor(u/0x1000) % 0x40 + 0x80
local b1 = math.floor(u/0x40000) + 0xF0
UTF8 = string.char(b1, b2, b3, b4)
elseif u < 0x800000 then -- less than 24 bits
local b5 = u % 0x40 + 0x80
local b4 = math.floor(u/0x40) % 0x40 + 0x80
local b3 = math.floor(u/0x1000) % 0x40 + 0x80
local b2 = math.floor(u/0x40000) % 0x40 + 0x80
local b1 = math.floor(u/0x1000000) + 0xF8
UTF8 = string.char(b1, b2, b3, b4, b5)
else
print("Error: Code point too large for Codea's Lua.")
end
return UTF8
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment