mirror of
https://github.com/AlexandreRouma/SDRPlusPlus.git
synced 2025-06-28 21:37:50 +02:00
Fixed OS EventHandler compilation issue
This commit is contained in:
50
core/src/utils/color.h
Normal file
50
core/src/utils/color.h
Normal file
@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
#include <algorithm>
|
||||
#include <math.h>
|
||||
|
||||
namespace color {
|
||||
inline void RGBtoHSL(float r, float g, float b, float& h, float& s, float& l) {
|
||||
// Calculate minimum, maximum and delta of components
|
||||
float cmax = std::max<float>(std::max<float>(r, g), b);
|
||||
float cmin = std::min<float>(std::min<float>(r, g), b);
|
||||
float delta = cmax - cmin;
|
||||
|
||||
// Calculate the hue
|
||||
if (delta == 0) { h = 0; }
|
||||
else if (r > g && r > b) { h = 60.0f * fmodf((g - b) / delta, 6.0f); }
|
||||
else if (g > r && g > b) { h = 60.0f * (((b - r) / delta) + 2.0f); }
|
||||
else { h = 60.0f * (((r - g) / delta) + 4.0f); }
|
||||
|
||||
// Calculate lightness
|
||||
l = (cmin + cmax) / 2.0f;
|
||||
|
||||
// Calculate saturation
|
||||
s = (delta == 0) ? 0 : (delta / (1.0f - fabsf((2.0f * l) - 1.0f)));
|
||||
}
|
||||
|
||||
inline void HSLtoRGB(float h, float s, float l, float& r, float& g, float& b) {
|
||||
// Calculate coefficients
|
||||
float c = s * (1.0f - fabsf((2.0f * l) - 1.0f));
|
||||
float x = c * (1.0f - fabsf(fmodf(h / 60.0f, 2.0f) - 1.0f));
|
||||
float m = l - (c / 2.0f);
|
||||
|
||||
// Affect coefficients to R, G or B depending on hue
|
||||
if (h < 60) { r = c; g = x; b = 0; }
|
||||
else if (h < 120) { r = x; g = c; b = 0; }
|
||||
else if (h < 180) { r = 0; g = c; b = x; }
|
||||
else if (h < 240) { r = 0; g = x; b = c; }
|
||||
else if (h < 300) { r = x; g = 0; b = c; }
|
||||
else { r = c; g = 0; b = x; }
|
||||
|
||||
// Add m
|
||||
r += m;
|
||||
g += m;
|
||||
b += m;
|
||||
}
|
||||
|
||||
inline void interpRGB(float ar, float ag, float ab, float br, float bg, float bb, float& or, float& og, float& ob, float ratio) {
|
||||
or = ar + (br - ar) * ratio;
|
||||
og = ag + (bg - ag) * ratio;
|
||||
ob = ab + (bb - ab) * ratio;
|
||||
}
|
||||
}
|
44
core/src/utils/event.h
Normal file
44
core/src/utils/event.h
Normal file
@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
template <class T>
|
||||
struct EventHandler {
|
||||
EventHandler() {}
|
||||
EventHandler(void (*handler)(T, void*), void* ctx) {
|
||||
this->handler = handler;
|
||||
this->ctx = ctx;
|
||||
}
|
||||
|
||||
void (*handler)(T, void*);
|
||||
void* ctx;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class Event {
|
||||
public:
|
||||
Event() {}
|
||||
~Event() {}
|
||||
|
||||
void emit(T value) {
|
||||
for (auto const& handler : handlers) {
|
||||
handler.handler(value, handler.ctx);
|
||||
}
|
||||
}
|
||||
|
||||
void bindHandler(const EventHandler<T>& handler) {
|
||||
handlers.push_back(handler);
|
||||
}
|
||||
|
||||
void unbindHandler(const EventHandler<T>& handler) {
|
||||
if (handlers.find(handler) == handlers.end()) {
|
||||
spdlog::error("Tried to remove a non-existant event handler");
|
||||
return;
|
||||
}
|
||||
handlers.erase(std::remove(handlers.begin(), handlers.end(), handler), handlers.end());
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<EventHandler<T>> handlers;
|
||||
|
||||
};
|
Reference in New Issue
Block a user