added color

This commit is contained in:
2026-03-08 23:08:23 +03:00
parent 943472cf64
commit afb2a13dd6
3 changed files with 187 additions and 0 deletions

View File

@@ -22,6 +22,7 @@
#include <omath/linear_algebra/vector2.hpp>
#include <omath/linear_algebra/vector3.hpp>
#include <omath/linear_algebra/vector4.hpp>
#include <omath/utility/color.hpp>
#include <omath/projection/camera.hpp>
#include <omath/projection/error_codes.hpp>
#include <sol/sol.hpp>
@@ -197,6 +198,44 @@ namespace
return "unknown error";
}
void register_color(sol::table& omath_table)
{
omath_table.new_usertype<omath::Color>(
"Color",
sol::factories(
[](float r, float g, float b, float a) { return omath::Color(r, g, b, a); },
[]() { return omath::Color(); }),
"from_rgba", [](uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
return omath::Color::from_rgba(r, g, b, a);
},
"from_hsv", sol::overload(
[](float h, float s, float v) { return omath::Color::from_hsv(h, s, v); },
[](const omath::Hsv& hsv) { return omath::Color::from_hsv(hsv); }),
"red", []() { return omath::Color::red(); },
"green", []() { return omath::Color::green(); },
"blue", []() { return omath::Color::blue(); },
"r", sol::property([](const omath::Color& c) { return c.value().x; }),
"g", sol::property([](const omath::Color& c) { return c.value().y; }),
"b", sol::property([](const omath::Color& c) { return c.value().z; }),
"a", sol::property([](const omath::Color& c) { return c.value().w; }),
"to_hsv", &omath::Color::to_hsv,
"set_hue", &omath::Color::set_hue,
"set_saturation", &omath::Color::set_saturation,
"set_value", &omath::Color::set_value,
"blend", &omath::Color::blend,
sol::meta_function::to_string, &omath::Color::to_string);
omath_table.new_usertype<omath::Hsv>(
"Hsv", sol::constructors<omath::Hsv()>(),
"hue", &omath::Hsv::hue,
"saturation", &omath::Hsv::saturation,
"value", &omath::Hsv::value);
}
template<class AngleType>
void register_angle(sol::table& table, const char* name)
{
@@ -398,6 +437,7 @@ namespace omath::lua
register_vec2(omath_table);
register_vec3(omath_table);
register_vec4(omath_table);
register_color(omath_table);
register_shared_types(omath_table);
register_engine<OpenGLEngineTraits>(omath_table, "opengl");
register_engine<FrostbiteEngineTraits>(omath_table, "frostbite");

View File

@@ -0,0 +1,51 @@
//
// Created by orange on 08.03.2026.
//
#include <gtest/gtest.h>
#include <lua.hpp>
#include <omath/lua/lua.hpp>
class LuaColor : public ::testing::Test
{
protected:
lua_State* L = nullptr;
void SetUp() override
{
L = luaL_newstate();
luaL_openlibs(L);
omath::lua::register_lib(L);
if (luaL_dofile(L, LUA_SCRIPTS_DIR "/color_tests.lua") != LUA_OK)
FAIL() << lua_tostring(L, -1);
}
void TearDown() override { lua_close(L); }
void check(const char* func_name)
{
lua_getglobal(L, func_name);
if (lua_pcall(L, 0, 0, 0) != LUA_OK)
{
FAIL() << lua_tostring(L, -1);
lua_pop(L, 1);
}
}
};
TEST_F(LuaColor, Constructor_float) { check("Color_Constructor_float"); }
TEST_F(LuaColor, Constructor_default) { check("Color_Constructor_default"); }
TEST_F(LuaColor, Constructor_clamping) { check("Color_Constructor_clamping"); }
TEST_F(LuaColor, from_rgba) { check("Color_from_rgba"); }
TEST_F(LuaColor, from_hsv_components) { check("Color_from_hsv_components"); }
TEST_F(LuaColor, from_hsv_struct) { check("Color_from_hsv_struct"); }
TEST_F(LuaColor, red) { check("Color_red"); }
TEST_F(LuaColor, green) { check("Color_green"); }
TEST_F(LuaColor, blue) { check("Color_blue"); }
TEST_F(LuaColor, to_hsv) { check("Color_to_hsv"); }
TEST_F(LuaColor, set_hue) { check("Color_set_hue"); }
TEST_F(LuaColor, set_saturation) { check("Color_set_saturation"); }
TEST_F(LuaColor, set_value) { check("Color_set_value"); }
TEST_F(LuaColor, blend) { check("Color_blend"); }
TEST_F(LuaColor, blend_clamped_ratio) { check("Color_blend_clamped_ratio"); }
TEST_F(LuaColor, to_string) { check("Color_to_string"); }
TEST_F(LuaColor, Hsv_fields) { check("Hsv_fields"); }

96
tests/lua/color_tests.lua Normal file
View File

@@ -0,0 +1,96 @@
local function approx(a, b, eps) return math.abs(a - b) < (eps or 1e-4) end
function Color_Constructor_float()
local c = omath.Color.new(1, 0.5, 0.25, 1)
assert(approx(c.r, 1) and approx(c.g, 0.5) and approx(c.b, 0.25) and approx(c.a, 1))
end
function Color_Constructor_default()
local c = omath.Color.new()
assert(c ~= nil)
end
function Color_Constructor_clamping()
local c = omath.Color.new(2, -1, 0.5, 1)
assert(approx(c.r, 1) and approx(c.g, 0) and approx(c.b, 0.5))
end
function Color_from_rgba()
local c = omath.Color.from_rgba(255, 128, 0, 255)
assert(approx(c.r, 1) and approx(c.g, 128/255) and approx(c.b, 0) and approx(c.a, 1))
end
function Color_from_hsv_components()
local c = omath.Color.from_hsv(0, 1, 1)
assert(approx(c.r, 1) and approx(c.g, 0) and approx(c.b, 0))
end
function Color_from_hsv_struct()
local hsv = omath.Hsv.new()
hsv.hue = 0
hsv.saturation = 1
hsv.value = 1
local c = omath.Color.from_hsv(hsv)
assert(approx(c.r, 1) and approx(c.g, 0) and approx(c.b, 0))
end
function Color_red()
local c = omath.Color.red()
assert(approx(c.r, 1) and approx(c.g, 0) and approx(c.b, 0) and approx(c.a, 1))
end
function Color_green()
local c = omath.Color.green()
assert(approx(c.r, 0) and approx(c.g, 1) and approx(c.b, 0) and approx(c.a, 1))
end
function Color_blue()
local c = omath.Color.blue()
assert(approx(c.r, 0) and approx(c.g, 0) and approx(c.b, 1) and approx(c.a, 1))
end
function Color_to_hsv()
local hsv = omath.Color.red():to_hsv()
assert(approx(hsv.hue, 0) and approx(hsv.saturation, 1) and approx(hsv.value, 1))
end
function Color_set_hue()
local c = omath.Color.red()
c:set_hue(1/3)
assert(approx(c.g, 1, 1e-3))
end
function Color_set_saturation()
local c = omath.Color.red()
c:set_saturation(0)
assert(approx(c.r, c.g) and approx(c.g, c.b))
end
function Color_set_value()
local c = omath.Color.red()
c:set_value(0)
assert(approx(c.r, 0) and approx(c.g, 0) and approx(c.b, 0))
end
function Color_blend()
local c = omath.Color.red():blend(omath.Color.blue(), 0.5)
assert(approx(c.r, 0.5) and approx(c.b, 0.5))
end
function Color_blend_clamped_ratio()
local c = omath.Color.red():blend(omath.Color.blue(), 2.0)
assert(approx(c.r, 0) and approx(c.b, 1))
end
function Color_to_string()
local s = tostring(omath.Color.red())
assert(s == "[r:255, g:0, b:0, a:255]")
end
function Hsv_fields()
local hsv = omath.Hsv.new()
hsv.hue = 0.5
hsv.saturation = 0.8
hsv.value = 0.9
assert(approx(hsv.hue, 0.5) and approx(hsv.saturation, 0.8) and approx(hsv.value, 0.9))
end