M5Unit-COLOR 0.1.0 git rev:ba99933
Loading...
Searching...
No Matches
unit_color_utility.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_COLOR_UTILITY_UNIT_COLOR_UTILITY_HPP
11#define M5_UNIT_COLOR_UTILITY_UNIT_COLOR_UTILITY_HPP
12
14#include <cmath>
15#include <tuple>
16#include <cassert>
17
18namespace m5 {
19namespace unit {
20namespace tcs3472x {
21
24constexpr float GA{1.0f};
25// constexpr float GA{1.08f}; // Placing the parts behind clear glass requires using GA = 1.08
26constexpr float DF{310.f};
27constexpr float DGF{GA * DF};
28constexpr float R_Coef{0.136f};
29constexpr float G_Coef{1.0f};
30constexpr float B_Coef{-0.444f};
31constexpr float CT_Coef{3810.f};
32constexpr float CT_Offset{1391.f};
34
35//@name For A/WTIME
37constexpr float AT_NORMAL_FACTOR = 2.4f;
38constexpr float AT_NORMAL_MIN = AT_NORMAL_FACTOR; // 2.4ms
39constexpr float AT_NORMAL_MAX = 256 * AT_NORMAL_FACTOR; // 614.4ms
40//
41constexpr float WT_NORMAL_FACTOR = AT_NORMAL_FACTOR;
42constexpr float WT_NORMAL_MIN = WT_NORMAL_FACTOR; // 2.4ms
43constexpr float WT_NORMAL_MAX = 256 * WT_NORMAL_FACTOR; // 614.4ms
44constexpr float WT_LONG_FACTOR = 2.4f * 12.f;
45constexpr float WT_LONG_MIN = WT_LONG_FACTOR; // 28.8ms
46constexpr float WT_LONG_MAX = 256 * WT_LONG_FACTOR; // 7372.8ms
48
54 Calibration(const uint16_t br, const uint16_t wr, const uint16_t bg, const uint16_t wg, const uint16_t bb,
55 const uint16_t wb)
56 : blackR{br}, whiteR{wr}, blackG{bg}, whiteG{wg}, blackB{bb}, whiteB{wb}
57 {
58 assert(wr > br && "White must be greater than black");
59 assert(wg > bg && "White must be greater than black");
60 assert(wb > bb && "White must be greater than black");
61 }
62
63 uint16_t blackR{}, whiteR{};
64 uint16_t blackG{}, whiteG{};
65 uint16_t blackB{}, whiteB{};
66
68 inline uint8_t R8(const Data& d) const
69 {
70 return linear(d.RnoIR16(), blackR, whiteR);
71 }
73 inline uint8_t G8(const Data& d) const
74 {
75 return linear(d.GnoIR16(), blackG, whiteG);
76 }
78 inline uint8_t B8(const Data& d) const
79 {
80 return linear(d.BnoIR16(), blackB, whiteB);
81 }
82
84 inline static uint8_t linear(const uint16_t raw, const uint16_t low, const uint16_t high)
85 {
86 return std::fmax(std::fmin(std::round((float)(raw - low) / (float)(high - low) * 255.f), 255.f), 0.0f);
87 }
88};
89
91inline float atime_to_ms(const uint8_t a)
92{
93 return 2.4f * (256 - a);
94}
95
97inline float wtime_to_ms(const uint8_t w, const bool wlong)
98{
99 return atime_to_ms(w) * (wlong ? 12 : 1);
100}
101
103inline uint8_t ms_to_atime(const float ms)
104{
105 int tmp = 256 - std::round(ms / 2.4f);
106 return static_cast<uint8_t>(std::max(std::min(tmp, 0xFF), 0x00));
107}
108
110std::tuple<uint8_t, bool> ms_to_wtime(const float ms);
111
126float calculateLux(const uint16_t rawR, const uint16_t rawG, const uint16_t rawB, const uint16_t rawC,
127 const float atime_ms, const Gain gc, const float dgf = DGF, const float coefR = R_Coef,
128 const float coefG = G_Coef, const float coefB = B_Coef);
129
140float calculateColorTemperature(const uint16_t rawR, const uint16_t rawG, const uint16_t rawB, const uint16_t rawC,
141 const float coefCT = CT_Coef, const float offsetCT = CT_Offset);
142
149uint16_t calculateSaturation(const uint8_t atime);
150
157inline uint16_t calculateSaturation(const float atime_ms)
158{
159 return calculateSaturation(ms_to_atime(atime_ms));
160}
161
176float calculateCRATIO(const uint16_t rawR, const uint16_t rawG, const uint16_t rawB, const uint16_t rawC);
177
185float calculateCPL(const float atime_ms, const Gain gc, const float dgf = DGF);
186
194inline float calculateMaxLux(const float atime_ms, const Gain gc, const float dgf = DGF)
195{
196 return 65535.0f / (3.0f * calculateCPL(atime_ms, gc, dgf));
197}
198
200template <typename T, T... Ints>
201struct integer_sequence {
202 typedef T value_type;
203 static constexpr std::size_t size()
204 {
205 return sizeof...(Ints);
206 }
207};
208
209// Porting C++14 features to C++11
210// clang-format off
211template <size_t... Ints>
212using index_sequence = integer_sequence<size_t, Ints...>;
213
214template <typename T, size_t N, T... Is>
215struct make_integer_sequence : make_integer_sequence<T, N - 1, N - 1, Is...> {};
216
217template <typename T, T... Is>
218struct make_integer_sequence<T, 0, Is...> : integer_sequence<T, Is...> {};
219
220template <size_t N>
221using make_index_sequence = make_integer_sequence<size_t, N>;
222// clang-format on
223
224// constexpr version of std::pow is GCC4.6.1 or higher
225#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ > 6 || (__GNUC_MINOR__ == 6 && __GNUC_PATCHLEVEL__ > 0)))
226#define M5_UNIT_COLOR_UTILITY_UNIT_COLOR_UTILITY_CAN_CONSTEXPR_MAKE_GAMMA_TABLE
227#define M5_UNIT_COLOR_UTILITY_UNIT_COLOR_UTILITY_CONSTEXPR constexpr
228#else
229#define M5_UNIT_COLOR_UTILITY_UNIT_COLOR_UTILITY_CONSTEXPR
230#endif
231
232M5_UNIT_COLOR_UTILITY_UNIT_COLOR_UTILITY_CONSTEXPR uint8_t calculate_gamma(const float x, const float gamma)
233{
234 return static_cast<uint8_t>(std::pow(x, gamma) * 255);
235}
236
237template <size_t... Indices>
238M5_UNIT_COLOR_UTILITY_UNIT_COLOR_UTILITY_CONSTEXPR std::array<uint8_t, sizeof...(Indices)> make_gamma_table_impl(
239 const float gamma, index_sequence<Indices...>)
240{
241 return {calculate_gamma(static_cast<float>(Indices) / 255.0f, gamma)...};
242}
244
252template <size_t Size = 256>
253M5_UNIT_COLOR_UTILITY_UNIT_COLOR_UTILITY_CONSTEXPR std::array<uint8_t, Size> make_gamma_table(const float gamma = 2.2f)
254{
255 return make_gamma_table_impl(gamma, make_index_sequence<Size>{});
256}
257
258} // namespace tcs3472x
259} // namespace unit
260} // namespace m5
261#endif
Top level namespace of M5stack.
For TCS3472x.
Unit-related namespace.
Raw value to determine black and white.
Definition unit_color_utility.hpp:53
uint8_t G8(const Data &d) const
Get the read value with calibration.
Definition unit_color_utility.hpp:73
static uint8_t linear(const uint16_t raw, const uint16_t low, const uint16_t high)
Linear interpolation in a specified range.
Definition unit_color_utility.hpp:84
uint8_t B8(const Data &d) const
Get the read value with calibration.
Definition unit_color_utility.hpp:78
uint8_t R8(const Data &d) const
Get the read value with calibration.
Definition unit_color_utility.hpp:68
Measurement data group.
Definition unit_TCS3472x.hpp:64
uint16_t RnoIR16() const
Gets the raw red value without IR component.
Definition unit_TCS3472x.hpp:90
uint16_t BnoIR16() const
Gets the raw blue value without IR component.
Definition unit_TCS3472x.hpp:100
uint16_t GnoIR16() const
Gets the raw green value without IR component.
Definition unit_TCS3472x.hpp:95
TCS3472x Unit for M5UnitUnified.
Gain
RGBC Gain Control.
Definition unit_TCS3472x.hpp:53
uint16_t calculateSaturation(const uint8_t raw)
Calculate Saturation.
Definition unit_color_utility.cpp:59
float calculateCPL(const float atime_ms, const Gain gc, const float dgf)
Calculate Counts per Lux (CPL)
Definition unit_color_utility.cpp:78
uint8_t ms_to_atime(const float ms)
ms to ATIME
Definition unit_color_utility.hpp:103
float atime_to_ms(const uint8_t a)
ATIME to ms.
Definition unit_color_utility.hpp:91
M5_UNIT_COLOR_UTILITY_UNIT_COLOR_UTILITY_CONSTEXPR std::array< uint8_t, Size > make_gamma_table(const float gamma=2.2f)
Make gamma table.
Definition unit_color_utility.hpp:253
float wtime_to_ms(const uint8_t w, const bool wlong)
WTIME and WLONG to ms.
Definition unit_color_utility.hpp:97
float calculateMaxLux(const float atime_ms, const Gain gc, const float dgf=DGF)
Calculate maximum Lux.
Definition unit_color_utility.hpp:194