diff --git a/FlexLove.lua b/FlexLove.lua index d14c473..b1a6fcc 100644 --- a/FlexLove.lua +++ b/FlexLove.lua @@ -1,11 +1,120 @@ -local Color = require("game.utils.color") -local enums = require("game.utils.enums") -local FlexDirection = enums.FlexDirection -local JustifyContent = enums.JustifyContent -local AlignContent = enums.AlignContent -local AlignItems = enums.AlignItems -local Positioning = enums.Positioning -local TextAlign = enums.TextAlign +-- Utility class for color handling +---@class Color +---@field r number +---@field g number +---@field b number +---@field a number +local Color = {} +Color.__index = Color + +--- Create a new color instance +---@param r number +---@param g number +---@param b number +---@param a number? -- default 1 +---@return Color +function Color.new(r, g, b, a) + local self = setmetatable({}, Color) + self.r = r or 0 + self.g = g or 0 + self.b = b or 0 + self.a = a or 1 + return self +end + +--- Convert hex string to color +---@param hex string -- e.g. "#RRGGBB" or "#RRGGBBAA" +---@return Color +function Color.fromHex(hex) + local hex = hex:gsub("#", "") + if #hex == 6 then + local r = tonumber("0x" .. hex:sub(1, 2)) + local g = tonumber("0x" .. hex:sub(3, 4)) + local b = tonumber("0x" .. hex:sub(5, 6)) + return Color.new(r, g, b, 1) + elseif #hex == 8 then + local r = tonumber("0x" .. hex:sub(1, 2)) + local g = tonumber("0x" .. hex:sub(3, 4)) + local b = tonumber("0x" .. hex:sub(5, 6)) + local a = tonumber("0x" .. hex:sub(7, 8)) / 255 + return Color.new(r, g, b, a) + else + error("Invalid hex string") + end +end + +--- Convert color to hex string +---@return string +function Color:toHex() + local r = math.floor(self.r * 255) + local g = math.floor(self.g * 255) + local b = math.floor(self.b * 255) + local a = math.floor(self.a * 255) + if self.a ~= 1 then + return string.format("#%02X%02X%02X%02X", r, g, b, a) + else + return string.format("#%02X%02X%02X", r, g, b) + end +end + +---@return number r, number g, number b, number a +function Color:toRGBA() + return self.r, self.g, self.b, self.a +end + +local enums + +--- @enum TextAlign +enums.TextAlign = { + START = "start", + CENTER = "center", + END = "end", + JUSTIFY = "justify", +} + +--- @enum Positioning +enums.Positioning = { + ABSOLUTE = "absolute", + FLEX = "flex", +} + +--- @enum FlexDirection +enums.FlexDirection = { + HORIZONTAL = "horizontal", + VERTICAL = "vertical", +} + +--- @enum JustifyContent +enums.JustifyContent = { + FLEX_START = "flex-start", + CENTER = "center", + SPACE_AROUND = "space-around", + FLEX_END = "flex-end", + SPACE_EVENLY = "space-evenly", + SPACE_BETWEEN = "space-between", +} + +--- @enum AlignItems +enums.AlignItems = { + STRETCH = "stretch", + FLEX_START = "flex-start", + FLEX_END = "flex-end", + CENTER = "center", + BASELINE = "baseline", +} + +--- @enum AlignContent +enums.AlignContent = { + STRETCH = "stretch", + FLEX_START = "flex-start", + FLEX_END = "flex-end", + CENTER = "center", + SPACE_BETWEEN = "space-between", + SPACE_AROUND = "space-around", +} + +local Positioning, FlexDirection, JustifyContent, AlignContent, AlignItems, TextAlign = + enums.Positioning, enums.FlexDirection, enums.JustifyContent, enums.AlignContent, enums.AlignItems, enums.TextAlign --- Top level GUI manager local Gui = { topWindows = {} } @@ -720,4 +829,4 @@ end Gui.Button = Button Gui.Window = Window -return Gui +return Gui, Color, enums diff --git a/README.md b/README.md index 3ad1dff..46664b0 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # FlexLove A Löve Gui based on Flexbox + +This is a single file lib, to use all you need is FlexLove.lua. But you shouldn't use this lib, it is way to early for sane use.