Working on randomizing themes

This commit is contained in:
Ryan Ward 2023-01-12 17:15:28 -05:00
parent da62ef0af8
commit 6b0a2d074e
3 changed files with 81 additions and 26 deletions

View File

@ -50,9 +50,31 @@ function color.hsl(h, s, l)
end return (r+m)*255,(g+m)*255,(b+m)*255 end return (r+m)*255,(g+m)*255,(b+m)*255
end end
function color.new(r, b, g, fmt) function color.isLight(r, g, b)
if type(r) == "string" then 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 elseif type(r) == "table" then
return r return r
end end
@ -60,22 +82,22 @@ function color.new(r, b, g, fmt)
if fmt then if fmt then
temp = {r,b,g} temp = {r,b,g}
else else
temp = {love.math.colorFromBytes(r, b, g)} temp = {love.math.colorFromBytes(r, g, b)}
end end
setmetatable(temp, mt) setmetatable(temp, mt)
return temp return temp
end end
function color.random() 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 end
function color.indexColor(name,r,b,g) function color.indexColor(name,r, g, b)
if type(r)=="string" then 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 end
-- Other ways to index a color -- 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(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)] color[string.upper(string.sub(name,1,1))..string.lower(string.sub(name,2))]=color[string.lower(name)]
end end

View File

@ -14,14 +14,25 @@ local frame, image, text, box, video, button, anim = 0, 1, 2, 4, 8, 16, 32
local global_drag local global_drag
local object_focus = gui 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.__index = gui
gui.MOUSE_PRIMARY = 1 gui.MOUSE_PRIMARY = 1
gui.MOUSE_SECONDARY = 2 gui.MOUSE_SECONDARY = 2
gui.MOUSE_MIDDLE = 3 gui.MOUSE_MIDDLE = 3
gui.ALLIGN_CENTER = 0 gui.ALIGN_CENTER = 0
gui.ALLIGN_LEFT = 1 gui.ALIGN_LEFT = 1
gui.ALLIGN_RIGHT = 2 gui.ALIGN_RIGHT = 2
-- Connections -- Connections
gui.Events = {} -- We are using fastmode for all connection objects. gui.Events = {} -- We are using fastmode for all connection objects.
@ -166,7 +177,7 @@ function gui:getObjectFocus()
end end
function gui:hasType(t) function gui:hasType(t)
return band(object_focus.type, t) == t return band(self.type, t) == t
end end
function gui:move(x,y) function gui:move(x,y)
@ -650,7 +661,8 @@ end
function gui:newTextBase(typ, txt, x, y, w, h, sx, sy, sw, sh) 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) local c = self:newBase(text + typ, x, y, w, h, sx, sy, sw, sh)
c.text = txt c.text = txt
c.align = gui.ALLIGN_LEFT c.align = gui.ALIGN_LEFT
c.adjust = 0
c.textScaleX = 1 c.textScaleX = 1
c.textScaleY = 1 c.textScaleY = 1
c.textOffsetX = 0 c.textOffsetX = 0
@ -748,7 +760,7 @@ function gui:newTextBase(typ, txt, x, y, w, h, sx, sy, sw, sh)
end end
function gui:newTextButton(txt, x, y, w, h, sx, sy, sw, sh) 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:respectHierarchy(true)
c.OnEnter(function(c, x, y, dx, dy, istouch) 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.OnReturn = multi:newConnection():fastMode()
c.cur_pos = 0 c.cur_pos = 0
c.adjust = 0
c.selection = {0,0} c.selection = {0,0}
function c:getUniques() function c:getUniques()
@ -1140,12 +1151,12 @@ local drawtypes = {
[2] = function(child, x, y, w, h) [2] = function(child, x, y, w, h)
love.graphics.setColor(child.textColor[1],child.textColor[2],child.textColor[3],child.textVisibility) love.graphics.setColor(child.textColor[1],child.textColor[2],child.textColor[3],child.textVisibility)
love.graphics.setFont(child.font) love.graphics.setFont(child.font)
if child.align == gui.ALLIGN_LEFT then if child.align == gui.ALIGN_LEFT then
child.adjust = 0 child.adjust = 0
elseif child.align == gui.ALLIGN_CENTER then elseif child.align == gui.ALIGN_CENTER then
local fw = child.font:getWidth(child.text) local fw = child.font:getWidth(child.text)
child.adjust = (w-fw)/2 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) local fw = child.font:getWidth(child.text)
child.adjust = w - fw - 4 child.adjust = w - fw - 4
end end

View File

@ -8,25 +8,45 @@ local function generate_harmonious_colors(num_colors)
local colors = {} local colors = {}
for i = 1, num_colors do 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 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 end
return colors return colors
end end
function theme:random() function theme:random(seed)
local harmonious_colors = generate_harmonious_colors(5) local seed = seed or math.random(0,9999999999)
return theme:new(unpack(harmonious_colors)) 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 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 = {} local c = {}
setmetatable(c, theme) setmetatable(c, theme)
c.colorPrimary = color.new(colorPrimary) c.colorPrimary = color.new(colorPrimary)
c.colorPrimaryDark = color.darken(c.colorPrimary,.4) c.colorPrimaryDark = color.darken(c.colorPrimary,.4)
c.colorPrimaryText = color.new(primaryText) c.colorPrimaryText = color.new(primaryText)
c.colorButtonNormal = color.new(buttonNormal) c.colorButtonNormal = color.darken(c.colorPrimary,.6)
c.colorButtonHighlight = color.new(buttonHighlight) c.colorButtonHighlight = color.darken(c.colorButtonNormal,.2)
c.colorButtonText = color.new(buttonText) c.colorButtonText = color.new(buttonText)
c.fontPrimary = primaryTextFont or defaultFont c.fontPrimary = primaryTextFont or defaultFont
c.fontButton = buttonTextFont or defaultFont c.fontButton = buttonTextFont or defaultFont
@ -65,6 +85,8 @@ function theme:setFontButton(c)
self.fontButton = color.new(c) self.fontButton = color.new(c)
end end
function theme:getSeed()
return self.seed
end
return theme return theme