mirror of
https://github.com/Insality/druid.git
synced 2025-11-26 10:50:52 +01:00
Update color
This commit is contained in:
@@ -1,18 +1,21 @@
|
|||||||
---@alias color vector4|vector3|string
|
---@alias color vector4|vector3|string
|
||||||
|
|
||||||
|
---Color palette and utility functions for working with colors.
|
||||||
|
---Supports palette management, hex conversion, RGB/HSB conversion, and color interpolation.
|
||||||
|
---@class druid.palette
|
||||||
|
local M = {}
|
||||||
|
|
||||||
local PALETTE_DATA = {}
|
local PALETTE_DATA = {}
|
||||||
local COLOR_WHITE = vmath.vector4(1, 1, 1, 1)
|
local COLOR_WHITE = vmath.vector4(1, 1, 1, 1)
|
||||||
local COLOR_X = hash("color.x")
|
local COLOR_X = hash("color.x")
|
||||||
local COLOR_Y = hash("color.y")
|
local COLOR_Y = hash("color.y")
|
||||||
local COLOR_Z = hash("color.z")
|
local COLOR_Z = hash("color.z")
|
||||||
local ALPHA = hash("color.w")
|
|
||||||
|
|
||||||
local M = {}
|
|
||||||
|
|
||||||
|
|
||||||
---Get color by string (hex or from palette)
|
---Get color by ID from palette, hex string, or return vector as-is.
|
||||||
---@param color_id string|vector4 Color id from palette or hex color
|
---If color_id is not found in palette and not a hex string, returns white.
|
||||||
---@return vector4
|
---@param color_id string|vector4|vector3 Color id from palette, hex color string, or vector
|
||||||
|
---@return vector4|vector3
|
||||||
function M.get_color(color_id)
|
function M.get_color(color_id)
|
||||||
if type(color_id) ~= "string" then
|
if type(color_id) ~= "string" then
|
||||||
return color_id
|
return color_id
|
||||||
@@ -33,8 +36,8 @@ function M.get_color(color_id)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Add palette to palette data
|
---Add colors to palette. Colors can be hex strings or vector4 values.
|
||||||
---@param palette_data table<string, vector4>
|
---@param palette_data table<string, vector4|string> Table with color IDs as keys
|
||||||
function M.add_palette(palette_data)
|
function M.add_palette(palette_data)
|
||||||
for color_id, color in pairs(palette_data) do
|
for color_id, color in pairs(palette_data) do
|
||||||
if type(color) == "string" then
|
if type(color) == "string" then
|
||||||
@@ -46,14 +49,16 @@ function M.add_palette(palette_data)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
---Get all palette colors.
|
||||||
|
---@return table<string, vector4>
|
||||||
function M.get_palette()
|
function M.get_palette()
|
||||||
return PALETTE_DATA
|
return PALETTE_DATA
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Set color of gui node without changing alpha
|
---Set GUI node color. Does not change alpha.
|
||||||
---@param gui_node node
|
---@param gui_node node
|
||||||
---@param color vector4|vector3|string Color in vector4, vector3 or color id from palette
|
---@param color vector4|vector3|string
|
||||||
function M.set_color(gui_node, color)
|
function M.set_color(gui_node, color)
|
||||||
if type(color) == "string" then
|
if type(color) == "string" then
|
||||||
color = M.get_color(color)
|
color = M.get_color(color)
|
||||||
@@ -65,11 +70,11 @@ function M.set_color(gui_node, color)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Lerp colors via color HSB values
|
---Interpolate between two colors using HSB space (better visual results than RGB).
|
||||||
---@param t number Lerp value. 0 - color1, 1 - color2
|
---@param t number Lerp value (0 = color1, 1 = color2)
|
||||||
---@param color1 vector4 Color 1
|
---@param color1 vector4
|
||||||
---@param color2 vector4 Color 2
|
---@param color2 vector4
|
||||||
---@return vector4 result Color between color1 and color2
|
---@return vector4
|
||||||
function M.lerp(t, color1, color2)
|
function M.lerp(t, color1, color2)
|
||||||
local h1, s1, v1 = M.rgb2hsb(color1.x, color1.y, color1.z)
|
local h1, s1, v1 = M.rgb2hsb(color1.x, color1.y, color1.z)
|
||||||
local h2, s2, v2 = M.rgb2hsb(color2.x, color2.y, color2.z)
|
local h2, s2, v2 = M.rgb2hsb(color2.x, color2.y, color2.z)
|
||||||
@@ -84,8 +89,8 @@ function M.lerp(t, color1, color2)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Convert hex color to rgb values.
|
---Convert hex string to RGB values (0-1 range). Supports #RGB and #RRGGBB formats.
|
||||||
---@param hex string Hex color. #00BBAA or 00BBAA or #0BA or 0BA
|
---@param hex string
|
||||||
---@return number, number, number
|
---@return number, number, number
|
||||||
function M.hex2rgb(hex)
|
function M.hex2rgb(hex)
|
||||||
if not hex or #hex < 3 then
|
if not hex or #hex < 3 then
|
||||||
@@ -102,9 +107,9 @@ function M.hex2rgb(hex)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Convert hex color to vector4.
|
---Convert hex string to vector4.
|
||||||
---@param hex string Hex color. #00BBAA or 00BBAA or #0BA or 0BA
|
---@param hex string
|
||||||
---@param alpha number|nil Alpha value. Default is 1
|
---@param alpha number|nil Default is 1
|
||||||
---@return vector4
|
---@return vector4
|
||||||
function M.hex2vector4(hex, alpha)
|
function M.hex2vector4(hex, alpha)
|
||||||
local r, g, b = M.hex2rgb(hex)
|
local r, g, b = M.hex2rgb(hex)
|
||||||
@@ -112,11 +117,12 @@ function M.hex2vector4(hex, alpha)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Convert hsb color to rgb colo
|
---Convert RGB to HSB.
|
||||||
---@param r number Red value
|
---@param r number
|
||||||
---@param g number Green value
|
---@param g number
|
||||||
---@param b number Blue value
|
---@param b number
|
||||||
---@param alpha number|nil Alpha value. Default is 1
|
---@param alpha number|nil
|
||||||
|
---@return number, number, number, number
|
||||||
function M.rgb2hsb(r, g, b, alpha)
|
function M.rgb2hsb(r, g, b, alpha)
|
||||||
alpha = alpha or 1
|
alpha = alpha or 1
|
||||||
local min, max = math.min(r, g, b), math.max(r, g, b)
|
local min, max = math.min(r, g, b), math.max(r, g, b)
|
||||||
@@ -136,17 +142,16 @@ function M.rgb2hsb(r, g, b, alpha)
|
|||||||
h = (h / 6) % 1
|
h = (h / 6) % 1
|
||||||
end
|
end
|
||||||
|
|
||||||
alpha = alpha > 1 and alpha / 100 or alpha
|
|
||||||
|
|
||||||
return h, s, v, alpha
|
return h, s, v, alpha
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Convert hsb color to rgb color
|
---Convert HSB to RGB.
|
||||||
---@param h number Hue
|
---@param h number
|
||||||
---@param s number Saturation
|
---@param s number
|
||||||
---@param v number Value
|
---@param v number
|
||||||
---@param alpha number|nil Alpha value. Default is 1
|
---@param alpha number|nil
|
||||||
|
---@return number, number, number, number|nil
|
||||||
function M.hsb2rgb(h, s, v, alpha)
|
function M.hsb2rgb(h, s, v, alpha)
|
||||||
local r, g, b
|
local r, g, b
|
||||||
local i = math.floor(h * 6)
|
local i = math.floor(h * 6)
|
||||||
@@ -169,10 +174,11 @@ function M.hsb2rgb(h, s, v, alpha)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---Convert rgb color to hex color
|
---Convert RGB to hex string (uppercase, without #).
|
||||||
---@param red number Red value
|
---@param red number
|
||||||
---@param green number Green value
|
---@param green number
|
||||||
---@param blue number Blue value
|
---@param blue number
|
||||||
|
---@return string
|
||||||
function M.rgb2hex(red, green, blue)
|
function M.rgb2hex(red, green, blue)
|
||||||
local r = string.format("%x", math.floor(red * 255))
|
local r = string.format("%x", math.floor(red * 255))
|
||||||
local g = string.format("%x", math.floor(green * 255))
|
local g = string.format("%x", math.floor(green * 255))
|
||||||
@@ -181,15 +187,14 @@ function M.rgb2hex(red, green, blue)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
-- Auto-load palette from config if druid.palette_path is set in game.project
|
||||||
local DEFAULT_PALETTE_PATH = sys.get_config_string("druid.palette_path")
|
local DEFAULT_PALETTE_PATH = sys.get_config_string("druid.palette_path")
|
||||||
if DEFAULT_PALETTE_PATH then
|
if DEFAULT_PALETTE_PATH then
|
||||||
local loaded_palette = sys.load_resource(DEFAULT_PALETTE_PATH)
|
local loaded_palette = sys.load_resource(DEFAULT_PALETTE_PATH)
|
||||||
local data = loaded_palette and json.decode(loaded_palette)
|
local data = loaded_palette and json.decode(loaded_palette)
|
||||||
if not data then
|
if data then
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
M.add_palette(data)
|
M.add_palette(data)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user