Last active
October 17, 2015 02:13
-
-
Save moollaza/923ada344d44ddfbf04e to your computer and use it in GitHub Desktop.
Corona Labs Lua - Hex String to RGB Tuple Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local colorGen = {} | |
-- Table of named HEX colors | |
local _colors = { | |
["darkest-grey"] = "#404040", -- .25 | |
["darker-grey"] = "#727272", -- .37 | |
["dark-grey"] = "#868686", -- .45 | |
["grey"] = "#929292", -- .50 | |
["medium-grey"] = "#BFBFBF", -- .70 | |
["light-grey"] = "#EFEFEF", -- .92 | |
["lighter-grey"] = "#F7F7F7", -- .97 | |
["near-white"] = "#F9F9F9", -- .98 | |
["dark-blue"] = "#3192A7", | |
["blue"] = "#53B3C7", | |
["light-blue"] = "#7DCFDE", | |
["orange"] = "#F39C12", | |
["green"] = "#86d479", | |
["light-green"] = "#66E666", | |
["red"] = "#E66666" | |
} | |
-- source https://gist.github.com/jasonbradley/4357406 | |
local function hex2rgb (hex) | |
local hex = hex:gsub("#","") | |
local r, g, b = tonumber("0x"..hex:sub(1,2)), tonumber("0x"..hex:sub(3,4)), tonumber("0x"..hex:sub(5,6)) | |
return r/255, g/255, b/255 -- Corona needs percentages | |
end | |
function colorGen:new () | |
local obj = {} | |
self.__index = self | |
return setmetatable(obj, self) | |
end | |
function colorGen:get (name) | |
if not _colors[name] then | |
print("ERROR: "..name.." does not exist") | |
end | |
return hex2rgb( _colors[name] ) | |
end | |
function colorGen:getOver (name) | |
local r, g, b = hex2rgb( _colors[name] ) | |
return r, g, b, .75 | |
end | |
return colorGen | |
--[[ | |
Usage: | |
local btn = widget.newButton { | |
x = 0, | |
y = 0, | |
label = "My Button", | |
labelColor = { default = { colors:get("orange") }, over = { colors:getOver("orange") } }, | |
fillColor = { default = { colors:get("medium-grey") }, over = { colors:getOver("medium-grey") } }, | |
font = "Custom Font", | |
shape = "circle", | |
radius = 10, | |
fontSize = 12, | |
onRelease = onBtnRelease | |
} | |
--]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a pretty rudimentary helper I wrote so I could reference the same colours throughout my app more easily.
I have a color pallette for my app with named colours which I can refer to whenever I need to set the colour for something.
get
method returns a 3-value tuple containing the Red, Green and Blue values as decimals.getOver
method returns a 4-value tuple containing the Red, Green, Blue and Alpha values as decimals. I've chosen a 75% transparency for all my colours which works well for my project.Feel free to copy and modify this as needed.