---------------------------------------------------------------------------
-- 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 = {
    -- Whites and Grays
    WHITE       = "#FFFFFF",
    CREAM       = "#FFFDD0",
    LIGHTGREY   = "#D3D3D3",
    SILVER      = "#C0C0C0",
    GREY        = "#808080",
    DARK_GRAY   = "#A9A9A9",
    BLACK       = "#000000",

    -- Reds
    PINK        = "#FF69B4",
    FLAMINGO    = "#FC8EAC",
    RED         = "#FF0000",
    CRIMSON     = "#DC143C",
    DARK_RED    = "#8B0000",
    MAROON      = "#800000",

    -- Oranges and Yellows
    PEACH       = "#FFDAB9",
    GOLD        = "#FFD700",
    YELLOW      = "#FFFF00",
    ORANGE      = "#FFA500",
    PUMPKIN     = "#FF7518",
    TAN         = "#D2B48C",
    BROWN       = "#A52A2A",
    DARK_BROWN  = "#8B4513",

    -- Greens
    LIGHT_GREEN = "#90EE90",
    MINT        = "#98FF98",
    LIME        = "#00FF00",
    GREEN       = "#008000",
    EMERALD     = "#50C878",
    OLIVE       = "#808000",
    DARK_GREEN  = "#006400",

    -- Blues
    LIGHT_BLUE  = "#ADD8E6",
    CYAN        = "#00FFFF",
    TURQUOISE   = "#40E0D0",
    TEAL        = "#008080",
    BLUE        = "#0000FF",
    NAVY        = "#000080",
    INDIGO      = "#4B0082",

    -- Purples
    LAVENDER    = "#E6E6FA",
    VIOLET      = "#EE82EE",
    MAGENTA     = "#FF00FF",
    PURPLE      = "#800080",
}

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