/
ArtemNech
/
simpleOpenGL
Обзор
Документация
Войти
/
ArtemNech
/
simpleOpenGL
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
backend/graphics-units/NColor.h
141 строка
5 KB
Artem Nechitaylenko
Qt GUI isolated from backend except QEvent. Need fix
18 май 2026, 13:41
18 май 2026, 13:41
f09d1f2
Код
Авторство
О чём код?
// // Created by artem on 20.10.25. // #ifndef NYM_PROJECT_NCOLOR_H #define NYM_PROJECT_NCOLOR_H #include <cstdint> #include <string> #include <algorithm> namespace ngraph { class NColor { public: NColor() = default; NColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) : m_r(r), m_g(g), m_b(b), m_a(a) {} // Основные цвета для SFC static NColor Black() { return {0, 0, 0}; } static NColor White() { return {255, 255, 255}; } static NColor Red() { return {255, 0, 0}; } static NColor Green() { return {0, 255, 0}; } static NColor Blue() { return {0, 0, 255}; } static NColor Gray() { return {128, 128, 128}; } static NColor DarkGray() { return {64, 64, 64}; } static NColor LightGray() { return {192, 192, 192}; } // Цвета состояний SFC static NColor ActiveStep() { return {255, 255, 0}; } static NColor InactiveStep() { return {200, 200, 200}; } // --- Геттеры --- [[nodiscard]] uint8_t red() const { return m_r; } [[nodiscard]] float redF() const { return static_cast<float>(m_r) / 255.0f; } [[nodiscard]] uint8_t green() const { return m_g; } [[nodiscard]] float greenF() const { return static_cast<float>(m_g) / 255.0f; } [[nodiscard]] uint8_t blue() const { return m_b; } [[nodiscard]] float blueF() const { return static_cast<float>(m_b) / 255.0f; } [[nodiscard]] uint8_t alpha() const { return m_a; } [[nodiscard]] float alphaF() const { return static_cast<float>(m_a) / 255.0f; } // --- Сеттеры (в стиле Qt) --- void setRed(uint8_t r) { m_r = r; } void setGreen(uint8_t g) { m_g = g; } void setBlue(uint8_t b) { m_b = b; } void setAlpha(uint8_t a) { m_a = a; } void setRgb(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) { m_r = r; m_g = g; m_b = b; m_a = a; } // --- HSV --- void getHsv(int* h, int* s, int* v, int* a = nullptr) const; void setHsv(int h, int s, int v, int a = 255); [[nodiscard]] int hue() const { int h; getHsv(&h, nullptr, nullptr); return h; } [[nodiscard]] int saturation() const { int s; getHsv(nullptr, &s, nullptr); return s; } [[nodiscard]] int value() const { int v; getHsv(nullptr, nullptr, &v); return v; } // --- HSL --- void getHsl(int* h, int* s, int* l, int* a = nullptr) const; void setHsl(int h, int s, int l, int a = 255); [[nodiscard]] int lightness() const { int l; getHsl(nullptr, nullptr, &l); return l; } // --- Трансформации --- [[nodiscard]] NColor lighter(int factor = 150) const; [[nodiscard]] NColor darker(int factor = 200) const; [[nodiscard]] NColor toGrayscale() const; // Смешивание с другим цветом (ratio: 0.0 = this, 1.0 = other) [[nodiscard]] NColor mix(const NColor& other, float ratio = 0.5f) const; // --- Строковое представление --- [[nodiscard]] std::string name() const; // "#RRGGBB" (без альфы) [[nodiscard]] std::string toHex() const; // "#RRGGBBAA" static NColor fromString(const std::string& str); static NColor fromRgb(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) { return {r, g, b, a}; } static NColor fromHsv(int h, int s, int v, int a = 255) { NColor c; c.setHsv(h, s, v, a); return c; } // --- Удобные методы --- [[nodiscard]] NColor withAlpha(uint8_t alpha) const { return {m_r, m_g, m_b, alpha}; } // Операторы сравнения bool operator==(const NColor& other) const { return m_r == other.m_r && m_g == other.m_g && m_b == other.m_b && m_a == other.m_a; } bool operator!=(const NColor& other) const { return !(*this == other); } private: uint8_t m_r{0}, m_g{0}, m_b{0}, m_a{255}; // Вспомогательная: конвертирует HSV-оттенок в RGB-компонент static float hueToRgb(float p, float q, float t); }; // Простой градиент (вертикальный/горизонтальный) class NGradient { public: enum Type { LinearVertical, LinearHorizontal }; NGradient() = default; NGradient(const NColor& start, const NColor& end, Type type = LinearVertical) : m_start(start), m_end(end), m_type(type) {} [[nodiscard]] NColor startColor() const { return m_start; } [[nodiscard]] NColor endColor() const { return m_end; } [[nodiscard]] Type type() const { return m_type; } private: NColor m_start{NColor::White()}; NColor m_end{NColor::Black()}; Type m_type{LinearVertical}; }; } #endif //NYM_PROJECT_NCOLOR_H