1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-06-26 10:41:33 +00:00

Use the Color type for Light colours in Lua

This commit is contained in:
Evil Eye 2025-06-12 17:17:28 +02:00
parent b988190fba
commit be0cbb7277
5 changed files with 43 additions and 24 deletions

View file

@ -82,7 +82,7 @@ message(STATUS "Configuring OpenMW...")
set(OPENMW_VERSION_MAJOR 0)
set(OPENMW_VERSION_MINOR 49)
set(OPENMW_VERSION_RELEASE 0)
set(OPENMW_LUA_API_REVISION 75)
set(OPENMW_LUA_API_REVISION 76)
set(OPENMW_POSTPROCESSING_API_REVISION 2)
set(OPENMW_VERSION_COMMITHASH "")

View file

@ -5,6 +5,7 @@
#include <components/esm3/loadligh.hpp>
#include <components/lua/luastate.hpp>
#include <components/lua/util.hpp>
#include <components/misc/color.hpp>
#include <components/misc/resourcehelpers.hpp>
#include <components/resource/resourcesystem.hpp>
@ -62,7 +63,13 @@ namespace
if (rec["radius"] != sol::nil)
light.mData.mRadius = rec["radius"];
if (rec["color"] != sol::nil)
light.mData.mColor = rec["color"];
{
sol::object color = rec["color"];
if (color.is<Misc::Color>())
light.mData.mColor = color.as<Misc::Color>().toRGBA();
else
light.mData.mColor = color.as<uint32_t>();
}
setRecordFlag(rec, "isCarriable", ESM::Light::Carry, light);
setRecordFlag(rec, "isDynamic", ESM::Light::Dynamic, light);
setRecordFlag(rec, "isFire", ESM::Light::Fire, light);
@ -104,7 +111,8 @@ namespace MWLua
record["value"] = sol::readonly_property([](const ESM::Light& rec) -> int { return rec.mData.mValue; });
record["duration"] = sol::readonly_property([](const ESM::Light& rec) -> int { return rec.mData.mTime; });
record["radius"] = sol::readonly_property([](const ESM::Light& rec) -> int { return rec.mData.mRadius; });
record["color"] = sol::readonly_property([](const ESM::Light& rec) -> int { return rec.mData.mColor; });
record["color"] = sol::readonly_property(
[](const ESM::Light& rec) -> Misc::Color { return Misc::Color::fromRGB(rec.mData.mColor); });
record["isCarriable"] = sol::readonly_property(
[](const ESM::Light& rec) -> bool { return rec.mData.mFlags & ESM::Light::Carry; });
record["isDynamic"] = sol::readonly_property(

View file

@ -6,13 +6,12 @@
#include <sstream>
#include <system_error>
#include <components/sceneutil/util.hpp>
namespace Misc
{
Color::Color(float r, float g, float b, float a)
: mR(std::clamp(r, 0.f, 1.f))
, mG(std::clamp(g, 0.f, 1.f))
, mB(std::clamp(b, 0.f, 1.f))
, mA(std::clamp(a, 0.f, 1.f))
: mValue(std::clamp(r, 0.f, 1.f), std::clamp(g, 0.f, 1.f), std::clamp(b, 0.f, 1.f), std::clamp(a, 0.f, 1.f))
{
}
@ -27,26 +26,31 @@ namespace Misc
{
if (hex.size() != 6)
throw std::logic_error(std::string("Invalid hex color: ") += hex);
std::array<float, 3> rgb;
for (size_t i = 0; i < rgb.size(); i++)
Color col;
col.mValue.a() = 1;
for (size_t i = 0; i < 3; i++)
{
auto sub = hex.substr(i * 2, 2);
int v = 0;
auto [_, ec] = std::from_chars(sub.data(), sub.data() + sub.size(), v, 16);
if (ec != std::errc())
throw std::logic_error(std::string("Invalid hex color: ") += hex);
rgb[i] = v / 255.0f;
col.mValue[i] = v / 255.0f;
}
return Color(rgb[0], rgb[1], rgb[2], 1);
return col;
}
Color Color::fromRGB(unsigned int value)
{
return Color(SceneUtil::colourFromRGB(value));
}
std::string Color::toHex() const
{
std::string result(6, '0');
std::array<float, 3> rgb = { mR, mG, mB };
for (size_t i = 0; i < rgb.size(); i++)
for (size_t i = 0; i < 3; i++)
{
int b = static_cast<int>(rgb[i] * 255.0f);
int b = static_cast<int>(mValue[i] * 255.0f);
char* start = result.data() + i * 2;
if (b < 16)
start++;
@ -59,6 +63,6 @@ namespace Misc
bool operator==(const Color& l, const Color& r)
{
return l.mR == r.mR && l.mG == r.mG && l.mB == r.mB && l.mA == r.mA;
return l.mValue == r.mValue;
}
}

View file

@ -3,31 +3,38 @@
#include <string>
#include <osg/Vec4>
namespace Misc
{
class Color
{
explicit Color(osg::Vec4&& value)
: mValue(value)
{
}
public:
Color() = default;
Color(float r, float g, float b, float a);
float r() const { return mR; }
float g() const { return mG; }
float b() const { return mB; }
float a() const { return mA; }
float r() const { return mValue.r(); }
float g() const { return mValue.g(); }
float b() const { return mValue.b(); }
float a() const { return mValue.a(); }
std::string toString() const;
static Color fromHex(std::string_view hex);
static Color fromRGB(unsigned int value);
std::string toHex() const;
unsigned int toRGBA() const { return mValue.asRGBA(); }
friend bool operator==(const Color& l, const Color& r);
private:
float mR;
float mG;
float mB;
float mA;
osg::Vec4 mValue;
};
}

View file

@ -1673,7 +1673,7 @@
-- @field #number value
-- @field #number duration
-- @field #number radius
-- @field #number color
-- @field openmw.util#Color color
-- @field #boolean isCarriable True if the light can be carried by actors and appears up in their inventory.
-- @field #boolean isDynamic If true, the light will apply to actors and other moving objects
-- @field #boolean isFire True if the light acts like a fire.