-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RGB / RGBA builtins for the scripting API (#92)
* RGB / RGBA builtins for the scripting API * Rename GetRel.. to GetFloat.. * Remove deleted keys.h * ColorRGBA inherits from ColorRGB * Yeah Mr. White, Yeah Math
- Loading branch information
Showing
3 changed files
with
200 additions
and
0 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
code/framework/src/scripting/engines/node/builtins/color_rgb.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* MafiaHub OSS license | ||
* Copyright (c) 2021-2023, MafiaHub. All rights reserved. | ||
* | ||
* This file comes from MafiaHub, hosted at https://github.com/MafiaHub/Framework. | ||
* See LICENSE file in the source repository for information regarding licensing. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <glm/glm.hpp> | ||
#include <v8pp/class.hpp> | ||
#include <v8pp/module.hpp> | ||
|
||
#include <iomanip> | ||
#include <list> | ||
#include <sstream> | ||
|
||
namespace Framework::Scripting::Engines::Node::Builtins { | ||
class ColorRGB { | ||
protected: | ||
glm::ivec4 _data; | ||
|
||
public: | ||
ColorRGB(int r, int g, int b) { | ||
_data = {r, g, b, 255}; | ||
} | ||
|
||
int GetR() const { | ||
return _data.r; | ||
} | ||
|
||
int GetG() const { | ||
return _data.g; | ||
} | ||
|
||
int GetB() const { | ||
return _data.b; | ||
} | ||
|
||
float GetFloatR() const { | ||
return static_cast<float>(_data.r) / 255.0f; | ||
} | ||
|
||
float GetFloatG() const { | ||
return static_cast<float>(_data.g) / 255.0f; | ||
} | ||
|
||
float GetFloatB() const { | ||
return static_cast<float>(_data.b) / 255.0f; | ||
} | ||
|
||
std::string ToString() const { | ||
std::ostringstream ss; | ||
ss << "RGB{ r: " << _data.r << ", g: " << _data.g << ", b: " << _data.b << " }"; | ||
return ss.str(); | ||
} | ||
|
||
std::list<int> ToArray() const { | ||
return {_data.r, _data.g, _data.b}; | ||
} | ||
|
||
void Add(int r, int g, int b) { | ||
glm::ivec4 newVec(r, g, b, 0); | ||
_data += newVec; | ||
} | ||
|
||
void Sub(int r, int g, int b) { | ||
glm::ivec4 newVec(r, g, b, 0); | ||
_data -= newVec; | ||
} | ||
|
||
void Mul(int r, int g, int b) { | ||
glm::ivec4 newVec(r, g, b, 1); | ||
_data *= newVec; | ||
} | ||
|
||
void Div(int r, int g, int b) { | ||
glm::ivec4 newVec(r, g, b, 1); | ||
_data /= newVec; | ||
} | ||
|
||
static ColorRGB FromVec4(const glm::vec4 &vec) { | ||
return ColorRGB(static_cast<int>(vec.r * 255.0f), static_cast<int>(vec.g * 255.0f), static_cast<int>(vec.b * 255.0f)); | ||
} | ||
|
||
static void Register(v8::Isolate *isolate, v8pp::module *rootModule) { | ||
if (!rootModule) { | ||
return; | ||
} | ||
|
||
v8pp::class_<ColorRGB> cls(isolate); | ||
cls.ctor<int, int, int>(); | ||
cls.property("r", &ColorRGB::GetR); | ||
cls.property("g", &ColorRGB::GetG); | ||
cls.property("b", &ColorRGB::GetB); | ||
cls.function("toString", &ColorRGB::ToString); | ||
cls.function("toArray", &ColorRGB::ToArray); | ||
cls.function("add", &ColorRGB::Add); | ||
cls.function("sub", &ColorRGB::Sub); | ||
cls.function("mul", &ColorRGB::Mul); | ||
cls.function("div", &ColorRGB::Div); | ||
|
||
rootModule->class_("RGB", cls); | ||
} | ||
}; | ||
} // namespace Framework::Scripting::Engines::Node::Builtins |
89 changes: 89 additions & 0 deletions
89
code/framework/src/scripting/engines/node/builtins/color_rgba.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* MafiaHub OSS license | ||
* Copyright (c) 2021-2023, MafiaHub. All rights reserved. | ||
* | ||
* This file comes from MafiaHub, hosted at https://github.com/MafiaHub/Framework. | ||
* See LICENSE file in the source repository for information regarding licensing. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <glm/glm.hpp> | ||
#include <v8pp/class.hpp> | ||
#include <v8pp/module.hpp> | ||
|
||
#include <iomanip> | ||
#include <list> | ||
#include <sstream> | ||
|
||
#include "color_rgb.h" | ||
|
||
namespace Framework::Scripting::Engines::Node::Builtins { | ||
class ColorRGBA: public ColorRGB { | ||
public: | ||
ColorRGBA(int r, int g, int b, int a): ColorRGB(r, g, b) { | ||
_data.a = a; | ||
} | ||
|
||
int GetA() const { | ||
return _data.a; | ||
} | ||
|
||
float GetFloatA() const { | ||
return static_cast<float>(_data.a) / 255.0f; | ||
} | ||
|
||
std::string ToString() const { | ||
std::ostringstream ss; | ||
ss << "RGBA{ r: " << _data.r << ", g: " << _data.g << ", b: " << _data.b << ", a: " << _data.a << " }"; | ||
return ss.str(); | ||
} | ||
|
||
std::list<int> ToArray() const { | ||
return {_data.r, _data.g, _data.b, _data.a}; | ||
} | ||
|
||
void Add(int r, int g, int b, int a) { | ||
glm::ivec4 newVec(r, g, b, a); | ||
_data += newVec; | ||
} | ||
|
||
void Sub(int r, int g, int b, int a) { | ||
glm::ivec4 newVec(r, g, b, a); | ||
_data -= newVec; | ||
} | ||
|
||
void Mul(int r, int g, int b, int a) { | ||
glm::ivec4 newVec(r, g, b, a); | ||
_data *= newVec; | ||
} | ||
|
||
void Div(int r, int g, int b, int a) { | ||
glm::ivec4 newVec(r, g, b, a); | ||
_data /= newVec; | ||
} | ||
|
||
static ColorRGBA FromVec4(const glm::vec4 &vec) { | ||
return ColorRGBA(static_cast<int>(vec.r * 255.0f), static_cast<int>(vec.g * 255.0f), static_cast<int>(vec.b * 255.0f), static_cast<int>(vec.a * 255.0f)); | ||
} | ||
|
||
static void Register(v8::Isolate *isolate, v8pp::module *rootModule) { | ||
if (!rootModule) { | ||
return; | ||
} | ||
|
||
v8pp::class_<ColorRGBA> cls(isolate); | ||
cls.inherit<ColorRGB>(); | ||
cls.ctor<int, int, int, int>(); | ||
cls.property("a", &ColorRGBA::GetA); | ||
cls.function("toString", &ColorRGBA::ToString); | ||
cls.function("toArray", &ColorRGBA::ToArray); | ||
cls.function("add", &ColorRGBA::Add); | ||
cls.function("sub", &ColorRGBA::Sub); | ||
cls.function("mul", &ColorRGBA::Mul); | ||
cls.function("div", &ColorRGBA::Div); | ||
|
||
rootModule->class_("RGBA", cls); | ||
} | ||
}; | ||
} // namespace Framework::Scripting::Engines::Node::Builtins |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters