**LootiScript API Binding** - Color palette management and manipulation.
LootiScript API Binding - Color palette management and manipulation.
> Note: This package is used as an API binding for LootiScript in the l8b engine.
Create a new color palette.
``lua
// Create empty palette
local pal = Palette({})
// Create with colors
local pal = Palette({
name = "My Palette",
colors = {"#FF0000", "#00FF00", "#0000FF"}
})
`
Parameters:
- options (table) - Palette optionsname
- (string, optional) - Palette namecolors
- (array, optional) - Array of hex color strings
Get a color by index.
`lua
local red = pal.get(0) // Returns "#FF0000"
local green = pal.get(1) // Returns "#00FF00"
// Wraps around if index exceeds palette size
local color = pal.get(10) // Returns color at index 10 % size
`
Parameters:
- index (number) - Color index
Returns: Hex color string (e.g., "#FF0000")
Get a color as RGB object.
`lua
local rgb = pal.getRGB(0)
// Returns: {r = 255, g = 0, b = 0}
local r = rgb.r
local g = rgb.g
local b = rgb.b
`
Parameters:
- index (number) - Color index
Returns: RGB object {r, g, b}
Set a color at a specific index.
`lua
// Set color at index 0
pal.set(0, "#FFFFFF")
// Expands palette if index is beyond current size
pal.set(10, "#000000") // Fills gaps with #000000
`
Parameters:
- index (number) - Color indexcolor
- (string) - Hex color string
Add a color to the end of the palette.
`lua`
local index = pal.add("#FFFF00") // Returns new index
Parameters:
- color (string) - Hex color string
Returns: Index of added color
Remove a color at an index.
`lua`
pal.remove(2) // Removes color at index 2
Parameters:
- index (number) - Color index to remove
Get all colors in the palette.
`lua
local colors = pal.getAll()
// Returns: {"#FF0000", "#00FF00", "#0000FF"}
for i = 1, #colors do
Console.log(colors[i])
end
`
Returns: Array of hex color strings
Replace the entire palette.
`lua`
pal.setPalette({"#FF0000", "#00FF00", "#0000FF", "#FFFF00"})
Parameters:
- colors (array) - Array of hex color strings
Reset palette to original colors.
`lua
pal.reset()
// Or reset to specific palette data
pal.reset({
name = "New Palette",
colors = {"#000000", "#FFFFFF"}
})
`
Parameters:
- paletteData (table, optional) - Palette data to reset to
Create a lightened version of a color.
`lua
// Lighten color at index 0 by 20%
local lighter = pal.lighten(0, 0.2)
// Default amount is 0.2 (20%)
local lighter = pal.lighten(0)
`
Parameters:
- index (number) - Color indexamount
- (number, optional) - Lighten amount 0.0-1.0, default: 0.2
Returns: Hex color string
Create a darkened version of a color.
`lua
// Darken color at index 0 by 20%
local darker = pal.darken(0, 0.2)
// Default amount is 0.2 (20%)
local darker = pal.darken(0)
`
Parameters:
- index (number) - Color indexamount
- (number, optional) - Darken amount 0.0-1.0, default: 0.2
Returns: Hex color string
Mix two colors.
`lua
// Mix colors at index 0 and 1 (50/50)
local mixed = pal.mix(0, 1, 0.5)
// More of color 1 (70% color1, 30% color0)
local mixed = pal.mix(0, 1, 0.7)
`
Parameters:
- index1 (number) - First color indexindex2
- (number) - Second color indexratio
- (number, optional) - Mix ratio 0.0-1.0, default: 0.5
Returns: Hex color string
Create a gradient between two colors.
`lua
// Create 5-step gradient from color 0 to color 1
local colors = pal.gradient(0, 1, 5)
// Returns: {"#FF0000", "#BF3F00", "#7F7F00", "#3FBF00", "#00FF00"}
for i = 1, #colors do
Console.log(colors[i])
end
`
Parameters:
- startIndex (number) - Start color indexendIndex
- (number) - End color indexsteps
- (number) - Number of gradient steps
Returns: Array of hex color strings
Find the closest color in the palette to a target color.
`lua`
// Find closest color to red
local index = pal.findClosest("#FF0010")
Parameters:
- targetHex (string) - Target hex color
Returns: Index of closest color
Convert RGB values to hex color.
`lua`
local hex = Palette.rgbToHex(255, 128, 0)
// Returns: "#FF8000"
Parameters:
- r (number) - Red value 0-255g
- (number) - Green value 0-255b
- (number) - Blue value 0-255
Returns: Hex color string
`lua
// Palette size
local count = pal.size
// Palette name
local name = pal.paletteName
`
`lua
// Create a palette
local pal = Palette({
name = "Game Palette",
colors = {
"#000000", // Black
"#FFFFFF", // White
"#FF0000", // Red
"#00FF00", // Green
"#0000FF" // Blue
}
})
// Use colors
screen.setColor(pal.get(2)) // Red
screen.fillRect(10, 10, 50, 50)
// Create gradient
local gradient = pal.gradient(0, 1, 10)
for i = 1, #gradient do
screen.setColor(gradient[i])
screen.fillRect(i * 10, 100, 10, 50)
end
// Mix colors
local purple = pal.mix(2, 4, 0.5) // Mix red and blue
screen.setColor(purple)
screen.fillRect(100, 100, 50, 50)
``