added Color class

This commit is contained in:
Bob Polis 2020-10-27 22:32:37 +01:00
parent b85d4a2248
commit 516b62e266
2 changed files with 112 additions and 0 deletions

76
Color.cpp Normal file
View File

@ -0,0 +1,76 @@
//
// Color.cpp
// screensaver
//
// Created by Bob Polis at 2020-10-27
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#include "Color.hpp"
#include <algorithm>
#include <cmath>
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 {
int hi = static_cast<int>(round(fabs(hsb.h / 60)));
double f = hsb.h / 60 - hi;
double p = hsb.b * (1 - hsb.s);
double q = hsb.b * (1 - f * hsb.s);
double t = hsb.b * (1- (1 - f) * hsb.s);
switch (hi) {
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:
_rgb.r = hsb.b;
_rgb.g = p;
_rgb.b = q;
break;
default:
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 (_rgb.r > _rgb.g && _rgb.r > _rgb.b) { // red is max
hsb.h = ((_rgb.g - _rgb.b) / delta) * 60;
} else if (_rgb.g > _rgb.r && _rgb.g > _rgb.b) { // green is max
hsb.h = ((_rgb.b - _rgb.r) / delta) * 60;
} else { // blue is max
hsb.h = ((_rgb.r - _rgb.g) / delta) * 60;
}
hsb.s = delta / rgb_max;
hsb.b = rgb_max;
return hsb;
}

36
Color.hpp Normal file
View File

@ -0,0 +1,36 @@
//
// Color.hpp
// screensaver
//
// Created by Bob Polis at 2020-10-27
// Copyright (c) 2020 SwiftCoder. All rights reserved.
//
#ifndef _Color_H_
#define _Color_H_
struct RGB {
double r;
double g;
double b;
};
struct HSB {
double h;
double s;
double b;
};
class Color {
public:
Color(const RGB& rgb);
Color(const HSB& hsb);
operator RGB() const { return _rgb; }
operator HSB() const;
private:
RGB _rgb;
};
#endif // _Color_H_