mirror of
https://github.com/orange-cpp/omath.git
synced 2026-04-18 17:03:27 +00:00
added color
This commit is contained in:
51
tests/general/unit_test_lua_color.cpp
Normal file
51
tests/general/unit_test_lua_color.cpp
Normal 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
96
tests/lua/color_tests.lua
Normal 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
|
||||
Reference in New Issue
Block a user