ReduxLib C++ 2026.1.2
Loading...
Searching...
No Matches
ColorData.h
1// Copyright (c) Bagholders of Redux Robotics and other contributors.
2// This is open source and can be modified and shared under the Mozilla Public License v2.0.
3
4#pragma once
5#include <algorithm>
6#include <cmath>
7
9
10/**
11 * Normalized RGB color reading.
12 *
13 * All channels are normalized to [0..1]. HSV helper functions also return normalized values:
14 * - Hue in [0..1)
15 * - Saturation in [0..1]
16 * - Value in [0..1]
17 */
18struct ColorData {
19 /**
20 * Color data struct
21 * @param red value [0..1]
22 * @param green value [0..1]
23 * @param blue value [0..1]
24 */
25 constexpr ColorData(double red, double green, double blue) :
26 red{red}, green{green}, blue{blue} {};
27
28 /** red value [0..1] */
29 double red;
30 /** green value [0..1] */
31 double green;
32 /** blue value [0..1] */
33 double blue;
34
35 /**
36 * Gets normalized HSV hue derived from this RGB value.
37 * @return hue in [0..1)
38 */
39 constexpr double GetHSVHue() const {
40 return HSVHue(red, green, blue);
41 }
42
43 /**
44 * Gets normalized HSV saturation derived from this RGB value.
45 * @return saturation in [0..1]
46 */
47 constexpr double GetHSVSaturation() const {
48 return HSVSaturation(red, green, blue);
49 }
50
51 /**
52 * Gets normalized HSV value derived from this RGB value.
53 * @return value in [0..1]
54 */
55 constexpr double GetHSVValue() const {
56 return HSVValue(red, green, blue);
57 }
58
59 /**
60 * Constructs a ColorData value from a packed CAN color message payload.
61 * @param data packed message data (see protocol details)
62 * @return ColorData normalized to [0..1]
63 */
65 constexpr double FACTOR = 1.0 / ((1 << 20) - 1);
66 return ColorData{data.red * FACTOR, data.green * FACTOR, data.blue * FACTOR};
67 }
68
69 /**
70 * Computes normalized HSV hue from normalized RGB.
71 * @param r red in [0..1]
72 * @param g green in [0..1]
73 * @param b blue in [0..1]
74 * @return hue in [0..1)
75 */
76 static constexpr double HSVHue(double r, double g, double b) {
77 double maxVal = std::max(r, std::max(g, b));
78 double minVal = std::min(r, std::min(g, b));
79 double chroma = maxVal - minVal;
80
81 if (chroma == 0.0) return 0.0;
82 if (maxVal == r) { return std::fmod(((g - b) / chroma), 6.0) / 6.0; }
83 if (maxVal == g) { return (((b - r) / chroma) + 2) / 6.0; }
84 if (maxVal == b) { return (((r - g) / chroma) + 4) / 6.0; }
85 return 0.0;
86 }
87
88 /**
89 * Computes normalized HSV saturation from normalized RGB.
90 * @param r red in [0..1]
91 * @param g green in [0..1]
92 * @param b blue in [0..1]
93 * @return saturation in [0..1]
94 */
95 static constexpr double HSVSaturation(double r, double g, double b) {
96 double maxVal = std::max(r, std::max(g, b));
97 double minVal = std::min(r, std::min(g, b));
98
99 if (maxVal == 0) return 0;
100 return (maxVal - minVal) / maxVal;
101 }
102
103 /**
104 * Computes normalized HSV value from normalized RGB.
105 * @param r red in [0..1]
106 * @param g green in [0..1]
107 * @param b blue in [0..1]
108 * @return value in [0..1]
109 */
110 static constexpr double HSVValue(double r, double g, double b) {
111 return std::max(r, std::max(g, b));
112 }
113};
114
115} // namespace redux::sensors::canandcolor
Definition Canandcolor.h:19
constexpr double GetHSVHue() const
Definition ColorData.h:39
static constexpr double HSVHue(double r, double g, double b)
Definition ColorData.h:76
double red
Definition ColorData.h:29
constexpr double GetHSVValue() const
Definition ColorData.h:55
double blue
Definition ColorData.h:33
double green
Definition ColorData.h:31
static constexpr double HSVSaturation(double r, double g, double b)
Definition ColorData.h:95
constexpr ColorData(double red, double green, double blue)
Definition ColorData.h:25
static constexpr double HSVValue(double r, double g, double b)
Definition ColorData.h:110
static constexpr ColorData FromColorMessage(details::msg::ColorOutput data)
Definition ColorData.h:64
constexpr double GetHSVSaturation() const
Definition ColorData.h:47
Definition CanandcolorDetails.h:1067
uint32_t green
Definition CanandcolorDetails.h:1073
uint32_t red
Definition CanandcolorDetails.h:1070
uint32_t blue
Definition CanandcolorDetails.h:1076