diff --git a/gui/color.lua b/gui/color.lua index dbad5d1..060ae15 100644 --- a/gui/color.lua +++ b/gui/color.lua @@ -50,9 +50,31 @@ function color.hsl(h, s, l) end return (r+m)*255,(g+m)*255,(b+m)*255 end -function color.new(r, b, g, fmt) +function color.isLight(r, g, b) if type(r) == "string" then - r, b, g = tonumber(string.sub(r,1,2),16),tonumber(string.sub(r,3,4),16),tonumber(string.sub(r,5,6),16) + r, g, b = tonumber(string.sub(r,1,2),16),tonumber(string.sub(r,3,4),16),tonumber(string.sub(r,5,6),16) + elseif type(r) == "table" then + r, g, b = r[1], r[2], r[3] + end + return (r + g + b) / 3 >= 128 +end + +function color.rgbToHex(r, g, b) + if type(r) == "string" then + r, g, b = tonumber(string.sub(r,1,2),16),tonumber(string.sub(r,3,4),16),tonumber(string.sub(r,5,6),16) + elseif type(r) == "table" then + r, g, b = r[1], r[2], r[3] + end + + r, g, b = love.math.colorToBytes(r,g,b,0) + + local rgb = (r * 0x10000) + (g * 0x100) + b + return string.format("%06x", rgb) + end + +function color.new(r, g, b, fmt) + if type(r) == "string" then + r, g, b = tonumber(string.sub(r,1,2),16),tonumber(string.sub(r,3,4),16),tonumber(string.sub(r,5,6),16) elseif type(r) == "table" then return r end @@ -60,22 +82,22 @@ function color.new(r, b, g, fmt) if fmt then temp = {r,b,g} else - temp = {love.math.colorFromBytes(r, b, g)} + temp = {love.math.colorFromBytes(r, g, b)} end setmetatable(temp, mt) return temp end function color.random() - return color.new(math.random(0,10000)/10000, math.random(0,10000)/10000, math.random(0,10000)/10000, true) + return color.new(math.random(0,10000000)/10000000, math.random(0,10000000)/10000000, math.random(0,10000000)/10000000, true) end -function color.indexColor(name,r,b,g) +function color.indexColor(name,r, g, b) if type(r)=="string" then - r,b,g=tonumber(string.sub(r,1,2),16),tonumber(string.sub(r,3,4),16),tonumber(string.sub(r,5,6),16) + r, g, b=tonumber(string.sub(r,1,2),16),tonumber(string.sub(r,3,4),16),tonumber(string.sub(r,5,6),16) end -- Other ways to index a color - color[string.lower(name)]=color.new(r,b,g) + color[string.lower(name)]=color.new(r, g, b) color[string.upper(name)]=color[string.lower(name)] -- These should link to the original and not be cloned color[string.upper(string.sub(name,1,1))..string.lower(string.sub(name,2))]=color[string.lower(name)] end diff --git a/gui/init.lua b/gui/init.lua index 829c97a..12bcab2 100644 --- a/gui/init.lua +++ b/gui/init.lua @@ -14,14 +14,25 @@ local frame, image, text, box, video, button, anim = 0, 1, 2, 4, 8, 16, 32 local global_drag local object_focus = gui +-- Types +gui.TYPE_FRAME = frame +gui.TYPE_IMAGE = image +gui.TYPE_TEXT = text +gui.TYPE_BOX = box +gui.TYPE_VIDEO = video +gui.TYPE_BUTTON = button +gui.TYPE_ANIM = anim + +-- + gui.__index = gui gui.MOUSE_PRIMARY = 1 gui.MOUSE_SECONDARY = 2 gui.MOUSE_MIDDLE = 3 -gui.ALLIGN_CENTER = 0 -gui.ALLIGN_LEFT = 1 -gui.ALLIGN_RIGHT = 2 +gui.ALIGN_CENTER = 0 +gui.ALIGN_LEFT = 1 +gui.ALIGN_RIGHT = 2 -- Connections gui.Events = {} -- We are using fastmode for all connection objects. @@ -166,7 +177,7 @@ function gui:getObjectFocus() end function gui:hasType(t) - return band(object_focus.type, t) == t + return band(self.type, t) == t end function gui:move(x,y) @@ -650,7 +661,8 @@ end function gui:newTextBase(typ, txt, x, y, w, h, sx, sy, sw, sh) local c = self:newBase(text + typ, x, y, w, h, sx, sy, sw, sh) c.text = txt - c.align = gui.ALLIGN_LEFT + c.align = gui.ALIGN_LEFT + c.adjust = 0 c.textScaleX = 1 c.textScaleY = 1 c.textOffsetX = 0 @@ -748,7 +760,7 @@ function gui:newTextBase(typ, txt, x, y, w, h, sx, sy, sw, sh) end function gui:newTextButton(txt, x, y, w, h, sx, sy, sw, sh) - local c = self:newTextBase(frame, txt, x, y, w, h, sx, sy, sw, sh) + local c = self:newTextBase(button, txt, x, y, w, h, sx, sy, sw, sh) c:respectHierarchy(true) c.OnEnter(function(c, x, y, dx, dy, istouch) @@ -804,7 +816,6 @@ function gui:newTextBox(txt, x, y, w, h, sx, sy, sw, sh) c.OnReturn = multi:newConnection():fastMode() c.cur_pos = 0 - c.adjust = 0 c.selection = {0,0} function c:getUniques() @@ -1140,12 +1151,12 @@ local drawtypes = { [2] = function(child, x, y, w, h) love.graphics.setColor(child.textColor[1],child.textColor[2],child.textColor[3],child.textVisibility) love.graphics.setFont(child.font) - if child.align == gui.ALLIGN_LEFT then + if child.align == gui.ALIGN_LEFT then child.adjust = 0 - elseif child.align == gui.ALLIGN_CENTER then + elseif child.align == gui.ALIGN_CENTER then local fw = child.font:getWidth(child.text) child.adjust = (w-fw)/2 - elseif child.align == gui.ALLIGN_RIGHT then + elseif child.align == gui.ALIGN_RIGHT then local fw = child.font:getWidth(child.text) child.adjust = w - fw - 4 end diff --git a/gui/theme.lua b/gui/theme.lua index 22f2d0b..f3590ad 100644 --- a/gui/theme.lua +++ b/gui/theme.lua @@ -8,25 +8,45 @@ local function generate_harmonious_colors(num_colors) local colors = {} for i = 1, num_colors do local new_hue = (base_hue + (360 / num_colors) * i) % 360 -- offset hue by 1/n of the color wheel - table.insert(colors, color.new(color.hsl(new_hue, math.random(50, 100), math.random(20, 70)))) + table.insert(colors, color.new(color.hsl(new_hue, math.random(50, 100), math.random(30, 80)))) end return colors end -function theme:random() - local harmonious_colors = generate_harmonious_colors(5) - return theme:new(unpack(harmonious_colors)) +function theme:random(seed) + local seed = seed or math.random(0,9999999999) + math.randomseed(seed) + local harmonious_colors = generate_harmonious_colors(3) + local t = theme:new(unpack(harmonious_colors)) + if not color.isLight(t.colorPrimary) then + t.colorPrimaryText = color.lighten(t.colorPrimaryText, .5) + t.colorButtonNormal = color.lighten(t.colorButtonNormal, .2) + else + t.colorPrimaryText = color.darken(t.colorPrimaryText, .3) + end + if not color.isLight(t.colorButtonNormal) then + t.colorButtonText = color.lighten(t.colorButtonText, .5) + else + t.colorButtonText = color.darken(t.colorButtonText, .3) + end + t.seed = seed + return t end -function theme:new(colorPrimary, primaryText, buttonNormal, buttonHighlight, - buttonText, primaryTextFont, buttonTextFont) + + +function theme:dump() + return '"' .. table.concat({color.rgbToHex(self.colorPrimary), color.rgbToHex(self.colorPrimaryText), color.rgbToHex(self.colorButtonText)},"\",\"") .. '"' +end + +function theme:new(colorPrimary, primaryText, buttonText, primaryTextFont, buttonTextFont) local c = {} setmetatable(c, theme) c.colorPrimary = color.new(colorPrimary) c.colorPrimaryDark = color.darken(c.colorPrimary,.4) c.colorPrimaryText = color.new(primaryText) - c.colorButtonNormal = color.new(buttonNormal) - c.colorButtonHighlight = color.new(buttonHighlight) + c.colorButtonNormal = color.darken(c.colorPrimary,.6) + c.colorButtonHighlight = color.darken(c.colorButtonNormal,.2) c.colorButtonText = color.new(buttonText) c.fontPrimary = primaryTextFont or defaultFont c.fontButton = buttonTextFont or defaultFont @@ -65,6 +85,8 @@ function theme:setFontButton(c) self.fontButton = color.new(c) end - +function theme:getSeed() + return self.seed +end return theme \ No newline at end of file