---------------------------------------------------------------------------
-- Voxelis - Voxel survival sandbox for Luanti
-- Copyright (C) 2024 Mad Star Studio LLC
-- 
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 3 of the License, or
-- (at your option) any later version.
--
-- You should have received a copy of the GNU General Public License along
-- with this program; if not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------

vox_colors = {
    WHITE       = "#FFFFFF",
    LIGHTGREY   = "#D3D3D3",
    SILVER      = "#C0C0C0",
    GREY        = "#808080",
    BLACK       = "#000000",
    RED         = "#FF0000",
    DARK_RED    = "#8B0000",
    YELLOW      = "#FFFF00",
    GOLD        = "#FFD700",
    ORANGE      = "#FFA500",
    PUMPKIN     = "#FF7518",
    CREAM       = "#FFFDD0",
    TAN         = "#D2B48C",
    BROWN       = "#A52A2A",
    DARK_BROWN  = "#8B4513",
    LIME        = "#00FF00",
    MINT        = "#98FF98",
    EMERALD     = "#50C878",
    DARK_GREEN  = "#006400",
    TURQUOISE   = "#40E0D0",
    TEAL        = "#008080",
    CYAN        = "#00FFFF",
    BLUE        = "#0000FF",
    NAVY        = "#000080",
    MAGENTA     = "#FF00FF",
    VIOLET      = "#EE82EE",
    PURPLE      = "#800080",
    INDIGO      = "#4B0082",
    PINK        = "#FF69B4",
    FLAMINGO    = "#FC8EAC",
}

vox_colors.bg_brightness = 0.8;

function vox_colors.bg(color)
    return vox_colors.multiplier(color, vox_colors.bg_brightness);
end

-- Validate that the provided color string is a valid hex color
function vox_colors.validate(color)
    if color == nil then
        return false;
    end

    -- Hex colors should always be a length of 7 (1 pound, 6 hex)
    if string.len(color) ~= 7 then
        return false;
    end

    -- Ensure first character is a pound
    if string.sub(color, 1, 1) ~= "#" then
        return false;
    end
    
    -- Ensure that RR, GG, and BB are valid hex fields
    if (string.match(string.sub(color, 2, 3), "[0-9A-Fa-f]") == nil) then
        return false;
    end

    return true;
end

-- Extract color information from the color and return a [ Number, Number, Number ] array
function vox_colors.extract_channels(color)
    local result = {};

    result[0] = tonumber(string.sub(color, 2, 3), 16);
    result[1] = tonumber(string.sub(color, 4, 5), 16);
    result[2] = tonumber(string.sub(color, 6, 7), 16);

    return result;
end

function vox_colors.from_channels(red, green, blue)
    return "#" .. string.format("%02X", red) .. string.format("%02X", green) .. string.format("%02X", blue);
end

-- Multiply the color by a percentage
function vox_colors.multiplier(color, percentage) 
    local channels = vox_colors.extract_channels(color);
    return vox_colors.from_channels(
        math.floor(channels.red * percentage),
        math.floor(channels.green * percentage),
        math.floor(channels.blue * percentage)
    );
end