test alignment
This commit is contained in:
170
examples/ThemeColorAccessDemo.lua
Normal file
170
examples/ThemeColorAccessDemo.lua
Normal file
@@ -0,0 +1,170 @@
|
||||
-- Theme Color Access Demo
|
||||
-- Demonstrates various ways to access and use theme colors
|
||||
|
||||
package.path = package.path .. ";./?.lua;../?.lua"
|
||||
|
||||
local FlexLove = require("FlexLove")
|
||||
local Theme = FlexLove.Theme
|
||||
local Gui = FlexLove.Gui
|
||||
local Color = FlexLove.Color
|
||||
|
||||
-- Initialize love stubs for testing
|
||||
love = {
|
||||
graphics = {
|
||||
newFont = function(size) return { getHeight = function() return size end } end,
|
||||
getFont = function() return { getHeight = function() return 12 end } end,
|
||||
getWidth = function() return 1920 end,
|
||||
getHeight = function() return 1080 end,
|
||||
newImage = function() return {} end,
|
||||
newQuad = function() return {} end,
|
||||
},
|
||||
}
|
||||
|
||||
print("=== Theme Color Access Demo ===\n")
|
||||
|
||||
-- Load and activate the space theme
|
||||
Theme.load("space")
|
||||
Theme.setActive("space")
|
||||
|
||||
print("1. Basic Color Access")
|
||||
print("---------------------")
|
||||
|
||||
-- Method 1: Using Theme.getColor() (Recommended)
|
||||
local primaryColor = Theme.getColor("primary")
|
||||
local secondaryColor = Theme.getColor("secondary")
|
||||
local textColor = Theme.getColor("text")
|
||||
local textDarkColor = Theme.getColor("textDark")
|
||||
|
||||
print(string.format("Primary: r=%.2f, g=%.2f, b=%.2f", primaryColor.r, primaryColor.g, primaryColor.b))
|
||||
print(string.format("Secondary: r=%.2f, g=%.2f, b=%.2f", secondaryColor.r, secondaryColor.g, secondaryColor.b))
|
||||
print(string.format("Text: r=%.2f, g=%.2f, b=%.2f", textColor.r, textColor.g, textColor.b))
|
||||
print(string.format("Text Dark: r=%.2f, g=%.2f, b=%.2f", textDarkColor.r, textDarkColor.g, textDarkColor.b))
|
||||
|
||||
print("\n2. Get All Available Colors")
|
||||
print("----------------------------")
|
||||
|
||||
-- Method 2: Get all color names
|
||||
local colorNames = Theme.getColorNames()
|
||||
if colorNames then
|
||||
print("Available colors in theme:")
|
||||
for _, name in ipairs(colorNames) do
|
||||
print(" - " .. name)
|
||||
end
|
||||
end
|
||||
|
||||
print("\n3. Get All Colors at Once")
|
||||
print("-------------------------")
|
||||
|
||||
-- Method 3: Get all colors as a table
|
||||
local allColors = Theme.getAllColors()
|
||||
if allColors then
|
||||
print("All colors:")
|
||||
for name, color in pairs(allColors) do
|
||||
print(string.format(" %s: r=%.2f, g=%.2f, b=%.2f, a=%.2f", name, color.r, color.g, color.b, color.a))
|
||||
end
|
||||
end
|
||||
|
||||
print("\n4. Safe Color Access with Fallback")
|
||||
print("-----------------------------------")
|
||||
|
||||
-- Method 4: Get color with fallback
|
||||
local accentColor = Theme.getColorOrDefault("accent", Color.new(1, 0, 0, 1)) -- Falls back to red
|
||||
local primaryColor2 = Theme.getColorOrDefault("primary", Color.new(1, 0, 0, 1)) -- Uses theme color
|
||||
|
||||
print(string.format("Accent (fallback): r=%.2f, g=%.2f, b=%.2f", accentColor.r, accentColor.g, accentColor.b))
|
||||
print(string.format("Primary (theme): r=%.2f, g=%.2f, b=%.2f", primaryColor2.r, primaryColor2.g, primaryColor2.b))
|
||||
|
||||
print("\n5. Using Colors in GUI Elements")
|
||||
print("--------------------------------")
|
||||
|
||||
-- Create a container with theme colors
|
||||
local container = Gui.new({
|
||||
width = 400,
|
||||
height = 300,
|
||||
backgroundColor = Theme.getColor("secondary"),
|
||||
positioning = FlexLove.enums.Positioning.FLEX,
|
||||
flexDirection = FlexLove.enums.FlexDirection.VERTICAL,
|
||||
gap = 10,
|
||||
padding = { top = 20, right = 20, bottom = 20, left = 20 },
|
||||
})
|
||||
|
||||
-- Create a button with primary color
|
||||
local button = Gui.new({
|
||||
parent = container,
|
||||
width = 360,
|
||||
height = 50,
|
||||
backgroundColor = Theme.getColor("primary"),
|
||||
textColor = Theme.getColor("text"),
|
||||
text = "Click Me!",
|
||||
textSize = 18,
|
||||
})
|
||||
|
||||
-- Create a text label with dark text
|
||||
local label = Gui.new({
|
||||
parent = container,
|
||||
width = 360,
|
||||
height = 30,
|
||||
backgroundColor = Theme.getColorOrDefault("background", Color.new(0.2, 0.2, 0.2, 1)),
|
||||
textColor = Theme.getColor("textDark"),
|
||||
text = "This is a label with dark text",
|
||||
textSize = 14,
|
||||
})
|
||||
|
||||
print("Created GUI elements with theme colors:")
|
||||
print(string.format(" Container: %d children", #container.children))
|
||||
print(string.format(" Button background: r=%.2f, g=%.2f, b=%.2f", button.backgroundColor.r, button.backgroundColor.g, button.backgroundColor.b))
|
||||
print(string.format(" Label text color: r=%.2f, g=%.2f, b=%.2f", label.textColor.r, label.textColor.g, label.textColor.b))
|
||||
|
||||
print("\n6. Creating Color Variations")
|
||||
print("-----------------------------")
|
||||
|
||||
-- Create variations of theme colors
|
||||
local primaryDark = Color.new(
|
||||
primaryColor.r * 0.7,
|
||||
primaryColor.g * 0.7,
|
||||
primaryColor.b * 0.7,
|
||||
primaryColor.a
|
||||
)
|
||||
|
||||
local primaryLight = Color.new(
|
||||
math.min(1, primaryColor.r * 1.3),
|
||||
math.min(1, primaryColor.g * 1.3),
|
||||
math.min(1, primaryColor.b * 1.3),
|
||||
primaryColor.a
|
||||
)
|
||||
|
||||
local primaryTransparent = Color.new(
|
||||
primaryColor.r,
|
||||
primaryColor.g,
|
||||
primaryColor.b,
|
||||
0.5
|
||||
)
|
||||
|
||||
print(string.format("Primary (original): r=%.2f, g=%.2f, b=%.2f, a=%.2f", primaryColor.r, primaryColor.g, primaryColor.b, primaryColor.a))
|
||||
print(string.format("Primary (dark): r=%.2f, g=%.2f, b=%.2f, a=%.2f", primaryDark.r, primaryDark.g, primaryDark.b, primaryDark.a))
|
||||
print(string.format("Primary (light): r=%.2f, g=%.2f, b=%.2f, a=%.2f", primaryLight.r, primaryLight.g, primaryLight.b, primaryLight.a))
|
||||
print(string.format("Primary (50%% alpha): r=%.2f, g=%.2f, b=%.2f, a=%.2f", primaryTransparent.r, primaryTransparent.g, primaryTransparent.b, primaryTransparent.a))
|
||||
|
||||
print("\n7. Quick Reference")
|
||||
print("------------------")
|
||||
print([[
|
||||
// Basic usage:
|
||||
local color = Theme.getColor("primary")
|
||||
|
||||
// With fallback:
|
||||
local color = Theme.getColorOrDefault("accent", Color.new(1, 0, 0, 1))
|
||||
|
||||
// Get all colors:
|
||||
local colors = Theme.getAllColors()
|
||||
|
||||
// Get color names:
|
||||
local names = Theme.getColorNames()
|
||||
|
||||
// Use in elements:
|
||||
local button = Gui.new({
|
||||
backgroundColor = Theme.getColor("primary"),
|
||||
textColor = Theme.getColor("text"),
|
||||
})
|
||||
]])
|
||||
|
||||
print("\n=== Demo Complete ===")
|
||||
181
examples/ThemeColorAccessSimple.lua
Normal file
181
examples/ThemeColorAccessSimple.lua
Normal file
@@ -0,0 +1,181 @@
|
||||
-- Simple Theme Color Access Demo
|
||||
-- Shows how to access theme colors without creating GUI elements
|
||||
|
||||
package.path = package.path .. ";./?.lua;../?.lua"
|
||||
|
||||
local FlexLove = require("FlexLove")
|
||||
local Theme = FlexLove.Theme
|
||||
local Color = FlexLove.Color
|
||||
|
||||
-- Initialize minimal love stubs
|
||||
love = {
|
||||
graphics = {
|
||||
newFont = function(size) return { getHeight = function() return size end } end,
|
||||
newImage = function() return {} end,
|
||||
newQuad = function() return {} end,
|
||||
},
|
||||
}
|
||||
|
||||
print("=== Theme Color Access - Simple Demo ===\n")
|
||||
|
||||
-- Load and activate the space theme
|
||||
Theme.load("space")
|
||||
Theme.setActive("space")
|
||||
|
||||
print("✓ Theme 'space' loaded and activated\n")
|
||||
|
||||
-- ============================================
|
||||
-- METHOD 1: Basic Color Access (Recommended)
|
||||
-- ============================================
|
||||
print("METHOD 1: Theme.getColor(colorName)")
|
||||
print("------------------------------------")
|
||||
|
||||
local primaryColor = Theme.getColor("primary")
|
||||
local secondaryColor = Theme.getColor("secondary")
|
||||
local textColor = Theme.getColor("text")
|
||||
local textDarkColor = Theme.getColor("textDark")
|
||||
|
||||
print(string.format("primary = Color(r=%.2f, g=%.2f, b=%.2f, a=%.2f)",
|
||||
primaryColor.r, primaryColor.g, primaryColor.b, primaryColor.a))
|
||||
print(string.format("secondary = Color(r=%.2f, g=%.2f, b=%.2f, a=%.2f)",
|
||||
secondaryColor.r, secondaryColor.g, secondaryColor.b, secondaryColor.a))
|
||||
print(string.format("text = Color(r=%.2f, g=%.2f, b=%.2f, a=%.2f)",
|
||||
textColor.r, textColor.g, textColor.b, textColor.a))
|
||||
print(string.format("textDark = Color(r=%.2f, g=%.2f, b=%.2f, a=%.2f)",
|
||||
textDarkColor.r, textDarkColor.g, textDarkColor.b, textDarkColor.a))
|
||||
|
||||
-- ============================================
|
||||
-- METHOD 2: Get All Color Names
|
||||
-- ============================================
|
||||
print("\nMETHOD 2: Theme.getColorNames()")
|
||||
print("--------------------------------")
|
||||
|
||||
local colorNames = Theme.getColorNames()
|
||||
print("Available colors:")
|
||||
for i, name in ipairs(colorNames) do
|
||||
print(string.format(" %d. %s", i, name))
|
||||
end
|
||||
|
||||
-- ============================================
|
||||
-- METHOD 3: Get All Colors at Once
|
||||
-- ============================================
|
||||
print("\nMETHOD 3: Theme.getAllColors()")
|
||||
print("-------------------------------")
|
||||
|
||||
local allColors = Theme.getAllColors()
|
||||
print("All colors with values:")
|
||||
for name, color in pairs(allColors) do
|
||||
print(string.format(" %-10s = (%.2f, %.2f, %.2f, %.2f)",
|
||||
name, color.r, color.g, color.b, color.a))
|
||||
end
|
||||
|
||||
-- ============================================
|
||||
-- METHOD 4: Safe Access with Fallback
|
||||
-- ============================================
|
||||
print("\nMETHOD 4: Theme.getColorOrDefault(colorName, fallback)")
|
||||
print("-------------------------------------------------------")
|
||||
|
||||
-- Try to get a color that exists
|
||||
local existingColor = Theme.getColorOrDefault("primary", Color.new(1, 0, 0, 1))
|
||||
print(string.format("Existing color 'primary': (%.2f, %.2f, %.2f) ✓",
|
||||
existingColor.r, existingColor.g, existingColor.b))
|
||||
|
||||
-- Try to get a color that doesn't exist (will use fallback)
|
||||
local missingColor = Theme.getColorOrDefault("accent", Color.new(1, 0, 0, 1))
|
||||
print(string.format("Missing color 'accent' (fallback): (%.2f, %.2f, %.2f) ✓",
|
||||
missingColor.r, missingColor.g, missingColor.b))
|
||||
|
||||
-- ============================================
|
||||
-- PRACTICAL EXAMPLES
|
||||
-- ============================================
|
||||
print("\n=== Practical Usage Examples ===\n")
|
||||
|
||||
print("Example 1: Using colors in element creation")
|
||||
print("--------------------------------------------")
|
||||
print([[
|
||||
local button = Gui.new({
|
||||
width = 200,
|
||||
height = 50,
|
||||
backgroundColor = Theme.getColor("primary"),
|
||||
textColor = Theme.getColor("text"),
|
||||
text = "Click Me!"
|
||||
})
|
||||
]])
|
||||
|
||||
print("\nExample 2: Creating color variations")
|
||||
print("-------------------------------------")
|
||||
print([[
|
||||
local primary = Theme.getColor("primary")
|
||||
|
||||
-- Darker version (70% brightness)
|
||||
local primaryDark = Color.new(
|
||||
primary.r * 0.7,
|
||||
primary.g * 0.7,
|
||||
primary.b * 0.7,
|
||||
primary.a
|
||||
)
|
||||
|
||||
-- Lighter version (130% brightness)
|
||||
local primaryLight = Color.new(
|
||||
math.min(1, primary.r * 1.3),
|
||||
math.min(1, primary.g * 1.3),
|
||||
math.min(1, primary.b * 1.3),
|
||||
primary.a
|
||||
)
|
||||
|
||||
-- Semi-transparent version
|
||||
local primaryTransparent = Color.new(
|
||||
primary.r,
|
||||
primary.g,
|
||||
primary.b,
|
||||
0.5 -- 50% opacity
|
||||
)
|
||||
]])
|
||||
|
||||
print("\nExample 3: Safe color access")
|
||||
print("-----------------------------")
|
||||
print([[
|
||||
-- With fallback to white if color doesn't exist
|
||||
local bgColor = Theme.getColorOrDefault("background", Color.new(1, 1, 1, 1))
|
||||
|
||||
-- With fallback to theme's secondary color
|
||||
local borderColor = Theme.getColorOrDefault(
|
||||
"border",
|
||||
Theme.getColor("secondary")
|
||||
)
|
||||
]])
|
||||
|
||||
print("\nExample 4: Dynamic color selection")
|
||||
print("-----------------------------------")
|
||||
print([[
|
||||
-- Get all available colors
|
||||
local colors = Theme.getAllColors()
|
||||
|
||||
-- Pick a random color
|
||||
local colorNames = {}
|
||||
for name in pairs(colors) do
|
||||
table.insert(colorNames, name)
|
||||
end
|
||||
local randomColorName = colorNames[math.random(#colorNames)]
|
||||
local randomColor = colors[randomColorName]
|
||||
]])
|
||||
|
||||
print("\n=== Quick Reference ===\n")
|
||||
print("Theme.getColor(name) -- Get a specific color")
|
||||
print("Theme.getColorOrDefault(n, fb) -- Get color with fallback")
|
||||
print("Theme.getAllColors() -- Get all colors as table")
|
||||
print("Theme.getColorNames() -- Get array of color names")
|
||||
print("Theme.hasActive() -- Check if theme is active")
|
||||
print("Theme.getActive() -- Get active theme object")
|
||||
|
||||
print("\n=== Available Colors in 'space' Theme ===\n")
|
||||
for i, name in ipairs(colorNames) do
|
||||
local color = allColors[name]
|
||||
print(string.format("%-10s RGB(%.0f, %.0f, %.0f)",
|
||||
name,
|
||||
color.r * 255,
|
||||
color.g * 255,
|
||||
color.b * 255))
|
||||
end
|
||||
|
||||
print("\n=== Demo Complete ===")
|
||||
Reference in New Issue
Block a user