-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathcolor.cpp
More file actions
26 lines (20 loc) · 804 Bytes
/
color.cpp
File metadata and controls
26 lines (20 loc) · 804 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <array>
#include "color.hpp"
std::array<float, 3> rgb2hsv(double r, double g, double b) {
r /= 255.;
g /= 255.;
b /= 255.;
const double color_max = (std::max)(r, (std::max)(g, b));
const double color_min = (std::min)(r, (std::min)(g, b));
const double diff = color_max - color_min;
double hue = 0.;
if (diff > 0.) {
if (color_max == r) hue = 60. * ((g - b) / diff);
else if (color_max == g) hue = 60. * (((b - r) / diff) + 2.);
else hue = 60. * (((r - g) / diff) + 4.);
}
if (hue < 0.) hue += 360.;
const double saturation = (color_max == 0. ? 0. : 100. * (diff / color_max));
const double value = 100. * color_max;
return { static_cast<float>(hue), static_cast<float>(saturation), static_cast<float>(value) };
}