Add Color class from libscgui

This commit is contained in:
Bob Polis 2024-11-29 14:11:24 +01:00
parent b6e80d6668
commit 13ff3d3ebd
2 changed files with 160 additions and 0 deletions

124
src/Color.cpp Normal file
View File

@ -0,0 +1,124 @@
#include "Color.hpp"
#include <algorithm>
#include <cmath>
#include <stdexcept>
#include <sstream>
#include <iomanip>
Color::Color(const RGB& rgb) : _rgb {rgb} {}
Color::Color(const HSB& hsb) {
if (hsb.s <= 0) {
_rgb.r = _rgb.g = _rgb.b = hsb.b;
} else {
double hi = hsb.h / 60;
int i = static_cast<int>(hi);
double f = hi - i;
double p = hsb.b * (1.0 - hsb.s);
double q = hsb.b * (1.0 - (hsb.s * f));
double t = hsb.b * (1.0 - (hsb.s * (1.0 -f)));
switch (i) {
case 0:
_rgb.r = hsb.b;
_rgb.g = t;
_rgb.b = p;
break;
case 1:
_rgb.r = q;
_rgb.g = hsb.b;
_rgb.b = p;
break;
case 2:
_rgb.r = p;
_rgb.g = hsb.b;
_rgb.b = t;
break;
case 3:
_rgb.r = p;
_rgb.g = q;
_rgb.b = hsb.b;
break;
case 4:
_rgb.r = t;
_rgb.g = p;
_rgb.b = hsb.b;
break;
case 5:
default:
_rgb.r = hsb.b;
_rgb.g = p;
_rgb.b = q;
break;
}
}
}
Color::operator HSB() const {
HSB hsb;
double rgb_max = std::max<double>({_rgb.r, _rgb.g, _rgb.b});
double rgb_min = std::min<double>({_rgb.r, _rgb.g, _rgb.b});
double delta = rgb_max - rgb_min;
if (delta < 0.00001) {
hsb.h = 0;
hsb.s = 0;
hsb.b = rgb_max;
return hsb;
}
if (_rgb.r >= rgb_max) { // between yellow and magenta
hsb.h = (_rgb.g - _rgb.b) / delta;
} else if (_rgb.g >= rgb_max) { // between cyan and yellow
hsb.h = 2.0 + (_rgb.b - _rgb.r) / delta;
} else { // between magenta and cyan
hsb.h = 4.0 + (_rgb.r - _rgb.g) / delta;
}
hsb.h *= 60;
hsb.s = rgb_max > 0.0 ? delta / rgb_max : 0;
hsb.b = rgb_max;
return hsb;
}
Color::Color(const std::string& hex) {
if (hex[0] != '#') throw std::runtime_error {"invalid hex color - must start with '#'"};
if (hex.size() != 7) throw std::runtime_error {"invalid hex color - must be 7 chars"};
unsigned int val;
std::istringstream iss {hex.substr(1, 2)};
iss >> std::hex >> val;
_rgb.r = val / 255.0;
iss.str(hex.substr(3, 2));
iss.seekg(0);
iss >> std::hex >> val;
_rgb.g = val / 255.0;
iss.str(hex.substr(5, 2));
iss.seekg(0);
iss >> std::hex >> val;
_rgb.b = val / 255.0;
}
Color& Color::operator=(const RGB& rgb) {
_rgb = rgb;
return *this;
}
Color& Color::operator=(const HSB& hsb) {
Color tmp {hsb};
_rgb = tmp._rgb;
return *this;
}
Color& Color::operator=(const std::string& hex) {
Color tmp {hex};
_rgb = tmp._rgb;
return *this;
}
std::string Color::hex() const {
std::ostringstream oss;
oss << '#' << std::hex <<
static_cast<unsigned int>(round(_rgb.r * 255)) <<
static_cast<unsigned int>(round(_rgb.g * 255)) <<
static_cast<unsigned int>(round(_rgb.b * 255));
return oss.str();
}

36
src/Color.hpp Normal file
View File

@ -0,0 +1,36 @@
#ifndef _Color_H_
#define _Color_H_
#include <string>
struct RGB {
double r; // 0..1
double g; // 0..1
double b; // 0..1
};
struct HSB {
double h; // 0..360
double s; // 0..1
double b; // 0..1
};
class Color {
public:
Color(const RGB& rgb);
Color(const HSB& hsb);
Color(const std::string& hex);
operator RGB() const { return _rgb; }
operator HSB() const;
std::string hex() const;
Color& operator=(const RGB& rgb);
Color& operator=(const HSB& hsb);
Color& operator=(const std::string& hex);
private:
RGB _rgb;
};
#endif // _Color_H_