Added RGB, hex, and HSL to Color class

This commit is contained in:
Josh
2018-06-07 13:20:05 -05:00
parent c5572a0078
commit ffa8604802
3 changed files with 50 additions and 16 deletions

View File

@@ -1,6 +1,6 @@
love.filesystem.setRequirePath(love.filesystem.getRequirePath() .. ";JUI/?.lua")
print("Loading JUI library...")
return {
JUIScene = require("lib.classes.JUIScene"),

View File

@@ -2,28 +2,55 @@ local Color = {}
setmetatable(Color,{
__index = Color,
__add = function(a,b) return Color:new(a.x+b.x,a.y+b.y) end,
__add = function(a,b) return Color:new() end,
__tostring = function(a) return "("..a.x..','..a.y..")" end
})
function Color:new(red, green, blue)
function Color:new(red, green, blue, alpha)
return setmetatable({
r = red or 0,
g = green or 0,
b = blue or 0
b = blue or 0,
a = alpha or 1,
}, getmetatable(self))
end
function Color:fromRGB()
function Color:fromRGB(red, green, blue, alpha)
return Color:new(red/255, green/255, blue/255, (alpha or 255)/255)
end
function Color:fromHex()
function Color:fromHSL(h, s, l, alpha)
if s == 0 then return l,l,l end
h, s, l = h/256*6, s/255, l/255
local c = (1-math.abs(2*l-1))*s
local x = (1-math.abs(h%2-1))*c
local m,r,g,b = (l-.5*c), 0,0,0
if h < 1 then r,g,b = c,x,0
elseif h < 2 then r,g,b = x,c,0
elseif h < 3 then r,g,b = 0,c,x
elseif h < 4 then r,g,b = 0,x,c
elseif h < 5 then r,g,b = x,0,c
else r,g,b = c,0,x
end
local r, g, b = r+m, g+m, b+m
return Color:new(r, g, b, (alpha or 255)/255)
end
function Color:fromHex(hex, alpha)
local hex = hex:gsub("#","")
local r, g, b
if hex:len() == 3 then
r, g, b = (tonumber("0x"..hex:sub(1,1))*17)/255, (tonumber("0x"..hex:sub(2,2))*17)/255, (tonumber("0x"..hex:sub(3,3))*17)/255
else
r, g, b = tonumber("0x"..hex:sub(1,2))/255, tonumber("0x"..hex:sub(3,4))/255, tonumber("0x"..hex:sub(5,6))/255
end
return Color:new(r, g, b, (alpha or 255)/255)
end
function Color:out()
return self.r, self.g, self.b
return self.r, self.g, self.b, self.a
end
return Color

View File

@@ -17,21 +17,16 @@ local emptyColor = JUI.Label:new()
emptyColor:setPosition(JUI.Dimension:new(0.2, 0.8))
emptyColor:setBorderColor(JUI.Color:new(0.5, 0.5, 0.5))
emptyColor:setTextSize(22)
emptyColor:setBackgroundColor(JUI.Color:new(200/255, 69/255, 128/255))
emptyColor:setBackgroundColor(JUI.Color:fromRGB(200, 69, 128, 128))
emptyColor:setTextAlignment("center")
emptyColor:setText("Bottom Text")
emptyColor.mouseEnter:connect(function()
emptyColor:setText("Inside")
end)
emptyColor.mouseExit:connect(function()
emptyColor:setText("Outside")
end)
local button = JUI.Button:new()
button:setSize(JUI.Dimension:new(0.3, 0.5))
button:setPosition(JUI.Dimension:new(0.2, 0.2))
button:setBackgroundColor(JUI.Color:new(0.2, 0.3, 0.5))
button:setBackgroundColor(JUI.Color:fromHSL(1, 128, 196))
button.mouseClickDown:connect(function()
button:setText("Thug")
@@ -40,12 +35,24 @@ local button = JUI.Button:new()
button.mouseClickUp:connect(function()
button:setText("not Thug")
end)
button.mouseEnter:connect(function()
button:setText("Inside")
end)
button.mouseExit:connect(function()
button:setText("Outside")
end)
JUI:parent(mainmenu, background)
JUI:parent(mainmenu, emptyColor)
JUI:parent(background, button)
print((JUI.Color:fromHex("#FFF")):out())
print((JUI.Color:fromHex("#ADFAAD")):out())
print((JUI.Color:fromHSL(1, 128, 196)):out())
local function round(number, decimalPlaces)
local placer = 10^(-decimalPlaces)
return (math.floor(number)/placer)*placer