Skip to content

Commit

Permalink
add palette.hpp for enhanced color blending
Browse files Browse the repository at this point in the history
  • Loading branch information
leeendl committed Nov 30, 2023
1 parent 682570f commit 6bbf780
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 39 deletions.
31 changes: 10 additions & 21 deletions include/image.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
#include <opencv2/opencv.hpp>
#include <dpp/message.h>
#include <filesystem>
#define cache_channel 1176057541013282896

namespace rgb {
const std::vector<double>
white{ 255, 255, 255 },
embeded{ 43, 45, 49 },
discord{ 49,51,56 };
}

class image {
dpp::snowflake id{};
Expand All @@ -29,23 +21,22 @@ class image {
std::vector<int> dim() {
return { this->img.rows, this->img.cols };
}
/*
* @param dim y, x e.g. { 100, 100 }
/*
* @param dim y, x e.g. { 100, 100 }
* @param file_name ignore dim and RGBA and import a existing image
*/
image(dpp::snowflake id, std::vector<int> dim, std::vector<double> RGBA, std::string file_name = "") {
if (RGBA.size() == 3) RGBA[4] = 255;
image(dpp::snowflake id, std::vector<int> dim, palette BGR, std::string file_name = "") {
this->id = id;
image_write((file_name.empty()) ?
cv::Mat::zeros(dim[0], dim[1], CV_8UC3) + cv::Scalar(RGBA[0], RGBA[1], RGBA[2], RGBA[3]) :
cv::Mat::zeros(dim[0], dim[1], CV_8UC3) + cv::Scalar(BGR[0], BGR[1], BGR[2], BGR[3]) :
cv::imread(cv::String(std::format(".\\cache\\{0}.jpg", file_name))));
}
/* adds a image within the original image */
image& add_image(std::string file_name, std::vector<int> at) {
try {
cv::Mat image = this->img;
cv::Mat background = cv::imread(cv::String(std::format(".\\cache\\{0}.jpg", file_name)));
cv::Mat object = cv::Mat::zeros(image.size(), image.type()); // -> becomes a object within image.
cv::Mat object = cv::Mat::zeros(image.size(), image.type());
cv::resize(background, background, cv::Size(), 0.5, 0.5);
cv::Rect roi(at[0], at[1], background.cols, background.rows);
background.copyTo(object(roi));
Expand All @@ -55,16 +46,14 @@ class image {
std::cout << e.what() << std::endl;
}
}
image& add_line(std::vector<int> pt1, std::vector<int> pt2, std::vector<double> RGBA, int thickness = 1) {
if (RGBA.size() == 3) RGBA[4] = 255;
cv::line(this->img,
image& add_line(std::vector<int> pt1, std::vector<int> pt2, palette BGR, int thickness = 1) {
cv::line(this->img,
cv::Point(std::clamp<int>(pt1[0], 0, this->dim()[1]), std::clamp<int>(pt1[1], 0, this->dim()[0])),
cv::Point(std::clamp<int>(pt2[0], 0, this->dim()[1]), std::clamp<int>(pt2[1], 0, this->dim()[0])),
cv::Scalar(RGBA[0], RGBA[1], RGBA[2], RGBA[3]), thickness);
cv::Scalar(BGR[0], BGR[1], BGR[2], BGR[3]), thickness);
}
image& add_text(std::string text, std::vector<int> at, cv::HersheyFonts font, std::vector<double> RGBA, int thickness = 1) {
if (RGBA.size() == 3) RGBA[4] = 255;
cv::putText(this->img, cv::String(text), cv::Point(at[0], at[1]), font, 1.0, cv::Scalar(RGBA[0], RGBA[1], RGBA[2], RGBA[3]), thickness);
image& add_text(cv::String& text, std::vector<int> at, cv::HersheyFonts font, palette BGR, int thickness = 1) {
cv::putText(this->img, text, cv::Point(at[0], at[1]), font, 1.0, cv::Scalar(BGR[0], BGR[1], BGR[2], BGR[3]), thickness);
}
~image() {
/* this deletes the physical copy and keeps the cloud copy (via discord) */
Expand Down
70 changes: 70 additions & 0 deletions include/palette.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* color palette. made by LeeEndl
* goal is to make color blending legible by english readers.
*
* though it is incomplete my goal is to merge primary colors (RGB scheme) using math operators. e.g. red() + blue() -> outcome: purple.
* and yes. this will also work with subtraction. (red() + blue()) - blue() -> outcome: red.
* ultimatly this should in thoery work for more complex equations. e.g. ({0}, {0}, {0}) + blue() - (255 / 6) -> outcome: black-blue (dark blue)
* |
* ---> nullfying all colors is black. TODO: make more legible like black()
*/
#include <vector>
#include <algorithm>

class red {
protected:
double R;
public:
operator double& () noexcept {
return R;
};
red() : R(255) {};
red(double val) noexcept : R(std::clamp<double>(std::move(val), 0.0, 255.0)) {};
red operator/(double val) {
return R / std::move(val);
}
};
class green {
protected:
double G;
public:
operator double& () {
return G;
};
green() : G(255) {};
green(double val) noexcept : G(std::clamp<double>(std::move(val), 0.0, 255.0)) {};
green operator/(double val) {
return G / std::move(val);
}
};
class blue {
protected:
double B;
public:
operator double& () {
return B;
};
blue() : B(255) {};
blue(double val) noexcept : B(std::clamp<double>(std::move(val), 0.0, 255.0)) {};
blue operator/(double val) {
return B / std::move(val);
}
};
class palette {
protected:
std::vector<double> P;
public:
operator std::vector<double>& () {
return P;
};
palette(blue B, green G = { 0 }, red R = { 0 }, double alpha = 255) {
this->P = { std::move(R), std::move(G), std::move(B), alpha };
}
palette(std::vector<double> val) {
val[3] = 255;
this->P = std::move(val);
}
double operator[](short pos) {
return this->P[pos];
}
};
29 changes: 11 additions & 18 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <dpp/dpp.h>
#include <dpp/nlohmann/json.hpp>
#include <palette.hpp>
#include <image.hpp>
#include <utility.hpp>
using namespace std::chrono;
Expand Down Expand Up @@ -80,13 +81,13 @@ static void command_sent(std::unique_ptr<dpp::slashcommand_t> event)
{
if (event->command.get_command_name() == "purge")
{
bot->messages_get(event->command.channel_id, 0, event->command.id, 0, std::clamp((int)get<int64_t>(event->get_parameter("amount")), 1, 100),
bot->messages_get(event->command.channel.id, 0, event->command.id, 0, std::clamp((int)get<int64_t>(event->get_parameter("amount")), 1, 100),
[&event](const dpp::confirmation_callback_t& mg_cb)
{
if (mg_cb.is_error() or std::get<dpp::message_map>(mg_cb.value).empty()) return;
std::vector<dpp::snowflake> message_vector;
for (const auto& [id, m] : std::move(std::get<dpp::message_map>(mg_cb.value))) message_vector.emplace_back(id);
bot->message_delete_bulk(message_vector, event->command.channel_id,
bot->message_delete_bulk(message_vector, event->command.channel.id,
[&event, mg_cb](const dpp::confirmation_callback_t& mdb_cb)
{
std::unique_ptr<dpp::message> msg = std::make_unique<dpp::message>();
Expand All @@ -110,7 +111,7 @@ static void command_sent(std::unique_ptr<dpp::slashcommand_t> event)
->set_flags(dpp::m_ephemeral));
else
{
bot->message_create(std::make_unique<dpp::message>(event->command.channel_id,
bot->message_create(std::make_unique<dpp::message>(event->command.channel.id,
std::make_unique<dpp::embed>()
->set_title({ get<std::string>(event->get_parameter("title")) }))
->add_component(std::make_unique<dpp::component>()->add_component(std::make_unique<dpp::component>()
Expand All @@ -131,25 +132,17 @@ static void command_sent(std::unique_ptr<dpp::slashcommand_t> event)
}
if (event->command.get_command_name() == "lvl")
{
image img(event->command.member.user_id, { 140, 500 }, rgb::embeded);

/* structure the progression bar */
img.add_line({ 20, 140 / 2 }, { 480, 140 / 2 }, rgb::white, 4);
img.add_line({ 20 /* + XP */ /* -> TODO */, 140 / 2}, {480, 140 / 2}, {120, 120, 120}, 4);
img.add_text(event->command.member.get_user()->username,
{ (480 / 2) - static_cast<int>(event->command.member.get_user()->username.size() * 4), 140 / 2 - 35 }, cv::FONT_HERSHEY_PLAIN, rgb::white);

/* GET profile picture from discod, and adds the image as a object */ // -> TODO: convert .GIF to .jpg via URLDownloadToFileW()
image img(event->command.member.user_id, { 140, 500 }, { blue(43), green(45), red(49) });
img.add_line({ 20, 140 / 2 }, { 480, 140 / 2 }, { {}, {}, {}}, 4);
img.add_line({ 20 /* + XP */, 140 / 2 }, { 480, 140 / 2 }, { blue() / 2.0, green() / 2.0, red() / 2.0 }, 4);
img.add_text(event->command.member.get_user()->username,
{ (480 / 2) - static_cast<int>(event->command.member.get_user()->username.size()) * 4, 140 / 2 - 35 }, cv::FONT_HERSHEY_PLAIN, { {}, {}, {} });
URLDownloadToFileW(NULL,
to_wstring(event->command.member.get_user()->get_avatar_url(128, dpp::i_jpg)).c_str(),
to_wstring(".\\cache\\" + std::to_string(event->command.member.user_id) + ".jpg").c_str(), 0, NULL);
img.add_image(std::to_string(event->command.member.user_id), {0, 0});

img.add_image(std::to_string(event->command.member.user_id), { 0, 0 });
img.image_write();
bot->message_create(dpp::message(cache_channel, "").add_file(img.path().c_str(), img.raw()), [&event](const dpp::confirmation_callback_t& callback) {
event->reply(dpp::message(event->command.channel_id, std::make_unique<dpp::embed>()
->set_image(std::get<dpp::message>(callback.value).attachments[0].url)));
});
event->reply(dpp::message(event->command.channel.id, "").add_file(img.path().c_str(), img.raw()));
}
std::this_thread::sleep_for(1s);
cmd_sender.erase(event->command.member.user_id);
Expand Down
1 change: 1 addition & 0 deletions neko.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
<ClInclude Include="include\dpp\webhook.h" />
<ClInclude Include="include\dpp\wsclient.h" />
<ClInclude Include="include\image.hpp" />
<ClInclude Include="include\palette.hpp" />
<ClInclude Include="include\utility.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
1 change: 1 addition & 0 deletions neko.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -212,5 +212,6 @@
</ClInclude>
<ClInclude Include="include\image.hpp" />
<ClInclude Include="include\utility.hpp" />
<ClInclude Include="include\palette.hpp" />
</ItemGroup>
</Project>

0 comments on commit 6bbf780

Please sign in to comment.