10#ifndef M5_UNIT_COLOR_UTILITY_UNIT_COLOR_UTILITY_HPP
11#define M5_UNIT_COLOR_UTILITY_UNIT_COLOR_UTILITY_HPP
24constexpr float GA{1.0f};
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};
37constexpr float AT_NORMAL_FACTOR = 2.4f;
38constexpr float AT_NORMAL_MIN = AT_NORMAL_FACTOR;
39constexpr float AT_NORMAL_MAX = 256 * AT_NORMAL_FACTOR;
41constexpr float WT_NORMAL_FACTOR = AT_NORMAL_FACTOR;
42constexpr float WT_NORMAL_MIN = WT_NORMAL_FACTOR;
43constexpr float WT_NORMAL_MAX = 256 * WT_NORMAL_FACTOR;
44constexpr float WT_LONG_FACTOR = 2.4f * 12.f;
45constexpr float WT_LONG_MIN = WT_LONG_FACTOR;
46constexpr float WT_LONG_MAX = 256 * WT_LONG_FACTOR;
54 Calibration(
const uint16_t br,
const uint16_t wr,
const uint16_t bg,
const uint16_t wg,
const uint16_t bb,
56 : blackR{br}, whiteR{wr}, blackG{bg}, whiteG{wg}, blackB{bb}, whiteB{wb}
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");
63 uint16_t blackR{}, whiteR{};
64 uint16_t blackG{}, whiteG{};
65 uint16_t blackB{}, whiteB{};
68 inline uint8_t
R8(
const Data& d)
const
73 inline uint8_t
G8(
const Data& d)
const
78 inline uint8_t
B8(
const Data& d)
const
84 inline static uint8_t
linear(
const uint16_t raw,
const uint16_t low,
const uint16_t high)
86 return std::fmax(std::fmin(std::round((
float)(raw - low) / (
float)(high - low) * 255.f), 255.f), 0.0f);
93 return 2.4f * (256 - a);
105 int tmp = 256 - std::round(ms / 2.4f);
106 return static_cast<uint8_t
>(std::max(std::min(tmp, 0xFF), 0x00));
110std::tuple<uint8_t, bool> ms_to_wtime(
const float ms);
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);
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);
149uint16_t calculateSaturation(
const uint8_t atime);
176float calculateCRATIO(
const uint16_t rawR,
const uint16_t rawG,
const uint16_t rawB,
const uint16_t rawC);
185float calculateCPL(
const float atime_ms,
const Gain gc,
const float dgf = DGF);
196 return 65535.0f / (3.0f *
calculateCPL(atime_ms, gc, dgf));
200template <
typename T, T... Ints>
201struct integer_sequence {
202 typedef T value_type;
203 static constexpr std::size_t size()
205 return sizeof...(Ints);
211template <
size_t... Ints>
212using index_sequence = integer_sequence<size_t, Ints...>;
214template <
typename T,
size_t N, T... Is>
215struct make_integer_sequence : make_integer_sequence<T, N - 1, N - 1, Is...> {};
217template <
typename T, T... Is>
218struct make_integer_sequence<T, 0, Is...> : integer_sequence<T, Is...> {};
221using make_index_sequence = make_integer_sequence<size_t, N>;
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
229#define M5_UNIT_COLOR_UTILITY_UNIT_COLOR_UTILITY_CONSTEXPR
232M5_UNIT_COLOR_UTILITY_UNIT_COLOR_UTILITY_CONSTEXPR uint8_t calculate_gamma(
const float x,
const float gamma)
234 return static_cast<uint8_t
>(std::pow(x, gamma) * 255);
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...>)
241 return {calculate_gamma(
static_cast<float>(Indices) / 255.0f, gamma)...};
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)
255 return make_gamma_table_impl(gamma, make_index_sequence<Size>{});
Top level namespace of M5stack.
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